Debian Codegraph @

internal/integration/reql_tests/reql_writehook_test.go

raw symbols SCIP
1 package reql_tests
2
3 import (
4 "github.com/stretchr/testify/suite"
5 r "gopkg.in/rethinkdb/rethinkdb-go.v6"
6 "gopkg.in/rethinkdb/rethinkdb-go.v6/internal/compare"
7 "testing"
8 )
9
10 // Test edge cases of write hook
11 func TestWriteHookSuite(t *testing.T) {
12 suite.Run(t, new(WriteHook))
13 }
14
15 type WriteHook struct {
16 suite.Suite
17
18 session *r.Session
19 }
20
21 func (suite *WriteHook) SetupTest() {
22 suite.T().Log("Setting up WriteHook")
23
24 session, err := r.Connect(r.ConnectOpts{
25 Address: url,
26 })
27 suite.Require().NoError(err, "Error returned when connecting to server")
28 suite.session = session
29
30 r.DBDrop("db_hook").Exec(suite.session)
31 err = r.DBCreate("db_hook").Exec(suite.session)
32 suite.Require().NoError(err)
33 err = r.DB("db_hook").Wait().Exec(suite.session)
34 suite.Require().NoError(err)
35
36 r.DB("db_hook").TableDrop("test").Exec(suite.session)
37 err = r.DB("db_hook").TableCreate("test").Exec(suite.session)
38 suite.Require().NoError(err)
39 err = r.DB("db_hook").Table("test").Wait().Exec(suite.session)
40 suite.Require().NoError(err)
41 }
42
43 func (suite *WriteHook) TearDownSuite() {
44 suite.T().Log("Tearing down WriteHook")
45
46 if suite.session != nil {
47 err := r.DBDrop("db_hook").Exec(suite.session)
48 suite.Require().NoError(err)
49
50 suite.session.Close()
51 }
52 }
53
54 func (suite *WriteHook) TestCases() {
55 suite.T().Log("Running WriteHook: Test edge cases of write hooks")
56
57 runOpts := r.RunOpts{
58 GeometryFormat: "raw",
59 GroupFormat: "map",
60 }
61
62 table := r.DB("db_hook").Table("test")
63 wcField := "write_counter"
64
65 {
66 var q = table.SetWriteHook(func(id r.Term, oldVal r.Term, newVal r.Term) r.Term {
67 return newVal
68 })
69 var expected_ = compare.PartialMatch(map[string]interface{}{"created": 1})
70
71 suite.T().Logf("About to run line #1: %v", q)
72
73 runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
74 suite.T().Log("Finished running line #1")
75 }
76
77 {
78 var q = table.SetWriteHook(nil)
79 var expected_ = compare.PartialMatch(map[string]interface{}{"deleted": 1})
80
81 suite.T().Logf("About to run line #2: %v", q)
82
83 runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
84 suite.T().Log("Finished running line #2")
85 }
86
87 {
88 var q = table.SetWriteHook(func(id r.Term, oldVal r.Term, newVal r.Term) r.Term {
89 return newVal
90 })
91 var expected_ = compare.PartialMatch(map[string]interface{}{"created": 1})
92
93 suite.T().Logf("About to run line #3: %v", q)
94
95 runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
96 suite.T().Log("Finished running line #3")
97 }
98
99 {
100 var q = table.SetWriteHook(func(id r.Term, oldVal r.Term, newVal r.Term) r.Term {
101 return r.Branch(oldVal.And(newVal),
102 newVal.Merge(map[string]r.Term{wcField: oldVal.Field(wcField).Add(1)}),
103 newVal,
104 newVal.Merge(r.Expr(map[string]int{wcField: 1})),
105 r.Error("no delete"),
106 )
107 })
108 var expected_ = compare.PartialMatch(map[string]interface{}{"replaced": 1})
109
110 suite.T().Logf("About to run line #4: %v", q)
111
112 runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
113 suite.T().Log("Finished running line #4")
114 }
115
116 {
117 var q = table.Insert(map[string]interface{}{"id": 1}, r.InsertOpts{ReturnChanges: true})
118 var expected_ = compare.PartialMatch(map[string]interface{}{
119 "changes": []interface{}{
120 map[string]interface{}{
121 "new_val": map[string]interface{}{"id": 1, wcField: 1},
122 "old_val": interface{}(nil),
123 }},
124 "inserted": 1,
125 })
126
127 suite.T().Logf("About to run line #5: %v", q)
128
129 runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
130 suite.T().Log("Finished running line #5")
131 }
132
133 {
134 var q = table.Replace(map[string]interface{}{"id": 1, "text": "abc"}, r.ReplaceOpts{ReturnChanges: true})
135 var expected_ = compare.PartialMatch(map[string]interface{}{
136 "changes": []interface{}{
137 map[string]interface{}{
138 "new_val": map[string]interface{}{"id": 1, wcField: 2, "text": "abc"},
139 "old_val": map[string]interface{}{"id": 1, wcField: 1},
140 }},
141 "replaced": 1,
142 })
143
144 suite.T().Logf("About to run line #6: %v", q)
145
146 runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
147 suite.T().Log("Finished running line #6")
148 }
149
150 {
151 var q = table.Get(1).Update(map[string]interface{}{"text": "def"}, r.UpdateOpts{ReturnChanges: true})
152 var expected_ = compare.PartialMatch(map[string]interface{}{
153 "changes": []interface{}{
154 map[string]interface{}{
155 "new_val": map[string]interface{}{"id": 1, wcField: 3, "text": "def"},
156 "old_val": map[string]interface{}{"id": 1, wcField: 2, "text": "abc"},
157 }},
158 "replaced": 1,
159 })
160
161 suite.T().Logf("About to run line #7: %v", q)
162
163 runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
164 suite.T().Log("Finished running line #7")
165 }
166
167 {
168 var q = table.Get(1).Delete()
169 var expected_ = compare.PartialMatch(map[string]interface{}{
170 "first_error": "Error in write hook: no delete",
171 "errors": 1,
172 })
173
174 suite.T().Logf("About to run line #8: %v", q)
175
176 runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
177 suite.T().Log("Finished running line #8")
178 }
179
180 {
181 var q = table.Insert(map[string]interface{}{"id": 2}, r.InsertOpts{ReturnChanges: true, IgnoreWriteHook: true})
182 var expected_ = compare.PartialMatch(map[string]interface{}{
183 "changes": []interface{}{
184 map[string]interface{}{
185 "new_val": map[string]interface{}{"id": 2},
186 "old_val": interface{}(nil),
187 }},
188 "inserted": 1,
189 })
190
191 suite.T().Logf("About to run line #9: %v", q)
192
193 runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
194 suite.T().Log("Finished running line #9")
195 }
196
197 {
198 var q = table.Replace(map[string]interface{}{"id": 2, "text": "abc"}, r.ReplaceOpts{ReturnChanges: true, IgnoreWriteHook: true})
199 var expected_ = compare.PartialMatch(map[string]interface{}{
200 "changes": []interface{}{
201 map[string]interface{}{
202 "new_val": map[string]interface{}{"id": 2, "text": "abc"},
203 "old_val": map[string]interface{}{"id": 2},
204 }},
205 "replaced": 1,
206 })
207
208 suite.T().Logf("About to run line #10: %v", q)
209
210 runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
211 suite.T().Log("Finished running line #10")
212 }
213
214 {
215 var q = table.Get(2).Update(map[string]interface{}{"text": "def"}, r.UpdateOpts{ReturnChanges: true, IgnoreWriteHook: true})
216 var expected_ = compare.PartialMatch(map[string]interface{}{
217 "changes": []interface{}{
218 map[string]interface{}{
219 "new_val": map[string]interface{}{"id": 2, "text": "def"},
220 "old_val": map[string]interface{}{"id": 2, "text": "abc"},
221 }},
222 "replaced": 1,
223 })
224
225 suite.T().Logf("About to run line #11: %v", q)
226
227 runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
228 suite.T().Log("Finished running line #11")
229 }
230
231 {
232 var q = table.Get(2).Delete(r.DeleteOpts{IgnoreWriteHook: true})
233 var expected_ = compare.PartialMatch(map[string]interface{}{"deleted": 1})
234
235 suite.T().Logf("About to run line #12: %v", q)
236
237 runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
238 suite.T().Log("Finished running line #12")
239 }
240 }