2025-11-28 23:30:49 +08:00
|
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"testing"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// TestProfileRepository_QueryConditions 测试档案查询条件逻辑
|
|
|
|
|
|
func TestProfileRepository_QueryConditions(t *testing.T) {
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
|
name string
|
|
|
|
|
|
uuid string
|
|
|
|
|
|
userID int64
|
|
|
|
|
|
wantValid bool
|
|
|
|
|
|
}{
|
|
|
|
|
|
{
|
|
|
|
|
|
name: "有效的UUID",
|
|
|
|
|
|
uuid: "123e4567-e89b-12d3-a456-426614174000",
|
|
|
|
|
|
userID: 1,
|
|
|
|
|
|
wantValid: true,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
name: "UUID为空",
|
|
|
|
|
|
uuid: "",
|
|
|
|
|
|
userID: 1,
|
|
|
|
|
|
wantValid: false,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
name: "用户ID为0",
|
|
|
|
|
|
uuid: "123e4567-e89b-12d3-a456-426614174000",
|
|
|
|
|
|
userID: 0,
|
|
|
|
|
|
wantValid: false,
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
|
isValid := tt.uuid != "" && tt.userID > 0
|
|
|
|
|
|
if isValid != tt.wantValid {
|
|
|
|
|
|
t.Errorf("Query condition validation failed: got %v, want %v", isValid, tt.wantValid)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TestProfileRepository_CountLogic 测试统计逻辑
|
|
|
|
|
|
func TestProfileRepository_CountLogic(t *testing.T) {
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
|
name string
|
|
|
|
|
|
userID int64
|
|
|
|
|
|
wantCount int64
|
|
|
|
|
|
}{
|
|
|
|
|
|
{
|
|
|
|
|
|
name: "有效用户ID",
|
|
|
|
|
|
userID: 1,
|
|
|
|
|
|
wantCount: 0, // 实际值取决于数据库
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
name: "用户ID为0",
|
|
|
|
|
|
userID: 0,
|
|
|
|
|
|
wantCount: 0,
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
|
// 验证统计逻辑:用户ID应该大于0
|
|
|
|
|
|
if tt.userID <= 0 && tt.wantCount != 0 {
|
|
|
|
|
|
t.Error("Invalid userID should not count profiles")
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TestProfileRepository_UpdateFieldsLogic 测试更新字段逻辑
|
|
|
|
|
|
func TestProfileRepository_UpdateFieldsLogic(t *testing.T) {
|
|
|
|
|
|
tests := []struct {
|
2025-12-07 20:51:20 +08:00
|
|
|
|
name string
|
|
|
|
|
|
uuid string
|
|
|
|
|
|
updates map[string]interface{}
|
2025-11-28 23:30:49 +08:00
|
|
|
|
wantValid bool
|
|
|
|
|
|
}{
|
|
|
|
|
|
{
|
|
|
|
|
|
name: "有效的更新",
|
|
|
|
|
|
uuid: "123e4567-e89b-12d3-a456-426614174000",
|
|
|
|
|
|
updates: map[string]interface{}{
|
2025-12-07 20:51:20 +08:00
|
|
|
|
"name": "NewName",
|
2025-11-28 23:30:49 +08:00
|
|
|
|
"skin_id": int64(1),
|
|
|
|
|
|
},
|
|
|
|
|
|
wantValid: true,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
2025-12-07 20:51:20 +08:00
|
|
|
|
name: "UUID为空",
|
|
|
|
|
|
uuid: "",
|
|
|
|
|
|
updates: map[string]interface{}{"name": "NewName"},
|
2025-11-28 23:30:49 +08:00
|
|
|
|
wantValid: false,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
2025-12-07 20:51:20 +08:00
|
|
|
|
name: "更新字段为空",
|
|
|
|
|
|
uuid: "123e4567-e89b-12d3-a456-426614174000",
|
|
|
|
|
|
updates: map[string]interface{}{},
|
2025-11-28 23:30:49 +08:00
|
|
|
|
wantValid: true, // 空更新也是有效的,只是不会更新任何字段
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
|
isValid := tt.uuid != "" && tt.updates != nil
|
|
|
|
|
|
if isValid != tt.wantValid {
|
|
|
|
|
|
t.Errorf("Update fields validation failed: got %v, want %v", isValid, tt.wantValid)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TestProfileRepository_FindOneProfileLogic 测试查找单个档案的逻辑
|
|
|
|
|
|
func TestProfileRepository_FindOneProfileLogic(t *testing.T) {
|
|
|
|
|
|
tests := []struct {
|
2025-12-07 20:51:20 +08:00
|
|
|
|
name string
|
2025-11-28 23:30:49 +08:00
|
|
|
|
profileCount int
|
2025-12-07 20:51:20 +08:00
|
|
|
|
wantError bool
|
2025-11-28 23:30:49 +08:00
|
|
|
|
}{
|
|
|
|
|
|
{
|
2025-12-07 20:51:20 +08:00
|
|
|
|
name: "有档案时返回第一个",
|
2025-11-28 23:30:49 +08:00
|
|
|
|
profileCount: 1,
|
2025-12-07 20:51:20 +08:00
|
|
|
|
wantError: false,
|
2025-11-28 23:30:49 +08:00
|
|
|
|
},
|
|
|
|
|
|
{
|
2025-12-07 20:51:20 +08:00
|
|
|
|
name: "多个档案时返回第一个",
|
2025-11-28 23:30:49 +08:00
|
|
|
|
profileCount: 3,
|
2025-12-07 20:51:20 +08:00
|
|
|
|
wantError: false,
|
2025-11-28 23:30:49 +08:00
|
|
|
|
},
|
|
|
|
|
|
{
|
2025-12-07 20:51:20 +08:00
|
|
|
|
name: "没有档案时应该错误",
|
2025-11-28 23:30:49 +08:00
|
|
|
|
profileCount: 0,
|
2025-12-07 20:51:20 +08:00
|
|
|
|
wantError: true,
|
2025-11-28 23:30:49 +08:00
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
|
// 验证逻辑:如果没有档案,访问索引0会panic或返回错误
|
|
|
|
|
|
hasError := tt.profileCount == 0
|
|
|
|
|
|
if hasError != tt.wantError {
|
|
|
|
|
|
t.Errorf("FindOneProfile logic failed: got error=%v, want error=%v", hasError, tt.wantError)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|