-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook_test.go
155 lines (129 loc) · 3.17 KB
/
hook_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package pg_test
import (
"gopkg.in/pg.v5"
"gopkg.in/pg.v5/orm"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type HookTest struct {
Id int
Value string
afterQuery int
afterSelect int
beforeInsert int
afterInsert int
beforeUpdate int
afterUpdate int
beforeDelete int
afterDelete int
}
func (t *HookTest) AfterQuery(db orm.DB) error {
t.afterQuery++
return nil
}
func (t *HookTest) AfterSelect(db orm.DB) error {
t.afterSelect++
return nil
}
func (t *HookTest) BeforeInsert(db orm.DB) error {
t.beforeInsert++
return nil
}
func (t *HookTest) AfterInsert(db orm.DB) error {
t.afterInsert++
return nil
}
func (t *HookTest) BeforeUpdate(db orm.DB) error {
t.beforeUpdate++
return nil
}
func (t *HookTest) AfterUpdate(db orm.DB) error {
t.afterUpdate++
return nil
}
func (t *HookTest) BeforeDelete(db orm.DB) error {
t.beforeDelete++
return nil
}
func (t *HookTest) AfterDelete(db orm.DB) error {
t.afterDelete++
return nil
}
var _ = Describe("HookTest", func() {
var db *pg.DB
BeforeEach(func() {
db = pg.Connect(pgOptions())
qs := []string{
"CREATE TEMP TABLE hook_tests (id int, value text)",
"INSERT INTO hook_tests VALUES (1, '')",
}
for _, q := range qs {
_, err := db.Exec(q)
Expect(err).NotTo(HaveOccurred())
}
})
AfterEach(func() {
Expect(db.Close()).NotTo(HaveOccurred())
})
It("calls AfterQuery for struct", func() {
var hook HookTest
_, err := db.QueryOne(&hook, "SELECT 1 AS id")
Expect(err).NotTo(HaveOccurred())
Expect(hook.afterQuery).To(Equal(1))
Expect(hook.afterSelect).To(Equal(0))
})
It("calls AfterQuery and AfterSelect for struct model", func() {
var hook HookTest
err := db.Model(&hook).Select()
Expect(err).NotTo(HaveOccurred())
Expect(hook.afterQuery).To(Equal(1))
Expect(hook.afterSelect).To(Equal(1))
})
It("calls AfterQuery for slice", func() {
var hooks []HookTest
_, err := db.Query(&hooks, "SELECT 1 AS id")
Expect(err).NotTo(HaveOccurred())
Expect(hooks).To(HaveLen(1))
Expect(hooks[0].afterQuery).To(Equal(1))
Expect(hooks[0].afterSelect).To(Equal(0))
})
It("calls AfterQuery and AfterSelect for slice model", func() {
var hooks []HookTest
err := db.Model(&hooks).Select()
Expect(err).NotTo(HaveOccurred())
Expect(hooks).To(HaveLen(1))
Expect(hooks[0].afterQuery).To(Equal(1))
Expect(hooks[0].afterSelect).To(Equal(1))
})
It("calls BeforeInsert and AfterInsert", func() {
hook := &HookTest{
Id: 1,
Value: "value",
}
err := db.Insert(&hook)
Expect(err).NotTo(HaveOccurred())
Expect(hook.afterQuery).To(Equal(0))
Expect(hook.beforeInsert).To(Equal(1))
Expect(hook.afterInsert).To(Equal(1))
})
It("calls BeforeUpdate and AfterUpdate", func() {
hook := &HookTest{
Id: 1,
}
err := db.Update(&hook)
Expect(err).NotTo(HaveOccurred())
Expect(hook.afterQuery).To(Equal(0))
Expect(hook.beforeUpdate).To(Equal(1))
Expect(hook.afterUpdate).To(Equal(1))
})
It("calls BeforeDelete and AfterDelete", func() {
hook := &HookTest{
Id: 1,
}
err := db.Delete(&hook)
Expect(err).NotTo(HaveOccurred())
Expect(hook.afterQuery).To(Equal(0))
Expect(hook.beforeDelete).To(Equal(1))
Expect(hook.afterDelete).To(Equal(1))
})
})