38 lines
852 B
Go
38 lines
852 B
Go
package user
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestUserTableName(t *testing.T) {
|
|
var u User
|
|
if got := u.TableName(); got != "users" {
|
|
t.Fatalf("unexpected table name: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestUserBeforeCreate(t *testing.T) {
|
|
t.Run("generates id when empty", func(t *testing.T) {
|
|
u := &User{}
|
|
if err := u.BeforeCreate(&gorm.DB{}); err != nil {
|
|
t.Fatalf("BeforeCreate error: %v", err)
|
|
}
|
|
if len(u.ID) != 16 {
|
|
t.Fatalf("expected generated 16-byte ID, got %d", len(u.ID))
|
|
}
|
|
})
|
|
|
|
t.Run("keeps existing id", func(t *testing.T) {
|
|
existing := []byte("1234567890abcdef")
|
|
u := &User{ID: append([]byte(nil), existing...)}
|
|
if err := u.BeforeCreate(&gorm.DB{}); err != nil {
|
|
t.Fatalf("BeforeCreate error: %v", err)
|
|
}
|
|
if string(u.ID) != string(existing) {
|
|
t.Fatalf("expected existing id to stay unchanged")
|
|
}
|
|
})
|
|
}
|