callbacks_test.go 925 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package schema_test
  2. import (
  3. "reflect"
  4. "sync"
  5. "testing"
  6. "github.com/jinzhu/gorm"
  7. "github.com/jinzhu/gorm/schema"
  8. )
  9. type UserWithCallback struct {
  10. }
  11. func (UserWithCallback) BeforeSave(*gorm.DB) {
  12. }
  13. func (UserWithCallback) AfterCreate(*gorm.DB) {
  14. }
  15. func TestCallback(t *testing.T) {
  16. user, err := schema.Parse(&UserWithCallback{}, &sync.Map{}, schema.NamingStrategy{})
  17. if err != nil {
  18. t.Fatalf("failed to parse user with callback, got error %v", err)
  19. }
  20. for _, str := range []string{"BeforeSave", "AfterCreate"} {
  21. if !reflect.Indirect(reflect.ValueOf(user)).FieldByName(str).Interface().(bool) {
  22. t.Errorf("%v should be true", str)
  23. }
  24. }
  25. for _, str := range []string{"BeforeCreate", "BeforeUpdate", "AfterUpdate", "AfterSave", "BeforeDelete", "AfterDelete", "AfterFind"} {
  26. if reflect.Indirect(reflect.ValueOf(user)).FieldByName(str).Interface().(bool) {
  27. t.Errorf("%v should be false", str)
  28. }
  29. }
  30. }