Repository navigation
Expand file tree
/
Copy pathsqlite_test.go
More file actions
136 lines (114 loc) · 4.4 KB
/
Copy pathsqlite_test.go
File metadata and controls
136 lines (114 loc) · 4.4 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
package sql
import (
"database/sql"
"testing"
"github.com/google/go-cmp/cmp"
flux "github.com/influxdata/flux"
"github.com/influxdata/flux/values"
_ "github.com/mattn/go-sqlite3"
)
func TestBoolTranslation(t *testing.T) {
// write values to "DB" - as would be done by sql.to() - using sqlite3 as it allows maximum levels of type abuse, and therefore likely the largest
// potential for issues
db, err := sql.Open("sqlite3", ":memory:")
if !cmp.Equal(err, nil) {
t.Fatalf("unexpected result -want/+got\n\n%s\n\n", cmp.Diff(nil, err))
}
// create table - column type can be anything
q := "CREATE TABLE IF NOT EXISTS bools (name TEXT, age INT, employed BOOL)"
_, err = db.Exec(q)
if !cmp.Equal(err, nil) {
t.Fatalf("unexpected result -want/+got\n\n%s\n\n", cmp.Diff(nil, err))
}
// insert first row - BOOL as BOOL, which sqlite WILL accept as a write - and silently store true as 1
q = "INSERT INTO bools (name, age, employed) VALUES (\"albert\",110,true)"
_, err = db.Exec(q)
if !cmp.Equal(err, nil) {
t.Fatalf("unexpected result -want/+got\n\n%s\n\n", cmp.Diff(nil, err))
}
// insert second row - BOOL as INT - again, will accept ok
q = "INSERT INTO bools (name, age, employed) VALUES (\"mary\",10,1)"
_, err = db.Exec(q)
if !cmp.Equal(err, nil) {
t.Fatalf("unexpected result -want/+got\n\n%s\n\n", cmp.Diff(nil, err))
}
q = "SELECT * FROM bools"
results, err := db.Query(q)
if !cmp.Equal(err, nil) {
t.Fatalf("unexpected result -want/+got\n\n%s\n\n", cmp.Diff(nil, err))
}
// read the data back out and check that Flux fails here - we should not be doing magic type casts
TestReader := &SqliteRowReader{
Cursor: results,
}
TestReader.columnNames = []string{"name", "age", "employed"}
TestReader.columnTypes = []flux.ColType{flux.TString, flux.TInt, flux.TInt}
// all rows should fail to parse into Flux types becuase there is no SQLite boolean type, regardless of what the column "type" is
for TestReader.Next() {
row, _ := TestReader.GetNextRow()
// fail as BOOL
if cmp.Equal(values.NewBool(true), row[2]) {
t.Fatalf("unexpected result -want/+got\n\n%s\n\n", cmp.Diff(values.NewBool(true), row[2]))
}
// succeed as INT, which is what it actually is
if !cmp.Equal(values.NewInt(1), row[2]) {
t.Fatalf("unexpected result -want/+got\n\n%s\n\n", cmp.Diff(values.NewInt(1), row[2]))
}
}
}
func TestNulTranslation(t *testing.T) {
// write values to "DB" - as would be done by sql.to() - using sqlite3 as it allows maximum levels of type abuse, and therefore likely the largest
// potential for issues
db, err := sql.Open("sqlite3", ":memory:")
if !cmp.Equal(err, nil) {
t.Fatalf("unexpected result -want/+got\n\n%s\n\n", cmp.Diff(nil, err))
}
// create table
q := "CREATE TABLE IF NOT EXISTS magic (name TEXT, age INT, employed BADINTBOOL)"
_, err = db.Exec(q)
if !cmp.Equal(err, nil) {
t.Fatalf("unexpected result -want/+got\n\n%s\n\n", cmp.Diff(nil, err))
}
// insert first row - null string
q = "INSERT INTO magic (age, employed) VALUES (11,true)"
_, err = db.Exec(q)
if !cmp.Equal(err, nil) {
t.Fatalf("unexpected result -want/+got\n\n%s\n\n", cmp.Diff(nil, err))
}
// insert second row - null int
q = "INSERT INTO magic (name, employed) VALUES (\"mary\",true)"
_, err = db.Exec(q)
if !cmp.Equal(err, nil) {
t.Fatalf("unexpected result -want/+got\n\n%s\n\n", cmp.Diff(nil, err))
}
// insert third row - null bool
q = "INSERT INTO magic (name, age) VALUES (\"casper\",10)"
_, err = db.Exec(q)
if !cmp.Equal(err, nil) {
t.Fatalf("unexpected result -want/+got\n\n%s\n\n", cmp.Diff(nil, err))
}
q = "SELECT * FROM magic"
results, err := db.Query(q)
if !cmp.Equal(err, nil) {
t.Fatalf("unexpected result -want/+got\n\n%s\n\n", cmp.Diff(nil, err))
}
// read the data back out and check
TestReader := &SqliteRowReader{
Cursor: results,
}
TestReader.columnNames = []string{"name", "age", "employed"}
TestReader.columnTypes = []flux.ColType{flux.TString, flux.TInt, flux.TBool}
// number of columns == number of rows - and Nill goes left -> right. otherwise the following loop will fail
i := 0
for TestReader.Next() {
row, err := TestReader.GetNextRow()
// none should throw errors - expect them all to handle Nulls from DB correctly
if !cmp.Equal(err, nil) {
t.Fatalf("unexpected result -want/+got\n\n%s\n\n", cmp.Diff(nil, err))
}
if !row[i].IsNull() {
t.Fatalf("unexpected result -want/+got\n\n%s\n\n", cmp.Diff(true, row[i].IsNull()))
}
i++
}
}