Debian Codegraph @

helper_test.go

raw symbols SCIP
1 package gojsonq
2
3 import (
4 "bytes"
5 "encoding/json"
6 "testing"
7 )
8
9 func Test_abs(t *testing.T) {
10 testCases := []struct {
11 data int
12 expected int
13 }{
14 {
15 data: 15,
16 expected: 15,
17 },
18 {
19 data: -25,
20 expected: 25,
21 },
22 {
23 data: 0,
24 expected: 0,
25 },
26 }
27 for _, tc := range testCases {
28 if o := abs(tc.data); o != tc.expected {
29 t.Errorf("expected: %v got: %v", tc.expected, o)
30 }
31 }
32 }
33
34 func Test_isIndex(t *testing.T) {
35 testCases := []struct {
36 node string
37 expected bool
38 }{
39 {
40 node: "items",
41 expected: false,
42 },
43 {
44 node: "[0]",
45 expected: true,
46 },
47 {
48 node: "[101]",
49 expected: true,
50 },
51 {
52 node: "101",
53 expected: false,
54 },
55 }
56 for _, tc := range testCases {
57 if o := isIndex(tc.node); o != tc.expected {
58 t.Errorf("expected: %v got: %v", tc.expected, o)
59 }
60 }
61 }
62
63 func Test_getIndex(t *testing.T) {
64 testCases := []struct {
65 node string
66 expected int
67 }{
68 {
69 node: "Invalid integer",
70 expected: -1,
71 },
72 {
73 node: "item",
74 expected: -1,
75 },
76 {
77 node: "[0]",
78 expected: 0,
79 },
80 {
81 node: "101",
82 expected: -1,
83 },
84 {
85 node: "[101]",
86 expected: 101,
87 },
88 }
89 for _, tc := range testCases {
90 if o, _ := getIndex(tc.node); o != tc.expected {
91 t.Errorf("expected: %v got: %v", tc.expected, o)
92 }
93 }
94 }
95
96 func Test_toString(t *testing.T) {
97 testCases := []struct {
98 val interface{}
99 expected string
100 }{
101 {
102 val: 10,
103 expected: "10",
104 },
105 {
106 val: -10,
107 expected: "-10",
108 },
109 {
110 val: 10.99,
111 expected: "10.99",
112 },
113 {
114 val: -10.99,
115 expected: "-10.99",
116 },
117 {
118 val: true,
119 expected: "true",
120 },
121 }
122
123 for _, tc := range testCases {
124 if o := toString(tc.val); o != tc.expected {
125 t.Errorf("expected: %v got: %v", tc.expected, o)
126 }
127 }
128 }
129
130 func Test_toFloat64(t *testing.T) {
131 testCases := []struct {
132 val interface{}
133 expected float64
134 }{
135 {
136 val: 10,
137 expected: 10,
138 },
139 {
140 val: int8(1),
141 expected: 1,
142 },
143 {
144 val: int16(91),
145 expected: 91,
146 },
147 {
148 val: int32(88),
149 expected: 88,
150 },
151 {
152 val: int64(898),
153 expected: 898,
154 },
155 {
156 val: float32(99.01),
157 // The nearest IEEE754 float32 value of 99.01 is 99.01000213623047; which are not equal (while using ==).
158 // Need suggestions for precision float value.
159 // one way to solve the comparison using convertFloat(string with float precision)==float64
160 expected: 99.01000213623047,
161 },
162 {
163 val: float32(-99),
164 expected: -99,
165 },
166 {
167 val: -99.91,
168 expected: -99.91,
169 },
170 {
171 val: "",
172 expected: 0,
173 },
174 {
175 val: []int{},
176 expected: 0,
177 },
178 }
179
180 for _, tc := range testCases {
181 if o, _ := toFloat64(tc.val); o != tc.expected {
182 t.Errorf("expected: %v got: %v", tc.expected, o)
183 }
184 }
185 }
186
187 func Test_sorter(t *testing.T) {
188 testCases := []struct {
189 tag string
190 asc bool
191 inArr []interface{}
192 outArr []interface{}
193 }{
194 {
195 tag: "list of string, result should be in ascending order",
196 asc: true,
197 inArr: []interface{}{"x", "b", "a", "c", "z"},
198 outArr: []interface{}{"a", "b", "c", "x", "z"},
199 },
200 {
201 tag: "list of string, result should be in descending order",
202 asc: false,
203 inArr: []interface{}{"x", "b", "a", "c", "z"},
204 outArr: []interface{}{"z", "x", "c", "b", "a"},
205 },
206 {
207 tag: "list of float64, result should be in ascending order",
208 asc: true,
209 inArr: []interface{}{8.0, 7.0, 1.0, 3.0, 5.0, 8.0},
210 outArr: []interface{}{1.0, 3.0, 5.0, 7.0, 8.0, 8.0},
211 },
212 {
213 tag: "list of float64, result should be in descending order",
214 asc: false,
215 inArr: []interface{}{8.0, 7.0, 1.0, 3.0, 5.0, 8.0},
216 outArr: []interface{}{8.0, 8.0, 7.0, 5.0, 3.0, 1.0},
217 },
218 }
219
220 for _, tc := range testCases {
221 obb, _ := json.Marshal(sortList(tc.inArr, tc.asc))
222 ebb, _ := json.Marshal(tc.outArr)
223 if !bytes.Equal(obb, ebb) {
224 t.Errorf("expected: %v got: %v", string(obb), string(ebb))
225 }
226 }
227 }
228
229 func Test_sortMap(t *testing.T) {
230 testCases := []struct {
231 tag string
232 inObjs interface{}
233 outObjs interface{}
234 key string
235 asc bool
236 }{
237 {
238 tag: "should return in ascending order of string value name",
239 key: "name",
240 asc: true,
241 inObjs: []map[string]interface{}{
242 {"name": "Z", "height": 5.8},
243 {"name": "A", "height": 5.5},
244 {"name": "D", "height": 4.9},
245 {"name": "X", "height": 5.9},
246 },
247 outObjs: []map[string]interface{}{
248 {"name": "A", "height": 5.5},
249 {"name": "D", "height": 4.9},
250 {"name": "X", "height": 5.9},
251 {"name": "Z", "height": 5.8},
252 },
253 },
254 {
255 tag: "should return in descending order of string value name",
256 key: "name",
257 asc: false,
258 inObjs: []map[string]interface{}{
259 {"name": "Z", "height": 5.8},
260 {"name": "A", "height": 5.5},
261 {"name": "D", "height": 4.9},
262 {"name": "X", "height": 5.9},
263 },
264 outObjs: []map[string]interface{}{
265 {"name": "Z", "height": 5.8},
266 {"name": "X", "height": 5.9},
267 {"name": "D", "height": 4.9},
268 {"name": "A", "height": 5.5},
269 },
270 },
271 {
272 tag: "should return in ascending order of float value height",
273 key: "height",
274 asc: true,
275 inObjs: []map[string]interface{}{
276 {"name": "Z", "height": 5.8},
277 {"name": "A", "height": 5.5},
278 {"name": "D", "height": 4.9},
279 {"name": "X", "height": 5.9},
280 },
281 outObjs: []map[string]interface{}{
282 {"name": "D", "height": 4.9},
283 {"name": "A", "height": 5.5},
284 {"name": "Z", "height": 5.8},
285 {"name": "X", "height": 5.9},
286 },
287 },
288 {
289 tag: "should return in descending order of float value height",
290 key: "height",
291 asc: false,
292 inObjs: []map[string]interface{}{
293 {"name": "Z", "height": 5.8},
294 {"name": "A", "height": 5.5},
295 {"name": "D", "height": 4.9},
296 {"name": "X", "height": 5.9},
297 },
298 outObjs: []map[string]interface{}{
299 {"name": "X", "height": 5.9},
300 {"name": "Z", "height": 5.8},
301 {"name": "A", "height": 5.5},
302 {"name": "D", "height": 4.9},
303 },
304 },
305 {
306 key: "height",
307 asc: false,
308 inObjs: []string{"a", "z", "x"},
309 outObjs: []string{"a", "z", "x"},
310 },
311 {
312 key: "invalid_key",
313 asc: false,
314 inObjs: []string{"x", "z", "a"},
315 outObjs: []string{"x", "z", "a"},
316 },
317 }
318
319 for _, tc := range testCases {
320 inObjs := tc.inObjs
321 sm := &sortMap{}
322 sm.key = tc.key
323 sm.desc = !tc.asc
324 sm.Sort(inObjs)
325 assertInterface(t, inObjs, tc.outObjs, tc.tag)
326 }
327 }
328
329 func Test_getNestedValue(t *testing.T) {
330 var content interface{}
331 if err := json.Unmarshal([]byte(jsonStr), &content); err != nil {
332 t.Error("failed to decode json:", err)
333 }
334
335 testCases := []struct {
336 tag string
337 query string
338 expected interface{}
339 expectError bool
340 }{
341 {
342 tag: "accessing node",
343 query: "vendor.name",
344 expected: `Star Trek`,
345 expectError: false,
346 },
347 {
348 tag: "should return nil",
349 query: "vendor.xox",
350 expected: nil,
351 expectError: true,
352 },
353 {
354 tag: "should return a map",
355 query: "vendor.items.[0]",
356 expected: map[string]interface{}{"id": 1, "name": "MacBook Pro 13 inch retina", "price": 1350},
357 expectError: false,
358 },
359 {
360 tag: "accessing not existed index",
361 query: "vendor.items.[10]",
362 expected: nil,
363 expectError: true,
364 },
365 {
366 tag: "accessing invalid index error",
367 query: "vendor.items.[x]",
368 expected: nil,
369 expectError: true,
370 },
371 {
372 tag: "should receive valid float value",
373 query: "vendor.items.[0].price",
374 expected: 1350,
375 expectError: false,
376 },
377 }
378
379 for _, tc := range testCases {
380 out, err := getNestedValue(content, tc.query, defaultSeparator)
381 if tc.expectError && err == nil {
382 t.Error("failed to catch error")
383 }
384 if !tc.expectError {
385 assertInterface(t, tc.expected, out, tc.tag)
386 }
387 }
388 }
389
390 func Test_makeAlias(t *testing.T) {
391 testCases := []struct {
392 tag string
393 input string
394 node string
395 alias string
396 separator string
397 }{
398 {
399 tag: "scenario 1",
400 input: "user.name as uname",
401 node: "user.name",
402 alias: "uname",
403 separator: ".",
404 },
405 {
406 tag: "scenario 2",
407 input: "post.title",
408 node: "post.title",
409 alias: "title",
410 separator: ".",
411 },
412 {
413 tag: "scenario 3",
414 input: "name",
415 node: "name",
416 alias: "name",
417 separator: ".",
418 },
419 {
420 tag: "scenario 4",
421 input: "post->title",
422 node: "post->title",
423 alias: "title",
424 separator: "->",
425 },
426 }
427
428 for _, tc := range testCases {
429 n, a := makeAlias(tc.input, tc.separator)
430 if tc.node != n || tc.alias != a {
431 t.Errorf("Tag: %v\nExpected: %v %v \nGot: %v %v\n", tc.tag, tc.node, tc.alias, n, a)
432 }
433 }
434 }
435
436 func Test_length(t *testing.T) {
437 testCases := []struct {
438 tag string
439 input interface{}
440 output int
441 errExpected bool
442 }{
443 {
444 tag: "scenario 1: should return 5 with no error",
445 input: "Hello",
446 output: 5,
447 errExpected: false,
448 },
449 {
450 tag: "scenario 2: must return error with -1",
451 input: 45,
452 output: -1,
453 errExpected: true,
454 },
455 {
456 tag: "scenario 3: must return length of array",
457 input: []interface{}{"john", "31", false},
458 output: 3,
459 errExpected: false,
460 },
461 {
462 tag: "scenario 4: must return length of map",
463 input: map[string]interface{}{"name": "john", "age": 31, "is_designer": false},
464 output: 3,
465 errExpected: false,
466 },
467 }
468
469 for _, tc := range testCases {
470 out, outErr := length(tc.input)
471 if out != tc.output {
472 if tc.errExpected && outErr == nil {
473 t.Errorf("tag: %s\nExpected: %v\nGot: %v", tc.tag, tc.output, out)
474 }
475 }
476 }
477 }