forked from sqle/sqle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine_test.go
116 lines (90 loc) · 2.18 KB
/
engine_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
package sqle_test
import (
gosql "database/sql"
"testing"
"gopkg.in/sqle/sqle.v0"
"gopkg.in/sqle/sqle.v0/mem"
"gopkg.in/sqle/sqle.v0/sql"
"github.com/stretchr/testify/require"
)
const (
driverName = "engine_tests"
)
func TestQueries(t *testing.T) {
testQuery(t,
"SELECT i FROM mytable;",
[][]interface{}{{int64(1)}, {int64(2)}, {int64(3)}},
)
testQuery(t,
"SELECT i FROM mytable WHERE i = 2;",
[][]interface{}{{int64(2)}},
)
testQuery(t,
"SELECT i FROM mytable ORDER BY i DESC;",
[][]interface{}{{int64(3)}, {int64(2)}, {int64(1)}},
)
testQuery(t,
"SELECT i FROM mytable WHERE s = 'a' ORDER BY i DESC;",
[][]interface{}{{int64(1)}},
)
testQuery(t,
"SELECT i FROM mytable WHERE s = 'a' ORDER BY i DESC LIMIT 1;",
[][]interface{}{{int64(1)}},
)
testQuery(t,
"SELECT COUNT(*) FROM mytable;",
[][]interface{}{{int64(3)}},
)
testQuery(t,
"SELECT COUNT(*) AS c FROM mytable;",
[][]interface{}{{int64(3)}},
)
}
func testQuery(t *testing.T, q string, r [][]interface{}) {
t.Run(q, func(t *testing.T) {
assert := require.New(t)
e := newEngine(t)
sqle.DefaultEngine = e
db, err := gosql.Open(sqle.DriverName, "")
assert.NoError(err)
defer func() { assert.NoError(db.Close()) }()
res, err := db.Query(q)
assert.NoError(err)
defer func() { assert.NoError(res.Close()) }()
cols, err := res.Columns()
assert.NoError(err)
assert.Equal(len(r[0]), len(cols))
vals := make([]interface{}, len(cols))
valPtrs := make([]interface{}, len(cols))
for i := 0; i < len(cols); i++ {
valPtrs[i] = &vals[i]
}
i := 0
for {
if !res.Next() {
break
}
err := res.Scan(valPtrs...)
assert.NoError(err)
assert.Equal(r[i], vals)
i++
}
assert.NoError(res.Err())
assert.Equal(len(r), i)
})
}
func newEngine(t *testing.T) *sqle.Engine {
assert := require.New(t)
table := mem.NewTable("mytable", sql.Schema{
{"i", sql.BigInteger},
{"s", sql.String},
})
assert.Nil(table.Insert(sql.NewRow(int64(1), "a")))
assert.Nil(table.Insert(sql.NewRow(int64(2), "b")))
assert.Nil(table.Insert(sql.NewRow(int64(3), "c")))
db := mem.NewDatabase("mydb")
db.AddTable("mytable", table)
e := sqle.New()
e.AddDatabase(db)
return e
}