forked from AreaHQ/go-fixtures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_test.go
583 lines (483 loc) · 12.8 KB
/
load_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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
package fixtures
import (
"database/sql"
"log"
"os"
"testing"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
)
var testDbPath = "/tmp/fixtures_testdb.sqlite"
var testSchema = `CREATE TABLE some_table(
id INT PRIMARY KEY NOT NULL,
string_field CHAR(50) NOT NULL,
boolean_field BOOL NOT NULL,
created_at DATETIME,
updated_at DATETIME
);
CREATE TABLE other_table(
id INT PRIMARY KEY NOT NULL,
int_field INT NOT NULL,
boolean_field BOOL NOT NULL,
created_at DATETIME,
updated_at DATETIME
);
CREATE TABLE join_table(
some_id INT NOT NULL,
other_id INT NOT NULL,
PRIMARY KEY(some_id, other_id)
);
CREATE TABLE string_key_table(
id varchar(50) PRIMARY KEY NOT NULL,
created_at DATETIME,
updated_at DATETIME
)`
var fixtureFile = "fixtures/test_fixtures1.yml"
var fixtureFiles = []string{
"fixtures/test_fixtures1.yml",
"fixtures/test_fixtures2.yml",
}
var testData = `
---
- table: 'some_table'
pk:
id: 1
fields:
string_field: 'foobar'
boolean_field: true
created_at: 'ON_INSERT_NOW()'
updated_at: 'ON_UPDATE_NOW()'
- table: 'other_table'
pk:
id: 2
fields:
int_field: 123
boolean_field: false
created_at: 'ON_INSERT_NOW()'
updated_at: 'ON_UPDATE_NOW()'
- table: 'join_table'
pk:
some_id: 1
other_id: 2
- table: 'string_key_table'
pk:
id: 'new_id'
fields:
created_at: 'ON_INSERT_NOW()'
updated_at: 'ON_UPDATE_NOW()'
`
func TestLoadWorksWithValidData(t *testing.T) {
// Delete the test database
os.Remove(testDbPath)
var (
db *sql.DB
err error
)
// Connect to an in-memory SQLite database
db, err = sql.Open("sqlite3", testDbPath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Create a test schema
_, err = db.Exec(testSchema)
if err != nil {
log.Fatal(err)
}
// Let's load the fixture, since the database is empty, this should run inserts
err = Load([]byte(testData), db, "sqlite")
// Error should be nil
assert.Nil(t, err)
var (
count int
rows *sql.Rows
id int
stringField string
booleanField bool
intField int
createdAt *time.Time
updatedAt *time.Time
someID int
otherID int
)
// Check row counts
db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count)
assert.Equal(t, 1, count)
db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count)
assert.Equal(t, 1, count)
db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count)
assert.Equal(t, 1, count)
db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count)
assert.Equal(t, 1, count)
// Check correct data has been loaded into some_table
rows, err = db.Query("SELECT id, string_field, boolean_field, " +
"created_at, updated_at FROM some_table")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
if err := rows.Scan(
&id,
&stringField,
&booleanField,
&createdAt,
&updatedAt,
); err != nil {
log.Fatal(err)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
assert.Equal(t, 1, id)
assert.Equal(t, "foobar", stringField)
assert.Equal(t, true, booleanField)
assert.NotNil(t, createdAt)
assert.Nil(t, updatedAt)
}
// Check correct data has been loaded into other_table
rows, err = db.Query("SELECT id, int_field, boolean_field, " +
"created_at, updated_at FROM other_table")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
if err := rows.Scan(
&id,
&intField,
&booleanField,
&createdAt,
&updatedAt,
); err != nil {
log.Fatal(err)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
assert.Equal(t, 2, id)
assert.Equal(t, 123, intField)
assert.Equal(t, false, booleanField)
assert.NotNil(t, createdAt)
assert.Nil(t, updatedAt)
}
// Check correct data has been loaded into join_table
rows, err = db.Query("SELECT some_id, other_id FROM join_table")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
if err := rows.Scan(
&someID,
&otherID,
); err != nil {
log.Fatal(err)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
assert.Equal(t, 1, someID)
assert.Equal(t, 2, otherID)
}
// Let's reload the fixture, this should run updates
err = Load([]byte(testData), db, "sqlite")
// Error should be nil
assert.Nil(t, err)
// Check row counts, should be unchanged
db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count)
assert.Equal(t, 1, count)
db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count)
assert.Equal(t, 1, count)
db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count)
assert.Equal(t, 1, count)
// Check correct data has been loaded into some_table
rows, err = db.Query("SELECT id, string_field, boolean_field, " +
"created_at, updated_at FROM some_table")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
if err := rows.Scan(
&id,
&stringField,
&booleanField,
&createdAt,
&updatedAt,
); err != nil {
log.Fatal(err)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
assert.Equal(t, 1, id)
assert.Equal(t, "foobar", stringField)
assert.Equal(t, true, booleanField)
assert.NotNil(t, createdAt)
assert.NotNil(t, updatedAt)
}
// Check correct data has been loaded into other_table
rows, err = db.Query("SELECT id, int_field, boolean_field, " +
"created_at, updated_at FROM other_table")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
if err := rows.Scan(
&id,
&intField,
&booleanField,
&createdAt,
&updatedAt,
); err != nil {
log.Fatal(err)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
assert.Equal(t, 2, id)
assert.Equal(t, 123, intField)
assert.Equal(t, false, booleanField)
assert.NotNil(t, createdAt)
assert.NotNil(t, updatedAt)
}
// Check correct data has been loaded into join_table
rows, err = db.Query("SELECT some_id, other_id FROM join_table")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
if err := rows.Scan(
&someID,
&otherID,
); err != nil {
log.Fatal(err)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
assert.Equal(t, 1, someID)
assert.Equal(t, 2, otherID)
}
}
func TestLoadFileWorksWithValidFile(t *testing.T) {
// Delete the test database
os.Remove(testDbPath)
var (
db *sql.DB
err error
)
// Connect to an in-memory SQLite database
db, err = sql.Open("sqlite3", testDbPath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Create a test schema
_, err = db.Exec(testSchema)
if err != nil {
log.Fatal(err)
}
var count int
// Check row counts to show no data
db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count)
assert.Equal(t, 0, count)
db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count)
assert.Equal(t, 0, count)
db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count)
assert.Equal(t, 0, count)
db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count)
assert.Equal(t, 0, count)
// Let's load the fixture, since the database is empty, this should run inserts
err = LoadFile(fixtureFile, db, "sqlite")
// Error should be nil
assert.Nil(t, err)
var (
rows *sql.Rows
id int
stringField string
booleanField bool
createdAt *time.Time
updatedAt *time.Time
)
// Check row counts
db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count)
assert.Equal(t, 1, count)
db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count)
assert.Equal(t, 0, count)
db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count)
assert.Equal(t, 0, count)
db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count)
assert.Equal(t, 0, count)
// Check correct data has been loaded into some_table
rows, err = db.Query("SELECT id, string_field, boolean_field, " +
"created_at, updated_at FROM some_table")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
if err := rows.Scan(
&id,
&stringField,
&booleanField,
&createdAt,
&updatedAt,
); err != nil {
log.Fatal(err)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
assert.Equal(t, 1, id)
assert.Equal(t, "foobar", stringField)
assert.Equal(t, true, booleanField)
assert.NotNil(t, createdAt)
assert.Nil(t, updatedAt)
}
// Let's reload the fixture, this should run updates
err = LoadFile(fixtureFile, db, "sqlite")
// Error should be nil
assert.Nil(t, err)
// Check row counts, should be unchanged
db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count)
assert.Equal(t, 1, count)
db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count)
assert.Equal(t, 0, count)
db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count)
assert.Equal(t, 0, count)
db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count)
assert.Equal(t, 0, count)
}
func TestLoadFileFailssWithMissingFile(t *testing.T) {
// Delete the test database
os.Remove(testDbPath)
var (
db *sql.DB
err error
)
// Connect to an in-memory SQLite database
db, err = sql.Open("sqlite3", testDbPath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Create a test schema
_, err = db.Exec(testSchema)
if err != nil {
log.Fatal(err)
}
// Let's load the fixture, since the database is empty, this should run inserts
err = LoadFile("bad_filename.yml", db, "sqlite")
// Error should be nil
assert.EqualError(t, err, "Error loading file bad_filename.yml: open bad_filename.yml: no such file or directory")
}
func TestLoadFilesWorksWithValidFiles(t *testing.T) {
// Delete the test database
os.Remove(testDbPath)
var (
db *sql.DB
err error
)
// Connect to an in-memory SQLite database
db, err = sql.Open("sqlite3", testDbPath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Create a test schema
_, err = db.Exec(testSchema)
if err != nil {
log.Fatal(err)
}
var count int
// Check rows are empty first
db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count)
assert.Equal(t, 0, count)
db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count)
assert.Equal(t, 0, count)
db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count)
assert.Equal(t, 0, count)
db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count)
assert.Equal(t, 0, count)
// Let's load the fixture, since the database is empty, this should run inserts
err = LoadFiles(fixtureFiles, db, "sqlite")
// Error should be nil
assert.Nil(t, err)
// Check row counts
db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count)
assert.Equal(t, 1, count)
db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count)
assert.Equal(t, 1, count)
db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count)
assert.Equal(t, 1, count)
db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count)
assert.Equal(t, 1, count)
// Let's reload the fixtures, this should run updates
err = LoadFiles(fixtureFiles, db, "sqlite")
// Error should be nil
assert.Nil(t, err)
// Check row counts, should be unchanged
db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count)
assert.Equal(t, 1, count)
db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count)
assert.Equal(t, 1, count)
db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count)
assert.Equal(t, 1, count)
}
func TestLoadFilesFailsWithABadFile(t *testing.T) {
// Delete the test database
os.Remove(testDbPath)
var (
db *sql.DB
err error
)
// Connect to an in-memory SQLite database
db, err = sql.Open("sqlite3", testDbPath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Create a test schema
_, err = db.Exec(testSchema)
if err != nil {
log.Fatal(err)
}
var count int
// Check rows are empty first
db.QueryRow("SELECT COUNT(*) FROM some_table").Scan(&count)
assert.Equal(t, 0, count)
db.QueryRow("SELECT COUNT(*) FROM other_table").Scan(&count)
assert.Equal(t, 0, count)
db.QueryRow("SELECT COUNT(*) FROM join_table").Scan(&count)
assert.Equal(t, 0, count)
db.QueryRow("SELECT COUNT(*) FROM string_key_table").Scan(&count)
assert.Equal(t, 0, count)
var badList = []string{
fixtureFile,
"bad_file",
}
// Let's load the fixture, since the database is empty, this should run inserts
err = LoadFiles(badList, db, "sqlite")
// Error should be nil
assert.EqualError(t, err, "Error loading file bad_file: open bad_file: no such file or directory")
}
func TestCheckPostgresPKWorks(t *testing.T) {
expected := "SELECT data_type " +
"FROM information_schema.columns " +
"WHERE table_name='test_table' " +
"AND column_name='id';"
actual := checkPostgresPKDataType("test_table")
assert.Equal(t, actual, expected, "Data type sql should match")
}
func TestFixPostgresSequenceWorks(t *testing.T) {
expected := "SELECT pg_catalog.setval(" +
"pg_get_serial_sequence('test_table', 'id'), " +
"(SELECT MAX(id) FROM test_table));"
actual := fixPostgresPKSequence("test_table")
assert.Equal(t, actual, expected, "Sequence fix sql should match")
}