forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks_test.go
70 lines (54 loc) · 1.33 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
package pop_test
import (
"testing"
"github.com/gobuffalo/pop"
"github.com/stretchr/testify/require"
)
func Test_Callbacks(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)
user := &CallbacksUser{
BeforeS: "BS",
BeforeC: "BC",
BeforeU: "BU",
BeforeD: "BD",
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.NoError(tx.Destroy(user))
r.Equal("BeforeDestroy", user.BeforeD)
r.Equal("AfterDestroy", user.AfterD)
})
}
func Test_Callbacks_on_Slice(t *testing.T) {
transaction(func(tx *pop.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)
}
})
}