Files
backend/internal/repository/profile_repository_test.go
lan 4b4980820f
Some checks failed
SonarQube Analysis / sonarqube (push) Has been cancelled
Test / test (push) Has been cancelled
Test / lint (push) Has been cancelled
Test / build (push) Has been cancelled
chore: 初始化仓库,排除二进制文件和覆盖率文件
2025-11-28 23:30:49 +08:00

185 lines
4.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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_SetActiveLogic 测试设置活跃档案的逻辑
func TestProfileRepository_SetActiveLogic(t *testing.T) {
tests := []struct {
name string
uuid string
userID int64
otherProfiles int
wantAllInactive bool
}{
{
name: "设置一个档案为活跃,其他应该变为非活跃",
uuid: "profile-1",
userID: 1,
otherProfiles: 2,
wantAllInactive: true,
},
{
name: "只有一个档案时",
uuid: "profile-1",
userID: 1,
otherProfiles: 0,
wantAllInactive: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// 验证逻辑:设置一个档案为活跃时,应该先将所有档案设为非活跃
if !tt.wantAllInactive {
t.Error("Setting active profile should first set all profiles to inactive")
}
})
}
}
// 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)
}
})
}
}