Files
backend/internal/service/serialize_service_test.go
lafay 801f1b1397 refactor: Implement dependency injection for handlers and services
- Refactored AuthHandler, UserHandler, TextureHandler, ProfileHandler, CaptchaHandler, and YggdrasilHandler to use dependency injection.
- Removed direct instantiation of services and repositories within handlers, replacing them with constructor injection.
- Updated the container to initialize service instances and provide them to handlers.
- Enhanced code structure for better testability and adherence to Go best practices.
2025-12-02 19:47:04 +08:00

200 lines
4.6 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 service
import (
"carrotskin/internal/model"
"testing"
"go.uber.org/zap/zaptest"
"gorm.io/datatypes"
)
// TestSerializeUser_NilUser 实际调用SerializeUser函数测试nil用户
func TestSerializeUser_NilUser(t *testing.T) {
logger := zaptest.NewLogger(t)
result := SerializeUser(logger, nil, "test-uuid")
if result != nil {
t.Error("SerializeUser() 对于nil用户应返回nil")
}
}
// TestSerializeUser_ActualCall 实际调用SerializeUser函数
func TestSerializeUser_ActualCall(t *testing.T) {
logger := zaptest.NewLogger(t)
t.Run("Properties为nil时", func(t *testing.T) {
user := &model.User{
ID: 1,
Username: "testuser",
Email: "test@example.com",
}
result := SerializeUser(logger, user, "test-uuid-123")
if result == nil {
t.Fatal("SerializeUser() 返回的结果不应为nil")
}
if result["id"] != "test-uuid-123" {
t.Errorf("id = %v, want 'test-uuid-123'", result["id"])
}
// 当 Properties 为 nil 时properties 应该为 nil
if result["properties"] != nil {
t.Error("当 user.Properties 为 nil 时properties 应为 nil")
}
})
t.Run("Properties有值时", func(t *testing.T) {
propsJSON := datatypes.JSON(`[{"name":"test","value":"value"}]`)
user := &model.User{
ID: 1,
Username: "testuser",
Email: "test@example.com",
Properties: &propsJSON,
}
result := SerializeUser(logger, user, "test-uuid-456")
if result == nil {
t.Fatal("SerializeUser() 返回的结果不应为nil")
}
if result["id"] != "test-uuid-456" {
t.Errorf("id = %v, want 'test-uuid-456'", result["id"])
}
if result["properties"] == nil {
t.Error("当 user.Properties 有值时properties 不应为 nil")
}
})
}
// TestProperty_Structure 测试Property结构
func TestProperty_Structure(t *testing.T) {
prop := Property{
Name: "textures",
Value: "base64value",
Signature: "signature",
}
if prop.Name == "" {
t.Error("Property name should not be empty")
}
if prop.Value == "" {
t.Error("Property value should not be empty")
}
// Signature是可选的
if prop.Signature == "" {
t.Log("Property signature is optional")
}
}
// TestSerializeService_PropertyFields 测试Property字段
func TestSerializeService_PropertyFields(t *testing.T) {
tests := []struct {
name string
property Property
wantValid bool
}{
{
name: "有效的Property",
property: Property{
Name: "textures",
Value: "base64value",
Signature: "signature",
},
wantValid: true,
},
{
name: "缺少Name的Property",
property: Property{
Name: "",
Value: "base64value",
Signature: "signature",
},
wantValid: false,
},
{
name: "缺少Value的Property",
property: Property{
Name: "textures",
Value: "",
Signature: "signature",
},
wantValid: false,
},
{
name: "没有Signature的Property有效",
property: Property{
Name: "textures",
Value: "base64value",
Signature: "",
},
wantValid: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
isValid := tt.property.Name != "" && tt.property.Value != ""
if isValid != tt.wantValid {
t.Errorf("Property validation failed: got %v, want %v", isValid, tt.wantValid)
}
})
}
}
// TestSerializeUser_InputValidation 测试SerializeUser输入验证
func TestSerializeUser_InputValidation(t *testing.T) {
tests := []struct {
name string
user *struct{}
wantValid bool
}{
{
name: "用户不为nil",
user: &struct{}{},
wantValid: true,
},
{
name: "用户为nil",
user: nil,
wantValid: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
isValid := tt.user != nil
if isValid != tt.wantValid {
t.Errorf("Input validation failed: got %v, want %v", isValid, tt.wantValid)
}
})
}
}
// TestSerializeProfile_Structure 测试SerializeProfile返回结构
func TestSerializeProfile_Structure(t *testing.T) {
// 测试返回的数据结构应该包含的字段
expectedFields := []string{"id", "name", "properties"}
// 验证字段名称
for _, field := range expectedFields {
if field == "" {
t.Error("Field name should not be empty")
}
}
// 验证properties应该是数组
// 注意:这里只测试逻辑,不测试实际序列化
}
// TestSerializeProfile_PropertyName 测试Property名称
func TestSerializeProfile_PropertyName(t *testing.T) {
// textures是固定的属性名
propertyName := "textures"
if propertyName != "textures" {
t.Errorf("Property name = %s, want 'textures'", propertyName)
}
}