You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

166 lines
4.0 KiB

package profile
import (
"context"
"testing"
"time"
"github.com/youruser/base/internal/svc"
"github.com/youruser/base/internal/types"
"github.com/youruser/base/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Ensure imports are used
var _ = svc.ServiceContext{}
var _ = types.GetProfileResponse{}
// setupTestDB 创建测试数据库并返回 ServiceContext(使用MySQL)
func setupTestDB(t *testing.T) (*svc.ServiceContext, func()) {
t.Helper()
// 使用 MySQL 进行测试
dsn := "root:root@tcp(127.0.0.1:3306)/testdb?charset=utf8mb4&parseTime=true&loc=Local"
db, err := gorm.Open(dsn, &gorm.Config{})
require.NoError(t, err)
// 自动迁移表
err = db.AutoMigrate(&model.User{}, &model.Profile{})
require.NoError(t, err)
// 创建测试 ServiceContext
svcCtx := &svc.ServiceContext{
DB: db,
}
// 清理函数
cleanup := func() {
sqlDB, _ := db.DB()
if sqlDB != nil {
sqlDB.Close()
}
}
return svcCtx, cleanup
}
// createTestUser 创建测试用户
func createTestUser(t *testing.T, db *gorm.DB) *model.User {
t.Helper()
now := time.Now()
user := &model.User{
Username: "testuser",
Email: "test@example.com",
Password: "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8",
Phone: "13800138000",
Status: 1,
CreatedAt: now,
UpdatedAt: now,
}
err := db.Create(user).Error
require.NoError(t, err)
return user
}
// TestGetProfile_Success 测试成功获取个人信息
func TestGetProfile_Success(t *testing.T) {
svcCtx, cleanup := setupTestDB(t)
defer cleanup()
// 创建测试用户
user := createTestUser(t, svcCtx.DB)
createTestProfile(t, svcCtx.DB, user.Id)
// 设置上下文,包含用户ID
ctx := context.WithValue(context.Background(), "userId", user.Id)
// 创建 Logic 实例
logic := NewGetProfileLogic(ctx, svcCtx)
// 执行测试
resp, err := logic.GetProfile()
// 验证结果
require.NoError(t, err)
require.NotNil(t, resp)
assert.Equal(t, user.Id, resp.Id)
assert.Equal(t, "testuser", resp.Username)
assert.Equal(t, "test@example.com", resp.Email)
assert.Equal(t, "13800138000", resp.Phone)
assert.Equal(t, "http://example.com/avatar.jpg", resp.Avatar)
assert.Equal(t, "This is a test bio", resp.Bio)
assert.Equal(t, 1, resp.Status)
}
// TestUpdateProfile_Success 测试成功更新个人资料
func TestUpdateProfile_Success(t *testing.T) {
svcCtx, cleanup := setupTestDB(t)
defer cleanup()
// 创建测试用户
user := createTestUser(t, svcCtx.DB)
// 设置上下文,包含用户ID
ctx := context.WithValue(context.Background(), "userId", user.Id)
// 创建 Logic 实例
logic := NewUpdateProfileLogic(ctx, svcCtx)
// 准备更新请求数据
req := &types.UpdateProfileRequest{
Username: "updateduser",
Phone: "13900139000",
Avatar: "http://example.com/newavatar.jpg",
Bio: "Updated bio text",
}
// 执行测试
resp, err := logic.UpdateProfile(req)
// 验证结果
require.NoError(t, err)
require.NotNil(t, resp)
assert.Equal(t, user.Id, resp.Id)
assert.Equal(t, "updateduser", resp.Username)
assert.Equal(t, "13900139000", resp.Phone)
assert.Equal(t, "http://example.com/newavatar.jpg", resp.Avatar)
assert.Equal(t, "Updated bio text", resp.Bio)
}
// TestChangePassword_Success 测试成功修改密码
func TestChangePassword_Success(t *testing.T) {
svcCtx, cleanup := setupTestDB(t)
defer cleanup()
// 创建测试用户,密码为 "password"
user := createTestUser(t, svcCtx.DB)
// 设置上下文,包含用户ID
ctx := context.WithValue(context.Background(), "userId", user.Id)
// 创建 Logic 实例
logic := NewChangePasswordLogic(ctx, svcCtx)
// 准备修改密码请求数据
req := &types.ChangePasswordRequest{
OldPassword: "password",
NewPassword: "newpassword123",
}
// 执行测试
resp, err := logic.ChangePassword(req)
// 验证结果
require.NoError(t, err)
require.NotNil(t, resp)
assert.Equal(t, 200, resp.Code)
assert.Equal(t, "修改密码成功", resp.Message)
}
}