gorm/schema/callbacks_test.go

40 lines
939 B
Go
Raw Permalink Normal View History

2020-02-23 16:22:35 +03:00
package schema_test
import (
"reflect"
"sync"
"testing"
2020-06-02 04:16:07 +03:00
"gorm.io/gorm"
"gorm.io/gorm/schema"
2020-02-23 16:22:35 +03:00
)
type UserWithCallback struct{}
2020-02-23 16:22:35 +03:00
2020-05-31 18:55:56 +03:00
func (UserWithCallback) BeforeSave(*gorm.DB) error {
return nil
2020-02-23 16:22:35 +03:00
}
2020-05-31 18:55:56 +03:00
func (UserWithCallback) AfterCreate(*gorm.DB) error {
return nil
2020-02-23 16:22:35 +03:00
}
func TestCallback(t *testing.T) {
2020-02-24 03:51:35 +03:00
user, err := schema.Parse(&UserWithCallback{}, &sync.Map{}, schema.NamingStrategy{})
2020-02-23 16:22:35 +03:00
if err != nil {
t.Fatalf("failed to parse user with callback, got error %v", err)
}
for _, str := range []string{"BeforeSave", "AfterCreate"} {
if !reflect.Indirect(reflect.ValueOf(user)).FieldByName(str).Interface().(bool) {
t.Errorf("%v should be true", str)
}
}
for _, str := range []string{"BeforeCreate", "BeforeUpdate", "AfterUpdate", "AfterSave", "BeforeDelete", "AfterDelete", "AfterFind"} {
if reflect.Indirect(reflect.ValueOf(user)).FieldByName(str).Interface().(bool) {
t.Errorf("%v should be false", str)
}
}
}