173 lines
3.8 KiB
Go
173 lines
3.8 KiB
Go
|
|
package service
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"carrotskin/internal/model"
|
|||
|
|
"testing"
|
|||
|
|
|
|||
|
|
"go.uber.org/zap/zaptest"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// 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)
|
|||
|
|
user := &model.User{
|
|||
|
|
ID: 1,
|
|||
|
|
Username: "testuser",
|
|||
|
|
Email: "test@example.com",
|
|||
|
|
Properties: "{}",
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
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"])
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if result["properties"] == nil {
|
|||
|
|
t.Error("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)
|
|||
|
|
}
|
|||
|
|
}
|