-
Notifications
You must be signed in to change notification settings - Fork 4
/
gloat_test.go
230 lines (177 loc) · 4.26 KB
/
gloat_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
package gloat
import (
"database/sql"
"errors"
"net/url"
"os"
"strings"
"testing"
// Needed to establish database connections during testing.
_ "github.com/go-sql-driver/mysql"
"github.com/gsamokovarov/assert"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
var (
gl Gloat
db *sql.DB
dbURL string
dbSrc string
dbSrcBroken string
dbDriver string
)
type testingStore struct{ applied Migrations }
func (s *testingStore) Collect() (Migrations, error) { return s.applied, nil }
func (s *testingStore) Insert(migration *Migration, _ SQLExecer) error { return nil }
func (s *testingStore) Remove(migration *Migration, _ SQLExecer) error { return nil }
type testingExecutor struct{}
func (e *testingExecutor) Up(*Migration, Store) error { return nil }
func (e *testingExecutor) Down(*Migration, Store) error { return nil }
type stubbedExecutor struct {
up func(*Migration, Store) error
down func(*Migration, Store) error
}
func (e *stubbedExecutor) Up(m *Migration, s Store) error {
if e.up != nil {
return e.up(m, s)
}
return nil
}
func (e *stubbedExecutor) Down(m *Migration, s Store) error {
if e.down != nil {
e.down(m, s)
}
return nil
}
func cleanState(fn func()) error {
_, err := db.Exec(`
DROP TABLE IF EXISTS schema_migrations;
DROP TABLE IF EXISTS users;
`)
if err != nil {
return err
}
fn()
return nil
}
func databaseStoreFactory(driver string, db *sql.DB) (Store, error) {
switch driver {
case "postgres", "postgresql":
return NewPostgreSQLStore(db), nil
case "mysql":
return NewMySQLStore(db), nil
case "sqlite", "sqlite3":
return NewMySQLStore(db), nil
}
return nil, errors.New("unsupported database driver " + driver)
}
func TestUnapplied(t *testing.T) {
gl.Store = &testingStore{applied: Migrations{}}
migrations, err := gl.Unapplied()
assert.Nil(t, err)
assert.Len(t, 4, migrations)
assert.Equal(t, 20170329154959, migrations[0].Version)
}
func TestUnapplied_Empty(t *testing.T) {
gl.Store = &testingStore{
applied: Migrations{
&Migration{Version: 20170329154959},
&Migration{Version: 20170511172647},
&Migration{Version: 20180905150724},
&Migration{Version: 20180920181906},
},
}
migrations, err := gl.Unapplied()
assert.Nil(t, err)
assert.Len(t, 0, migrations)
}
func TestUnapplied_MissingInSource(t *testing.T) {
gl.Store = &testingStore{
applied: Migrations{
&Migration{Version: 20170329154959},
&Migration{Version: 20170511172647},
&Migration{Version: 20180905150724},
&Migration{Version: 20180920181906},
},
}
prevSource := gl.Source
defer func() { gl.Source = prevSource }()
gl.Source = &testingStore{}
migrations, err := gl.Unapplied()
assert.Nil(t, err)
assert.Len(t, 0, migrations)
}
func TestCurrent(t *testing.T) {
gl.Store = &testingStore{
applied: Migrations{
&Migration{Version: 20170329154959},
},
}
migration, err := gl.Current()
assert.Nil(t, err)
assert.NotNil(t, migration)
assert.Equal(t, 20170329154959, migration.Version)
}
func TestCurrent_Nil(t *testing.T) {
gl.Store = &testingStore{}
migration, err := gl.Current()
assert.Nil(t, err)
assert.Nil(t, migration)
}
func TestApply(t *testing.T) {
called := false
gl.Store = &testingStore{}
gl.Executor = &stubbedExecutor{
up: func(*Migration, Store) error {
called = true
return nil
},
}
gl.Apply(nil)
assert.True(t, called)
}
func TestRevert(t *testing.T) {
called := false
gl.Store = &testingStore{}
gl.Executor = &stubbedExecutor{
down: func(*Migration, Store) error {
called = true
return nil
},
}
gl.Revert(nil)
assert.True(t, called)
}
func init() {
gl = Gloat{
Source: NewFileSystemSource("testdata/migrations"),
Executor: &testingExecutor{},
}
dbURL = os.Getenv("DATABASE_URL")
dbSrc = os.Getenv("DATABASE_SRC")
{
u, err := url.Parse(dbURL)
if err != nil {
panic(err)
}
dbDriver = u.Scheme
}
// Do a bit of post-processing so we can connect to non-postgres databases.
if dbDriver != "postgres" {
parts := strings.SplitN(dbURL, "://", 2)
if len(parts) != 2 {
panic("Cannot split " + dbURL + " into parts")
}
dbURL = parts[1]
}
{
var err error
db, err = sql.Open(dbDriver, dbURL)
if err != nil {
panic(err)
}
if err := db.Ping(); err != nil {
panic(err)
}
}
}