38 lines
864 B
Go
38 lines
864 B
Go
package medicine
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestMedicineTableName(t *testing.T) {
|
|
var m Medicine
|
|
if got := m.TableName(); got != "medicines" {
|
|
t.Fatalf("expected table name medicines, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestMedicineBeforeCreate(t *testing.T) {
|
|
t.Run("generates id when empty", func(t *testing.T) {
|
|
m := &Medicine{}
|
|
if err := m.BeforeCreate(&gorm.DB{}); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if len(m.ID) == 0 {
|
|
t.Fatalf("expected id to be generated")
|
|
}
|
|
})
|
|
|
|
t.Run("keeps existing id", func(t *testing.T) {
|
|
existing := []byte("already-set-id")
|
|
m := &Medicine{ID: append([]byte(nil), existing...)}
|
|
if err := m.BeforeCreate(&gorm.DB{}); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if string(m.ID) != string(existing) {
|
|
t.Fatalf("expected existing id to stay unchanged")
|
|
}
|
|
})
|
|
}
|