forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialect_sqlite_test.go
165 lines (145 loc) · 5.32 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
// +build sqlite
package pop
import (
"fmt"
"io/ioutil"
"os"
"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, err := ioutil.TempDir("", "")
r.NoError(err)
p := filepath.Join(d, "testdb.sqlite")
defer os.RemoveAll(p)
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)
dir, err := ioutil.TempDir("", "")
r.NoError(err)
p := filepath.Join(dir, "testdb.sqlite")
defer os.RemoveAll(p)
cd := &ConnectionDetails{
Dialect: "sqlite",
Database: p,
}
dialect, err := newSQLite(cd)
r.NoError(err)
r.NoError(dialect.CreateDB())
// Creating DB twice should produce an error
r.EqualError(dialect.CreateDB(), fmt.Sprintf("could not create SQLite database '%s'; database exists", p))
}