test: 补齐 handler/repository/domain 层单元测试
This commit is contained in:
136
internal/domain/device_test.go
Normal file
136
internal/domain/device_test.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestDeviceType_Constants(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value DeviceType
|
||||
expected int
|
||||
}{
|
||||
{"Unknown", DeviceTypeUnknown, 0},
|
||||
{"Web", DeviceTypeWeb, 1},
|
||||
{"Mobile", DeviceTypeMobile, 2},
|
||||
{"Desktop", DeviceTypeDesktop, 3},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if int(tc.value) != tc.expected {
|
||||
t.Errorf("expected %d, got %d", tc.expected, int(tc.value))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeviceStatus_Constants(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value DeviceStatus
|
||||
expected int
|
||||
}{
|
||||
{"Inactive", DeviceStatusInactive, 0},
|
||||
{"Active", DeviceStatusActive, 1},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if int(tc.value) != tc.expected {
|
||||
t.Errorf("expected %d, got %d", tc.expected, int(tc.value))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDevice_TableName(t *testing.T) {
|
||||
var d Device
|
||||
if got := d.TableName(); got != "devices" {
|
||||
t.Errorf("expected table name 'devices', got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDevice_StructFields(t *testing.T) {
|
||||
now := time.Now()
|
||||
trustExpires := now.Add(24 * time.Hour)
|
||||
|
||||
d := Device{
|
||||
ID: 1,
|
||||
UserID: 2,
|
||||
DeviceID: "device-123",
|
||||
DeviceName: "Test Device",
|
||||
DeviceType: DeviceTypeWeb,
|
||||
DeviceOS: "Windows",
|
||||
DeviceBrowser: "Chrome",
|
||||
IP: "127.0.0.1",
|
||||
Location: "Beijing",
|
||||
IsTrusted: true,
|
||||
TrustExpiresAt: &trustExpires,
|
||||
Status: DeviceStatusActive,
|
||||
LastActiveTime: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
if d.ID != 1 {
|
||||
t.Errorf("expected ID 1, got %d", d.ID)
|
||||
}
|
||||
if d.UserID != 2 {
|
||||
t.Errorf("expected UserID 2, got %d", d.UserID)
|
||||
}
|
||||
if d.DeviceID != "device-123" {
|
||||
t.Errorf("expected DeviceID 'device-123', got %q", d.DeviceID)
|
||||
}
|
||||
if d.DeviceName != "Test Device" {
|
||||
t.Errorf("expected DeviceName 'Test Device', got %q", d.DeviceName)
|
||||
}
|
||||
if d.DeviceType != DeviceTypeWeb {
|
||||
t.Errorf("expected DeviceTypeWeb, got %d", d.DeviceType)
|
||||
}
|
||||
if d.DeviceOS != "Windows" {
|
||||
t.Errorf("expected DeviceOS 'Windows', got %q", d.DeviceOS)
|
||||
}
|
||||
if d.DeviceBrowser != "Chrome" {
|
||||
t.Errorf("expected DeviceBrowser 'Chrome', got %q", d.DeviceBrowser)
|
||||
}
|
||||
if d.IP != "127.0.0.1" {
|
||||
t.Errorf("expected IP '127.0.0.1', got %q", d.IP)
|
||||
}
|
||||
if d.Location != "Beijing" {
|
||||
t.Errorf("expected Location 'Beijing', got %q", d.Location)
|
||||
}
|
||||
if !d.IsTrusted {
|
||||
t.Error("expected IsTrusted to be true")
|
||||
}
|
||||
if d.TrustExpiresAt == nil || !d.TrustExpiresAt.Equal(trustExpires) {
|
||||
t.Error("expected TrustExpiresAt to match")
|
||||
}
|
||||
if d.Status != DeviceStatusActive {
|
||||
t.Errorf("expected DeviceStatusActive, got %d", d.Status)
|
||||
}
|
||||
if d.LastActiveTime.IsZero() {
|
||||
t.Error("expected LastActiveTime to be set")
|
||||
}
|
||||
if d.CreatedAt.IsZero() {
|
||||
t.Error("expected CreatedAt to be set")
|
||||
}
|
||||
if d.UpdatedAt.IsZero() {
|
||||
t.Error("expected UpdatedAt to be set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDevice_DefaultStatus(t *testing.T) {
|
||||
var d Device
|
||||
if d.Status != DeviceStatusInactive {
|
||||
t.Errorf("expected default status Inactive(0), got %d", d.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDevice_DefaultDeviceType(t *testing.T) {
|
||||
var d Device
|
||||
if d.DeviceType != DeviceTypeUnknown {
|
||||
t.Errorf("expected default device type Unknown(0), got %d", d.DeviceType)
|
||||
}
|
||||
}
|
||||
35
internal/domain/password_history_test.go
Normal file
35
internal/domain/password_history_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPasswordHistory_TableName(t *testing.T) {
|
||||
var h PasswordHistory
|
||||
if got := h.TableName(); got != "password_histories" {
|
||||
t.Errorf("expected table name 'password_histories', got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPasswordHistory_StructTags(t *testing.T) {
|
||||
h := PasswordHistory{
|
||||
ID: 1,
|
||||
UserID: 2,
|
||||
PasswordHash: "hash123",
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if h.ID != 1 {
|
||||
t.Errorf("expected ID 1, got %d", h.ID)
|
||||
}
|
||||
if h.UserID != 2 {
|
||||
t.Errorf("expected UserID 2, got %d", h.UserID)
|
||||
}
|
||||
if h.PasswordHash != "hash123" {
|
||||
t.Errorf("expected PasswordHash 'hash123', got %q", h.PasswordHash)
|
||||
}
|
||||
if h.CreatedAt.IsZero() {
|
||||
t.Error("expected CreatedAt to be set")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user