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.
269 lines
6.9 KiB
269 lines
6.9 KiB
package profile
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"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.UpdateProfileRequest{}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// TestUpdateProfile_CreateNewProfile 测试用户第一次更新时创建个人资料
|
|
func TestUpdateProfile_CreateNewProfile(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{
|
|
Avatar: "http://example.com/avatar.jpg",
|
|
Bio: "First bio",
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.UpdateProfile(req)
|
|
|
|
// 验证结果
|
|
require.NoError(t, err)
|
|
require.NotNil(t, resp)
|
|
|
|
assert.Equal(t, user.Id, resp.Id)
|
|
assert.Equal(t, "http://example.com/avatar.jpg", resp.Avatar)
|
|
assert.Equal(t, "First bio", resp.Bio)
|
|
|
|
// 验证数据库中确实创建了个人资料
|
|
profile, err := model.FindProfileByUserId(context.Background(), svcCtx.DB, user.Id)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, profile)
|
|
assert.Equal(t, "http://example.com/avatar.jpg", profile.Avatar)
|
|
}
|
|
|
|
// TestUpdateProfile_UpdateExistingProfile 测试更新已存在的个人资料
|
|
func TestUpdateProfile_UpdateExistingProfile(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 := NewUpdateProfileLogic(ctx, svcCtx)
|
|
|
|
// 准备更新请求数据 - 只更新部分字段
|
|
req := &types.UpdateProfileRequest{
|
|
Bio: "Updated bio only",
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.UpdateProfile(req)
|
|
|
|
// 验证结果
|
|
require.NoError(t, err)
|
|
require.NotNil(t, resp)
|
|
|
|
assert.Equal(t, "Updated bio only", resp.Bio)
|
|
assert.Equal(t, "http://example.com/avatar.jpg", resp.Avatar) // 保持不变
|
|
|
|
// 验证数据库中确实更新了
|
|
profile, err := model.FindProfileByUserId(context.Background(), svcCtx.DB, user.Id)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "Updated bio only", profile.Bio)
|
|
assert.Equal(t, "http://example.com/avatar.jpg", profile.Avatar)
|
|
}
|
|
|
|
// TestUpdateProfile_UpdateOnlyUsername 测试只更新用户名
|
|
func TestUpdateProfile_UpdateOnlyUsername(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: "newusername",
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.UpdateProfile(req)
|
|
|
|
// 验证结果
|
|
require.NoError(t, err)
|
|
require.NotNil(t, resp)
|
|
|
|
assert.Equal(t, "newusername", resp.Username)
|
|
assert.Equal(t, "test@example.com", resp.Email) // 保持不变
|
|
}
|
|
|
|
// TestUpdateProfile_NoUserIdInContext 测试上下文中没有用户ID
|
|
func TestUpdateProfile_NoUserIdInContext(t *testing.T) {
|
|
svcCtx, cleanup := setupTestDB(t)
|
|
defer cleanup()
|
|
|
|
// 创建测试用户
|
|
createTestUser(t, svcCtx.DB)
|
|
|
|
// 设置上下文,不包含用户ID
|
|
ctx := context.Background()
|
|
|
|
// 创建 Logic 实例
|
|
logic := NewUpdateProfileLogic(ctx, svcCtx)
|
|
|
|
// 准备更新请求数据
|
|
req := &types.UpdateProfileRequest{
|
|
Username: "updateduser",
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.UpdateProfile(req)
|
|
|
|
// 验证结果 - 应该返回错误
|
|
require.Error(t, err)
|
|
require.Nil(t, resp)
|
|
assert.Contains(t, err.Error(), "未获取到用户信息")
|
|
}
|
|
|
|
// TestUpdateProfile_UserNotFound 测试用户不存在
|
|
func TestUpdateProfile_UserNotFound(t *testing.T) {
|
|
svcCtx, cleanup := setupTestDB(t)
|
|
defer cleanup()
|
|
|
|
// 设置上下文,使用不存在的用户ID
|
|
ctx := context.WithValue(context.Background(), "userId", int64(99999))
|
|
|
|
// 创建 Logic 实例
|
|
logic := NewUpdateProfileLogic(ctx, svcCtx)
|
|
|
|
// 准备更新请求数据
|
|
req := &types.UpdateProfileRequest{
|
|
Username: "updateduser",
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.UpdateProfile(req)
|
|
|
|
// 验证结果 - 应该返回错误
|
|
require.Error(t, err)
|
|
require.Nil(t, resp)
|
|
assert.Contains(t, err.Error(), "用户不存在")
|
|
}
|
|
|
|
// TestUpdateProfile_EmptyRequest 测试空请求
|
|
func TestUpdateProfile_EmptyRequest(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{}
|
|
|
|
// 执行测试 - 应该正常工作,只是不做任何更新
|
|
resp, err := logic.UpdateProfile(req)
|
|
|
|
// 验证结果
|
|
require.NoError(t, err)
|
|
require.NotNil(t, resp)
|
|
|
|
// 验证数据没有变化
|
|
assert.Equal(t, "testuser", resp.Username)
|
|
assert.Equal(t, "13800138000", resp.Phone)
|
|
}
|
|
|
|
// BenchmarkUpdateProfile 性能测试
|
|
func BenchmarkUpdateProfile(b *testing.B) {
|
|
svcCtx, _ := setupTestDB(&testing.T{})
|
|
defer func() {
|
|
sqlDB, _ := svcCtx.DB.DB()
|
|
if sqlDB != nil {
|
|
sqlDB.Close()
|
|
}
|
|
}()
|
|
|
|
// 创建测试用户
|
|
user := createTestUser(&testing.T{}, svcCtx.DB)
|
|
|
|
// 设置上下文,包含用户ID
|
|
ctx := context.WithValue(context.Background(), "userId", user.Id)
|
|
|
|
// 准备更新请求数据
|
|
req := &types.UpdateProfileRequest{
|
|
Username: "updateduser",
|
|
Bio: "Updated bio",
|
|
}
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
logic := NewUpdateProfileLogic(ctx, svcCtx)
|
|
_, _ = logic.UpdateProfile(req)
|
|
}
|
|
}
|
|
|