149 lines
3.3 KiB
Go
149 lines
3.3 KiB
Go
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 {
|
||
name string
|
||
uuid string
|
||
updates map[string]interface{}
|
||
wantValid bool
|
||
}{
|
||
{
|
||
name: "有效的更新",
|
||
uuid: "123e4567-e89b-12d3-a456-426614174000",
|
||
updates: map[string]interface{}{
|
||
"name": "NewName",
|
||
"skin_id": int64(1),
|
||
},
|
||
wantValid: true,
|
||
},
|
||
{
|
||
name: "UUID为空",
|
||
uuid: "",
|
||
updates: map[string]interface{}{"name": "NewName"},
|
||
wantValid: false,
|
||
},
|
||
{
|
||
name: "更新字段为空",
|
||
uuid: "123e4567-e89b-12d3-a456-426614174000",
|
||
updates: map[string]interface{}{},
|
||
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 {
|
||
name string
|
||
profileCount int
|
||
wantError bool
|
||
}{
|
||
{
|
||
name: "有档案时返回第一个",
|
||
profileCount: 1,
|
||
wantError: false,
|
||
},
|
||
{
|
||
name: "多个档案时返回第一个",
|
||
profileCount: 3,
|
||
wantError: false,
|
||
},
|
||
{
|
||
name: "没有档案时应该错误",
|
||
profileCount: 0,
|
||
wantError: true,
|
||
},
|
||
}
|
||
|
||
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)
|
||
}
|
||
})
|
||
}
|
||
}
|