-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_test.go
176 lines (140 loc) · 3.48 KB
/
db_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
package dbmate
import (
"net/url"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
var testdataDir string
func newTestDB(t *testing.T, u *url.URL) *DB {
var err error
// only chdir once, because testdata is relative to current directory
if testdataDir == "" {
testdataDir, err = filepath.Abs("./testdata")
require.Nil(t, err)
err = os.Chdir(testdataDir)
require.Nil(t, err)
}
return NewDB(u)
}
func testURLs(t *testing.T) []*url.URL {
return []*url.URL{
postgresTestURL(t),
mySQLTestURL(t),
sqliteTestURL(t),
}
}
func testRecordOnlyURL(t *testing.T, u *url.URL) {
db := newTestDB(t, u)
// drop and recreate database
err := db.Drop()
require.Nil(t, err)
err = db.Create()
require.Nil(t, err)
// actually do the thing!
err = db.RecordOnly()
require.Nil(t, err)
// verify results
sqlDB, err := GetDriverOpen(u)
require.Nil(t, err)
defer mustClose(sqlDB)
count := 0
err = sqlDB.QueryRow(`select count(*) from schema_migrations
where version = '20151129054053'`).Scan(&count)
require.Nil(t, err)
require.Equal(t, 1, count)
}
func TestRecordOnly(t *testing.T) {
for _, u := range testURLs(t) {
testRecordOnlyURL(t, u)
}
}
func testMigrateURL(t *testing.T, u *url.URL) {
db := newTestDB(t, u)
// drop and recreate database
err := db.Drop()
require.Nil(t, err)
err = db.Create()
require.Nil(t, err)
// migrate
err = db.Migrate(15)
require.Nil(t, err)
// verify results
sqlDB, err := GetDriverOpen(u)
require.Nil(t, err)
defer mustClose(sqlDB)
count := 0
err = sqlDB.QueryRow(`select count(*) from schema_migrations
where version = '20151129054053'`).Scan(&count)
require.Nil(t, err)
require.Equal(t, 1, count)
err = sqlDB.QueryRow("select count(*) from users").Scan(&count)
require.Nil(t, err)
require.Equal(t, 1, count)
}
func TestMigrate(t *testing.T) {
for _, u := range testURLs(t) {
testMigrateURL(t, u)
}
}
func testUpURL(t *testing.T, u *url.URL) {
db := newTestDB(t, u)
// drop database
err := db.Drop()
require.Nil(t, err)
// create and migrate
err = db.Up(30)
require.Nil(t, err)
// verify results
sqlDB, err := GetDriverOpen(u)
require.Nil(t, err)
defer mustClose(sqlDB)
count := 0
err = sqlDB.QueryRow(`select count(*) from schema_migrations
where version = '20151129054053'`).Scan(&count)
require.Nil(t, err)
require.Equal(t, 1, count)
err = sqlDB.QueryRow("select count(*) from users").Scan(&count)
require.Nil(t, err)
require.Equal(t, 1, count)
}
func TestUp(t *testing.T) {
for _, u := range testURLs(t) {
testUpURL(t, u)
}
}
func testRollbackURL(t *testing.T, u *url.URL) {
db := newTestDB(t, u)
// drop, recreate, and migrate database
err := db.Drop()
require.Nil(t, err)
err = db.Create()
require.Nil(t, err)
err = db.Migrate(30)
require.Nil(t, err)
// verify migration
sqlDB, err := GetDriverOpen(u)
require.Nil(t, err)
defer mustClose(sqlDB)
count := 0
err = sqlDB.QueryRow(`select count(*) from schema_migrations
where version = '20151129054053'`).Scan(&count)
require.Nil(t, err)
require.Equal(t, 1, count)
// rollback
err = db.Rollback()
require.Nil(t, err)
// verify rollback
err = sqlDB.QueryRow("select count(*) from schema_migrations").Scan(&count)
require.Nil(t, err)
require.Equal(t, 0, count)
err = sqlDB.QueryRow("select count(*) from users").Scan(&count)
require.NotNil(t, err)
require.Regexp(t, "(does not exist|doesn't exist|no such table)", err.Error())
}
func TestRollback(t *testing.T) {
for _, u := range testURLs(t) {
testRollbackURL(t, u)
}
}