-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapplied-migration_test.go
66 lines (54 loc) · 1.71 KB
/
applied-migration_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
package schema
import (
"testing"
"github.com/DATA-DOG/go-sqlmock"
)
func TestGetAppliedMigrations(t *testing.T) {
withEachTestDB(t, func(t *testing.T, tdb *TestDB) {
db := tdb.Connect(t)
defer func() { _ = db.Close() }()
migrator := makeTestMigrator(WithDialect(tdb.Dialect))
migrations := testMigrations(t, "useless-ansi")
err := migrator.Apply(db, migrations)
if err != nil {
t.Error(err)
}
expectedCount := len(migrations)
applied, err := migrator.GetAppliedMigrations(db)
if err != nil {
t.Error(err)
}
if len(applied) != expectedCount {
t.Errorf("Expected %d applied migrations. Got %d", expectedCount, len(applied))
}
})
}
func TestGetAppliedMigrationsErrorsWhenTheTableDoesntExist(t *testing.T) {
withEachTestDB(t, func(t *testing.T, tdb *TestDB) {
db := tdb.Connect(t)
defer func() { _ = db.Close() }()
migrator := makeTestMigrator()
migrations, err := migrator.GetAppliedMigrations(db)
if err == nil {
t.Error("Expected an error. Got none.")
}
if len(migrations) > 0 {
t.Error("Expected empty list of applied migrations")
}
})
}
func TestGetAppliedMigrationsHasFriendlyScanError(t *testing.T) {
withEachTestDB(t, func(t *testing.T, tdb *TestDB) {
migrator := makeTestMigrator(WithDialect(tdb.Dialect))
db, mock, err := sqlmock.New()
if err != nil {
t.Error(err)
}
// Build a rowset that is completely different than the AppliedMigration
// struct is expecting to force a Scan error
rows := sqlmock.NewRows([]string{"nonsense", "column", "names"}).AddRow(1, "trash", "data")
mock.ExpectQuery("^SELECT").RowsWillBeClosed().WillReturnRows(rows)
_, err = migrator.GetAppliedMigrations(db)
expectErrorContains(t, err, migrator.TableName)
})
}