-
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathtestfixtures_test.go
More file actions
362 lines (338 loc) · 10 KB
/
Copy pathtestfixtures_test.go
File metadata and controls
362 lines (338 loc) · 10 KB
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
package testfixtures
import (
"database/sql"
"errors"
"strings"
"testing"
"github.com/goccy/go-yaml"
)
func TestFixtureFile(t *testing.T) {
f := &fixtureFile{fileName: "posts.yml"}
file := f.fileNameWithoutExtension()
if file != "posts" {
t.Errorf("Should be 'posts', but returned %s", file)
}
}
func TestRequiredOptions(t *testing.T) {
t.Run("DatabaseIsRequired", func(t *testing.T) {
_, err := New()
if !errors.Is(err, errDatabaseIsRequired) {
t.Error("should return an error if database if not given")
}
})
t.Run("DialectIsRequired", func(t *testing.T) {
_, err := New(Database(&sql.DB{}))
if !errors.Is(err, errDialectIsRequired) {
t.Error("should return an error if dialect if not given")
}
})
t.Run("DialectWithPlaceholder", func(t *testing.T) {
loader, err := New(Database(&sql.DB{}), Dialect("clickhouse", WithCustomPlaceholder(ParamTypeQuestion)))
if err != nil {
t.Error("should return nil error")
}
if paramType := loader.helper.paramType(); paramType != ParamTypeQuestion {
t.Errorf("incorrect param type returned: %s", paramType)
}
})
}
func TestQuoteKeyword(t *testing.T) {
tests := []struct {
helper helper
keyword string
expected string
}{
{&postgreSQL{}, `posts_tags`, `"posts_tags"`},
{&postgreSQL{}, `"posts.tags"`, `"posts.tags"`},
{&postgreSQL{}, `test_schema.posts_tags`, `"test_schema"."posts_tags"`},
{&sqlserver{}, `posts_tags`, `[posts_tags]`},
{&sqlserver{}, `test_schema.posts_tags`, `[test_schema].[posts_tags]`},
}
for _, test := range tests {
actual := test.helper.quoteKeyword(test.keyword)
if test.expected != actual {
t.Errorf("TestQuoteKeyword keyword %s should have escaped to %s. Received %s instead", test.keyword, test.expected, actual)
}
}
}
func TestLoadPendingSources(t *testing.T) {
t.Run("SpannerRejectsDirectory", func(t *testing.T) {
l := &Loader{
db: &sql.DB{},
helper: &spanner{},
templateLeftDelim: "{{",
templateRightDelim: "}}",
templateOptions: []string{"missingkey=zero"},
fs: defaultFS{},
pendingSources: []pendingSource{
{kind: sourceDirectory, paths: []string{"testdata/fixtures_template"}},
},
}
err := l.loadPendingSources()
wantErr := `
testfixtures: Directory is not supported for Spanner to ensure support for INTERLEAVED tables.
Use Files():
ensure the order of the files is correct, parents loaded before children or
Use FilesMultiTables():
and order your table keys in the yaml files from parent to child`
if err == nil || err.Error() != wantErr {
t.Errorf("unexpected error\nwant: %s\ngot: %v", wantErr, err)
}
})
t.Run("SpannerRejectsPaths", func(t *testing.T) {
l := &Loader{
db: &sql.DB{},
helper: &spanner{},
templateLeftDelim: "{{",
templateRightDelim: "}}",
templateOptions: []string{"missingkey=zero"},
fs: defaultFS{},
pendingSources: []pendingSource{
{kind: sourcePaths, paths: []string{"testdata/fixtures_template"}},
},
}
err := l.loadPendingSources()
wantErr := `
testfixtures: Paths is not supported for Spanner to ensure support for INTERLEAVED tables.
Use Files():
ensure the order of the files is correct, parents loaded before children or
Use FilesMultiTables():
and order your table keys in the yaml files from parent to child`
if err == nil || err.Error() != wantErr {
t.Errorf("unexpected error\nwant: %s\ngot: %v", wantErr, err)
}
})
t.Run("NoPendingSources", func(t *testing.T) {
l := &Loader{
db: &sql.DB{},
helper: NewMockHelper("test_db"),
templateLeftDelim: "{{",
templateRightDelim: "}}",
templateOptions: []string{"missingkey=zero"},
fs: defaultFS{},
}
err := l.loadPendingSources()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(l.fixturesFiles) != 0 {
t.Errorf("expected no fixture files, got %d", len(l.fixturesFiles))
}
})
}
// Test that Template options work regardless of whether they come
// before or after Directory/Files/Paths options, and that all
// pending source kinds are loaded correctly.
// See: https://github.com/go-testfixtures/testfixtures/pull/349
func TestLoadPendingSourcesTemplateOptionOrdering(t *testing.T) {
templateData := map[string]interface{}{
"Ids": []int{1, 2, 3},
}
type item struct {
ID int `yaml:"id"`
Name string `yaml:"name,omitempty"`
}
wantItems := []item{
{ID: 1, Name: "item-1"},
{ID: 2, Name: "item-2"},
{ID: 3, Name: "item-3"},
}
wantIdsOnly := []item{
{ID: 1},
{ID: 2},
{ID: 3},
}
assertSingleTable := func(expected []item) func(*testing.T, *Loader) {
return func(t *testing.T, l *Loader) {
t.Helper()
if got, want := len(l.fixturesFiles), 1; got != want {
t.Fatalf("fixturesFiles length = %d, want %d", got, want)
}
content := l.fixturesFiles[0].content
if strings.Contains(string(content), "{{") {
t.Error("content still contains unresolved template marker \"{{\"")
}
var got []item
if err := yaml.Unmarshal(content, &got); err != nil {
t.Fatalf("yaml.Unmarshal: %v", err)
}
if len(got) != len(expected) {
t.Fatalf("got %d items, want %d", len(got), len(expected))
}
for i := range expected {
if got[i] != expected[i] {
t.Errorf("item[%d] = %+v, want %+v", i, got[i], expected[i])
}
}
}
}
assertMultiTable := func(expectedByFile map[string][]item) func(*testing.T, *Loader) {
return func(t *testing.T, l *Loader) {
t.Helper()
if got, want := len(l.fixturesFiles), len(expectedByFile); got != want {
t.Fatalf("fixturesFiles length = %d, want %d", got, want)
}
for _, f := range l.fixturesFiles {
if strings.Contains(string(f.content), "{{") {
t.Errorf("file %s: content still contains unresolved template marker \"{{\"", f.fileName)
}
expected, ok := expectedByFile[f.fileName]
if !ok {
t.Fatalf("unexpected fixture file name: %s", f.fileName)
}
var got []item
if err := yaml.Unmarshal(f.content, &got); err != nil {
t.Fatalf("file %s: yaml.Unmarshal: %v", f.fileName, err)
}
if len(got) != len(expected) {
t.Fatalf("file %s: got %d items, want %d", f.fileName, len(got), len(expected))
}
for i := range expected {
if got[i] != expected[i] {
t.Errorf("file %s: item[%d] = %+v, want %+v", f.fileName, i, got[i], expected[i])
}
}
}
}
}
tests := []struct {
name string
options []func(*Loader) error
assert func(*testing.T, *Loader)
}{
{
name: "TemplateBeforeDirectory",
options: []func(*Loader) error{
Template(), TemplateData(templateData),
Directory("testdata/fixtures_template"),
},
assert: assertSingleTable(wantItems),
},
{
name: "TemplateAfterDirectory",
options: []func(*Loader) error{
Directory("testdata/fixtures_template"),
Template(), TemplateData(templateData),
},
assert: assertSingleTable(wantItems),
},
{
name: "TemplateBeforeFiles",
options: []func(*Loader) error{
Template(), TemplateData(templateData),
Files("testdata/fixtures_template/items.yml"),
},
assert: assertSingleTable(wantItems),
},
{
name: "TemplateAfterFiles",
options: []func(*Loader) error{
Files("testdata/fixtures_template/items.yml"),
Template(), TemplateData(templateData),
},
assert: assertSingleTable(wantItems),
},
{
name: "TemplateBeforePaths",
options: []func(*Loader) error{
Template(), TemplateData(templateData),
Paths("testdata/fixtures_template"),
},
assert: assertSingleTable(wantItems),
},
{
name: "TemplateAfterPaths",
options: []func(*Loader) error{
Paths("testdata/fixtures_template"),
Template(), TemplateData(templateData),
},
assert: assertSingleTable(wantItems),
},
{
name: "PathsWithFile",
options: []func(*Loader) error{
Template(), TemplateData(templateData),
Paths("testdata/fixtures_template/items.yml"),
},
assert: assertSingleTable(wantItems),
},
{
name: "TemplateBeforeFilesMultiTables",
options: []func(*Loader) error{
Template(), TemplateData(templateData),
FilesMultiTables("testdata/fixtures_template_multi/multi_tables.yml"),
},
assert: assertMultiTable(map[string][]item{
"items.yml": wantItems,
"other_items.yml": wantIdsOnly,
}),
},
{
name: "TemplateAfterFilesMultiTables",
options: []func(*Loader) error{
FilesMultiTables("testdata/fixtures_template_multi/multi_tables.yml"),
Template(), TemplateData(templateData),
},
assert: assertMultiTable(map[string][]item{
"items.yml": wantItems,
"other_items.yml": wantIdsOnly,
}),
},
{
name: "MultipleSources",
options: []func(*Loader) error{
Template(), TemplateData(templateData),
Directory("testdata/fixtures_template"),
Files("testdata/fixtures_template/items.yml"),
},
assert: func(t *testing.T, l *Loader) {
t.Helper()
// Directory has 1 file (items.yml), Files adds 1 more
if got, want := len(l.fixturesFiles), 2; got != want {
t.Fatalf("fixturesFiles length = %d, want %d", got, want)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fullOpts := []func(*Loader) error{
Database(&sql.DB{}),
Dialect("clickhouse"),
}
fullOpts = append(fullOpts, tt.options...)
l, err := New(fullOpts...)
if err != nil {
t.Fatalf("New(): %v", err)
}
tt.assert(t, l)
})
}
}
func TestEnsureTestDatabase(t *testing.T) {
tests := []struct {
name string
isTestDatabase bool
}{
{"db_test", true},
{"dbTEST", true},
{"testdb", true},
{"production", false},
{"productionTestCopy", true},
{"t_e_s_t", false},
{"ТESТ", false}, // cyrillic T
}
for _, it := range tests {
var (
mockedHelper = NewMockHelper(it.name)
l = &Loader{helper: mockedHelper}
err = l.EnsureTestDatabase()
)
if err != nil && it.isTestDatabase {
t.Errorf("EnsureTestDatabase() should return nil for name = %s", it.name)
}
if err == nil && !it.isTestDatabase {
t.Errorf("EnsureTestDatabase() should return error for name = %s", it.name)
}
}
}