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.
405 lines
8.5 KiB
405 lines
8.5 KiB
package user
|
|
|
|
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.UpdateUserRequest{}
|
|
|
|
// TestUpdateUser_Success 测试成功更新用户
|
|
func TestUpdateUser_Success(t *testing.T) {
|
|
svcCtx, cleanup := setupUserTestDB(t)
|
|
defer cleanup()
|
|
|
|
// 创建测试用户
|
|
now := time.Now()
|
|
user := &model.User{
|
|
Username: "olduser",
|
|
Email: "old@example.com",
|
|
Password: "encryptedpassword",
|
|
Phone: "13800138000",
|
|
Status: 1,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
|
|
err := svcCtx.DB.Create(user).Error
|
|
require.NoError(t, err)
|
|
|
|
// 创建上下文
|
|
ctx := context.Background()
|
|
|
|
// 创建 Logic 实例
|
|
logic := NewUpdateUserLogic(ctx, svcCtx)
|
|
|
|
// 准备更新请求数据
|
|
req := &types.UpdateUserRequest{
|
|
Id: user.Id,
|
|
Username: "newuser",
|
|
Email: "new@example.com",
|
|
Phone: "13900139000",
|
|
Status: 2,
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.UpdateUser(req)
|
|
|
|
// 验证结果
|
|
require.NoError(t, err)
|
|
require.NotNil(t, resp)
|
|
assert.Equal(t, user.Id, resp.Id)
|
|
assert.Equal(t, "newuser", resp.Username)
|
|
assert.Equal(t, "new@example.com", resp.Email)
|
|
assert.Equal(t, "13900139000", resp.Phone)
|
|
assert.Equal(t, 2, resp.Status)
|
|
}
|
|
|
|
// TestUpdateUser_OnlyUsername 测试只更新用户名
|
|
func TestUpdateUser_OnlyUsername(t *testing.T) {
|
|
svcCtx, cleanup := setupUserTestDB(t)
|
|
defer cleanup()
|
|
|
|
// 创建测试用户
|
|
now := time.Now()
|
|
user := &model.User{
|
|
Username: "olduser",
|
|
Email: "test@example.com",
|
|
Password: "encryptedpassword",
|
|
Phone: "13800138000",
|
|
Status: 1,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
|
|
err := svcCtx.DB.Create(user).Error
|
|
require.NoError(t, err)
|
|
|
|
// 创建上下文
|
|
ctx := context.Background()
|
|
|
|
// 创建 Logic 实例
|
|
logic := NewUpdateUserLogic(ctx, svcCtx)
|
|
|
|
// 准备更新请求数据 - 只更新用户名
|
|
req := &types.UpdateUserRequest{
|
|
Id: user.Id,
|
|
Username: "newuser",
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.UpdateUser(req)
|
|
|
|
// 验证结果
|
|
require.NoError(t, err)
|
|
require.NotNil(t, resp)
|
|
assert.Equal(t, "newuser", resp.Username)
|
|
assert.Equal(t, "test@example.com", resp.Email) // 保持不变
|
|
assert.Equal(t, "13800138000", resp.Phone) // 保持不变
|
|
}
|
|
|
|
// TestUpdateUser_OnlyEmail 测试只更新邮箱
|
|
func TestUpdateUser_OnlyEmail(t *testing.T) {
|
|
svcCtx, cleanup := setupUserTestDB(t)
|
|
defer cleanup()
|
|
|
|
// 创建测试用户
|
|
now := time.Now()
|
|
user := &model.User{
|
|
Username: "testuser",
|
|
Email: "old@example.com",
|
|
Password: "encryptedpassword",
|
|
Phone: "13800138000",
|
|
Status: 1,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
|
|
err := svcCtx.DB.Create(user).Error
|
|
require.NoError(t, err)
|
|
|
|
// 创建上下文
|
|
ctx := context.Background()
|
|
|
|
// 创建 Logic 实例
|
|
logic := NewUpdateUserLogic(ctx, svcCtx)
|
|
|
|
// 准备更新请求数据 - 只更新邮箱
|
|
req := &types.UpdateUserRequest{
|
|
Id: user.Id,
|
|
Email: "new@example.com",
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.UpdateUser(req)
|
|
|
|
// 验证结果
|
|
require.NoError(t, err)
|
|
require.NotNil(t, resp)
|
|
assert.Equal(t, "new@example.com", resp.Email)
|
|
assert.Equal(t, "testuser", resp.Username) // 保持不变
|
|
}
|
|
|
|
// TestUpdateUser_DuplicateEmail 测试使用重复的邮箱
|
|
func TestUpdateUser_DuplicateEmail(t *testing.T) {
|
|
svcCtx, cleanup := setupUserTestDB(t)
|
|
defer cleanup()
|
|
|
|
// 创建两个测试用户
|
|
now := time.Now()
|
|
user1 := &model.User{
|
|
Username: "user1",
|
|
Email: "user1@example.com",
|
|
Password: "encryptedpassword",
|
|
Status: 1,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
|
|
user2 := &model.User{
|
|
Username: "user2",
|
|
Email: "user2@example.com",
|
|
Password: "encryptedpassword",
|
|
Status: 1,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
|
|
err := svcCtx.DB.Create(user1).Error
|
|
require.NoError(t, err)
|
|
err = svcCtx.DB.Create(user2).Error
|
|
require.NoError(t, err)
|
|
|
|
// 创建上下文
|
|
ctx := context.Background()
|
|
|
|
// 创建 Logic 实例
|
|
logic := NewUpdateUserLogic(ctx, svcCtx)
|
|
|
|
// 尝试将 user1 的邮箱更新为 user2 的邮箱
|
|
req := &types.UpdateUserRequest{
|
|
Id: user1.Id,
|
|
Email: "user2@example.com",
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.UpdateUser(req)
|
|
|
|
// 验证结果 - 应该返回错误
|
|
require.Error(t, err)
|
|
require.Nil(t, resp)
|
|
assert.Contains(t, err.Error(), "邮箱已被使用")
|
|
}
|
|
|
|
// TestUpdateUser_SameEmail 测试使用相同的邮箱(应该允许)
|
|
func TestUpdateUser_SameEmail(t *testing.T) {
|
|
svcCtx, cleanup := setupUserTestDB(t)
|
|
defer cleanup()
|
|
|
|
// 创建测试用户
|
|
now := time.Now()
|
|
user := &model.User{
|
|
Username: "testuser",
|
|
Email: "test@example.com",
|
|
Password: "encryptedpassword",
|
|
Phone: "13800138000",
|
|
Status: 1,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
|
|
err := svcCtx.DB.Create(user).Error
|
|
require.NoError(t, err)
|
|
|
|
// 创建上下文
|
|
ctx := context.Background()
|
|
|
|
// 创建 Logic 实例
|
|
logic := NewUpdateUserLogic(ctx, svcCtx)
|
|
|
|
// 使用相同的邮箱
|
|
req := &types.UpdateUserRequest{
|
|
Id: user.Id,
|
|
Email: "test@example.com",
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.UpdateUser(req)
|
|
|
|
// 验证结果 - 应该成功
|
|
require.NoError(t, err)
|
|
require.NotNil(t, resp)
|
|
assert.Equal(t, "test@example.com", resp.Email)
|
|
}
|
|
|
|
// TestUpdateUser_NotFound 测试更新不存在的用户
|
|
func TestUpdateUser_NotFound(t *testing.T) {
|
|
svcCtx, cleanup := setupUserTestDB(t)
|
|
defer cleanup()
|
|
|
|
// 创建上下文
|
|
ctx := context.Background()
|
|
|
|
// 创建 Logic 实例
|
|
logic := NewUpdateUserLogic(ctx, svcCtx)
|
|
|
|
// 使用不存在的用户ID
|
|
req := &types.UpdateUserRequest{
|
|
Id: 99999,
|
|
Username: "newuser",
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.UpdateUser(req)
|
|
|
|
// 验证结果 - 应该返回错误
|
|
require.Error(t, err)
|
|
require.Nil(t, resp)
|
|
assert.Contains(t, err.Error(), "用户不存在")
|
|
}
|
|
|
|
// TestUpdateUser_EmptyRequest 测试空请求
|
|
func TestUpdateUser_EmptyRequest(t *testing.T) {
|
|
svcCtx, cleanup := setupUserTestDB(t)
|
|
defer cleanup()
|
|
|
|
// 创建测试用户
|
|
now := time.Now()
|
|
user := &model.User{
|
|
Username: "testuser",
|
|
Email: "test@example.com",
|
|
Password: "encryptedpassword",
|
|
Phone: "13800138000",
|
|
Status: 1,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
|
|
err := svcCtx.DB.Create(user).Error
|
|
require.NoError(t, err)
|
|
|
|
// 创建上下文
|
|
ctx := context.Background()
|
|
|
|
// 创建 Logic 实例
|
|
logic := NewUpdateUserLogic(ctx, svcCtx)
|
|
|
|
// 空请求
|
|
req := &types.UpdateUserRequest{
|
|
Id: user.Id,
|
|
}
|
|
|
|
// 执行测试 - 应该成功,只是不做任何更新
|
|
resp, err := logic.UpdateUser(req)
|
|
|
|
// 验证结果
|
|
require.NoError(t, err)
|
|
require.NotNil(t, resp)
|
|
assert.Equal(t, "testuser", resp.Username)
|
|
assert.Equal(t, "test@example.com", resp.Email)
|
|
}
|
|
|
|
// TestUpdateUser_ChangeStatus 测试修改用户状态
|
|
func TestUpdateUser_ChangeStatus(t *testing.T) {
|
|
svcCtx, cleanup := setupUserTestDB(t)
|
|
defer cleanup()
|
|
|
|
// 创建测试用户
|
|
now := time.Now()
|
|
user := &model.User{
|
|
Username: "testuser",
|
|
Email: "test@example.com",
|
|
Password: "encryptedpassword",
|
|
Phone: "13800138000",
|
|
Status: 1,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
|
|
err := svcCtx.DB.Create(user).Error
|
|
require.NoError(t, err)
|
|
|
|
// 创建上下文
|
|
ctx := context.Background()
|
|
|
|
// 创建 Logic 实例
|
|
logic := NewUpdateUserLogic(ctx, svcCtx)
|
|
|
|
// 禁用用户
|
|
req := &types.UpdateUserRequest{
|
|
Id: user.Id,
|
|
Status: 2,
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.UpdateUser(req)
|
|
|
|
// 验证结果
|
|
require.NoError(t, err)
|
|
require.NotNil(t, resp)
|
|
assert.Equal(t, 2, resp.Status)
|
|
|
|
// 再次启用用户
|
|
logic2 := NewUpdateUserLogic(ctx, svcCtx)
|
|
req2 := &types.UpdateUserRequest{
|
|
Id: user.Id,
|
|
Status: 1,
|
|
}
|
|
|
|
resp2, err := logic2.UpdateUser(req2)
|
|
|
|
// 验证结果
|
|
require.NoError(t, err)
|
|
require.NotNil(t, resp2)
|
|
assert.Equal(t, 1, resp2.Status)
|
|
}
|
|
|
|
// BenchmarkUpdateUser 性能测试
|
|
func BenchmarkUpdateUser(b *testing.B) {
|
|
svcCtx, _ := setupUserTestDB(&testing.T{})
|
|
defer func() {
|
|
sqlDB, _ := svcCtx.DB.DB()
|
|
if sqlDB != nil {
|
|
sqlDB.Close()
|
|
}
|
|
}()
|
|
|
|
// 创建测试用户
|
|
now := time.Now()
|
|
user := &model.User{
|
|
Username: "testuser",
|
|
Email: "test@example.com",
|
|
Password: "encryptedpassword",
|
|
Phone: "13800138000",
|
|
Status: 1,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
|
|
err := svcCtx.DB.Create(user).Error
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
req := &types.UpdateUserRequest{
|
|
Id: user.Id,
|
|
Username: "newuser",
|
|
}
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
logic := NewUpdateUserLogic(ctx, svcCtx)
|
|
_, _ = logic.UpdateUser(req)
|
|
}
|
|
}
|
|
|