This repository has been archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdbconf_test.go
255 lines (217 loc) · 6.24 KB
/
dbconf_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
package goose
import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func setupDBConf(t *testing.T, confPath string, extraPath string) (string, string, func()) {
td, err := ioutil.TempDir("", "goose-test")
require.NoError(t, err)
defer func() {
if t.Failed() {
os.RemoveAll(td)
}
}()
confPath = filepath.Join(strings.Split(confPath, "/")...)
confPath = filepath.Join(td, confPath)
confDir := filepath.Dir(confPath)
err = os.MkdirAll(confDir, 0700)
require.NoError(t, err)
err = ioutil.WriteFile(confPath,
[]byte("\n"),
0600)
require.NoError(t, err)
extraDir := filepath.Join(strings.Split(extraPath, "/")...)
extraDir = filepath.Join(td, extraDir)
err = os.MkdirAll(extraDir, 0700)
require.NoError(t, err)
return confPath, extraDir, func() { os.RemoveAll(td) }
}
func TestFindDBConf_confDir(t *testing.T) {
confNames := []string{"db/dbconf.yaml", "db/dbconf.yml", "dbconf.yaml", "dbconf.yml"}
for _, confName := range confNames {
confPath, baseDir, clean := setupDBConf(t, confName, "")
defer clean()
path := findDBConf(baseDir)
assert.Equal(t, confPath, path)
}
}
func TestFindDBConf_deepDir(t *testing.T) {
confPath, deepDir, clean := setupDBConf(t, "db/dbconf.yaml", "a/b/c")
defer clean()
path := findDBConf(deepDir)
assert.Equal(t, confPath, path)
}
func TestFindDBConf_pwd(t *testing.T) {
confPath, deepDir, clean := setupDBConf(t, "dbconf.yaml", "a/b/c")
defer clean()
pwd, err := os.Getwd()
require.NoError(t, err)
defer os.Chdir(pwd)
err = os.Chdir(deepDir)
require.NoError(t, err)
path := findDBConf("")
assert.Equal(t, confPath, path)
}
func TestNewDBConf(t *testing.T) {
confPath, migrationsDir, clean := setupDBConf(t, "dbconf.yaml", "dbstuff")
defer clean()
err := ioutil.WriteFile(confPath,
[]byte(`
myenv:
driver: mysql
dsn: foo
migrationsDir: dbstuff
`),
0700)
require.NoError(t, err)
dbconf, err := NewDBConf(filepath.Dir(confPath), "myenv")
require.NoError(t, err)
assert.Equal(t, migrationsDir, dbconf.MigrationsDir)
assert.Equal(t, "mysql", dbconf.Driver.Name)
assert.Equal(t, "foo", dbconf.Driver.DSN)
}
func TestNewDBConf_default(t *testing.T) {
// Since the default uses env vars, and also no environment, this tests
// these 2 additional configurations as well.
defer os.Setenv("DB_MIGRATIONS_DIR", os.Getenv("DB_MIGRATIONS_DIR"))
os.Setenv("DB_MIGRATIONS_DIR", "/migdir")
defer os.Setenv("DB_DRIVER", os.Getenv("DB_DRIVER"))
os.Setenv("DB_DRIVER", "mysqlite3")
defer os.Setenv("DB_DRIVER_IMPORT", os.Getenv("DB_DRIVER_IMPORT"))
os.Setenv("DB_DRIVER_IMPORT", "github.com/myfork/sqlite3")
defer os.Setenv("DB_DIALECT", os.Getenv("DB_DIALECT"))
os.Setenv("DB_DIALECT", "sqlite3")
defer os.Setenv("DB_DSN", os.Getenv("DB_DSN"))
os.Setenv("DB_DSN", "foo")
dbconf, err := NewDBConf("", "")
require.NoError(t, err)
assert.Equal(t, "/migdir", dbconf.MigrationsDir)
assert.Equal(t, "mysqlite3", dbconf.Driver.Name)
assert.Equal(t, "github.com/myfork/sqlite3", dbconf.Driver.Import)
assert.Equal(t, &Sqlite3Dialect{}, dbconf.Driver.Dialect)
assert.Equal(t, "foo", dbconf.Driver.DSN)
}
func TestNewDBConf_driverImport(t *testing.T) {
confPath, migrationsDir, clean := setupDBConf(t, "dbconf.yaml", "migrations")
defer clean()
err := ioutil.WriteFile(confPath,
[]byte(`
myenv:
driver: github.com/myfork/mysql
dsn: foo
`),
0700)
require.NoError(t, err)
dbconf, err := NewDBConf(filepath.Dir(confPath), "myenv")
require.NoError(t, err)
assert.Equal(t, migrationsDir, dbconf.MigrationsDir)
assert.Equal(t, "mysql", dbconf.Driver.Name)
assert.Equal(t, "github.com/myfork/mysql", dbconf.Driver.Import)
assert.Equal(t, &MySqlDialect{}, dbconf.Driver.Dialect)
assert.Equal(t, "foo", dbconf.Driver.DSN)
}
func TestNewDBConf_driverDefaults(t *testing.T) {
tests := []struct {
names []string
driver DBDriver
}{
{
[]string{"postgres", "PoStGrEs"},
DBDriver{
Name: "postgres",
Import: "github.com/lib/pq",
Dialect: &PostgresDialect{},
},
},
{
[]string{"redshift"},
DBDriver{
Name: "postgres",
Import: "github.com/lib/pq",
Dialect: &RedshiftDialect{},
},
},
{
[]string{"mymysql"},
DBDriver{
Name: "mymysql",
Import: "github.com/ziutek/mymysql/godrv",
Dialect: &MySqlDialect{},
},
},
{
[]string{"mysql"},
DBDriver{
Name: "mysql",
Import: "github.com/go-sql-driver/mysql",
Dialect: &MySqlDialect{},
},
},
{
[]string{"sqlite3"},
DBDriver{
Name: "sqlite3",
Import: "github.com/mattn/go-sqlite3",
Dialect: &Sqlite3Dialect{},
},
},
}
for _, test := range tests {
for _, driverName := range test.names {
confPath, migrationsDir, clean := setupDBConf(t, "dbconf.yaml", "migrations")
defer clean()
err := ioutil.WriteFile(confPath,
[]byte(`
myenv:
driver: `+driverName+`
`),
0700)
require.NoError(t, err)
dbconf, err := NewDBConf(filepath.Dir(confPath), "myenv")
require.NoError(t, err)
assert.Equal(t, migrationsDir, dbconf.MigrationsDir)
assert.Equal(t, test.driver, dbconf.Driver)
}
}
}
func TestImportOverride(t *testing.T) {
dbconf, err := NewDBConf("_example", "customimport")
if err != nil {
t.Fatal(err)
}
got := dbconf.Driver.Import
want := "github.com/custom/driver"
if got != want {
t.Errorf("bad custom import. got %v want %v", got, want)
}
}
func TestDriverSetFromEnvironmentVariable(t *testing.T) {
databaseUrlEnvVariableKey := "DB_DRIVER"
databaseUrlEnvVariableVal := "sqlite3"
databaseOpenStringKey := "DATABASE_URL"
databaseOpenStringVal := "db.db"
os.Setenv(databaseUrlEnvVariableKey, databaseUrlEnvVariableVal)
os.Setenv(databaseOpenStringKey, databaseOpenStringVal)
dbconf, err := NewDBConf("_example", "environment_variable_config")
if err != nil {
t.Fatal(err)
}
got := reflect.TypeOf(dbconf.Driver.Dialect)
want := reflect.TypeOf(&Sqlite3Dialect{})
if got != want {
t.Errorf("Not able to read the driver type from environment variable."+
"got %v want %v", got, want)
}
gotOpenString := dbconf.Driver.DSN
wantOpenString := databaseOpenStringVal
if gotOpenString != wantOpenString {
t.Errorf("Not able to read the open string from the environment."+
"got %v want %v", gotOpenString, wantOpenString)
}
}