-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
callbacks_test.go
39 lines (31 loc) · 939 Bytes
/
callbacks_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package schema_test
import (
"reflect"
"sync"
"testing"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
type UserWithCallback struct{}
func (UserWithCallback) BeforeSave(*gorm.DB) error {
return nil
}
func (UserWithCallback) AfterCreate(*gorm.DB) error {
return nil
}
func TestCallback(t *testing.T) {
user, err := schema.Parse(&UserWithCallback{}, &sync.Map{}, schema.NamingStrategy{})
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)
}
}
}