generated from bool64/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbsteps_test.go
376 lines (320 loc) · 14 KB
/
dbsteps_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package dbsteps_test
import (
"bytes"
"database/sql"
"database/sql/driver"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/bool64/sqluct"
"github.com/cucumber/godog"
"github.com/godogx/dbsteps"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
)
func mustParseTime(value string) time.Time {
var (
t time.Time
err error
)
for _, layout := range []string{time.RFC3339, time.RFC3339Nano, "2006-01-02", "2006-01-02 15:04:05"} {
t, err = time.Parse(layout, value)
if err == nil {
break
}
}
if err != nil {
panic(err)
}
return t
}
func TestManager_RegisterContext(t *testing.T) {
type RowKey struct {
Foo *string `db:"foo"`
Bar sql.NullString `db:"bar"`
}
type row struct {
ID int `db:"id"`
RowKey
CreatedAt time.Time `db:"created_at"`
DeletedAt *time.Time `db:"deleted_at"`
}
dbm := dbsteps.NewManager()
db, mock, err := sqlmock.New()
assert.NoError(t, err)
dbm.Instances = map[string]dbsteps.Instance{
"my_db": {
Storage: sqluct.NewStorage(sqlx.NewDb(db, "sqlmock")),
Tables: map[string]interface{}{
"my_table": new(row),
"my_another_table": new(row),
},
},
}
// Given there are no rows in table "my_table" of database "my_db"
mock.ExpectExec(`DELETE FROM my_table`).
WillReturnResult(driver.ResultNoRows)
// And rows from this file are stored in table "my_table" of database "my_db"
// """
// _testdata/rows.csv
// """
mock.ExpectExec(`INSERT INTO my_table \(id,created_at,deleted_at,foo,bar\) VALUES .+`).
WithArgs(
1, mustParseTime("2021-01-01T00:00:00Z"), nil, "foo-1", "abc",
2, mustParseTime("2021-01-02T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z"), "foo-1", "def",
3, mustParseTime("2021-01-03T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z"), "foo-2", "hij",
).
WillReturnResult(driver.ResultNoRows)
// And these rows are stored in table "my_table" of database "my_db":
// | id | foo | bar | created_at | deleted_at |
// | 1 | foo-1 | abc | 2021-01-01T00:00:00Z | NULL |
mock.ExpectExec(`INSERT INTO my_table \(id,created_at,deleted_at,foo,bar\) VALUES .+`).
WithArgs(
1, mustParseTime("2021-01-01T00:00:00Z"), nil, "foo-1", "abc",
).
WillReturnResult(driver.ResultNoRows)
// Then only these rows are available in table "my_table" of database "my_db":
// | id | foo | bar | created_at | deleted_at |
// | 1 | foo-1 | abc | 2021-01-01T00:00:00Z | NULL |
// | 2 | foo-1 | def | 2021-01-02T00:00:00Z | 2021-01-03T00:00:00Z |
// | 3 | foo-2 | hij | 2021-01-03T00:00:00Z | 2021-01-03T00:00:00Z |
mock.ExpectQuery(`SELECT COUNT\(1\) AS c FROM my_table`).WillReturnRows(sqlmock.NewRows([]string{"c"}).AddRow(3))
mock.ExpectQuery(`SELECT .+ FROM my_table WHERE bar = \$1 AND created_at = \$2 AND deleted_at IS NULL`).
WithArgs(
"abc", mustParseTime("2021-01-01T00:00:00Z"),
).
WillReturnRows(sqlmock.NewRows([]string{"id", "foo", "bar", "created_at", "deleted_at"}).
AddRow(1, "foo-1", "abc", mustParseTime("2021-01-01T00:00:00Z"), nil))
mock.ExpectQuery(`SELECT .+ FROM my_table WHERE foo = \$1 AND bar = \$2 AND created_at = \$3 AND deleted_at = \$4`).
WithArgs(
"foo-1", "def", mustParseTime("2021-01-02T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z"),
).
WillReturnRows(sqlmock.NewRows([]string{"id", "foo", "bar", "created_at", "deleted_at"}).
AddRow(2, "foo-1", "def", mustParseTime("2021-01-02T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z")))
mock.ExpectQuery(`SELECT .+ FROM my_table WHERE foo = \$1 AND bar = \$2 AND created_at = \$3 AND deleted_at = \$4`).
WithArgs(
"foo-2", "hij", mustParseTime("2021-01-03T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z"),
).
WillReturnRows(sqlmock.NewRows([]string{"id", "foo", "bar", "created_at", "deleted_at"}).
AddRow(3, "foo-2", "hij", mustParseTime("2021-01-03T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z")))
// Assertion with interpolated variables.
// Then only these rows are available in table "my_table" of database "my_db":
// | id | foo | bar | created_at | deleted_at |
// | <id1> | foo-1 | abc | 2021-01-01T00:00:00Z | NULL |
// | <id2> | foo-1 | def | 2021-01-02T00:00:00Z | 2021-01-03T00:00:00Z |
// | <id3> | foo-2 | hij | 2021-01-03T00:00:00Z | 2021-01-03T00:00:00Z |
mock.ExpectQuery(`SELECT COUNT\(1\) AS c FROM my_table`).WillReturnRows(sqlmock.NewRows([]string{"c"}).AddRow(3))
mock.ExpectQuery(`SELECT .+ FROM my_table WHERE id = \$1 AND foo = \$2 AND bar = \$3 AND created_at = \$4 AND deleted_at IS NULL`).
WithArgs(
1, "foo-1", "abc", mustParseTime("2021-01-01T00:00:00Z"),
).
WillReturnRows(sqlmock.NewRows([]string{"id", "foo", "bar", "created_at", "deleted_at"}).
AddRow(1, "foo-1", "abc", mustParseTime("2021-01-01T00:00:00Z"), nil))
mock.ExpectQuery(`SELECT .+ FROM my_table WHERE id = \$1 AND foo = \$2 AND bar = \$3 AND created_at = \$4 AND deleted_at = \$5`).
WithArgs(
2, "foo-1", "def", mustParseTime("2021-01-02T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z"),
).
WillReturnRows(sqlmock.NewRows([]string{"id", "foo", "bar", "created_at", "deleted_at"}).
AddRow(2, "foo-1", "def", mustParseTime("2021-01-02T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z")))
mock.ExpectQuery(`SELECT .+ FROM my_table WHERE id = \$1 AND foo = \$2 AND bar = \$3 AND created_at = \$4 AND deleted_at = \$5`).
WithArgs(
3, "foo-2", "hij", mustParseTime("2021-01-03T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z"),
).
WillReturnRows(sqlmock.NewRows([]string{"id", "foo", "bar", "created_at", "deleted_at"}).
AddRow(3, "foo-2", "hij", mustParseTime("2021-01-03T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z")))
// And no rows are available in table "my_another_table" of database "my_db"
mock.ExpectQuery(`SELECT COUNT\(1\) AS c FROM my_another_table`).WillReturnRows(sqlmock.NewRows([]string{"c"}).AddRow(0))
buf := bytes.NewBuffer(nil)
suite := godog.TestSuite{
Name: "DatabaseContext",
TestSuiteInitializer: nil,
ScenarioInitializer: func(s *godog.ScenarioContext) {
dbm.RegisterSteps(s)
},
Options: &godog.Options{
Format: "pretty",
Output: buf,
Paths: []string{"_testdata/Database.feature"},
Strict: true,
Randomize: time.Now().UTC().UnixNano(),
},
}
status := suite.Run()
if status != 0 {
t.Fatal(buf.String())
}
}
func TestManager_RegisterContext_fail(t *testing.T) {
type RowKey struct {
Foo string `db:"foo"`
Bar sql.NullString `db:"bar"`
}
type row struct {
ID int `db:"id"`
RowKey
CreatedAt time.Time `db:"created_at"`
DeletedAt *time.Time `db:"deleted_at"`
}
dbm := dbsteps.NewManager()
db, mock, err := sqlmock.New()
assert.NoError(t, err)
dbm.Instances = map[string]dbsteps.Instance{
"my_db": {
Storage: sqluct.NewStorage(sqlx.NewDb(db, "sqlmock")),
Tables: map[string]interface{}{
"my_table": new(row),
},
},
}
mock.ExpectQuery(`SELECT COUNT\(1\) AS c FROM my_table`).WillReturnRows(sqlmock.NewRows([]string{"c"}).AddRow(2))
createdAt := time.Date(2020, 1, 1, 1, 1, 1, 0, time.UTC)
mock.ExpectQuery(`SELECT id, created_at, deleted_at, foo, bar FROM my_table LIMIT 50`).
WillReturnRows(sqlmock.NewRows([]string{"id", "created_at", "deleted_at", "foo", "bar"}).
AddRow(1, createdAt, nil, "my-foo", "bar-1").
AddRow(2, createdAt, nil, "my-foo", "bar-122"))
buf := bytes.NewBuffer(nil)
suite := godog.TestSuite{
Name: "DatabaseContext",
TestSuiteInitializer: nil,
ScenarioInitializer: func(s *godog.ScenarioContext) {
dbm.RegisterSteps(s)
},
Options: &godog.Options{
Format: "pretty",
Output: buf,
Paths: []string{"_testdata/DatabaseFail.feature"},
Strict: true,
},
}
status := suite.Run()
assert.Contains(t, buf.String(), `
| id | foo | bar | created_at | deleted_at |
| 1 | my-foo | bar-1 | 2020-01-01T01:01:01Z | NULL |
| 2 | my-foo | bar-122 | 2020-01-01T01:01:01Z | NULL |
`)
if status == 0 {
t.Fatal(buf.String())
}
}
func TestManager_RegisterContext_default(t *testing.T) {
type RowKey struct {
Foo *string `db:"foo"`
Bar sql.NullString `db:"bar"`
}
type row struct {
ID int `db:"id"`
RowKey
CreatedAt time.Time `db:"created_at"`
DeletedAt *time.Time `db:"deleted_at"`
}
dbm := dbsteps.NewManager()
db, mock, err := sqlmock.New()
assert.NoError(t, err)
dbm.Instances = map[string]dbsteps.Instance{
"default": {
Storage: sqluct.NewStorage(sqlx.NewDb(db, "sqlmock")),
Tables: map[string]interface{}{
"my_table": new(row),
"my_another_table": new(row),
},
PostCleanup: map[string][]string{
"my_table": {"RESET SEQUENCE"},
},
},
}
// Given there are no rows in table "my_table"
mock.ExpectExec(`DELETE FROM my_table`).
WillReturnResult(driver.ResultNoRows)
// PostCleanup raw statement.
mock.ExpectExec(`RESET SEQUENCE`).
WillReturnResult(driver.ResultNoRows)
// And rows from this file are stored in table "my_table"
// """
// _testdata/rows.csv
// """
mock.ExpectExec(`INSERT INTO my_table \(id,created_at,deleted_at,foo,bar\) VALUES .+`).
WithArgs(
1, mustParseTime("2021-01-01T00:00:00Z"), nil, "foo-1", "abc",
2, mustParseTime("2021-01-02T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z"), "foo-1", "def",
3, mustParseTime("2021-01-03T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z"), "foo-2", "hij",
).
WillReturnResult(driver.ResultNoRows)
// And these rows are stored in table "my_table":
// | id | foo | bar | created_at | deleted_at |
// | 1 | foo-1 | abc | 2021-01-01T00:00:00Z | NULL |
mock.ExpectExec(`INSERT INTO my_table \(id,created_at,deleted_at,foo,bar\) VALUES .+`).
WithArgs(
1, mustParseTime("2021-01-01T00:00:00Z"), nil, "foo-1", "abc",
).
WillReturnResult(driver.ResultNoRows)
// Then only these rows are available in table "my_table":
// | id | foo | bar | created_at | deleted_at |
// | 1 | foo-1 | abc | 2021-01-01T00:00:00Z | NULL |
// | 2 | foo-1 | def | 2021-01-02T00:00:00Z | 2021-01-03T00:00:00Z |
// | 3 | foo-2 | hij | 2021-01-03T00:00:00Z | 2021-01-03T00:00:00Z |
mock.ExpectQuery(`SELECT COUNT\(1\) AS c FROM my_table`).WillReturnRows(sqlmock.NewRows([]string{"c"}).AddRow(3))
mock.ExpectQuery(`SELECT .+ FROM my_table WHERE bar = \$1 AND created_at = \$2 AND deleted_at IS NULL`).
WithArgs(
"abc", mustParseTime("2021-01-01T00:00:00Z"),
).
WillReturnRows(sqlmock.NewRows([]string{"id", "foo", "bar", "created_at", "deleted_at"}).
AddRow(1, "foo-1", "abc", mustParseTime("2021-01-01T00:00:00Z"), nil))
mock.ExpectQuery(`SELECT .+ FROM my_table WHERE foo = \$1 AND bar = \$2 AND created_at = \$3 AND deleted_at = \$4`).
WithArgs(
"foo-1", "def", mustParseTime("2021-01-02T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z"),
).
WillReturnRows(sqlmock.NewRows([]string{"id", "foo", "bar", "created_at", "deleted_at"}).
AddRow(2, "foo-1", "def", mustParseTime("2021-01-02T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z")))
mock.ExpectQuery(`SELECT .+ FROM my_table WHERE foo = \$1 AND bar = \$2 AND created_at = \$3 AND deleted_at = \$4`).
WithArgs(
"foo-2", "hij", mustParseTime("2021-01-03T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z"),
).
WillReturnRows(sqlmock.NewRows([]string{"id", "foo", "bar", "created_at", "deleted_at"}).
AddRow(3, "foo-2", "hij", mustParseTime("2021-01-03T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z")))
// Assertion with interpolated variables.
// Then only these rows are available in table "my_table":
// | id | foo | bar | created_at | deleted_at |
// | <id1> | foo-1 | abc | 2021-01-01T00:00:00Z | NULL |
// | <id2> | foo-1 | def | 2021-01-02T00:00:00Z | 2021-01-03T00:00:00Z |
// | <id3> | foo-2 | hij | 2021-01-03T00:00:00Z | 2021-01-03T00:00:00Z |
mock.ExpectQuery(`SELECT COUNT\(1\) AS c FROM my_table`).WillReturnRows(sqlmock.NewRows([]string{"c"}).AddRow(3))
mock.ExpectQuery(`SELECT .+ FROM my_table WHERE id = \$1 AND foo = \$2 AND bar = \$3 AND created_at = \$4 AND deleted_at IS NULL`).
WithArgs(
1, "foo-1", "abc", mustParseTime("2021-01-01T00:00:00Z"),
).
WillReturnRows(sqlmock.NewRows([]string{"id", "foo", "bar", "created_at", "deleted_at"}).
AddRow(1, "foo-1", "abc", mustParseTime("2021-01-01T00:00:00Z"), nil))
mock.ExpectQuery(`SELECT .+ FROM my_table WHERE id = \$1 AND foo = \$2 AND bar = \$3 AND created_at = \$4 AND deleted_at = \$5`).
WithArgs(
2, "foo-1", "def", mustParseTime("2021-01-02T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z"),
).
WillReturnRows(sqlmock.NewRows([]string{"id", "foo", "bar", "created_at", "deleted_at"}).
AddRow(2, "foo-1", "def", mustParseTime("2021-01-02T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z")))
mock.ExpectQuery(`SELECT .+ FROM my_table WHERE id = \$1 AND foo = \$2 AND bar = \$3 AND created_at = \$4 AND deleted_at = \$5`).
WithArgs(
3, "foo-2", "hij", mustParseTime("2021-01-03T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z"),
).
WillReturnRows(sqlmock.NewRows([]string{"id", "foo", "bar", "created_at", "deleted_at"}).
AddRow(3, "foo-2", "hij", mustParseTime("2021-01-03T00:00:00Z"), mustParseTime("2021-01-03T00:00:00Z")))
// And no rows are available in table "my_another_table"
mock.ExpectQuery(`SELECT COUNT\(1\) AS c FROM my_another_table`).WillReturnRows(sqlmock.NewRows([]string{"c"}).AddRow(0))
buf := bytes.NewBuffer(nil)
suite := godog.TestSuite{
Name: "DatabaseContext",
TestSuiteInitializer: nil,
ScenarioInitializer: func(s *godog.ScenarioContext) {
dbm.RegisterSteps(s)
},
Options: &godog.Options{
Format: "pretty",
Output: buf,
Paths: []string{"_testdata/DatabaseDefault.feature"},
Strict: true,
Randomize: time.Now().UTC().UnixNano(),
},
}
status := suite.Run()
if status != 0 {
t.Fatal(buf.String())
}
}