-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparameter_test.go
270 lines (236 loc) · 6.59 KB
/
parameter_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
package kuzu
import (
"regexp"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func BasicParamTestHelper(t *testing.T, param any) {
_, conn := SetupTestDatabase(t)
var params = map[string]any{
"1": param,
}
preparedStatement, err := conn.Prepare("RETURN $1")
assert.Nil(t, err)
res, err := conn.Execute(preparedStatement, params)
assert.Nil(t, err)
assert.True(t, res.HasNext())
next, _ := res.Next()
value, _ := next.GetValue(0)
assert.Equal(t, param, value)
}
func FloatParamTestHelper(t *testing.T, param any) {
_, conn := SetupTestDatabase(t)
var params = map[string]any{
"1": param,
}
preparedStatement, err := conn.Prepare("RETURN $1")
assert.Nil(t, err)
res, err := conn.Execute(preparedStatement, params)
assert.Nil(t, err)
assert.True(t, res.HasNext())
next, _ := res.Next()
value, _ := next.GetValue(0)
assert.InDelta(t, param, value, floatEpsilon)
}
func TimeParamTestHelper(t *testing.T, param any) {
_, conn := SetupTestDatabase(t)
var params = map[string]any{
"1": param,
}
preparedStatement, err := conn.Prepare("RETURN $1")
assert.Nil(t, err)
res, err := conn.Execute(preparedStatement, params)
assert.Nil(t, err)
assert.True(t, res.HasNext())
next, _ := res.Next()
value, _ := next.GetValue(0)
valueTime := value.(time.Time).UTC()
paramTime := param.(time.Time).UTC()
assert.Equal(t, paramTime, valueTime)
}
func TestStringParam(t *testing.T) {
BasicParamTestHelper(t, "Hello World")
}
func TestBoolParam(t *testing.T) {
BasicParamTestHelper(t, true)
BasicParamTestHelper(t, false)
}
func TestInt64Param(t *testing.T) {
BasicParamTestHelper(t, int64(1000000000000))
}
func TestInt32Param(t *testing.T) {
BasicParamTestHelper(t, int32(200))
}
func TestInt16Param(t *testing.T) {
BasicParamTestHelper(t, int16(300))
}
func TestInt8Param(t *testing.T) {
BasicParamTestHelper(t, int8(4))
}
func TestUint64Param(t *testing.T) {
uintMax := ^uint(0)
uintMax64 := uint64(uintMax)
BasicParamTestHelper(t, uintMax64)
}
func TestUint32Param(t *testing.T) {
BasicParamTestHelper(t, uint32(600))
}
func TestUint16Param(t *testing.T) {
BasicParamTestHelper(t, uint16(700))
}
func TestUint8Param(t *testing.T) {
BasicParamTestHelper(t, uint8(8))
}
func TestFloat64Param(t *testing.T) {
FloatParamTestHelper(t, 3.14159)
}
func TestFloat32Param(t *testing.T) {
FloatParamTestHelper(t, float32(2.71828))
}
func TestTimeParam(t *testing.T) {
TimeParamTestHelper(t, time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
}
func TestTimeWithNanosecondsParam(t *testing.T) {
TimeParamTestHelper(t, time.Date(2020, 1, 1, 0, 0, 0, 1, time.UTC))
}
func TestDurationParam(t *testing.T) {
duration := time.Duration(1000000000)
BasicParamTestHelper(t, duration)
}
func TestNilParam(t *testing.T) {
BasicParamTestHelper(t, nil)
}
func TestStructParam(t *testing.T) {
goMap := map[string]any{
"name": "Alice",
"age": (int64)(30),
"isStudent": false,
}
BasicParamTestHelper(t, goMap)
}
func TestStructWithNestedStructParam(t *testing.T) {
goMap := map[string]any{
"name": "Alice",
"address": map[string]any{
"city": "New York",
"country": "USA",
},
}
BasicParamTestHelper(t, goMap)
}
func TestStructWithUnsupportedTypeParam(t *testing.T) {
goMap := map[string]any{
"name": "Alice",
"age": regexp.MustCompile(".*"),
}
_, conn := SetupTestDatabase(t)
preparedStatement, err := conn.Prepare("RETURN $1")
assert.Nil(t, err)
_, err = conn.Execute(preparedStatement, map[string]any{"1": goMap})
assert.NotNil(t, err)
expected := "failed to convert value in the map with error: unsupported type"
assert.Contains(t, err.Error(), expected)
}
func TestEmptyMapParam(t *testing.T) {
goMap := map[string]any{}
_, conn := SetupTestDatabase(t)
preparedStatement, err := conn.Prepare("RETURN $1")
assert.Nil(t, err)
_, err = conn.Execute(preparedStatement, map[string]any{"1": goMap})
assert.NotNil(t, err)
expected := "failed to create STRUCT value because the map is empty"
assert.Contains(t, err.Error(), expected)
}
func TestMapParam(t *testing.T) {
goMap := []MapItem{
{(int64)(1), "One"},
{(int64)(2), "Two"},
{(int64)(3), "Three"},
}
BasicParamTestHelper(t, goMap)
}
func TestMapParamNested(t *testing.T) {
goMap := []MapItem{
{(int64)(1),
[]MapItem{
{"a", "A"},
}},
{(int64)(2),
[]MapItem{
{"b", "B"},
}},
{(int64)(3),
[]MapItem{
{"c", "C"},
}},
}
BasicParamTestHelper(t, goMap)
}
func TestMapParamWithUnsupportedType(t *testing.T) {
goMap := []MapItem{
{(int64)(1), regexp.MustCompile(".*")},
}
_, conn := SetupTestDatabase(t)
preparedStatement, err := conn.Prepare("RETURN $1")
assert.Nil(t, err)
_, err = conn.Execute(preparedStatement, map[string]any{"1": goMap})
assert.NotNil(t, err)
expected := "failed to convert value in the slice with error: unsupported type:"
assert.Contains(t, err.Error(), expected)
}
func TestMapWithMixedTypesParam(t *testing.T) {
goMap := []MapItem{
{(int64)(1), "One"},
{(int64)(2), "Two"},
{(int64)(3), "Three"},
{(int64)(4), 4},
}
_, conn := SetupTestDatabase(t)
preparedStatement, err := conn.Prepare("RETURN $1")
assert.Nil(t, err)
_, err = conn.Execute(preparedStatement, map[string]any{"1": goMap})
assert.NotNil(t, err)
expected := "failed to create MAP value with status: 1"
assert.Contains(t, err.Error(), expected)
}
func TestSliceParam(t *testing.T) {
goSlice := []any{"One", "Two", "Three"}
BasicParamTestHelper(t, goSlice)
}
func TestSliceParamNested(t *testing.T) {
goSlice := []any{
[]any{"a", "A"},
[]any{"b", "B"},
[]any{"c", "C"},
}
BasicParamTestHelper(t, goSlice)
}
func TestSliceParamNestedStruct(t *testing.T) {
goSlice := []any{
map[string]any{"name": "Alice", "age": (int64)(30)},
map[string]any{"name": "Bob", "age": (int64)(40)},
map[string]any{"name": "Charlie", "age": (int64)(50)},
}
BasicParamTestHelper(t, goSlice)
}
func TestSliceParamWithUnsupportedType(t *testing.T) {
goSlice := []any{"One", regexp.MustCompile(".*")}
_, conn := SetupTestDatabase(t)
preparedStatement, err := conn.Prepare("RETURN $1")
assert.Nil(t, err)
_, err = conn.Execute(preparedStatement, map[string]any{"1": goSlice})
assert.NotNil(t, err)
expected := "failed to convert value in the slice with error: unsupported type:"
assert.Contains(t, err.Error(), expected)
}
func TestSliceWithMixedTypesParam(t *testing.T) {
goSlice := []any{"One", "Two", "Three", 4}
_, conn := SetupTestDatabase(t)
preparedStatement, err := conn.Prepare("RETURN $1")
assert.Nil(t, err)
_, err = conn.Execute(preparedStatement, map[string]any{"1": goSlice})
assert.NotNil(t, err)
expected := "failed to create LIST value with status: 1"
assert.Contains(t, err.Error(), expected)
}