forked from xbsoftware/querysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_test.go
213 lines (190 loc) · 5.21 KB
/
sql_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
package querysql
import (
"fmt"
"strconv"
"strings"
"testing"
)
var aAndB = `{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"less", "filter":1}}, { "field": "b", "condition":{ "type":"greater", "filter":"abc" }}]}`
var aOrB = `{ "glue":"or", "rules":[{ "field": "a", "condition":{ "type":"less", "filter":1}}, { "field": "b", "condition":{ "type":"greater", "filter":"abc" }}]}`
var cOrC = `{ "glue":"or", "rules":[{ "field": "a", "condition":{ "type":"is null" }}, { "field": "b", "condition":{ "type":"range100", "filter":500 }}]}`
var cases = [][]string{
[]string{`{}`, "", ""},
[]string{
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"equal", "filter":1 }}]}`,
"a = ?",
"1",
},
[]string{
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"notEqual", "filter":1 }}]}`,
"a <> ?",
"1",
},
[]string{
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"less", "filter":1 }}]}`,
"a < ?",
"1",
},
[]string{
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"lessOrEqual", "filter":1 }}]}`,
"a <= ?",
"1",
},
[]string{
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"greater", "filter":1 }}]}`,
"a > ?",
"1",
},
[]string{
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"greaterOrEqual", "filter":1 }}]}`,
"a >= ?",
"1",
},
[]string{
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"contains", "filter":1 }}]}`,
"INSTR(a, ?) > 0",
"1",
},
[]string{
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"notContains", "filter":1 }}]}`,
"INSTR(a, ?) < 0",
"1",
},
[]string{
aAndB,
"( a < ? AND b > ? )",
"1,abc",
},
[]string{
aOrB,
"( a < ? OR b > ? )",
"1,abc",
},
[]string{
`{ "glue":"AND", "rules":[` + aAndB + `,` + aOrB + `,{ "field":"c", "condition": { "type":"equal", "filter":3 } }]}`,
"( ( a < ? AND b > ? ) AND ( a < ? OR b > ? ) AND c = ? )",
"1,abc,1,abc,3",
},
[]string{
`{ "glue":"and", "rules":[{ "field": "a", "includes":[1,2,3]}]}`,
"a IN(?,?,?)",
"1,2,3",
},
[]string{
`{ "glue":"and", "rules":[{ "field": "a", "includes":["a","b","c"]}]}`,
"a IN(?,?,?)",
"a,b,c",
},
}
func anyToStringArray(some []interface{}) (string, error) {
out := make([]string, 0, len(some))
for _, x := range some {
str, strOk := x.(string)
if strOk {
out = append(out, str)
continue
}
num, numOk := x.(float64)
if numOk {
out = append(out, strconv.Itoa(int(num)))
continue
}
return "", fmt.Errorf("can't convert %+v to a string", x)
}
return strings.Join(out, ","), nil
}
func TestSQL(t *testing.T) {
for _, line := range cases {
format, err := FromJSON([]byte(line[0]))
if err != nil {
t.Errorf("can't parse json\nj: %s\n%f", line[0], err)
continue
}
sql, vals, err := GetSQL(format, nil)
if err != nil {
t.Errorf("can't generate sql\nj: %s\n%f", line[0], err)
continue
}
if sql != line[1] {
t.Errorf("wrong sql generated\nj: %s\ns: %s\nr: %s", line[0], line[1], sql)
continue
}
valsStr, err := anyToStringArray(vals)
if err != nil {
t.Errorf("can't convert parameters\nj: %s\n%f", line[0], err)
continue
}
if valsStr != line[2] {
t.Errorf("wrong sql generated\nj: %s\ns: %s\nr: %s", line[0], line[2], valsStr)
continue
}
}
}
func TestWhitelist(t *testing.T) {
format, err := FromJSON([]byte(aAndB))
if err != nil {
t.Errorf("can't parse json\nj: %s\n%f", aAndB, err)
return
}
_, _, err = GetSQL(format, nil)
if err != nil {
t.Errorf("doesn't work without config")
return
}
_, _, err = GetSQL(format, &SQLConfig{})
if err != nil {
t.Errorf("doesn't work without whitelist")
return
}
_, _, err = GetSQL(format, &SQLConfig{Whitelist: map[string]bool{"a": true, "b": true}})
if err != nil {
t.Errorf("doesn't work with fields allowed")
return
}
_, _, err = GetSQL(format, &SQLConfig{Whitelist: map[string]bool{"a": true}})
if err == nil {
t.Errorf("doesn't return error when field is not allowed")
return
}
_, _, err = GetSQL(format, &SQLConfig{Whitelist: map[string]bool{"b": true}})
if err == nil {
t.Errorf("doesn't return error when field is not allowed")
return
}
}
func TestCustomOperation(t *testing.T) {
format, err := FromJSON([]byte(cOrC))
if err != nil {
t.Errorf("can't parse json\nj: %s\n%f", aAndB, err)
return
}
sql, vals, err := GetSQL(format, &SQLConfig{
Operations: map[string]CustomOperation{
"is null" : func(n string, c Condition) (string, []interface{}, error) {
return fmt.Sprintf("%s IS NULL", n), NoValues, nil
},
"range100" : func(n string, c Condition) (string, []interface{}, error) {
return fmt.Sprintf("( %s > ? AND %s < ? + 100 )", n, n), []interface{}{c.Value, c.Value}, nil
},
},
})
if err != nil {
t.Errorf("can't generate sql: %s\n%f", cOrC, err)
return
}
check := "( a IS NULL OR ( b > ? AND b < ? + 100 ) )"
if sql != check {
t.Errorf("wrong sql generated\nj: %s\ns: %s\nr: %s", cOrC, check, sql)
return
}
valsStr, err := anyToStringArray(vals)
if err != nil {
t.Errorf("can't convert parameters\nj: %s\n%f", cOrC, err)
return
}
check = "500,500"
if valsStr != check {
t.Errorf("wrong sql generated\nj: %s\ns: %s\nr: %s", cOrC, check, valsStr)
return
}
}