-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcontext_test.go
248 lines (204 loc) · 5.6 KB
/
context_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
package goql
import (
"fmt"
"testing"
"github.com/fzerorubigd/goql/astdata"
"github.com/fzerorubigd/goql/parse"
"github.com/stretchr/testify/assert"
)
type tablet int
type row int
type c1 struct {
}
func (c c1) Value(in interface{}) Number {
r := in.(row)
return Number{Number: float64(r) * 2.0}
}
type c2 struct {
}
func (c c2) Value(in interface{}) String {
r := in.(row)
return String{String: fmt.Sprintf("%dth row", r)}
}
type c3 struct {
}
func (c c3) Value(in interface{}) Bool {
r := in.(row)
return Bool{Bool: r%2 == 0}
}
type c4 struct {
}
func (c c4) Value(in interface{}) Definition {
r := in.(row)
var def astdata.Definition
if r%2 == 0 {
def, _ = astdata.NewDefinition("int")
} else {
def, _ = astdata.NewDefinition("string")
}
return Definition{Definition: def}
}
type provider struct {
}
func (provider) Provide(in interface{}) []interface{} {
ln := int(in.(tablet))
res := make([]interface{}, ln)
for i := 0; i < ln; i++ {
res[i] = row(i)
}
return res
}
type concat struct {
}
func (concat) Execute(in ...Getter) (Getter, error) {
s := ""
for i := range in {
s += fmt.Sprint(in[i].Get())
}
return String{String: s}, nil
}
type wrong struct {
}
func (wrong) Execute(in ...Getter) (Getter, error) {
return nil, fmt.Errorf("hi, i am error")
}
func ast(q string) *parse.Query {
ast, err := parse.AST(q)
if err != nil {
panic(err)
}
return ast
}
func TestContext(t *testing.T) {
RegisterFunction("concat", concat{})
RegisterTable("test", provider{})
RegisterField("test", "c1", c1{})
RegisterField("test", "c2", c2{})
RegisterField("test", "c3", c3{})
q := "SELECT * FROM test"
row, data, err := execute(tablet(1), ast(q))
assert.NoError(t, err)
assert.Equal(t, 3, len(row))
assert.Equal(t, []string{"c1", "c2", "c3"}, row)
assert.Equal(t, 1, len(data))
row, data, err = execute(tablet(10), ast(q))
assert.NoError(t, err)
assert.Equal(t, 3, len(row))
assert.Equal(t, []string{"c1", "c2", "c3"}, row)
assert.Equal(t, 10, len(data))
q = "SELECT c1, c2 FROM test WHERE c3"
row, data, err = execute(tablet(10), ast(q))
assert.NoError(t, err)
assert.Equal(t, 2, len(row))
assert.Equal(t, []string{"c1", "c2"}, row)
assert.Equal(t, 5, len(data))
q = "SELECT c1, c2, c2 FROM test LIMIT 10"
row, data, err = execute(tablet(100), ast(q))
assert.NoError(t, err)
assert.Equal(t, 3, len(row))
assert.Equal(t, []string{"c1", "c2", "c2"}, row)
assert.Equal(t, 10, len(data))
q = `SELECT c1, c2, false FROM test WHERE "c2" like '%t_h%' OR "c3" > 0 or true and concat('t', 's') = 'ts' and c3 is not null LIMIT 10`
row, data, err = execute(tablet(100), ast(q))
assert.NoError(t, err)
assert.Equal(t, 3, len(row))
assert.Equal(t, []string{"c1", "c2", "static"}, row)
assert.Equal(t, 10, len(data))
q = "SELECT c1 FROM test WHERE c3 ORDER by c2 desc"
row, data, err = execute(tablet(10), ast(q))
assert.NoError(t, err)
assert.Equal(t, 1, len(row))
assert.Equal(t, []string{"c1"}, row)
assert.Equal(t, 5, len(data))
q = "SELECT * FROM test limit 5,50"
row, data, err = execute(tablet(10), ast(q))
assert.NoError(t, err)
assert.Equal(t, 3, len(row))
assert.Equal(t, []string{"c1", "c2", "c3"}, row)
assert.Equal(t, 5, len(data))
q = "SELECT * FROM test limit 15,50"
row, data, err = execute(tablet(10), ast(q))
assert.NoError(t, err)
assert.Equal(t, 3, len(row))
assert.Equal(t, []string{"c1", "c2", "c3"}, row)
assert.Equal(t, 0, len(data))
q = "SELECT concat(c2, 'string'), 10 FROM test where true limit 1"
row, data, err = execute(tablet(10), ast(q))
assert.NoError(t, err)
assert.Equal(t, 2, len(row))
assert.Equal(t, []string{"concat", "static"}, row)
assert.Equal(t, 1, len(data))
assert.Equal(t, "0th rowstring", data[0][0].Get())
assert.Equal(t, 10.0, data[0][1].Get())
}
func TestContextErr(t *testing.T) {
RegisterFunction("wrong", wrong{})
// Err
q := "SELECT c1, c2 FROM notexists"
row, data, err := execute(tablet(100), ast(q))
assert.Error(t, err)
assert.Nil(t, row)
assert.Nil(t, data)
// Err
q = "SELECT c1, no FROM test"
row, data, err = execute(tablet(100), ast(q))
assert.Error(t, err)
assert.Nil(t, row)
assert.Nil(t, data)
// Err
q = `SELECT c1 FROM test WHERE "no"`
row, data, err = execute(tablet(100), ast(q))
assert.Error(t, err)
assert.Nil(t, row)
assert.Nil(t, data)
// Err
q = "SELECT c1, noooo.no FROM test"
row, data, err = execute(tablet(100), ast(q))
assert.Error(t, err)
assert.Nil(t, row)
assert.Nil(t, data)
// Err
q = "SELECT c1, c2 FROM test WHERE noo is null"
row, data, err = execute(tablet(100), ast(q))
assert.Error(t, err)
assert.Nil(t, row)
assert.Nil(t, data)
// Err
q = "SELECT c1, c2 FROM test ORDER BY no"
row, data, err = execute(tablet(100), ast(q))
assert.Error(t, err)
assert.Nil(t, row)
assert.Nil(t, data)
q = "SELECT c1, fuuunccc(c2) FROM test"
row, data, err = execute(tablet(100), ast(q))
assert.Error(t, err)
assert.Nil(t, row)
assert.Nil(t, data)
q = "SELECT c1, concat(c22) FROM test"
row, data, err = execute(tablet(100), ast(q))
assert.Error(t, err)
assert.Nil(t, row)
assert.Nil(t, data)
// Not supporting func(*)
q = "SELECT c1, concat(*) FROM test"
row, data, err = execute(tablet(100), ast(q))
assert.Error(t, err)
assert.Nil(t, row)
assert.Nil(t, data)
q = "SELECT c1, wrong(c2) FROM test"
row, data, err = execute(tablet(100), ast(q))
assert.Error(t, err)
assert.Nil(t, row)
assert.Nil(t, data)
q = "SELECT c1, c2 FROM test WHERE wrong()"
row, data, err = execute(tablet(100), ast(q))
assert.Error(t, err)
assert.Nil(t, row)
assert.Nil(t, data)
g := func([]Getter) interface{} {
panic("err")
}
b, err := callWhere(g, nil)
assert.False(t, b)
assert.Error(t, err)
}