forked from blockloop/scan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite_test.go
289 lines (249 loc) · 8.85 KB
/
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
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
// +build integration
package scan_test
import (
"testing"
"time"
"github.com/LUSHDigital/scan"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
_ "github.com/mattn/go-sqlite3"
)
func TestSqliteScanOneScansSingleItem(t *testing.T) {
if testing.Short() {
t.Skip()
}
type Item struct {
Name string
Age int8
}
schema := `CREATE TABLE IF NOT EXISTS persons (
name VARCHAR(120),
age INT
);
INSERT INTO PERSONS (name, age) VALUES ('brett', 100)
`
db := mustDB(t, schema)
row, err := db.Query(`SELECT * FROM persons LIMIT 1`)
require.NoError(t, err, "execute query")
var item Item
require.NoError(t, scan.Row(&item, row))
assert.EqualValues(t, "brett", item.Name)
assert.EqualValues(t, 100, item.Age)
}
func TestSqliteScanOneScansSingleItemWithTags(t *testing.T) {
if testing.Short() {
t.Skip()
}
type Item struct {
MyName string `db:"name"`
MyAge int8 `db:"age"`
}
schema := `CREATE TABLE IF NOT EXISTS persons (
name VARCHAR(120),
age INT
);
INSERT INTO PERSONS (name, age) VALUES ('brett', 100)
`
db := mustDB(t, schema)
row, err := db.Query(`SELECT * FROM persons LIMIT 1`)
require.NoError(t, err, "execute query")
var item Item
require.NoError(t, scan.Row(&item, row))
assert.EqualValues(t, "brett", item.MyName)
assert.EqualValues(t, 100, item.MyAge)
}
func TestSqliteScanOneScansMultipleItems(t *testing.T) {
if testing.Short() {
t.Skip()
}
type Item struct {
Name string
Age int8
}
schema := `CREATE TABLE IF NOT EXISTS persons (
name VARCHAR(120),
age INT
);
INSERT INTO PERSONS (name, age) VALUES ('brett', 100), ('jones', 100)
`
db := mustDB(t, schema)
row, err := db.Query(`SELECT * FROM persons ORDER BY name ASC`)
require.NoError(t, err, "execute query")
var items []Item
require.NoError(t, scan.Rows(&items, row))
require.NotNil(t, items)
assert.EqualValues(t, "brett", items[0].Name)
assert.EqualValues(t, 100, items[0].Age)
assert.EqualValues(t, "jones", items[1].Name)
assert.EqualValues(t, 100, items[1].Age)
}
func TestSqliteScanOneScansMultipleItemsWithTags(t *testing.T) {
if testing.Short() {
t.Skip()
}
type Item struct {
MyName string `db:"name"`
MyAge int8 `db:"age"`
}
schema := `CREATE TABLE IF NOT EXISTS persons (
name VARCHAR(120),
age INT
);
INSERT INTO PERSONS (name, age) VALUES ('brett', 100), ('jones', 100);
`
db := mustDB(t, schema)
row, err := db.Query(`SELECT * FROM persons ORDER BY name ASC`)
require.NoError(t, err, "execute query")
var items []Item
require.NoError(t, scan.Rows(&items, row))
require.Len(t, items, 2)
assert.EqualValues(t, "brett", items[0].MyName)
assert.EqualValues(t, 100, items[0].MyAge)
assert.EqualValues(t, "jones", items[1].MyName)
assert.EqualValues(t, 100, items[1].MyAge)
}
func TestSqliteScanOneScansPrimitiveTypesStrings(t *testing.T) {
if testing.Short() {
t.Skip()
}
schema := `CREATE TABLE IF NOT EXISTS persons (
name VARCHAR(120),
age INT
);
INSERT INTO PERSONS (name, age) VALUES ('brett', 100), ('jones', 100);
`
db := mustDB(t, schema)
row, err := db.Query(`SELECT name FROM persons ORDER BY name ASC`)
require.NoError(t, err, "execute query")
var items []string
assert.NoError(t, scan.Rows(&items, row))
assert.EqualValues(t, []string{"brett", "jones"}, items)
}
func TestSqliteScanOneScansPrimitiveTypesInts(t *testing.T) {
if testing.Short() {
t.Skip()
}
schema := `CREATE TABLE IF NOT EXISTS persons (
name VARCHAR(120),
age INT
);
INSERT INTO PERSONS (name, age) VALUES ('brett', 100), ('jones', 100);
`
db := mustDB(t, schema)
row, err := db.Query(`SELECT age FROM persons ORDER BY name ASC`)
require.NoError(t, err, "execute query")
var items []int
assert.NoError(t, scan.Rows(&items, row))
assert.EqualValues(t, []int{100, 100}, items)
}
func TestSqliteScanOneScansPrimitiveTypesInterface(t *testing.T) {
if testing.Short() {
t.Skip()
}
schema := `CREATE TABLE IF NOT EXISTS persons (
name VARCHAR(120),
age INT
);
INSERT INTO PERSONS (name, age) VALUES ('brett', 100), ('jones', 100);
`
db := mustDB(t, schema)
row, err := db.Query(`SELECT age FROM persons ORDER BY name ASC`)
require.NoError(t, err, "execute query")
var items []interface{}
assert.NoError(t, scan.Rows(&items, row))
// int64 is what Scan uses by default for numbers
assert.Equal(t, []interface{}{int64(100), int64(100)}, items)
}
func TestSqliteScanOneScansWhenMoreColumnsThanProperties(t *testing.T) {
if testing.Short() {
t.Skip()
}
schema := `CREATE TABLE IF NOT EXISTS persons (
name VARCHAR(120),
age INT
);
INSERT INTO PERSONS (name, age) VALUES ('brett', 100), ('jones', 100);
`
db := mustDB(t, schema)
row, err := db.Query(`SELECT * FROM persons ORDER BY name ASC`)
require.NoError(t, err, "execute query")
type Item struct {
Name string `db:"name"`
}
var items []Item
assert.NoError(t, scan.Rows(&items, row))
assert.EqualValues(t, []Item{
{Name: "brett"},
{Name: "jones"},
}, items)
}
func TestSqliteScanRowsScansAllColumnTypes(t *testing.T) {
if testing.Short() {
t.Skip()
}
db := mustDB(t, allTypesSchema)
var items []rowItem
rows, err := db.Query(`SELECT * FROM all_types LIMIT 1`)
require.NoError(t, err)
err = scan.Rows(&items, rows)
require.NoError(t, err)
assert.EqualValues(t, 2147483640, items[0].ColInt)
assert.EqualValues(t, 2147483641, items[0].ColInteger)
assert.EqualValues(t, 126, items[0].ColTinyint)
assert.EqualValues(t, 127, items[0].ColSmallint)
assert.EqualValues(t, 2147483642, items[0].ColMediumint)
assert.EqualValues(t, 9223372036854775800, items[0].ColBigint)
assert.EqualValues(t, 9223372036854775801, items[0].ColUnsigned)
assert.EqualValues(t, 2147483643, items[0].ColInt2)
assert.EqualValues(t, 127, items[0].ColInt8)
assert.EqualValues(t, "a", items[0].ColCharacter)
assert.EqualValues(t, "ab", items[0].ColVarchar)
assert.EqualValues(t, "abc", items[0].ColVarying)
assert.EqualValues(t, "abcd", items[0].ColNchar)
assert.EqualValues(t, "abcde", items[0].ColNative)
assert.EqualValues(t, "abcdef", items[0].ColNvarchar)
assert.EqualValues(t, "abcdefgh", items[0].ColText)
assert.EqualValues(t, "abcdefghi", items[0].ColClob)
assert.EqualValues(t, "abcdefghij", items[0].ColBlob)
assert.EqualValues(t, "3.1", items[0].ColReal)
assert.EqualValues(t, 3.14, items[0].ColDouble)
assert.EqualValues(t, 3.141, items[0].ColFloat)
assert.EqualValues(t, 3141, items[0].ColNumeric)
assert.EqualValues(t, 3.1415, items[0].ColDecimal)
assert.Equal(t, true, items[0].ColBoolean)
assert.Equal(t, "2017-11-27", items[0].ColDate.Format("2006-01-02"))
assert.Equal(t, "2017-11-27 17:59", items[0].ColDatetime.Format("2006-01-02 15:04"))
}
var allTypesSchema = `
CREATE TABLE IF NOT EXISTS all_types ( col_int INT, col_integer INTEGER, col_tinyint INT, col_smallint SMALLINT, col_mediumint MEDIUMINT, col_bigint BIGINT, col_unsigned UNSIGNED BIG INT, col_int2 INT2, col_int8 INT8, col_character CHARACTER(20), col_varchar VARCHAR(255), col_varying VARYING CHARACTER(255), col_nchar NCHAR(55), col_native NATIVE CHARACTER(70), col_nvarchar NVARCHAR(100), col_text TEXT, col_clob CLOB, col_blob BLOB, col_real REAL, col_double DOUBLE, col_float FLOAT, col_numeric NUMERIC, col_decimal DECIMAL(10,5), col_boolean BOOLEAN, col_date DATE, col_datetime DATETIME);
INSERT INTO all_types ( col_int, col_integer, col_tinyint, col_smallint, col_mediumint, col_bigint, col_unsigned, col_int2, col_int8, col_character, col_varchar, col_varying, col_nchar, col_native, col_nvarchar, col_text, col_clob, col_blob, col_real, col_double, col_float, col_numeric, col_decimal, col_boolean, col_date, col_datetime)
VALUES ( 2147483640, 2147483641, 126, 127, 2147483642, 9223372036854775800, 9223372036854775801, 2147483643, 127, 'a', 'ab', 'abc', 'abcd', 'abcde', 'abcdef', 'abcdefgh', 'abcdefghi', 'abcdefghij', '3.1', 3.14, 3.141, 3141, 3.1415, 1, '2017-11-27', '2017-11-27 17:59:48');
`
type rowItem struct {
ColInt int `db:"col_int"`
ColInteger int `db:"col_integer"`
ColTinyint int8 `db:"col_tinyint"`
ColSmallint int16 `db:"col_smallint"`
ColMediumint int32 `db:"col_mediumint"`
ColBigint int64 `db:"col_bigint"`
ColUnsigned uint64 `db:"col_unsigned"`
ColInt2 int `db:"col_int2"`
ColInt8 int `db:"col_int8"`
ColCharacter string `db:"col_character"`
ColVarchar string `db:"col_varchar"`
ColVarying string `db:"col_varying"`
ColNchar string `db:"col_nchar"`
ColNative string `db:"col_native"`
ColNvarchar string `db:"col_nvarchar"`
ColText string `db:"col_text"`
ColClob string `db:"col_clob"`
ColBlob string `db:"col_blob"`
ColReal string `db:"col_real"`
ColDouble float64 `db:"col_double"`
ColFloat float64 `db:"col_float"`
ColNumeric int `db:"col_numeric"`
ColDecimal float32 `db:"col_decimal"`
ColBoolean bool `db:"col_boolean"`
ColDate time.Time `db:"col_date"`
ColDatetime time.Time `db:"col_datetime"`
}