This repository has been archived by the owner on Sep 11, 2019. It is now read-only.
forked from go-gremlin/gremlin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_test.go
219 lines (204 loc) · 5.91 KB
/
utils_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
package gremlin
import (
"encoding/json"
"fmt"
"regexp"
"testing"
)
func TestCharSliceToMap(t *testing.T) {
given := []rune{
singleQuote,
backslash,
pctSymbol,
doubleQuote,
}
expectedMap := make(map[rune]bool)
expectedMap[singleQuote] = true
expectedMap[backslash] = true
expectedMap[pctSymbol] = true
expectedMap[doubleQuote] = true
expected, _ := json.Marshal(expectedMap)
resultMap := CharSliceToMap(given)
result, _ := json.Marshal(resultMap)
if string(result) != string(expected) {
t.Error("given", given, "expected", expected, "result", result)
}
}
func TestInterfaceToString(t *testing.T) {
tests := [][]interface{}{
{"", ""},
{"test", "test"},
{1, ""},
{true, ""},
}
for _, test := range tests {
given := test[0]
expected := test[1].(string)
result := InterfaceToString(given)
if result != expected {
t.Error("given", given, "expected", expected, "result", result)
}
}
}
func TestCoalesceStrings(t *testing.T) {
tests := [][][]string{
{{"first"}, {"first"}},
{{"", "first"}, {"first"}},
{{"", "first", "", "second"}, {"first"}},
}
for _, test := range tests {
given := test[0]
expected := test[1][0]
result := CoalesceStrings(given...)
if result != expected {
t.Error("given", given, "expected", expected, "result", result)
}
}
}
func TestEscapeCharacters(t *testing.T) {
tests := [][]string{
{"this is a test", "this is a test"},
{`this is a %`, `this is a %%`},
{"", ""},
{`' \ % "`, `\' \\ %% \"`},
}
for _, test := range tests {
given := test[0]
expected := test[1]
result := escapeCharacters(given, ESCAPE_CHARS_GREMLIN)
if result != expected {
t.Error("given", given, "expected", expected, "result", result)
}
}
}
func TestEscapeGremlin(t *testing.T) {
tests := [][]string{
{"this is a test", "this is a test"},
{`this is a %`, `this is a %%`},
{"", ""},
{`' \ % "`, `\' \\ %% \"`},
}
for _, test := range tests {
given := test[0]
expected := test[1]
result := EscapeGremlin(given)
if result != expected {
t.Error("given", given, "expected", expected, "result", result)
}
}
}
func TestPrepareArgs(t *testing.T) {
tests := [][][]interface{}{
{{"blah"}, {"blah"}},
{{"blah", 1}, {"blah", 1}},
{{"blah", 1, `escape '`}, {"blah", 1, `escape \'`}},
{{` `, ` this`, `this `, ` this `}, {``, `this`, `this`, `this`}},
}
for _, test := range tests {
given := test[0]
expected := fmt.Sprintf("%v", test[1])
result := fmt.Sprintf("%v", PrepareArgs(given, EscapeGremlin))
if result != expected {
t.Error("given", given, "expected", expected, "result", result)
}
}
}
func TestMakeGremlinQuery(t *testing.T) {
vertexId := "vertexId"
label := "label"
currentTime := "test"
testCases := []struct {
given GremlinQuery
expected string
}{
// 1. Very basic test
{
GremlinQuery{
Query: "g.V('%s')",
Args: []interface{}{"test"},
},
"g.V('test')",
},
// 2. Basic test using one of our upsert queries
{
GremlinQuery{
Query: `g.V("%s").fold().coalesce(unfold().coalesce(has("hidden").drop(), g.V("%s")),g.addV("%s").property(id, "%s").property(single, "create_date", "%s")).property(single, "last_updated_date", "%s").property(single, "%v", "%v")`,
Args: []interface{}{vertexId, vertexId, label, vertexId, currentTime, currentTime, "prop_1", "prop_2"},
},
fmt.Sprintf(`g.V("%s").fold().coalesce(unfold().coalesce(has("hidden").drop(), g.V("%s")),g.addV("%s").property(id, "%s").property(single, "create_date", "%s")).property(single, "last_updated_date", "%s").property(single, "%v", "%v")`, vertexId, vertexId, label, vertexId, currentTime, currentTime, "prop_1", "prop_2"),
},
// 3. Test using %
{
GremlinQuery{
Query: `g.V("%s").property(single, "%s", "%s")`,
Args: []interface{}{"test%", "te%st", "\"%this"},
},
`g.V("test%%").property(single, "te%%st", "\"%%this")`,
},
// 4. Test using the rest of the punctuation
{
GremlinQuery{
Query: `g.V("%s")`,
Args: []interface{}{"test/-?!*()&_=,#'><+@;.:$\\"},
},
`g.V("test/-?!*()&_=,#\'><+@;.:$\\")`,
},
// 5. Test using a big prop from our system
{
GremlinQuery{
Query: `g.V("%s").property(single, "%s", "%s")`,
Args: []interface{}{"test", "query", `SELECT prop1 as proP1, prop2, prop3 as proP3, prop4 as prop$ FROM dummy.dummy WHERE some_id <> 111111 AND other LIKE \'%@test.test\' AND prop9 = 1 AND (prop_10 > 0 OR prop_11 >= NOW() - INTERVAL 1 WEEK)`},
},
`g.V("test").property(single, "query", "SELECT prop1 as proP1, prop2, prop3 as proP3, prop4 as prop$ FROM dummy.dummy WHERE some_id <> 111111 AND other LIKE \\\'%%@test.test\\\' AND prop9 = 1 AND (prop_10 > 0 OR prop_11 >= NOW() - INTERVAL 1 WEEK)")`,
},
}
argRegexp, _ := regexp.Compile(ARG_REGEX)
for _, test := range testCases {
result, err := MakeGremlinQuery(test.given, argRegexp)
if err != nil || result != test.expected {
t.Error("given", test.given, "expected", test.expected, "result", result, "err", err)
}
}
}
func TestMakeGremlinQueryError(t *testing.T) {
// Testing characters we don't allow
argRegexp, _ := regexp.Compile(ARG_REGEX)
testCases := []struct {
given GremlinQuery
}{
// 1. Cc - Basic control character
{
GremlinQuery{
Query: "g.V('%s')",
Args: []interface{}{fmt.Sprintf("test] %s", string('\x00'))},
},
},
// 2. Cf - formatting indicator
{
GremlinQuery{
Query: "g.V('%s')",
Args: []interface{}{fmt.Sprintf("test] %s", string('\u00AD'))},
},
},
// 3. Co - private code point
{
GremlinQuery{
Query: "g.V('%s')",
Args: []interface{}{fmt.Sprintf("test] %s", string('\uE000'))},
},
},
// 4. Cn - Unassigned code point that can break Neptune
{
GremlinQuery{
Query: "g.V('%s')",
Args: []interface{}{fmt.Sprintf("test] %s", string('\U0010e4c3'))},
},
},
}
for _, test := range testCases {
result, err := MakeGremlinQuery(test.given, argRegexp)
if err == nil {
t.Error("given", test.given, "result", result, "err", err)
}
}
}