-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathParser_test.go
More file actions
140 lines (132 loc) · 4.22 KB
/
Parser_test.go
File metadata and controls
140 lines (132 loc) · 4.22 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
137
138
139
140
package rqlParser
import (
"strings"
"testing"
)
type Test struct {
Name string // Name of the test
RQL string // Input RQL query
SQL string // Expected Output SQL
WantParseError bool // Test should raise an error when parsing the RQL query
WantTranslatorError bool // Test should raise an error when translating to SQL
}
func (test *Test) Run(t *testing.T) {
p := NewParser()
rqlNode, err := p.Parse(strings.NewReader(test.RQL))
if test.WantParseError != (err != nil) {
t.Fatalf("(%s) Expecting error :%v\nGot error : %v", test.Name, test.WantParseError, err)
}
sqlTranslator := NewSqlTranslator(rqlNode)
s, err := sqlTranslator.Sql()
if test.WantTranslatorError != (err != nil) {
t.Fatalf("(%s) Expecting error :%v\nGot error : %v \n\tSQL = %s", test.Name, test.WantTranslatorError, err, s)
}
if s != test.SQL {
t.Fatalf("(%s) Translated SQL doesn’t match the expected one %s vs %s", test.Name, s, test.SQL)
}
}
var tests = []Test{
{
Name: `Basic translation with double equal operators`,
RQL: `and(foo=eq=42,price=gt=10)`,
SQL: `WHERE ((foo = 42) AND (price > 10))`,
WantParseError: false,
WantTranslatorError: false,
},
{
Name: `Basic translation with func style operators`,
RQL: `and(eq(foo,42),gt(price,10),not(disabled))`,
SQL: `WHERE ((foo = 42) AND (price > 10) AND NOT(disabled))`,
WantParseError: false,
WantTranslatorError: false,
},
{
Name: `Basic translation with func simple equal operators`,
RQL: `foo=42&price=10`,
SQL: `WHERE ((foo = 42) AND (price = 10))`,
WantParseError: false,
WantTranslatorError: false,
},
{
Name: `Sort and limit`,
RQL: `eq(foo,42)&sort(+price,-length)&limit(10,20)`,
SQL: `WHERE (foo = 42) ORDER BY price, length DESC LIMIT 10 OFFSET 20`,
WantParseError: false,
WantTranslatorError: false,
},
{
Name: `Sort only`,
RQL: `sort(-price)`,
SQL: ` ORDER BY price DESC`,
WantParseError: false,
WantTranslatorError: false,
},
{
Name: `LIKE empty string`,
RQL: `foo=like=`,
SQL: `WHERE (foo LIKE '')`,
WantParseError: false,
WantTranslatorError: false,
},
{
Name: `Mixed style translation`,
RQL: `((eq(foo,42)>(price,10))|price=ge=500)&disabled=eq=false`,
SQL: `WHERE ((((foo = 42) AND (price > 10)) OR (price >= 500)) AND (disabled IS FALSE))`,
WantParseError: false,
WantTranslatorError: false,
},
{
Name: `Try a simple SQL injection`,
RQL: `foo=like=toto%27%3BSELECT%20column%20IN%20table`,
SQL: `WHERE (foo LIKE 'toto'';SELECT column IN table')`,
WantParseError: false,
WantTranslatorError: false,
},
{
Name: `Empty RQL`,
RQL: ``,
SQL: ``,
WantParseError: false,
WantTranslatorError: false,
},
{
Name: `Invalid RQL query (Unmanaged RQL operator)`,
RQL: `foo=missing_operator=42`,
SQL: ``,
WantParseError: false,
WantTranslatorError: true,
},
{
Name: `Invalid RQL query (Unescaped character)`,
RQL: `like(foo,hello world)`,
SQL: ``,
WantParseError: true,
WantTranslatorError: false,
},
{
Name: `Invalid RQL query (Missing comma)`,
RQL: `and(not(test),eq(foo,toto)gt(price,10))`,
SQL: ``,
WantParseError: true,
WantTranslatorError: false,
},
{
Name: `Invalid RQL query (Invalid field name)`,
RQL: `eq(foo%20tot,42)`,
SQL: ``,
WantParseError: false,
WantTranslatorError: true,
},
{
Name: `Invalid RQL query (Invalid field name 2)`,
RQL: `eq(foo*,toto)`,
SQL: ``,
WantParseError: false,
WantTranslatorError: true,
},
}
func TestParser(t *testing.T) {
for _, test := range tests {
test.Run(t)
}
}