forked from jmoiron/jsonq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsonq_test.go
324 lines (279 loc) · 6.46 KB
/
jsonq_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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package jsonq
import (
"encoding/json"
"errors"
"fmt"
"strings"
"testing"
)
const TestData = `{
"foo": 1,
"bar": 2,
"test": "Hello, world!",
"baz": 123.1,
"numstring": "42",
"floatstring": "42.1",
"array": [
{"foo": 1},
{"bar": 2},
{"baz": 3}
],
"subobj": {
"foo": 1,
"subarray": [1,2,3],
"subsubobj": {
"bar": 2,
"baz": 3,
"array": ["hello", "world"]
}
},
"collections": {
"bools": [false, true, false],
"strings": ["hello", "strings"],
"numbers": [1,2,3,4],
"arrays": [[1.0,2.0],[2.0,3.0],[4.0,3.0]],
"objects": [
{"obj1": 1},
{"obj2": 2}
]
},
"bool": true
}`
var nilStringPointer *string
var TestMap = map[string]interface{}{
"integer": iPtr(1),
"optInteger": nil,
"integer32": i32Ptr(2),
"optInteger32": nil,
"stringInt": sPtr("3"),
"string": sPtr("Hello, test!"),
"optString": nil,
"float": fPtr(123.4),
"optFloat": nil,
"array": aPtr([]interface{}{1, 2, 3}),
"optArray": nil,
"object": mPtr(map[string]interface{}{"hello": "there"}),
"optObject": nil,
"nilStringPointer": nilStringPointer,
}
func tErr(t *testing.T, err error) {
if err != nil {
t.Errorf("Error: %v\n", err)
}
}
func TestQuery(t *testing.T) {
data := map[string]interface{}{}
dec := json.NewDecoder(strings.NewReader(TestData))
err := dec.Decode(&data)
tErr(t, err)
q := NewQuery(data)
ival, err := q.Int("foo")
if ival != 1 {
t.Errorf("Expecting 1, got %v\n", ival)
}
tErr(t, err)
ival, err = q.Int("bar")
if ival != 2 {
t.Errorf("Expecting 2, got %v\n", ival)
}
tErr(t, err)
ival, err = q.Int("subobj", "foo")
if ival != 1 {
t.Errorf("Expecting 1, got %v\n", ival)
}
tErr(t, err)
// test that strings can get int-ed
ival, err = q.Int("numstring")
if ival != 42 {
t.Errorf("Expecting 42, got %v\n", ival)
}
tErr(t, err)
for i := 0; i < 3; i++ {
ival, err := q.Int("subobj", "subarray", fmt.Sprintf("%d", i))
if ival != i+1 {
t.Errorf("Expecting %d, got %v\n", i+1, ival)
}
tErr(t, err)
}
fval, err := q.Float("baz")
if fval != 123.1 {
t.Errorf("Expecting 123.1, got %f\n", fval)
}
tErr(t, err)
// test that strings can get float-ed
fval, err = q.Float("floatstring")
if fval != 42.1 {
t.Errorf("Expecting 42.1, got %v\n", fval)
}
tErr(t, err)
sval, err := q.String("test")
if sval != "Hello, world!" {
t.Errorf("Expecting \"Hello, World!\", got \"%v\"\n", sval)
}
tErr(t, err)
sval, err = q.String("subobj", "subsubobj", "array", "0")
if sval != "hello" {
t.Errorf("Expecting \"hello\", got \"%s\"\n", sval)
}
tErr(t, err)
bval, err := q.Bool("bool")
if !bval {
t.Errorf("Expecting true, got %v\n", bval)
}
tErr(t, err)
obj, err := q.Object("subobj", "subsubobj")
tErr(t, err)
q2 := NewQuery(obj)
sval, err = q2.String("array", "1")
if sval != "world" {
t.Errorf("Expecting \"world\", got \"%s\"\n", sval)
}
tErr(t, err)
aobj, err := q.Array("subobj", "subarray")
tErr(t, err)
if aobj[0].(float64) != 1 {
t.Errorf("Expecting 1, got %v\n", aobj[0])
}
iobj, err := q.Interface("numstring")
tErr(t, err)
if _, ok := iobj.(string); !ok {
t.Errorf("Expecting type string got: %s", iobj)
}
/*
Test Extraction of typed slices
*/
//test array of strings
astrings, err := q.ArrayOfStrings("collections", "strings")
tErr(t, err)
if astrings[0] != "hello" {
t.Errorf("Expecting hello, got %v\n", astrings[0])
}
//test array of ints
aints, err := q.ArrayOfInts("collections", "numbers")
tErr(t, err)
if aints[0] != 1 {
t.Errorf("Expecting 1, got %v\n", aints[0])
}
//test array of floats
afloats, err := q.ArrayOfFloats("collections", "numbers")
tErr(t, err)
if afloats[0] != 1.0 {
t.Errorf("Expecting 1.0, got %v\n", afloats[0])
}
//test array of bools
abools, err := q.ArrayOfBools("collections", "bools")
tErr(t, err)
if abools[0] {
t.Errorf("Expecting true, got %v\n", abools[0])
}
//test array of arrays
aa, err := q.ArrayOfArrays("collections", "arrays")
tErr(t, err)
if aa[0][0].(float64) != 1 {
t.Errorf("Expecting 1, got %v\n", aa[0][0])
}
//test array of objs
aobjs, err := q.ArrayOfObjects("collections", "objects")
tErr(t, err)
if aobjs[0]["obj1"].(float64) != 1 {
t.Errorf("Expecting 1, got %v\n", aobjs[0]["obj1"])
}
}
func TestQueryWithOptionalInt(t *testing.T) {
q := NewQuery(TestMap)
i, err := q.Int("integer")
if i != 1 {
t.Errorf("Expecting 1, got %v\n", i)
}
tErr(t, err)
_, err = q.Int("optInteger")
if err == nil {
t.Error("Expecting an error, got nil")
tErr(t, errors.New("No error was returned"))
}
i32, err := q.Int("integer32")
if i32 != 2 {
t.Errorf("Expecting 2, got %v\n", i32)
}
tErr(t, err)
si, err := q.Int("stringInt")
if si != 3 {
t.Errorf("Expecting 3, got %v\n", si)
}
tErr(t, err)
}
func TestQueryWithOptionalString(t *testing.T) {
q := NewQuery(TestMap)
s, err := q.String("string")
if s != "Hello, test!" {
t.Errorf("Expecting %q, got %q\n", "Hello, test!", s)
}
tErr(t, err)
_, err = q.String("optString")
if err == nil {
t.Error("Expecting an error, got nil")
tErr(t, errors.New("No error was returned"))
}
s, err = q.String("nilStringPointer")
if s != "" {
t.Errorf("Expecting %q, got %q\n", "", s)
}
tErr(t, err)
}
func TestQueryWithOptionalFloat(t *testing.T) {
q := NewQuery(TestMap)
f, err := q.Float("float")
if f != 123.4 {
t.Errorf("Expecting 123.4, got %v\n", f)
}
tErr(t, err)
_, err = q.Float("optFloat")
if err == nil {
t.Error("Expecting an error, got nil")
tErr(t, errors.New("No error was returned"))
}
}
func TestQueryWithOptionalArray(t *testing.T) {
q := NewQuery(TestMap)
a, err := q.Array("array")
if len(a) != 3 {
t.Errorf("Expecting [1, 2, 3], got %v\n", a)
}
tErr(t, err)
_, err = q.Array("optArray")
if err == nil {
t.Error("Expecting an error, got nil")
tErr(t, errors.New("No error was returned"))
}
}
func TestQueryWithOptionalObject(t *testing.T) {
q := NewQuery(TestMap)
o, err := q.Object("object")
if o["hello"] != "there" {
t.Errorf("Expecting 'hello'->'there', got %v", o)
}
tErr(t, err)
_, err = q.Object("optObject")
if err == nil {
t.Error("Expecting an error, got nil")
tErr(t, errors.New("No error was returned"))
}
}
func iPtr(i int) *int {
return &i
}
func i32Ptr(i int32) *int32 {
return &i
}
func sPtr(s string) *string {
return &s
}
func fPtr(f float64) *float64 {
return &f
}
func mPtr(m map[string]interface{}) *map[string]interface{} {
return &m
}
func aPtr(a []interface{}) *[]interface{} {
return &a
}