-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
testfixtures_test.go
81 lines (72 loc) · 1.98 KB
/
testfixtures_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
package testfixtures
import (
"database/sql"
"errors"
"testing"
)
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")
}
})
}
func TestQuoteKeyword(t *testing.T) {
tests := []struct {
helper helper
keyword string
expected string
}{
{&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 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)
}
}
}