forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks_test.go
89 lines (73 loc) · 1.79 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package pop
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_Callbacks(t *testing.T) {
if PDB == nil {
t.Skip("skipping integration tests")
}
transaction(func(tx *Connection) {
r := require.New(t)
user := &CallbacksUser{
BeforeS: "BS",
BeforeC: "BC",
BeforeU: "BU",
BeforeD: "BD",
BeforeV: "BV",
AfterS: "AS",
AfterC: "AC",
AfterU: "AU",
AfterD: "AD",
AfterF: "AF",
}
r.NoError(tx.Save(user))
r.Equal("BeforeSave", user.BeforeS)
r.Equal("BeforeCreate", user.BeforeC)
r.Equal("AfterSave", user.AfterS)
r.Equal("AfterCreate", user.AfterC)
r.Equal("BU", user.BeforeU)
r.Equal("AU", user.AfterU)
r.NoError(tx.Update(user))
r.Equal("BeforeUpdate", user.BeforeU)
r.Equal("AfterUpdate", user.AfterU)
r.Equal("BD", user.BeforeD)
r.Equal("AD", user.AfterD)
r.Equal("AF", user.AfterF)
r.NoError(tx.Find(user, user.ID))
r.Equal("AfterFind", user.AfterF)
r.Empty(user.AfterEF)
r.NoError(tx.Eager().Find(user, user.ID))
r.Equal("AfterEagerFind", user.AfterEF)
r.NoError(tx.Destroy(user))
r.Equal("BeforeDestroy", user.BeforeD)
r.Equal("AfterDestroy", user.AfterD)
verrs, err := tx.ValidateAndSave(user)
r.False(verrs.HasAny())
r.NoError(err)
r.Equal("BeforeValidate", user.BeforeV)
})
}
func Test_Callbacks_on_Slice(t *testing.T) {
if PDB == nil {
t.Skip("skipping integration tests")
}
transaction(func(tx *Connection) {
r := require.New(t)
for i := 0; i < 2; i++ {
r.NoError(tx.Create(&CallbacksUser{}))
}
users := CallbacksUsers{}
r.NoError(tx.All(&users))
r.Len(users, 2)
for _, u := range users {
r.Equal("AfterFind", u.AfterF)
r.Empty(u.AfterEF)
}
r.NoError(tx.Eager().All(&users))
r.Len(users, 2)
for _, u := range users {
r.Equal("AfterEagerFind", u.AfterEF)
}
})
}