-
Notifications
You must be signed in to change notification settings - Fork 0
/
trek_test.go
507 lines (476 loc) · 11.4 KB
/
trek_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
package trek
import (
"database/sql"
"errors"
_ "github.com/go-sql-driver/mysql" // mysql driver
_ "github.com/lib/pq" // postgres driver
"testing"
)
func connect(t *testing.T, kind string) (db *sql.DB) {
var err error
var options string
switch kind {
case POSTGRES:
options = "user=postgres dbname=trek sslmode=disable"
break
case MYSQL:
options = "root:@/trek"
break
default:
t.Error("Unsupported database kind")
return
}
db, err = sql.Open(kind, options)
if err != nil {
t.Errorf("Error connecting to %s", kind)
return
}
err = db.Ping()
if err != nil {
t.Error("Error pinging the database")
return
}
return
}
func dropTable(t *testing.T, db *sql.DB, table string) {
_, err := db.Exec(`DROP TABLE IF EXISTS ` + table)
if err != nil {
t.Error(err.Error())
}
}
func teardown(t *testing.T, db *sql.DB) {
migrations = []migration{}
if db != nil {
dropTable(t, db, "migrations")
db.Close()
}
}
func TestParseOptionsDefaultValues(t *testing.T) {
defer teardown(t, nil)
options := []string{}
config := parseOptions(options)
if config.Action != UP {
t.Error("Action was not UP")
}
if config.Database != POSTGRES {
t.Error("Database was not POSTGRES")
}
}
func TestParseOptionsCustomValues1(t *testing.T) {
defer teardown(t, nil)
options := []string{POSTGRES, UP}
config := parseOptions(options)
if config.Action != UP {
t.Error("Action was not UP")
}
if config.Database != POSTGRES {
t.Error("Database was not POSTGRES")
}
}
func TestParseOptionsCustomValues2(t *testing.T) {
defer teardown(t, nil)
options := []string{MYSQL, DOWN}
config := parseOptions(options)
if config.Action != DOWN {
t.Error("Action was not DOWN")
}
if config.Database != MYSQL {
t.Error("Database was not MYSQL")
}
}
func TestCreateTableError(t *testing.T) {
db := connect(t, POSTGRES)
defer teardown(t, db)
config := &configuration{Database: "foo", Action: DOWN}
dtbs := &database{db, config}
err := createTable(dtbs)
if err == nil {
t.Error("Error expected")
}
if err != errUnrecognizedDatabase {
t.Error("Unrecognized database was expected")
}
}
func TestCreateTablePostgres(t *testing.T) {
db := connect(t, POSTGRES)
defer teardown(t, db)
config := &configuration{Database: POSTGRES, Action: UP}
dtbs := &database{db, config}
err := createTable(dtbs)
if err != nil {
t.Error(err.Error())
}
_, err = db.Query(`SELECT * FROM migrations`)
if err != nil {
t.Error(err.Error())
}
}
func TestCreateTableMysql(t *testing.T) {
db := connect(t, MYSQL)
defer teardown(t, db)
config := &configuration{Database: MYSQL, Action: UP}
dtbs := &database{db, config}
err := createTable(dtbs)
if err != nil {
t.Error(err.Error())
}
_, err = db.Query(`SELECT * FROM migrations`)
if err != nil {
t.Error(err.Error())
}
}
func TestGetVersion0(t *testing.T) {
db := connect(t, POSTGRES)
defer teardown(t, db)
config := &configuration{Database: POSTGRES, Action: UP}
dtbs := &database{db, config}
err := createTable(dtbs)
if err != nil {
t.Error(err.Error())
}
currentVersion, err := getVersion(dtbs)
if err != nil {
t.Error(err.Error())
}
if currentVersion != 0 {
t.Error("Expected version different from 0")
}
}
func TestGetVersion1(t *testing.T) {
db := connect(t, POSTGRES)
defer teardown(t, db)
config := &configuration{Database: POSTGRES, Action: UP}
dtbs := &database{db, config}
err := createTable(dtbs)
if err != nil {
t.Error(err.Error())
}
_, err = db.Exec(`INSERT INTO migrations (version, running) VALUES (1, false)`)
if err != nil {
t.Error(err.Error())
}
currentVersion, err := getVersion(dtbs)
if err != nil {
t.Error(err.Error())
}
if currentVersion != 1 {
t.Error("Expected version different from 1")
}
}
func TestGetVersionError(t *testing.T) {
db := connect(t, POSTGRES)
defer teardown(t, db)
config := &configuration{Database: POSTGRES, Action: UP}
dtbs := &database{db, config}
err := createTable(dtbs)
if err != nil {
t.Error(err.Error())
}
_, err = db.Exec(`INSERT INTO migrations (version, running) VALUES (1, true)`)
if err != nil {
t.Error(err.Error())
}
_, err = getVersion(dtbs)
if err == nil {
t.Error("Expected already running migration error")
}
if err != errMigrationAlreadyRunning {
t.Error("Expected already running migration error")
}
}
func TestRegister(t *testing.T) {
defer teardown(t, nil)
var up = func(*sql.DB) error { return nil }
var down = func(*sql.DB) error { return nil }
err := Register(1, up, down)
if err != nil {
t.Error(err.Error())
}
if len(migrations) != 1 {
t.Error("Expected migrations to have one migration")
}
if migrations[0].Version != 1 {
t.Error("Expected migration to have version equal to 1")
}
}
func TestRegisterDuplicates(t *testing.T) {
defer teardown(t, nil)
var up = func(*sql.DB) error { return nil }
var down = func(*sql.DB) error { return nil }
err := Register(1, up, down)
if err != nil {
t.Error(err.Error())
}
err = Register(1, up, down)
if err == nil {
t.Error("Expected duplicate version error")
}
if err != errVersionAlreadyRegistered {
t.Error("Expected duplicate version error")
}
}
func TestSetVersionError(t *testing.T) {
db := connect(t, POSTGRES)
defer teardown(t, db)
config := &configuration{Database: "foo", Action: UP}
dtbs := &database{db, config}
err := setVersion(dtbs, 9, false)
if err == nil {
t.Error("Expected unrecognized database error")
}
if err != errUnrecognizedDatabase {
t.Error("Expected unrecognized database error")
}
}
func TestRunMigrationsError(t *testing.T) {
db := connect(t, POSTGRES)
defer teardown(t, db)
config := &configuration{Database: POSTGRES, Action: "foo"}
dtbs := &database{db, config}
newVersion, err := runMigrations(dtbs, 0)
if newVersion != 0 {
t.Error("Expected new version to be 0")
}
if err == nil {
t.Error("Expected unrecognized action error")
}
if err != errUnrecognizedAction {
t.Error("Expected unrecognized action error")
}
}
func TestRunError(t *testing.T) {
db := connect(t, POSTGRES)
defer teardown(t, db)
didChange, newVersion, err := Run(db)
if didChange {
t.Error("Expected not to change version")
}
if newVersion != 0 {
t.Error("Expected new version to be 0")
}
if err != nil {
t.Error(err.Error())
}
}
func TestRunPostgresUp(t *testing.T) {
db := connect(t, POSTGRES)
defer teardown(t, db)
migrations = []migration{
{
Version: 1,
Up: func(*sql.DB) error { return nil },
Down: func(*sql.DB) error { return nil },
},
}
didChange, newVersion, err := Run(db, POSTGRES, UP)
if !didChange {
t.Error("Expected to change version")
}
if newVersion != 1 {
t.Error("Expected new version to be 1")
}
if err != nil {
t.Error(err.Error())
}
}
func TestRunPostgresDown(t *testing.T) {
db := connect(t, POSTGRES)
defer teardown(t, db)
migrations = []migration{
{
Version: 1,
Up: func(*sql.DB) error { return nil },
Down: func(*sql.DB) error { return nil },
},
}
Run(db, POSTGRES, UP) // migrate to version 1
didChange, newVersion, err := Run(db, POSTGRES, DOWN)
if !didChange {
t.Error("Expected to change version")
}
if newVersion != 0 {
t.Error("Expected new version to be 0")
}
if err != nil {
t.Error(err.Error())
}
}
func TestRunMysqlUp(t *testing.T) {
db := connect(t, MYSQL)
defer teardown(t, db)
migrations = []migration{
{
Version: 1,
Up: func(*sql.DB) error { return nil },
Down: func(*sql.DB) error { return nil },
},
}
didChange, newVersion, err := Run(db, MYSQL, UP)
if !didChange {
t.Error("Expected to change version")
}
if newVersion != 1 {
t.Error("Expected new version to be 1")
}
if err != nil {
t.Error(err.Error())
}
}
func TestRunMysqlDown(t *testing.T) {
db := connect(t, MYSQL)
defer teardown(t, db)
migrations = []migration{
{
Version: 1,
Up: func(*sql.DB) error { return nil },
Down: func(*sql.DB) error { return nil },
},
}
Run(db, MYSQL, UP) // migrate to version 1
didChange, newVersion, err := Run(db, MYSQL, DOWN)
if !didChange {
t.Error("Expected to change version")
}
if newVersion != 0 {
t.Error("Expected new version to be 0")
}
if err != nil {
t.Error(err.Error())
}
}
func TestRunUpSameVersion(t *testing.T) {
db := connect(t, POSTGRES)
defer teardown(t, db)
migrations = []migration{
{
Version: 1,
Up: func(*sql.DB) error { return nil },
Down: func(*sql.DB) error { return nil },
},
}
Run(db, POSTGRES, UP) // migrate to version 1
didChange, newVersion, err := Run(db, POSTGRES, UP) // migrate to version 1 again
if didChange {
t.Error("Expected not to change version")
}
if newVersion != 1 {
t.Error("Expected new version to be 1")
}
if err != nil {
t.Error(err.Error())
}
}
func TestRunDownSameVersion(t *testing.T) {
db := connect(t, POSTGRES)
defer teardown(t, db)
migrations = []migration{
{
Version: 1,
Up: func(*sql.DB) error { return nil },
Down: func(*sql.DB) error { return nil },
},
}
Run(db, POSTGRES, UP) // migrate to version 1
Run(db, POSTGRES, DOWN) // migrate to version 0
didChange, newVersion, err := Run(db, POSTGRES, DOWN) // migrate to version 0 again
if didChange {
t.Error("Expected not to change version")
}
if newVersion != 0 {
t.Error("Expected new version to be 0")
}
if err != nil {
t.Error(err.Error())
}
}
func TestRunUpWithError(t *testing.T) {
db := connect(t, POSTGRES)
defer teardown(t, db)
migrations = []migration{
{
Version: 1,
Up: func(*sql.DB) error { return nil },
Down: func(*sql.DB) error { return nil },
},
{
Version: 2,
Up: func(*sql.DB) error { return errors.New("Foo") },
Down: func(*sql.DB) error { return nil },
},
}
didChange, newVersion, err := Run(db, POSTGRES, UP)
if !didChange {
t.Error("Expected to change version")
}
if newVersion != 1 {
t.Error("Expected new version to be 1")
}
if err == nil {
t.Error("Expected 'Foo' error")
}
if err.Error() != "Foo" {
t.Error("Expected 'Foo' error")
}
}
func TestRunDownWithError(t *testing.T) {
db := connect(t, POSTGRES)
defer teardown(t, db)
migrations = []migration{
{
Version: 2,
Up: func(*sql.DB) error { return nil },
Down: func(*sql.DB) error { return errors.New("Foo") },
},
{
Version: 1,
Up: func(*sql.DB) error { return nil },
Down: func(*sql.DB) error { return nil },
},
}
Run(db, POSTGRES, UP) // migrate to version 2
didChange, newVersion, err := Run(db, POSTGRES, DOWN)
if didChange {
t.Error("Expected not to change version")
}
if newVersion != 2 {
t.Error("Expected new version to be 2")
}
if err == nil {
t.Error("Expected 'Foo' error")
}
if err.Error() != "Foo" {
t.Error("Expected 'Foo' error")
}
}
func TestRunDownWithoutMigration(t *testing.T) {
db := connect(t, POSTGRES)
defer teardown(t, db)
migrations = []migration{
{
Version: 1,
Up: func(*sql.DB) error { return nil },
Down: func(*sql.DB) error { return nil },
},
}
Run(db, POSTGRES, UP) // migrate to version 1
migrations = []migration{
{
Version: 2,
Up: func(*sql.DB) error { return nil },
Down: func(*sql.DB) error { return nil },
},
}
didChange, newVersion, err := Run(db, POSTGRES, DOWN)
if didChange {
t.Error("Expected not to change version")
}
if newVersion != 1 {
t.Error("Expected new version to be 1")
}
if err == nil {
t.Error("Expected previous migration error")
}
if err != errPreviousMigrationNotFound {
t.Error("Expected previous migration error")
}
}