-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils_test.go
274 lines (229 loc) · 9.97 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package fireboltgosdk
import (
"database/sql/driver"
"os"
"reflect"
"strings"
"testing"
"time"
"github.com/matishsiao/goInfo"
)
func runParseSetStatementSuccess(t *testing.T, value, expectedKey, expectedValue string) {
key, value, err := parseSetStatement(value)
if err != nil {
t.Errorf("parseSetStatement returned an error, but shouldn't: %v", err)
}
if key != expectedKey {
t.Errorf("parseSetStatement real and expected key are different '%s' != '%s'", key, expectedKey)
}
if value != expectedValue {
t.Errorf("parseSetStatement real and expected value are different '%s' != '%s'", value, expectedValue)
}
}
func runParseSetStatementFail(t *testing.T, value string) {
_, _, err := parseSetStatement(value)
if err == nil {
t.Errorf("parseSetStatement didn't return an error for: %v", value)
}
}
func TestParseSetStatementSuccess(t *testing.T) {
runParseSetStatementSuccess(t, "SET time_zone=America/New_York", "time_zone", "America/New_York")
runParseSetStatementSuccess(t, " SET time_zone=America/New_York ", "time_zone", "America/New_York")
runParseSetStatementSuccess(t, " SET time_zone = America/New_York ", "time_zone", "America/New_York")
runParseSetStatementSuccess(t, "set time_zone=America/New_York", "time_zone", "America/New_York")
runParseSetStatementSuccess(t, "sEt time_zone=America/New_York", "time_zone", "America/New_York")
}
func TestParseSetStatementFail(t *testing.T) {
runParseSetStatementFail(t, "SELECT 1")
runParseSetStatementFail(t, "SET")
runParseSetStatementFail(t, "SET ==")
runParseSetStatementFail(t, "SET =1")
runParseSetStatementFail(t, "SET time_zone=")
}
func runPrepareStatementSuccess(t *testing.T, query string, params []driver.Value, expected string) {
var namedParams []driver.NamedValue
for _, param := range params {
namedParams = append(namedParams, driver.NamedValue{Value: param})
}
res, err := prepareStatement(query, namedParams)
if res != expected {
t.Errorf("expected string is not equal to result '%s' != '%s'", expected, res)
}
if err != nil {
t.Errorf("function returned an error, but it shouldn't: %v", err)
}
}
func runPrepareStatementFail(t *testing.T, query string, params []driver.Value) {
var namedParams []driver.NamedValue
for _, param := range params {
namedParams = append(namedParams, driver.NamedValue{Value: param})
}
_, err := prepareStatement(query, namedParams)
if err == nil {
t.Errorf("function didn't return an error, but it should")
}
}
func TestPrepareStatement(t *testing.T) {
runPrepareStatementSuccess(t, "select * from table", []driver.Value{}, "select * from table")
runPrepareStatementSuccess(t, "select * from table where id == ?", []driver.Value{1}, "select * from table where id == 1")
runPrepareStatementSuccess(t, "select * from table where id == '?'", []driver.Value{}, "select * from table where id == '?'")
runPrepareStatementSuccess(t, "insert into table values (?, ?, '?')", []driver.Value{1, "1"}, "insert into table values (1, '1', '?')")
runPrepareStatementSuccess(t, "select * from t where /*comment ?*/ id == ?", []driver.Value{"*/ 1 == 1 or /*"}, "select * from t where /*comment ?*/ id == '*/ 1 == 1 or /*'")
runPrepareStatementSuccess(t, "select * from t where id == ?", []driver.Value{"' or '' == '"}, "select * from t where id == '\\' or \\'\\' == \\''")
runPrepareStatementFail(t, "?", []driver.Value{})
runPrepareStatementFail(t, "?", []driver.Value{1, 2})
}
func runTestFormatValue(t *testing.T, value driver.Value, expected string) {
res, err := formatValue(value)
if err != nil {
t.Errorf("formatValue shouldn't return an error, but it did: %v", err)
}
if res != expected {
t.Errorf("formatValue real and expected resutls are different: '%s' != '%s'", res, expected)
}
}
func TestFormatValue(t *testing.T) {
loc, _ := time.LoadLocation("Europe/Berlin")
runTestFormatValue(t, "", "''")
runTestFormatValue(t, "abcd", "'abcd'")
runTestFormatValue(t, "test\\", "'test\\\\'")
runTestFormatValue(t, "test' OR '1' == '1", "'test\\' OR \\'1\\' == \\'1'")
runTestFormatValue(t, 1, "1")
runTestFormatValue(t, 1.123, "1.123")
runTestFormatValue(t, 1.123456, "1.123456")
runTestFormatValue(t, 1.1234567, "1.1234567")
runTestFormatValue(t, 1/float64(3), "0.3333333333333333")
runTestFormatValue(t, true, "true")
runTestFormatValue(t, false, "false")
runTestFormatValue(t, -10, "-10")
runTestFormatValue(t, nil, "NULL")
runTestFormatValue(t, []byte("abcd"), "E'\\x61\\x62\\x63\\x64'")
// Time
runTestFormatValue(t, time.Date(2022, 01, 10, 1, 3, 2, 123000, time.UTC), "'2022-01-10 01:03:02.000123'")
runTestFormatValue(t, time.Date(2022, 01, 10, 1, 3, 2, 123000, time.FixedZone("", 0)), "'2022-01-10 01:03:02.000123'")
runTestFormatValue(t, time.Date(2022, 01, 10, 0, 0, 0, 0, time.UTC), "'2022-01-10'")
runTestFormatValue(t, time.Date(2022, 01, 10, 0, 0, 0, 0, loc), "'2022-01-10 00:00:00.000000+01:00'")
runTestFormatValue(t, time.Date(2022, 01, 10, 1, 3, 2, 123000, loc), "'2022-01-10 01:03:02.000123+01:00'")
}
func TestConstructUserAgentString(t *testing.T) {
os.Setenv("FIREBOLT_GO_DRIVERS", "GORM/0.0.1")
os.Setenv("FIREBOLT_GO_CLIENTS", "Client1/0.2.3 Client2/0.3.4")
userAgentString := ConstructUserAgentString()
if !strings.Contains(userAgentString, sdkVersion) {
t.Errorf("sdk Version is not in userAgent string")
}
if !strings.Contains(userAgentString, "GoSDK") {
t.Errorf("sdk name is not in userAgent string")
}
if !strings.Contains(userAgentString, "GORM/0.0.1") {
t.Errorf("drivers is not in userAgent string")
}
if !strings.Contains(userAgentString, "Client1/0.2.3 Client2/0.3.4") {
t.Errorf("clients are not in userAgent string")
}
os.Unsetenv("FIREBOLT_GO_DRIVERS")
os.Unsetenv("FIREBOLT_GO_CLIENTS")
}
// FIR-25705
func TestConstructUserAgentStringFails(t *testing.T) {
// Save current function and restore at the end
old := goInfoFunc
defer func() { goInfoFunc = old }()
goInfoFunc = func() (goInfo.GoInfoObject, error) {
// Simulate goinfo failing
panic("Aaaaaaaaaa")
}
userAgentString := ConstructUserAgentString()
if userAgentString != "GoSDK" {
t.Errorf("UserAgent string was not generated correctly")
}
}
func runSplitStatement(t *testing.T, value string, expected []string) {
stmts, err := SplitStatements(value)
if err != nil {
t.Errorf("SplitStatements return an error for: %v", value)
}
if !reflect.DeepEqual(stmts, expected) {
t.Errorf("SplitStatements returned and expected are not equal: %v != %v", stmts, expected)
}
}
func TestSplitStatements(t *testing.T) {
runSplitStatement(t, "SELECT 1; SELECT 2;", []string{"SELECT 1", " SELECT 2"})
runSplitStatement(t, "SELECT 1;", []string{"SELECT 1"})
runSplitStatement(t, "SELECT 1; ", []string{"SELECT 1"})
runSplitStatement(t, "SELECT 1", []string{"SELECT 1"})
runSplitStatement(t, "SELECT 1; ; ; ; ", []string{"SELECT 1"})
runSplitStatement(t, "SET time_zone=America/New_York; SELECT 2 /*some ; comment*/", []string{"SET time_zone=America/New_York", " SELECT 2 /*some ; comment*/"})
runSplitStatement(t, "SET time_zone='America/New_York'; SELECT 2 /*some ; comment*/", []string{"SET time_zone='America/New_York'", " SELECT 2 /*some ; comment*/"})
runSplitStatement(t, "SELECT 1; SELECT 2; SELECT 3; SELECT 4; SELECT 5; SELECT 6", []string{"SELECT 1", " SELECT 2", " SELECT 3", " SELECT 4", " SELECT 5", " SELECT 6"})
}
func TestValueToNamedValue(t *testing.T) {
assert(len(valueToNamedValue([]driver.Value{})), 0, t, "valueToNamedValue of empty array is wrong")
namedValues := valueToNamedValue([]driver.Value{2, "string"})
assert(len(namedValues), 2, t, "len of namedValues is wrong")
assert(namedValues[0].Value, 2, t, "namedValues value is wrong")
assert(namedValues[1].Value, "string", t, "namedValues value is wrong")
}
func TestNewStructuredError(t *testing.T) {
errorDetails := ErrorDetails{
Severity: "error",
Name: "TestError",
Code: "123",
Description: "This is a test error",
Source: "TestSource",
Resolution: "Please fix the error",
Location: Location{
FailingLine: 10,
StartOffset: 20,
EndOffset: 30,
},
HelpLink: "https://example.com",
}
expectedMessage := "error: TestError (123) - This is a test error, TestSource, resolution: Please fix the error at {FailingLine:10 StartOffset:20 EndOffset:30}, see https://example.com"
err := NewStructuredError([]ErrorDetails{errorDetails})
if err.Message != expectedMessage {
t.Errorf("NewStructuredError returned incorrect error message, got: %s, want: %s", err.Message, expectedMessage)
}
}
func TestStructuredErrorWithMissingFields(t *testing.T) {
errorDetails := ErrorDetails{
Severity: "error",
Name: "TestError",
Code: "123",
Description: "This is a test error",
}
expectedMessage := "error: TestError (123) - This is a test error"
err := NewStructuredError([]ErrorDetails{errorDetails})
if err.Message != expectedMessage {
t.Errorf("NewStructuredError returned incorrect error message, got: %s, want: %s", err.Message, expectedMessage)
}
}
func TestStructuredErrorWithMultipleErrors(t *testing.T) {
errorDetails := ErrorDetails{
Severity: "error",
Name: "TestError",
Code: "123",
Description: "This is a test error",
Source: "TestSource",
Resolution: "Please fix the error",
Location: Location{
FailingLine: 10,
StartOffset: 20,
EndOffset: 30,
},
HelpLink: "https://example.com",
}
errorDetails2 := ErrorDetails{
Severity: "error",
Name: "TestError",
Code: "123",
Description: "This is a test error",
Source: "TestSource",
Resolution: "Please fix the error",
}
expectedMessage := "error: TestError (123) - This is a test error, TestSource, resolution: Please fix the error at {FailingLine:10 StartOffset:20 EndOffset:30}, see https://example.com\nerror: TestError (123) - This is a test error, TestSource, resolution: Please fix the error"
err := NewStructuredError([]ErrorDetails{errorDetails, errorDetails2})
if err.Message != expectedMessage {
t.Errorf("NewStructuredError returned incorrect error message, got: %s, want: %s", err.Message, expectedMessage)
}
}