2024-07-19 08:03:35 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBaseModel_BeforeCreate(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
ID string
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt gorm.DeletedAt
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test 1: Prefilled ID",
|
|
|
|
fields: fields{
|
|
|
|
ID: "1",
|
|
|
|
},
|
|
|
|
args: args{db: nil},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test 1: Autogenerate ID",
|
|
|
|
fields: fields{
|
|
|
|
ID: "",
|
|
|
|
},
|
|
|
|
args: args{db: nil},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-08-09 19:37:54 +00:00
|
|
|
base := &BaseModel[PostID]{
|
|
|
|
ID: PostID(tt.fields.ID),
|
2024-07-19 08:03:35 +00:00
|
|
|
CreatedAt: tt.fields.CreatedAt,
|
|
|
|
UpdatedAt: tt.fields.UpdatedAt,
|
|
|
|
DeletedAt: tt.fields.DeletedAt,
|
|
|
|
}
|
|
|
|
if err := base.BeforeCreate(tt.args.db); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("BeforeCreate() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|