-
-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathsqlite_test.go
More file actions
210 lines (196 loc) · 5.34 KB
/
Copy pathsqlite_test.go
File metadata and controls
210 lines (196 loc) · 5.34 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
package sqlite
import (
"database/sql"
"fmt"
"strings"
"testing"
"github.com/mattn/go-sqlite3"
"gorm.io/gorm"
)
func TestDialector(t *testing.T) {
// This is the DSN of the in-memory SQLite database for these tests.
const InMemoryDSN = "file:testdatabase?mode=memory&cache=shared"
// This is the custom SQLite driver name.
const CustomDriverName = "my_custom_driver"
// Register the custom SQlite3 driver.
// It will have one custom function called "my_custom_function".
sql.Register(CustomDriverName,
&sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
// Define the `concat` function, since we use this elsewhere.
err := conn.RegisterFunc(
"my_custom_function",
func(arguments ...interface{}) (string, error) {
return "my-result", nil // Return a string value.
},
true,
)
return err
},
},
)
rows := []struct {
description string
dialector *Dialector
openSuccess bool
query string
querySuccess bool
}{
{
description: "Default driver",
dialector: &Dialector{
DSN: InMemoryDSN,
},
openSuccess: true,
query: "SELECT 1",
querySuccess: true,
},
{
description: "Explicit default driver",
dialector: &Dialector{
DriverName: DriverName,
DSN: InMemoryDSN,
},
openSuccess: true,
query: "SELECT 1",
querySuccess: true,
},
{
description: "Bad driver",
dialector: &Dialector{
DriverName: "not-a-real-driver",
DSN: InMemoryDSN,
},
openSuccess: false,
},
{
description: "Explicit default driver, custom function",
dialector: &Dialector{
DriverName: DriverName,
DSN: InMemoryDSN,
},
openSuccess: true,
query: "SELECT my_custom_function()",
querySuccess: false,
},
{
description: "Custom driver",
dialector: &Dialector{
DriverName: CustomDriverName,
DSN: InMemoryDSN,
},
openSuccess: true,
query: "SELECT 1",
querySuccess: true,
},
{
description: "Custom driver, custom function",
dialector: &Dialector{
DriverName: CustomDriverName,
DSN: InMemoryDSN,
},
openSuccess: true,
query: "SELECT my_custom_function()",
querySuccess: true,
},
}
for rowIndex, row := range rows {
t.Run(fmt.Sprintf("%d/%s", rowIndex, row.description), func(t *testing.T) {
db, err := gorm.Open(row.dialector, &gorm.Config{})
if !row.openSuccess {
if err == nil {
t.Errorf("Expected Open to fail.")
}
return
}
if err != nil {
t.Errorf("Expected Open to succeed; got error: %v", err)
}
if db == nil {
t.Errorf("Expected db to be non-nil.")
}
if row.query != "" {
err = db.Exec(row.query).Error
if !row.querySuccess {
if err == nil {
t.Errorf("Expected query to fail.")
}
return
}
if err != nil {
t.Errorf("Expected query to succeed; got error: %v", err)
}
}
})
}
}
func TestExplainQuotesStrings(t *testing.T) {
out := Dialector{}.Explain("SELECT * FROM t WHERE name = ?", "hello")
if !strings.Contains(out, "'hello'") {
t.Errorf("Explain must quote string literals with single quotes, got: %s", out)
}
}
type explainDefaultModel struct {
ID int
Code string `gorm:"default:hello"`
}
func (explainDefaultModel) TableName() string { return "explain_defaults" }
// GORM embeds string default values into the DDL via Dialector.Explain;
// parseDDL must strip the single quotes (and double quotes from tables
// created by older versions) so migrations stay idempotent.
func TestDefaultValueRoundTrip(t *testing.T) {
db, err := gorm.Open(Open("file:explain_defaults?mode=memory&cache=shared"), &gorm.Config{})
if err != nil {
t.Fatalf("gorm.Open: %v", err)
}
t.Cleanup(func() {
if sqlDB, err := db.DB(); err == nil {
_ = sqlDB.Close()
}
})
if err := db.AutoMigrate(&explainDefaultModel{}); err != nil {
t.Fatal(err)
}
cols, err := db.Migrator().ColumnTypes(&explainDefaultModel{})
if err != nil {
t.Fatal(err)
}
for _, c := range cols {
if c.Name() == "code" {
if dv, ok := c.DefaultValue(); !ok || dv != "hello" {
t.Errorf("DefaultValue = (%q,%v), want (hello,true)", dv, ok)
}
}
}
// second AutoMigrate must not rebuild the table
var before string
if err := db.Raw("SELECT sql FROM sqlite_master WHERE type='table' AND name='explain_defaults'").Scan(&before).Error; err != nil {
t.Fatal(err)
}
if err := db.AutoMigrate(&explainDefaultModel{}); err != nil {
t.Fatal(err)
}
var after string
if err := db.Raw("SELECT sql FROM sqlite_master WHERE type='table' AND name='explain_defaults'").Scan(&after).Error; err != nil {
t.Fatal(err)
}
if before != after {
t.Errorf("DDL changed after second AutoMigrate:\n before: %s\n after: %s", before, after)
}
// double quotes from tables created by older driver versions still parse
d, err := parseDDL("CREATE TABLE `legacy` (`code` text DEFAULT \"hi\")")
if err != nil {
t.Fatal(err)
}
if dv, ok := d.columns[0].DefaultValue(); !ok || dv != "hi" {
t.Errorf("legacy DefaultValue = (%q,%v), want (hi,true)", dv, ok)
}
// only one outer quote pair is stripped; inner quotes survive
d, err = parseDDL("CREATE TABLE `q` (`code` text DEFAULT '\"x\"')")
if err != nil {
t.Fatal(err)
}
if dv, ok := d.columns[0].DefaultValue(); !ok || dv != `"x"` {
t.Errorf(`quoted DefaultValue = (%q,%v), want ("x",true)`, dv, ok)
}
}