forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialect_sqlite_test.go
198 lines (166 loc) · 6.08 KB
/
dialect_sqlite_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
//go:build sqlite
// +build sqlite
package pop
import (
"fmt"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
var sqliteDefaultOptions = map[string]string{"_busy_timeout": "5000", "_fk": "true"}
func Test_ConnectionDetails_Finalize_SQLite_URL_Only(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{
URL: "sqlite3:///tmp/foo.db",
}
err := cd.Finalize() // calls withURL() and urlParserSQLite3(cd)
r.NoError(err)
r.Equal("sqlite3", cd.Dialect, "given dialect: N/A")
r.Equal("/tmp/foo.db", cd.Database, "given url: sqlite3:///tmp/foo.db")
r.EqualValues(sqliteDefaultOptions, cd.Options, "given url: sqlite3:///tmp/foo.db")
}
func Test_ConnectionDetails_Finalize_SQLite_OverrideOptions_URL_Only(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{
URL: "sqlite3:///tmp/foo.db?_fk=false&foo=bar",
}
err := cd.Finalize() // calls withURL() and urlParserSQLite3(cd)
r.NoError(err)
r.Equal("sqlite3", cd.Dialect, "given dialect: N/A")
r.Equal("/tmp/foo.db", cd.Database, "given url: sqlite3:///tmp/foo.db?_fk=false&foo=bar")
r.EqualValues(map[string]string{"_fk": "false", "foo": "bar", "_busy_timeout": "5000"}, cd.Options, "given url: sqlite3:///tmp/foo.db?_fk=false&foo=bar")
}
func Test_ConnectionDetails_Finalize_SQLite_SynURL_Only(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{
URL: "sqlite:///tmp/foo.db",
}
err := cd.Finalize() // calls withURL() and urlParserSQLite3(cd)
r.NoError(err)
r.Equal("sqlite3", cd.Dialect, "given dialect: N/A")
r.Equal("/tmp/foo.db", cd.Database, "given url: sqlite:///tmp/foo.db")
r.EqualValues(sqliteDefaultOptions, cd.Options, "given url: sqlite3:///tmp/foo.db")
}
func Test_ConnectionDetails_Finalize_SQLite_Dialect_URL(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{
Dialect: "sqlite3",
URL: "sqlite3:///tmp/foo.db",
}
err := cd.Finalize() // calls withURL() and urlParserSQLite3(cd)
r.NoError(err)
r.Equal("sqlite3", cd.Dialect, "given dialect: sqlite3")
r.Equal("/tmp/foo.db", cd.Database, "given url: sqlite3:///tmp/foo.db")
r.EqualValues(sqliteDefaultOptions, cd.Options, "given url: sqlite3:///tmp/foo.db")
}
func Test_ConnectionDetails_Finalize_SQLite_Dialect_SynURL(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{
Dialect: "sqlite3",
URL: "sqlite:///tmp/foo.db",
}
err := cd.Finalize() // calls withURL() and urlParserSQLite3(cd)
r.NoError(err)
r.Equal("sqlite3", cd.Dialect, "given dialect: sqlite3")
r.Equal("/tmp/foo.db", cd.Database, "given url: sqlite:///tmp/foo.db")
r.EqualValues(sqliteDefaultOptions, cd.Options, "given url: sqlite3:///tmp/foo.db")
}
func Test_ConnectionDetails_Finalize_SQLite_Synonym_URL(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{
Dialect: "sqlite",
URL: "sqlite3:///tmp/foo.db",
}
err := cd.Finalize() // calls withURL() and urlParserSQLite3(cd)
r.NoError(err)
r.Equal("sqlite3", cd.Dialect, "given dialect: sqlite")
r.Equal("/tmp/foo.db", cd.Database, "given url: sqlite3:///tmp/foo.db")
r.Equal(sqliteDefaultOptions, cd.Options, "given url: sqlite3:///tmp/foo.db")
}
func Test_ConnectionDetails_Finalize_SQLite_Synonym_SynURL(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{
Dialect: "sqlite",
URL: "sqlite:///tmp/foo.db",
}
err := cd.Finalize() // calls withURL() and urlParserSQLite3(cd)
r.NoError(err)
r.Equal("sqlite3", cd.Dialect, "given dialect: sqlite")
r.Equal("/tmp/foo.db", cd.Database, "given url: sqlite:///tmp/foo.db")
r.EqualValues(sqliteDefaultOptions, cd.Options, "given url: sqlite:///tmp/foo.db")
}
func Test_ConnectionDetails_Finalize_SQLite_Synonym_Path(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{
Dialect: "sqlite",
Database: "./foo.db",
}
err := cd.Finalize() // calls withURL() and urlParserSQLite3(cd)
r.NoError(err)
r.Equal("sqlite3", cd.Dialect, "given dialect: sqlite")
r.Equal("./foo.db", cd.Database, "given database: ./foo.db")
r.EqualValues(sqliteDefaultOptions, cd.Options, "given url: ./foo.db")
}
func Test_ConnectionDetails_Finalize_SQLite_OverrideOptions_Synonym_Path(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{
URL: "sqlite3:///tmp/foo.db?_fk=false&foo=bar",
}
err := cd.Finalize() // calls withURL() and urlParserSQLite3(cd)
r.NoError(err)
r.Equal("sqlite3", cd.Dialect, "given dialect: N/A")
r.Equal("/tmp/foo.db", cd.Database, "given url: sqlite3:///tmp/foo.db")
r.EqualValues(map[string]string{"_fk": "false", "foo": "bar", "_busy_timeout": "5000"}, cd.Options, "given url: sqlite3:///tmp/foo.db?_fk=false&foo=bar")
}
func Test_ConnectionDetails_FinalizeOSPath(t *testing.T) {
r := require.New(t)
d := t.TempDir()
p := filepath.Join(d, "testdb.sqlite")
cd := &ConnectionDetails{
Dialect: "sqlite",
Database: p,
}
r.NoError(cd.Finalize())
r.Equal("sqlite3", cd.Dialect)
r.EqualValues(p, cd.Database)
}
func TestSqlite_CreateDB(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{Dialect: "sqlite"}
dialect, err := newSQLite(cd)
r.NoError(err)
t.Run("CreateFile", func(t *testing.T) {
dir := t.TempDir()
cd.Database = filepath.Join(dir, "testdb.sqlite")
r.NoError(dialect.CreateDB())
r.FileExists(cd.Database)
})
t.Run("MemoryDB_tag", func(t *testing.T) {
dir := t.TempDir()
cd.Database = filepath.Join(dir, "file::memory:?cache=shared")
r.NoError(dialect.CreateDB())
r.NoFileExists(cd.Database)
})
t.Run("MemoryDB_only", func(t *testing.T) {
dir := t.TempDir()
cd.Database = filepath.Join(dir, ":memory:")
r.NoError(dialect.CreateDB())
r.NoFileExists(cd.Database)
})
t.Run("MemoryDB_param", func(t *testing.T) {
dir := t.TempDir()
cd.Database = filepath.Join(dir, "file:foobar?mode=memory&cache=shared")
r.NoError(dialect.CreateDB())
r.NoFileExists(cd.Database)
})
t.Run("CreateFile_ExistingDB", func(t *testing.T) {
dir := t.TempDir()
cd.Database = filepath.Join(dir, "testdb.sqlite")
r.NoError(dialect.CreateDB())
r.EqualError(dialect.CreateDB(), fmt.Sprintf("could not create SQLite database '%s'; database exists", cd.Database))
})
}
func TestSqlite_NewDriver(t *testing.T) {
_, err := newSQLiteDriver()
require.NoError(t, err)
}