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.
256 lines
5.2 KiB
256 lines
5.2 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.GetUserRequest{}
|
|
|
|
// TestGetUser_Success 测试成功获取用户
|
|
func TestGetUser_Success(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 := NewGetUserLogic(ctx, svcCtx)
|
|
|
|
// 准备获取用户请求数据
|
|
req := &types.GetUserRequest{
|
|
Id: user.Id,
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.GetUser(req)
|
|
|
|
// 验证结果
|
|
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, 1, resp.Status)
|
|
}
|
|
|
|
// TestGetUser_NotFound 测试用户不存在
|
|
func TestGetUser_NotFound(t *testing.T) {
|
|
svcCtx, cleanup := setupUserTestDB(t)
|
|
defer cleanup()
|
|
|
|
// 创建上下文
|
|
ctx := context.Background()
|
|
|
|
// 创建 Logic 实例
|
|
logic := NewGetUserLogic(ctx, svcCtx)
|
|
|
|
// 使用不存在的用户ID
|
|
req := &types.GetUserRequest{
|
|
Id: 99999,
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.GetUser(req)
|
|
|
|
// 验证结果 - 应该返回错误
|
|
require.Error(t, err)
|
|
require.Nil(t, resp)
|
|
assert.Contains(t, err.Error(), "用户不存在")
|
|
}
|
|
|
|
// TestGetUser_NegativeId 测试使用负数ID
|
|
func TestGetUser_NegativeId(t *testing.T) {
|
|
svcCtx, cleanup := setupUserTestDB(t)
|
|
defer cleanup()
|
|
|
|
// 创建上下文
|
|
ctx := context.Background()
|
|
|
|
// 创建 Logic 实例
|
|
logic := NewGetUserLogic(ctx, svcCtx)
|
|
|
|
// 使用负数ID
|
|
req := &types.GetUserRequest{
|
|
Id: -1,
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.GetUser(req)
|
|
|
|
// 验证结果 - 应该返回错误
|
|
require.Error(t, err)
|
|
require.Nil(t, resp)
|
|
assert.Contains(t, err.Error(), "用户不存在")
|
|
}
|
|
|
|
// TestGetUser_ZeroId 测试使用零ID
|
|
func TestGetUser_ZeroId(t *testing.T) {
|
|
svcCtx, cleanup := setupUserTestDB(t)
|
|
defer cleanup()
|
|
|
|
// 创建上下文
|
|
ctx := context.Background()
|
|
|
|
// 创建 Logic 实例
|
|
logic := NewGetUserLogic(ctx, svcCtx)
|
|
|
|
// 使用零ID
|
|
req := &types.GetUserRequest{
|
|
Id: 0,
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.GetUser(req)
|
|
|
|
// 验证结果 - 应该返回错误
|
|
require.Error(t, err)
|
|
require.Nil(t, resp)
|
|
assert.Contains(t, err.Error(), "用户不存在")
|
|
}
|
|
|
|
// TestGetUser_UserWithEmptyPhone 测试获取没有手机号的用户
|
|
func TestGetUser_UserWithEmptyPhone(t *testing.T) {
|
|
svcCtx, cleanup := setupUserTestDB(t)
|
|
defer cleanup()
|
|
|
|
// 创建测试用户,手机号为空
|
|
now := time.Now()
|
|
user := &model.User{
|
|
Username: "testuser",
|
|
Email: "test@example.com",
|
|
Password: "encryptedpassword",
|
|
Phone: "",
|
|
Status: 1,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
|
|
err := svcCtx.DB.Create(user).Error
|
|
require.NoError(t, err)
|
|
|
|
// 创建上下文
|
|
ctx := context.Background()
|
|
|
|
// 创建 Logic 实例
|
|
logic := NewGetUserLogic(ctx, svcCtx)
|
|
|
|
// 准备获取用户请求数据
|
|
req := &types.GetUserRequest{
|
|
Id: user.Id,
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.GetUser(req)
|
|
|
|
// 验证结果
|
|
require.NoError(t, err)
|
|
require.NotNil(t, resp)
|
|
assert.Equal(t, "", resp.Phone)
|
|
}
|
|
|
|
// TestGetUser_DisabledUser 测试获取禁用状态的用户
|
|
func TestGetUser_DisabledUser(t *testing.T) {
|
|
svcCtx, cleanup := setupUserTestDB(t)
|
|
defer cleanup()
|
|
|
|
// 创建测试用户,状态为禁用
|
|
now := time.Now()
|
|
user := &model.User{
|
|
Username: "disableduser",
|
|
Email: "disabled@example.com",
|
|
Password: "encryptedpassword",
|
|
Phone: "13800138000",
|
|
Status: 2, // 禁用状态
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
|
|
err := svcCtx.DB.Create(user).Error
|
|
require.NoError(t, err)
|
|
|
|
// 创建上下文
|
|
ctx := context.Background()
|
|
|
|
// 创建 Logic 实例
|
|
logic := NewGetUserLogic(ctx, svcCtx)
|
|
|
|
// 准备获取用户请求数据
|
|
req := &types.GetUserRequest{
|
|
Id: user.Id,
|
|
}
|
|
|
|
// 执行测试
|
|
resp, err := logic.GetUser(req)
|
|
|
|
// 验证结果
|
|
require.NoError(t, err)
|
|
require.NotNil(t, resp)
|
|
assert.Equal(t, 2, resp.Status)
|
|
}
|
|
|
|
// BenchmarkGetUser 性能测试
|
|
func BenchmarkGetUser(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.GetUserRequest{
|
|
Id: user.Id,
|
|
}
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
logic := NewGetUserLogic(ctx, svcCtx)
|
|
_, _ = logic.GetUser(req)
|
|
}
|
|
}
|
|
|