Files
user-system/internal/domain/user_test.go

82 lines
1.9 KiB
Go
Raw Normal View History

package domain
import (
"testing"
"time"
)
// TestUserModel 测试User模型基本属性
func TestUserModel(t *testing.T) {
u := &User{
Username: "testuser",
Email: StrPtr("test@example.com"),
Phone: StrPtr("13800138000"),
Password: "hashedpassword",
Status: UserStatusActive,
Gender: GenderMale,
CreatedAt: time.Now(),
}
if u.Username != "testuser" {
t.Errorf("Username = %v, want testuser", u.Username)
}
if u.Status != UserStatusActive {
t.Errorf("Status = %v, want %v", u.Status, UserStatusActive)
}
}
// TestUserTableName 测试User表名
func TestUserTableName(t *testing.T) {
u := User{}
if u.TableName() != "users" {
t.Errorf("TableName() = %v, want users", u.TableName())
}
}
// TestUserStatusConstants 测试用户状态常量值
func TestUserStatusConstants(t *testing.T) {
cases := []struct {
status UserStatus
value int
}{
{UserStatusInactive, 0},
{UserStatusActive, 1},
{UserStatusLocked, 2},
{UserStatusDisabled, 3},
}
for _, c := range cases {
if int(c.status) != c.value {
t.Errorf("UserStatus = %d, want %d", c.status, c.value)
}
}
}
// TestGenderConstants 测试性别常量
func TestGenderConstants(t *testing.T) {
if int(GenderUnknown) != 0 {
t.Errorf("GenderUnknown = %d, want 0", GenderUnknown)
}
if int(GenderMale) != 1 {
t.Errorf("GenderMale = %d, want 1", GenderMale)
}
if int(GenderFemale) != 2 {
t.Errorf("GenderFemale = %d, want 2", GenderFemale)
}
}
// TestUserActiveCheck 测试用户激活状态检查
func TestUserActiveCheck(t *testing.T) {
active := &User{Status: UserStatusActive}
inactive := &User{Status: UserStatusInactive}
locked := &User{Status: UserStatusLocked}
disabled := &User{Status: UserStatusDisabled}
if active.Status != UserStatusActive {
t.Error("active用户应为Active状态")
}
if inactive.Status == UserStatusActive {
t.Error("inactive用户不应为Active状态")
}
_ = locked
_ = disabled
}