Debian Codegraph @

jsonq_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package gojsonq
2
3 import (
4 "bytes"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "math"
9 "reflect"
10 "strings"
11 "testing"
12 )
13
14 func TestNew(t *testing.T) {
15 jq := New()
16 if reflect.ValueOf(jq).Type().String() != "*gojsonq.JSONQ" {
17 t.Error("failed to match JSONQ type")
18 }
19 }
20
21 func TestJSONQ_String(t *testing.T) {
22 jq := New()
23 expected := fmt.Sprintf("\nContent: %s\nQueries:%v\n", string(jq.raw), jq.queries)
24 if out := jq.String(); out != expected {
25 t.Errorf("Expected: %v\n Got: %v", expected, out)
26 }
27 }
28
29 func TestJSONQ_decode(t *testing.T) {
30 testCases := []struct {
31 tag string
32 jsonStr string
33 errExpect bool
34 }{
35 {
36 tag: "valid json",
37 jsonStr: `{"name": "John Doe", "age": 30}`,
38 errExpect: false,
39 },
40 {
41 tag: "invalid json should return error",
42 jsonStr: `{"name": "John Doe", "age": 30, "only_key"}`,
43 errExpect: true,
44 },
45 }
46
47 for _, tc := range testCases {
48 jq := New()
49 jq.raw = json.RawMessage(tc.jsonStr)
50 jq.decode()
51 if err := jq.Error(); err != nil && !tc.errExpect {
52 t.Errorf("failed %s", tc.tag)
53 }
54 }
55 }
56
57 func TestJSONQ_Copy(t *testing.T) {
58 jq := New()
59 mp := map[string]int{}
60 for i := 0; i < 100; i++ {
61 adr := fmt.Sprintf("%p", jq.Copy())
62 if _, ok := mp[adr]; ok {
63 t.Error("failed to copy JSONQ")
64 } else {
65 mp[adr] = i
66 }
67 }
68 }
69
70 func TestJSONQ_File(t *testing.T) {
71 filename := "data.json"
72
73 path, f := createTestFile(t, filename)
74 defer f()
75
76 testCases := []struct {
77 tag string
78 filename string
79 expectedErr bool
80 }{
81 {
82 tag: "valid file name does not expect error",
83 filename: path,
84 expectedErr: false,
85 },
86 {
87 tag: "invalid valid file name expecting error",
88 filename: "invalid_file.xjson",
89 expectedErr: true,
90 },
91 }
92
93 for _, tc := range testCases {
94 err := New().File(tc.filename).Error()
95 if tc.expectedErr && err == nil {
96 t.Errorf("%s", tc.tag)
97 }
98 }
99 }
100
101 func TestJSONQ_JSONString(t *testing.T) {
102 testCases := []struct {
103 tag string
104 jsonStr string
105 errExpect bool
106 }{
107 {
108 tag: "valid json",
109 jsonStr: `{"name": "John Doe", "age": 30}`,
110 errExpect: false,
111 },
112 {
113 tag: "invalid json should return error",
114 jsonStr: `{"name": "John Doe", "age": 30, "only_key"}`,
115 errExpect: true,
116 },
117 }
118
119 for _, tc := range testCases {
120 if err := New().FromString(tc.jsonStr).Error(); err != nil && !tc.errExpect {
121 t.Errorf("failed %s", tc.tag)
122 }
123 }
124 }
125
126 func TestJSONQ_FromString(t *testing.T) {
127 testCases := []struct {
128 tag string
129 inputStr string
130 errExpect bool
131 }{
132 // TODO: Doesn't need to test decoder for input content
133 {
134 tag: "valid json",
135 inputStr: `{"name": "John Doe", "age": 30}`,
136 errExpect: false,
137 },
138 {
139 tag: "invalid json should return error",
140 inputStr: `{"name": "John Doe", "age": 30, "only_key"}`,
141 errExpect: true,
142 },
143 }
144
145 for _, tc := range testCases {
146 if err := New().FromString(tc.inputStr).Error(); err != nil && !tc.errExpect {
147 t.Errorf("failed %s", tc.tag)
148 }
149 }
150 }
151
152 func TestJSONQ_Reader(t *testing.T) {
153 testCases := []struct {
154 tag string
155 jsonStr string
156 errExpect bool
157 }{
158 {
159 tag: "valid json",
160 jsonStr: `{"name": "John Doe", "age": 30}`,
161 errExpect: false,
162 },
163 {
164 tag: "invalid json should return error",
165 jsonStr: `{"name": "John Doe", "age": 30, "only_key"}`,
166 errExpect: true,
167 },
168 }
169
170 for _, tc := range testCases {
171 rdr := strings.NewReader(tc.jsonStr)
172 if err := New().Reader(rdr).Error(); err != nil && !tc.errExpect {
173 t.Errorf("failed %s", tc.tag)
174 }
175 }
176 }
177
178 type invalidReader string
179
180 func (invalidReader) Read(p []byte) (n int, err error) {
181 return 0, errors.New("this reader always return an error")
182 }
183
184 func TestJSONQ_Reader_expecting_error(t *testing.T) {
185 var rdr invalidReader
186 if err := New().Reader(rdr).Error(); err == nil {
187 t.Errorf("failed to catch Reader error")
188 }
189 }
190
191 func TestJSONQ_Errors(t *testing.T) {
192 testCases := []struct {
193 tag string
194 jsonStr string
195 }{
196 {
197 tag: "invalid json 1",
198 jsonStr: `{"name": "John Doe", "age": 30, :""}`,
199 },
200 {
201 tag: "invalid json 2",
202 jsonStr: `{"name": "John Doe", "age": 30, "only_key"}`,
203 },
204 }
205
206 for _, tc := range testCases {
207 if errs := New().FromString(tc.jsonStr).Errors(); len(errs) == 0 {
208 t.Errorf("failed %s", tc.tag)
209 }
210 }
211 }
212
213 func TestJSONQ_Macro(t *testing.T) {
214 jq := New()
215 jq.Macro("mac1", func(x, y interface{}) (bool, error) {
216 return true, nil
217 })
218
219 if _, ok := jq.queryMap["mac1"]; !ok {
220 t.Error("failed to register macro")
221 }
222
223 jq.Macro("mac1", func(x, y interface{}) (bool, error) {
224 return true, nil
225 })
226 if jq.Error() == nil {
227 t.Error("failed to throw error for already registered macro")
228 }
229 }
230
231 func TestJSONQ_From_Set(t *testing.T) {
232 node := "root.items.[0].name"
233 jq := New().From(node)
234 if jq.node != node {
235 t.Error("failed to set node name")
236 }
237 }
238
239 func TestJSONQ_Select(t *testing.T) {
240 jq := New().Select("id", "name")
241 if len(jq.attributes) != 2 {
242 t.Error("failed to set properties")
243 }
244 }
245
246 func TestJSONQ_Offset(t *testing.T) {
247 jq := New().Offset(3)
248 if jq.offsetRecords != 3 {
249 t.Error("failed to set offset records value")
250 }
251 }
252
253 func TestJSONQ_Limit(t *testing.T) {
254 jq := New().Limit(12)
255 if jq.limitRecords != 12 {
256 t.Error("failed to set limit records value")
257 }
258 }
259
260 func TestJSONQ_reset(t *testing.T) {
261 node := "root.items"
262 jq := New().From(node).Select("name", "age").WhereEqual("price", "1900").WhereEqual("id", 1)
263 jq.reset()
264 if len(jq.queries) != 0 ||
265 len(jq.attributes) != 0 ||
266 jq.queryIndex != 0 {
267 t.Error("reset failed")
268 }
269 }
270
271 func TestJSONQ_Reset(t *testing.T) {
272 node := "root.items"
273 jq := New().From(node).WhereEqual("price", "1900").WhereEqual("id", 1)
274 jq.Reset()
275 if len(jq.queries) != 0 || jq.queryIndex != 0 || jq.node != "" {
276 t.Error("reset failed")
277 }
278 }
279
280 func TestJSONQ_From(t *testing.T) {
281 testCases := []struct {
282 tag string
283 query string
284 expected string
285 expectError bool
286 }{
287 {
288 tag: "accessing node",
289 query: "vendor.name",
290 expected: `"Star Trek"`,
291 expectError: false,
292 },
293 {
294 tag: "accessing not existed index",
295 query: "vendor.items.[0]",
296 expected: `{"id":1,"name":"MacBook Pro 13 inch retina","price":1350}`,
297 expectError: false,
298 },
299 {
300 tag: "accessing not existed index",
301 query: "vendor.items.[10]",
302 expected: `null`,
303 expectError: false,
304 },
305 {
306 tag: "accessing invalid index error",
307 query: "vendor.items.[x]",
308 expectError: true,
309 },
310 }
311
312 for _, tc := range testCases {
313 jq := New().FromString(jsonStr)
314 out := jq.From(tc.query).Get()
315 if tc.expectError && jq.Error() == nil {
316 t.Error("failed to catch error")
317 }
318 if !tc.expectError {
319 assertJSON(t, out, tc.expected, tc.tag)
320 }
321 }
322
323 jq := New().FromString(jsonStr)
324 expJSON := `[{"id":3,"name":"Sony VAIO","price":1200}]`
325 out := jq.From("vendor.items").GroupBy("price").From("1200").Get()
326 assertJSON(t, out, expJSON, "accessing group by data")
327 }
328
329 func TestJSONQ_FromInterface(t *testing.T) {
330 var v map[string]interface{}
331 err := json.Unmarshal([]byte(jsonStr), &v)
332 if err != nil {
333 t.Error(err)
334 }
335 jq := New().FromInterface(v)
336 if jq.rootJSONContent == nil || jq.jsonContent == nil {
337 t.Errorf("failed to assign value using FromInterface method")
338 }
339
340 var customType float64
341 jq = New().FromInterface(customType)
342 if jq.Error() == nil {
343 t.Errorf("failed to set error properly for FromInterface method")
344 }
345 }
346
347 func TestJSONQ_Where_single_where(t *testing.T) {
348 jq := New().FromString(jsonStr).
349 From("vendor.items").
350 Where("price", "=", 1700)
351 expected := `[{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
352 out := jq.Get()
353 assertJSON(t, out, expected, "single Where")
354 }
355
356 func TestJSONQ_Where_deep_nested_value(t *testing.T) {
357 jq := New().FromString(jsonStrUsers).
358 From("users").
359 Where("name.first", "=", "John")
360 expected := `[{"id":1,"name":{"first":"John","last":"Ramboo"}},{"id":3,"name":{"first":"John","last":"Doe"}}]`
361 out := jq.Get()
362 assertJSON(t, out, expected, "single Where with nested value")
363 }
364
365 func TestJSONQ_Where_multiple_where_expecting_result(t *testing.T) {
366 jq := New().FromString(jsonStr).
367 From("vendor.items").
368 Where("price", "=", 1700).
369 Where("id", "=", 2)
370 expected := `[{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
371 out := jq.Get()
372 assertJSON(t, out, expected, "multiple Where expecting data")
373 }
374
375 func TestJSONQ_Where_multiple_where_expecting_empty_result(t *testing.T) {
376 jq := New().FromString(jsonStr).
377 From("vendor.items").
378 Where("price", "=", 1700).
379 Where("id", "=", "1700")
380 expected := `[]`
381 out := jq.Get()
382 assertJSON(t, out, expected, "multiple Where expecting empty result")
383 }
384
385 func TestJSONQ_Where_multiple_where_with_invalid_operator_expecting_error(t *testing.T) {
386 jq := New().FromString(jsonStr).
387 From("vendor.items").
388 Where("price", "invalid_op", 1700)
389 jq.Get()
390
391 if jq.Error() == nil {
392 t.Error("expecting: invalid operator invalid_op")
393 }
394 }
395
396 func TestJSONQ_Where_multiple_where_with_invalid_operand_expecting_error(t *testing.T) {
397 jq := New().FromString(jsonStr).
398 From("vendor.items").
399 Where("price", "contains", 1700)
400 jq.Get()
401
402 if jq.Error() == nil {
403 t.Error("expecting: invalid operator invalid_op")
404 }
405 }
406
407 func TestJSONQ_single_WhereEqual(t *testing.T) {
408 jq := New().FromString(jsonStr).
409 From("vendor.items").
410 WhereEqual("price", 1700)
411 expected := `[{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
412 out := jq.Get()
413 assertJSON(t, out, expected, "single WhereEqual")
414 }
415
416 func TestJSONQ_multiple_WhereEqual_expecting_data(t *testing.T) {
417 jq := New().FromString(jsonStr).
418 From("vendor.items").
419 WhereEqual("price", 1700).
420 WhereEqual("id", 2)
421 expected := `[{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
422 out := jq.Get()
423 assertJSON(t, out, expected, "multiple WhereEqual expecting data")
424 }
425
426 func TestJSONQ_multiple_WhereEqual_expecting_empty_data(t *testing.T) {
427 jq := New().FromString(jsonStr).
428 From("vendor.items").
429 WhereEqual("price", 1700).
430 WhereEqual("id", "1700")
431 expected := `[]`
432 out := jq.Get()
433 assertJSON(t, out, expected, "multiple WhereEqual expecting empty result")
434 }
435
436 func TestJSONQ_single_WhereNotEqual(t *testing.T) {
437 jq := New().FromString(jsonStr).
438 From("vendor.items").
439 WhereNotEqual("price", 850)
440 expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700},{"id":3,"name":"Sony VAIO","price":1200},{"id":6,"name":"HP core i7","price":950}]`
441 out := jq.Get()
442 assertJSON(t, out, expected, "single WhereNotEqual")
443 }
444
445 func TestJSONQ_multiple_WhereNotEqual(t *testing.T) {
446 jq := New().FromString(jsonStr).
447 From("vendor.items").
448 WhereNotEqual("price", 850).
449 WhereNotEqual("id", 2)
450 expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":3,"name":"Sony VAIO","price":1200},{"id":6,"name":"HP core i7","price":950}]`
451 out := jq.Get()
452 assertJSON(t, out, expected, "multiple WhereNotEqual expecting result")
453 }
454
455 func TestJSONQ_WhereNil(t *testing.T) {
456 jq := New().FromString(jsonStr).
457 From("vendor.items").
458 WhereNil("id")
459 expected := `[{"id":null,"name":"HP core i3 SSD","price":850}]`
460 out := jq.Get()
461 assertJSON(t, out, expected)
462 }
463
464 func TestJSONQ_WhereNotNil(t *testing.T) {
465 jq := New().FromString(jsonStr).
466 From("vendor.items").
467 WhereNotNil("id")
468 expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700},{"id":3,"name":"Sony VAIO","price":1200},{"id":4,"name":"Fujitsu","price":850},{"id":5,"key":2300,"name":"HP core i5","price":850},{"id":6,"name":"HP core i7","price":950}]`
469 out := jq.Get()
470 assertJSON(t, out, expected)
471 }
472
473 func TestJSONQ_WhereIn_expecting_result(t *testing.T) {
474 jq := New().FromString(jsonStr).
475 From("vendor.items").
476 WhereIn("id", []int{1, 3, 5})
477 expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":3,"name":"Sony VAIO","price":1200},{"id":5,"key":2300,"name":"HP core i5","price":850}]`
478 out := jq.Get()
479 assertJSON(t, out, expected, "WhereIn expecting result")
480 }
481
482 func TestJSONQ_WhereIn_expecting_empty_result(t *testing.T) {
483 jq := New().FromString(jsonStr).
484 From("vendor.items").
485 WhereIn("id", []int{18, 39, 85})
486 expected := `[]`
487 out := jq.Get()
488 assertJSON(t, out, expected, "WhereIn expecting empty result")
489 }
490
491 func TestJSONQ_WhereNotIn_expecting_result(t *testing.T) {
492 jq := New().FromString(jsonStr).
493 From("vendor.items").
494 WhereNotIn("id", []int{1, 3, 5, 6})
495 expected := `[{"id":2,"name":"MacBook Pro 15 inch retina","price":1700},{"id":4,"name":"Fujitsu","price":850},{"id":null,"name":"HP core i3 SSD","price":850}]`
496 out := jq.Get()
497 assertJSON(t, out, expected, "WhereIn expecting result")
498 }
499
500 func TestJSONQ_WhereNotIn_expecting_empty_result(t *testing.T) {
501 jq := New().FromString(jsonStr).
502 From("vendor.items").
503 WhereNotIn("price", []float64{850, 950, 1200, 1700, 1350})
504 expected := `[]`
505 out := jq.Get()
506 assertJSON(t, out, expected, "WhereIn expecting empty result")
507 }
508
509 func TestJSONQ_OrWhere(t *testing.T) {
510 jq := New().FromString(jsonStr).
511 From("vendor.items").
512 OrWhere("price", ">", 1200)
513 expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
514 out := jq.Get()
515 assertJSON(t, out, expected, "OrWhere expecting result")
516 }
517
518 func TestJSONQ_WhereStartsWith_expecting_result(t *testing.T) {
519 jq := New().FromString(jsonStr).
520 From("vendor.items").
521 WhereStartsWith("name", "Mac")
522 expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
523 out := jq.Get()
524 assertJSON(t, out, expected, "WhereStartsWith expecting result")
525 }
526
527 func TestJSONQ_WhereStartsWith_expecting_empty_result(t *testing.T) {
528 jq := New().FromString(jsonStr).
529 From("vendor.items").
530 WhereStartsWith("name", "xyz")
531 expected := `[]`
532 out := jq.Get()
533 assertJSON(t, out, expected, "WhereStartsWith expecting empty result")
534 }
535
536 func TestJSONQ_WhereEndsWith(t *testing.T) {
537 jq := New().FromString(jsonStr).
538 From("vendor.items").
539 WhereEndsWith("name", "retina")
540 expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
541 out := jq.Get()
542 assertJSON(t, out, expected, "WhereStartsWith expecting result")
543 }
544
545 func TestJSONQ_WhereEndsWith_empty_result(t *testing.T) {
546 jq := New().FromString(jsonStr).
547 From("vendor.items").
548 WhereEndsWith("name", "xyz")
549 expected := `[]`
550 out := jq.Get()
551 assertJSON(t, out, expected, "WhereStartsWith expecting empty result")
552 }
553
554 func TestJSONQ_WhereContains_expecting_result(t *testing.T) {
555 jq := New().FromString(jsonStr).
556 From("vendor.items").
557 WhereContains("name", "RetinA")
558 expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
559 out := jq.Get()
560 assertJSON(t, out, expected, "WhereContains expecting result")
561 }
562
563 func TestJSONQ_WhereContains_expecting_empty_result(t *testing.T) {
564 jq := New().FromString(jsonStr).
565 From("vendor.items").
566 WhereContains("name", "xyz")
567 expected := `[]`
568 out := jq.Get()
569 assertJSON(t, out, expected, "WhereContains expecting empty result")
570 }
571
572 func TestJSONQ_WhereStrictContains_expecting_result(t *testing.T) {
573 jq := New().FromString(jsonStr).
574 From("vendor.items").
575 WhereStrictContains("name", "retina")
576 expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
577 out := jq.Get()
578 assertJSON(t, out, expected, "WhereContains expecting result")
579 }
580
581 func TestJSONQ_WhereStrictContains_expecting_empty_result(t *testing.T) {
582 jq := New().FromString(jsonStr).
583 From("vendor.items").
584 WhereStrictContains("name", "RetinA")
585 expected := `[]`
586 out := jq.Get()
587 assertJSON(t, out, expected, "WhereContains expecting empty result")
588 }
589
590 func TestJSONQ_GroupBy(t *testing.T) {
591 jq := New().FromString(jsonStr).
592 From("vendor.items").
593 GroupBy("price")
594 expected := `{"1200":[{"id":3,"name":"Sony VAIO","price":1200}],"1350":[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350}],"1700":[{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}],"850":[{"id":4,"name":"Fujitsu","price":850},{"id":5,"key":2300,"name":"HP core i5","price":850},{"id":null,"name":"HP core i3 SSD","price":850}],"950":[{"id":6,"name":"HP core i7","price":950}]}`
595 out := jq.Get()
596 assertJSON(t, out, expected, "GroupBy expecting result")
597 }
598
599 func TestJSONQ_GroupBy_expecting_error(t *testing.T) {
600 jq := New().FromString(jsonStr).
601 From("vendor.items").
602 GroupBy("invalid_key")
603 expected := `{}`
604 out := jq.Get()
605 assertJSON(t, out, expected, "GroupBy expecting empty result")
606 if len(jq.Errors()) == 0 {
607 t.Error("failed to catch GroupBy error")
608 }
609 }
610
611 func TestJSONQ_GroupBy_nested_property(t *testing.T) {
612 jq := New().FromString(jsonStrUsers).
613 From("users").
614 GroupBy("name.first")
615 expected := `{"Ethan":[{"id":2,"name":{"first":"Ethan","last":"Hunt"}}],"John":[{"id":1,"name":{"first":"John","last":"Ramboo"}},{"id":3,"name":{"first":"John","last":"Doe"}}]}`
616 out := jq.Get()
617 assertJSON(t, out, expected, "GroupBy nested expecting result")
618 }
619
620 func TestJSONQ_GroupBy_nested_property_expecting_error(t *testing.T) {
621 jq := New().FromString(jsonStrUsers).
622 From("users").
623 GroupBy("name.invalid_key")
624 out := jq.Get()
625 expected := `{}`
626 assertJSON(t, out, expected, "Nsested GroupBy expecting empty result")
627 if len(jq.errors) == 0 {
628 t.Error("failed to catch GroupBy nested property error")
629 }
630 }
631
632 func TestJSONQ_Sort_string_ascending_order(t *testing.T) {
633 jq := New().FromString(jsonStr).
634 From("vendor.names").
635 Sort()
636 expected := `["Abby","Jane Doe","Jerry","John Doe","Nicolas","Tom"]`
637 out := jq.Get()
638 assertJSON(t, out, expected, "sorting array of string in ascending desc")
639 }
640
641 func TestJSONQ_Sort_float64_descending_order(t *testing.T) {
642 jq := New().FromString(jsonStr).
643 From("vendor.prices").
644 Sort("desc")
645 expected := `[2400,2100,1200,400.87,150.1,89.9]`
646 out := jq.Get()
647 assertJSON(t, out, expected, "sorting array of float in descending order")
648 }
649
650 func TestJSONQ_Sort_with_two_args_expecting_error(t *testing.T) {
651 jq := New().FromString(jsonStr).
652 From("vendor.prices").
653 Sort("asc", "desc")
654 jq.Get()
655 if jq.Error() == nil {
656 t.Error("expecting an error")
657 }
658 }
659
660 func TestJSONQ_SortBy_float_ascending_order(t *testing.T) {
661 jq := New().FromString(jsonStr).
662 From("vendor.items").
663 SortBy("price")
•664 expected := `[{"id":4,"name":"Fujitsu","price":850},{"id":5,"key":2300,"name":"HP core i5","price":850},{"id":null,"name":"HP core i3 SSD","price":850},{"id":6,"name":"HP core i7","price":950},{"id":3,"name":"Sony VAIO","price":1200},{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
665 out := jq.Get()
666 assertJSON(t, out, expected, "sorting array of object by its key (price-float64) in ascending desc")
667 }
668
669 func TestJSONQ_SortBy_float_descending_order(t *testing.T) {
670 jq := New().FromString(jsonStr).
671 From("vendor.items").
672 SortBy("price", "desc")
673 expected := `[{"id":2,"name":"MacBook Pro 15 inch retina","price":1700},{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":3,"name":"Sony VAIO","price":1200},{"id":6,"name":"HP core i7","price":950},{"id":4,"name":"Fujitsu","price":850},{"id":5,"key":2300,"name":"HP core i5","price":850},{"id":null,"name":"HP core i3 SSD","price":850}]`
674 out := jq.Get()
675 assertJSON(t, out, expected, "sorting array of object by its key (price-float64) in descending desc")
676 }
677
678 func TestJSONQ_SortBy_string_ascending_order(t *testing.T) {
679 jq := New().FromString(jsonStr).
680 From("vendor.items").
681 SortBy("name")
682 expected := `[{"id":4,"name":"Fujitsu","price":850},{"id":null,"name":"HP core i3 SSD","price":850},{"id":5,"key":2300,"name":"HP core i5","price":850},{"id":6,"name":"HP core i7","price":950},{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700},{"id":3,"name":"Sony VAIO","price":1200}]`
683 out := jq.Get()
684 assertJSON(t, out, expected, "sorting array of object by its key (name-string) in ascending desc")
685 }
686
687 func TestJSONQ_SortBy_string_descending_order(t *testing.T) {
688 jq := New().FromString(jsonStr).
689 From("vendor.items").
690 SortBy("name", "desc")
691 expected := `[{"id":3,"name":"Sony VAIO","price":1200},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700},{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":6,"name":"HP core i7","price":950},{"id":5,"key":2300,"name":"HP core i5","price":850},{"id":null,"name":"HP core i3 SSD","price":850},{"id":4,"name":"Fujitsu","price":850}]`
692 out := jq.Get()
693 assertJSON(t, out, expected, "sorting array of object by its key (name-string) in descending desc")
694 }
695
696 func TestJSONQ_SortBy_deep_nested_string_ascending_order(t *testing.T) {
697 jq := New().FromString(jsonStrUsers).
698 From("users").
699 SortBy("name.first")
700 expected := `[{"id":2,"name":{"first":"Ethan","last":"Hunt"}},{"id":1,"name":{"first":"John","last":"Ramboo"}},{"id":3,"name":{"first":"John","last":"Doe"}}]`
701 out := jq.Get()
702 assertJSON(t, out, expected, "sorting array of object by its key (name-string) in descending desc")
703 }
704
705 func TestJSONQ_SortBy_deep_nested_string_invalid_key_should_return_error(t *testing.T) {
706 jq := New().FromString(jsonStrUsers).
707 From("users").
708 SortBy("name.middle")
709 expected := `[{"id":1,"name":{"first":"John","last":"Ramboo"}},{"id":2,"name":{"first":"Ethan","last":"Hunt"}},{"id":3,"name":{"first":"John","last":"Doe"}}]` // no ordering, remain same
710 out := jq.Get()
711 assertJSON(t, out, expected, "sorting array of object by its key (name-string) in descending desc")
712 if len(jq.errors) == 0 {
713 t.Error("invalid path should return error/errors in SortBy")
714 }
715 }
716
717 func TestJSONQ_SortBy_no_argument_expecting_error(t *testing.T) {
718 jq := New().FromString(jsonStr).
719 From("vendor.items").
720 SortBy()
721 jq.Get()
722 if jq.Error() == nil {
723 t.Error("expecting an error")
724 }
725 }
726
727 func TestJSONQ_SortBy_more_than_two_argument_expecting_error(t *testing.T) {
728 jq := New().FromString(jsonStr).
729 From("vendor.items").
730 SortBy("name", "desc", "asc")
731 jq.Get()
732 if jq.Error() == nil {
733 t.Error("expecting an error")
734 }
735 }
736
737 func TestJSONQ_SortBy_expecting_as_provided_node_is_not_list(t *testing.T) {
738 jq := New().FromString(jsonStr).
739 From("name").
740 SortBy("name", "desc")
741 out := jq.Get()
742 expJSON := `"computers"`
743 assertJSON(t, out, expJSON)
744 }
745
746 func TestJSONQ_SortBy_expecting_empty_as_provided_node_is_not_list(t *testing.T) {
747 jq := New().FromString(jsonStr).
748 From("vendor.items").Where("price", ">", 2500).
749 SortBy("name", "desc")
750 out := jq.Get()
751 expJSON := `[]`
752 assertJSON(t, out, expJSON)
753 }
754
755 func TestJSONQ_Distinct(t *testing.T) {
756 jq := New().FromString(jsonStr).
757 From("vendor.items").
758 Distinct("price")
759 expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700},{"id":3,"name":"Sony VAIO","price":1200},{"id":4,"name":"Fujitsu","price":850},{"id":6,"name":"HP core i7","price":950}]`
760 out := jq.Get()
761 assertJSON(t, out, expected, "Distinct expecting result")
762 }
763
764 func TestJSONQ_Distinct_expecting_error(t *testing.T) {
765 jq := New().FromString(jsonStr).
766 From("vendor.items").
767 Distinct("invalid_key")
768 expected := `[]`
769 out := jq.Get()
770 assertJSON(t, out, expected, "Distinct expecting empty result")
771 if len(jq.Errors()) == 0 {
772 t.Error("failed to catch Distinct error")
773 }
774 }
775
776 func TestJSONQ_Only(t *testing.T) {
777 jq := New().FromString(jsonStr).
778 From("vendor.items")
779 expected := `[{"id":1,"price":1350},{"id":2,"price":1700},{"id":3,"price":1200},{"id":4,"price":850},{"id":5,"price":850},{"id":6,"price":950},{"id":null,"price":850}]`
780 out := jq.Only("id", "price")
781 assertJSON(t, out, expected)
782 }
783
784 func TestJSONQ_Only_with_distinct(t *testing.T) {
785 jq := New().FromString(jsonStr).
786 From("vendor.items").Distinct("price")
787 expected := `[{"id":1,"price":1350},{"id":2,"price":1700},{"id":3,"price":1200},{"id":4,"price":850},{"id":6,"price":950}]`
788 out := jq.Only("id", "price")
789 assertJSON(t, out, expected)
790 }
791
792 func TestJSONQ_OnlyR(t *testing.T) {
793 jq := New().FromString(jsonStr).
794 From("vendor.items")
795 result, err := jq.OnlyR("name", "price")
796 if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
797 t.Error("failed to match Result type")
798 }
799 }
800
801 func TestJSONQ_OnlyR_error(t *testing.T) {
802 jq := New().FromString(jsonStr).
803 From("invalid_path")
804 result, err := jq.OnlyR("name", "price")
805 if result != nil && err == nil {
806 t.Error("failed to catch error")
807 }
808 }
809
810 func TestJSONQ_First_expecting_result(t *testing.T) {
811 jq := New().FromString(jsonStr).
812 From("vendor.items")
813 expected := `{"id":1,"name":"MacBook Pro 13 inch retina","price":1350}`
814 out := jq.First()
815 assertJSON(t, out, expected, "First expecting result")
816 }
817
818 func TestJSONQ_First_expecting_empty_result(t *testing.T) {
819 jq := New().FromString(jsonStr).
820 From("vendor.items").
821 Where("price", ">", 1800)
822 expected := `null`
823 out := jq.First()
824 assertJSON(t, out, expected, "First expecting empty result")
825 }
826
827 func TestJSONQ_First_distinct_expecting_result(t *testing.T) {
828 jq := New().FromString(jsonStr).
829 From("vendor.items").Distinct("price").Where("price", "=", 850)
830 expected := `{"id":4,"name":"Fujitsu","price":850}`
831 out := jq.First()
832 assertJSON(t, out, expected, "First with distinct & where expecting result result")
833 }
834
835 func TestJSONQ_FirstR(t *testing.T) {
836 jq := New().FromString(jsonStr).
837 From("vendor.items").Distinct("price").Where("price", "=", 850)
838 result, err := jq.FirstR()
839 if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
840 t.Error("failed to match Result type")
841 }
842 }
843
844 func TestJSONQ_FirstR_error(t *testing.T) {
845 jq := New().FromString(jsonStr).
846 From("invalid").Distinct("price").Where("price", "=", 850)
847 result, err := jq.FirstR()
848 if result != nil && err == nil {
849 t.Error("failed to catch error")
850 }
851 }
852
853 func TestJSONQ_Last_expecting_result(t *testing.T) {
854 jq := New().FromString(jsonStr).
855 From("vendor.items")
856 expected := `{"id":null,"name":"HP core i3 SSD","price":850}`
857 out := jq.Last()
858 assertJSON(t, out, expected, "Last expecting result")
859 }
860
861 func TestJSONQ_Last_expecting_empty_result(t *testing.T) {
862 jq := New().FromString(jsonStr).
863 From("vendor.items").
864 Where("price", ">", 1800)
865 expected := `null`
866 out := jq.Last()
867 assertJSON(t, out, expected, "Last expecting empty result")
868 }
869
870 func TestJSONQ_Last_distinct_expecting_result(t *testing.T) {
871 jq := New().FromString(jsonStr).
872 From("vendor.items").Distinct("price").Where("price", "=", 850)
873 expected := `{"id":4,"name":"Fujitsu","price":850}`
874 out := jq.Last()
875 assertJSON(t, out, expected, "Last with distinct & where expecting result result")
876 }
877
878 func TestJSONQ_LastR(t *testing.T) {
879 jq := New().FromString(jsonStr).
880 From("vendor.items").Distinct("price").Where("price", "=", 850)
881 result, err := jq.LastR()
882 if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
883 t.Error("failed to match Result type")
884 }
885 }
886
887 func TestJSONQ_LastR_error(t *testing.T) {
888 jq := New().FromString(jsonStr).
889 From("invalid_path").Distinct("price").Where("price", "=", 850)
890 result, err := jq.LastR()
891 if result != nil && err == nil {
892 t.Error("failed to catch error")
893 }
894 }
895
896 func TestJSONQ_Nth_expecting_result(t *testing.T) {
897 jq := New().FromString(jsonStr).
898 From("vendor.items")
899 expected := `{"id":1,"name":"MacBook Pro 13 inch retina","price":1350}`
900 out := jq.Nth(1)
901 assertJSON(t, out, expected, "Nth expecting result")
902 }
903
904 func TestJSONQ_Nth_expecting_empty_result_with_error(t *testing.T) {
905 jq := New().FromString(jsonStr).
906 From("vendor.items").
907 Where("price", ">", 1800)
908 expected := `null`
909 out := jq.Nth(1)
910 assertJSON(t, out, expected, "Nth expecting empty result with an error")
911
912 if jq.Error() == nil {
913 t.Error("expecting an error for empty result nth value")
914 }
915 }
916
917 func TestJSONQ_Nth_expecting_empty_result_with_error_index_out_of_range(t *testing.T) {
918 jq := New().FromString(jsonStr).
919 From("vendor.items")
920 expected := `null`
921 out := jq.Nth(100)
922 assertJSON(t, out, expected, "Nth expecting empty result with an error of index out of range")
923
924 if jq.Error() == nil {
925 t.Error("expecting an error for empty result nth value")
926 }
927 }
928
929 func TestJSONQ_Nth_expecting_result_from_last_using_negative_index(t *testing.T) {
930 jq := New().FromString(jsonStr).
931 From("vendor.items")
932 expected := `{"id":null,"name":"HP core i3 SSD","price":850}`
933 out := jq.Nth(-1)
934 assertJSON(t, out, expected, "Nth expecting result form last when providing -1")
935 }
936
937 func TestJSONQ_Nth_expecting_error_providing_zero_as_index(t *testing.T) {
938 jq := New().FromString(jsonStr).
939 From("vendor.items").
940 Where("price", ">", 1800)
941 jq.Nth(0)
942 if jq.Error() == nil {
943 t.Error("expecting error")
944 }
945 }
946
947 func TestJSONQ_Nth_expecting_empty_result_as_node_is_map(t *testing.T) {
948 jq := New().FromString(jsonStr).
949 From("vendor.items.[0]")
950 out := jq.Nth(0)
951 expected := `null`
952 assertJSON(t, out, expected, "Nth expecting empty result if the node is a map")
953 }
954
955 func TestJSONQ_Nth_expecting_empty_result_as_node_is_object(t *testing.T) {
956 jq := New().FromString(jsonStr).
957 From("vendor.items.[0]")
958 out := jq.Nth(1)
959 expected := `null`
960 assertJSON(t, out, expected, "Nth expecting empty result if the node is a object")
961 }
962
963 func TestJSONQ_Nth_distinct_expecting_result(t *testing.T) {
964 jq := New().FromString(jsonStr).
965 From("vendor.items").Distinct("price")
966 expected := `{"id":1,"name":"MacBook Pro 13 inch retina","price":1350}`
967 out := jq.Nth(1)
968 assertJSON(t, out, expected, "Last with distinct & where expecting result result")
969 }
970
971 func TestJSONQ_NthR(t *testing.T) {
972 jq := New().FromString(jsonStr).
973 From("vendor.items").Distinct("price").Where("price", "=", 850)
974 result, err := jq.NthR(1)
975 if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
976 t.Error("failed to match Result type")
977 }
978 }
979
980 func TestJSONQ_NthR_error(t *testing.T) {
981 jq := New().FromString(jsonStr).
982 From("invalid_path").Distinct("price").Where("price", "=", 850)
983 result, err := jq.NthR(1)
984 if result != nil && err == nil {
985 t.Error("failed to catch error")
986 }
987 }
988
989 func TestJSONQ_Find_simple_property(t *testing.T) {
990 jq := New().FromString(jsonStr)
991 out := jq.Find("name")
992 expected := `"computers"`
993 assertJSON(t, out, expected, "Find expecting name computers")
994 }
995
996 func TestJSONQ_Find_nested_property(t *testing.T) {
997 jq := New().FromString(jsonStr)
998 out := jq.Find("vendor.items.[0]")
999 expected := `{"id":1,"name":"MacBook Pro 13 inch retina","price":1350}`
1000 assertJSON(t, out, expected, "Find expecting a nested object")
1001 }
1002
1003 func TestJSONQ_FindR(t *testing.T) {
1004 jq := New().FromString(jsonStr)
1005 result, err := jq.FindR("vendor.items.[0]")
1006 if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
1007 t.Error("failed to match Result type")
1008 }
1009 }
1010
1011 func TestJSONQ_FindR_error(t *testing.T) {
1012 jq := New().FromString(jsonStr)
1013 result, err := jq.FindR("invalid_path")
1014 if result != nil && err == nil {
1015 t.Error("failed to catch error")
1016 }
1017 }
1018
1019 func TestJSONQ_Pluck_expecting_list_of_float64(t *testing.T) {
1020 jq := New().FromString(jsonStr).
1021 From("vendor.items")
1022 out := jq.Pluck("price")
1023 expected := `[1350,1700,1200,850,850,950,850]`
1024 assertJSON(t, out, expected, "Pluck expecting prices from list of objects")
1025 }
1026
1027 func TestJSONQ_Pluck_expecting_empty_list_of_float64(t *testing.T) {
1028 jq := New().FromString(jsonStr).
1029 From("vendor.items")
1030 out := jq.Pluck("invalid_prop")
1031 expected := `[]`
1032 assertJSON(t, out, expected, "Pluck expecting empty list from list of objects, because of invalid property name")
1033 }
1034
1035 func TestJSONQ_Pluck_expecting_with_distinct(t *testing.T) {
1036 jq := New().FromString(jsonStr).
1037 From("vendor.items").Distinct("price").Limit(3)
1038 out := jq.Pluck("price")
1039 expected := `[1350,1700,1200]`
1040 assertJSON(t, out, expected, "Expecting distinct price with limit 3")
1041 }
1042
1043 func TestJSONQ_PluckR(t *testing.T) {
1044 jq := New().FromString(jsonStr).From("vendor.items")
1045 result, err := jq.PluckR("price")
1046 if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
1047 t.Error("failed to match Result type")
1048 }
1049 }
1050
1051 func TestJSONQ_PluckR_error(t *testing.T) {
1052 jq := New().FromString(jsonStr).From("invalid_path")
1053 result, err := jq.PluckR("price")
1054 if result != nil && err == nil {
1055 t.Error("failed to catch error")
1056 }
1057 }
1058
1059 func TestJSONQ_Count_expecting_int_from_list(t *testing.T) {
1060 jq := New().FromString(jsonStr).
1061 From("vendor.items")
1062 out := jq.Count()
1063 expected := `7`
1064 assertJSON(t, out, expected, "Count expecting a int number of total item of an array")
1065 }
1066
1067 func TestJSONQ_Count_expecting_int_from_list_of_objects(t *testing.T) {
1068 jq := New().FromString(jsonStr).
1069 From("vendor.items.[0]")
1070 out := jq.Count()
1071 expected := `3`
1072 assertJSON(t, out, expected, "Count expecting a int number of total item of an array of objects")
1073 }
1074
1075 func TestJSONQ_Count_expecting_int_from_objects(t *testing.T) {
1076 jq := New().FromString(jsonStr).
1077 From("vendor.items").
1078 GroupBy("price")
1079 out := jq.Count()
1080 expected := `5`
1081 assertJSON(t, out, expected, "Count expecting a int number of total item of an array of grouped objects")
1082 }
1083
1084 func TestJSONQ_Count_with_Distinct_expecting_int_from_objects(t *testing.T) {
1085 jq := New().FromString(jsonStr).
1086 From("vendor.items").Distinct("price")
1087 out := jq.Count()
1088 expected := `5`
1089 assertJSON(t, out, expected, "Count expecting a int number of total item of an array of distinct priced objects")
1090 }
1091
1092 func TestJSONQ_Out_expecting_result(t *testing.T) {
1093 type item struct {
1094 ID int `json:"id"`
1095 Name string `json:"name"`
1096 Price int `json:"price"`
1097 }
1098 exptItm := item{
1099 ID: 1,
1100 Name: "MacBook Pro 13 inch retina",
1101 Price: 1350,
1102 }
1103 itm := item{}
1104 jq := New().FromString(jsonStr).
1105 From("vendor.items.[0]")
1106 jq.Out(&itm)
1107 assertInterface(t, exptItm, itm, "failed to get Out result")
1108 }
1109
1110 func TestJSONQ_Out_expecting_decoding_error(t *testing.T) {
1111 type item struct {
1112 ID bool `json:"id"`
1113 Name string `json:"name"`
1114 Price int `json:"price"`
1115 }
1116 itm := item{}
1117 jq := New().FromString(jsonStr).
1118 From("vendor.items.[0]")
1119 jq.Out(&itm)
1120 if jq.Error() == nil {
1121 t.Errorf("failed to get Out decoding error: %v", jq.Error())
1122 }
1123 }
1124
1125 func TestJSONQ_Out_expecting_encoding_error(t *testing.T) {
1126 type item struct {
1127 ID bool `json:"id"`
1128 Name string `json:"name"`
1129 Price int `json:"price"`
1130 }
1131 itm := item{}
1132 jq := New()
1133 jq.jsonContent = math.Inf(1)
1134 jq.Out(&itm)
1135 if jq.Error() == nil {
1136 t.Errorf("failed to get Out encoding error: %v", jq.Error())
1137 }
1138 }
1139
1140 func TestJSONQ_Writer_expecting_result(t *testing.T) {
1141 var b bytes.Buffer
1142 New().FromString(jsonStr).From("vendor.prices").Writer(&b)
1143 expected := "[2400,2100,1200,400.87,89.9,150.1]\n"
1144 assertInterface(t, expected, b.String(), "failed to get Writer result")
1145 }
1146
1147 func TestJSONQ_Writer_encoding_error(t *testing.T) {
1148 var b bytes.Buffer
1149 jq := New().FromString(jsonStr)
1150 jq.jsonContent = math.Inf(1)
1151 jq.Writer(&b)
1152 if jq.Error() == nil {
1153 t.Errorf("failed to get Writer encoding error: %v", jq.Error())
1154 }
1155 }
1156
1157 func TestJSONQ_Sum_of_array_numeric_values(t *testing.T) {
1158 jq := New().FromString(jsonStr).
1159 From("vendor.prices")
1160 out := jq.Sum()
1161 expected := `6340.87`
1162 assertJSON(t, out, expected, "Sum expecting sum an array")
1163 }
1164
1165 func TestJSONQ_Sum_of_array_objects_property_numeric_values(t *testing.T) {
1166 jq := New().FromString(jsonStr).
1167 From("vendor.items")
1168 out := jq.Sum("price")
1169 expected := `7750`
1170 assertJSON(t, out, expected, "Sum expecting sum an array of objects property")
1171 }
1172
1173 func TestJSONQ_Sum_expecting_error_for_providing_property_of_array(t *testing.T) {
1174 jq := New().FromString(jsonStr).
1175 From("vendor.prices")
1176 jq.Sum("key")
1177 if jq.Error() == nil {
1178 t.Error("expecting: unnecessary property name for array")
1179 }
1180 }
1181
1182 func TestJSONQ_Sum_expecting_error_for_not_providing_property_of_array_of_objects(t *testing.T) {
1183 jq := New().FromString(jsonStr).
1184 From("vendor.items")
1185 jq.Sum()
1186 if jq.Error() == nil {
1187 t.Error("expecting: property name can not be empty for object")
1188 }
1189 }
1190
1191 func TestJSONQ_Sum_expecting_error_for_not_providing_property_of_object(t *testing.T) {
1192 jq := New().FromString(jsonStr).
1193 From("vendor.items.[0]")
1194 jq.Sum()
1195 if jq.Error() == nil {
1196 t.Error("expecting: property name can not be empty for object")
1197 }
1198 }
1199
1200 func TestJSONQ_Sum_expecting_error_for_providing_invalid_property_of_array_of_objects(t *testing.T) {
1201 jq := New().FromString(jsonStr).
1202 From("vendor.items")
1203 jq.Sum("invalid_property")
1204 if jq.Error() == nil {
1205 t.Error("expecting: property 'invalid_property' does not exist")
1206 }
1207 }
1208
1209 func TestJSONQ_Sum_expecting_error_for_providing_invalid_property_of_object(t *testing.T) {
1210 jq := New().FromString(jsonStr).
1211 From("vendor")
1212 jq.Sum("invalid_property")
1213 if jq.Error() == nil {
1214 t.Error("expecting: property 'invalid_property' does not exist")
1215 }
1216 }
1217
1218 func TestJSONQ_Sum_expecting_error_for_providing_non_numeric_property_of_array_of_objects(t *testing.T) {
1219 jq := New().FromString(jsonStr).
1220 From("vendor.items")
1221 jq.Sum("name")
1222 if jq.Error() == nil {
1223 t.Error("expecting: property 'MacBook Pro 13 inch retina' is not numeric")
1224 }
1225 }
1226
1227 func TestJSONQ_Sum_expecting_error_for_providing_non_numeric_property_of_object(t *testing.T) {
1228 jq := New().FromString(jsonStr).
1229 From("vendor")
1230 jq.Sum("name")
1231 if jq.Error() == nil {
1232 t.Error("expecting: property 'invalid_property' does not exist")
1233 }
1234 }
1235
1236 func TestJSONQ_Sum_expecting_result_from_nested_object(t *testing.T) {
1237 jq := New().FromString(jsonStr).
1238 From("vendor.items.[0]")
1239 out := jq.Sum("price")
1240 expected := `1350`
1241 assertJSON(t, out, expected, "Sum expecting result from nested object")
1242 }
1243
1244 func TestJSONQ_Sum_of_distinct_array_numeric_values(t *testing.T) {
1245 jq := New().FromString(jsonStr).
1246 From("vendor.items").Distinct("price").Limit(3)
1247 out := jq.Sum("price")
1248 expected := `4250`
1249 assertJSON(t, out, expected, "Sum expecting sum a distinct & limited array")
1250 }
1251
1252 func TestJSONQ_Avg_array(t *testing.T) {
1253 jq := New().FromString(jsonStr).
1254 From("vendor.prices")
1255 out := jq.Avg()
1256 expected := `1056.8116666666667`
1257 assertJSON(t, out, expected, "Avg expecting average an array")
1258 }
1259
1260 func TestJSONQ_Avg_array_of_objects(t *testing.T) {
1261 jq := New().FromString(jsonStr).
1262 From("vendor.items")
1263 out := jq.Avg("price")
1264 expected := `1107.142857142857`
1265 assertJSON(t, out, expected, "Avg expecting average an array of objects property")
1266 }
1267
1268 func TestJSONQ_Min_array(t *testing.T) {
1269 jq := New().FromString(jsonStr).
1270 From("vendor.prices")
1271 out := jq.Min()
1272 expected := `89.9`
1273 assertJSON(t, out, expected, "Min expecting min an array")
1274 }
1275
1276 func TestJSONQ_Min_array_of_objects(t *testing.T) {
1277 jq := New().FromString(jsonStr).
1278 From("vendor.items")
1279 out := jq.Min("price")
1280 expected := `850`
1281 assertJSON(t, out, expected, "Min expecting min an array of objects property")
1282 }
1283
1284 func TestJSONQ_Max_array(t *testing.T) {
1285 jq := New().FromString(jsonStr).
1286 From("vendor.prices")
1287 out := jq.Max()
1288 expected := `2400`
1289 assertJSON(t, out, expected, "Max expecting max an array")
1290 }
1291
1292 func TestJSONQ_Max_array_of_objects(t *testing.T) {
1293 jq := New().FromString(jsonStr).
1294 From("vendor.items")
1295 out := jq.Max("price")
1296 expected := `1700`
1297 assertJSON(t, out, expected, "Max expecting max an array of objects property")
1298 }
1299
1300 // TODO: Need to write some more combined query test
1301 func TestJSONQ_CombinedWhereOrWhere(t *testing.T) {
1302 jq := New().FromString(jsonStr).
1303 From("vendor.items").
1304 Where("id", "=", 1).
1305 OrWhere("name", "=", "Sony VAIO").
1306 Where("price", "=", 1200)
1307 out := jq.Get()
1308 expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":3,"name":"Sony VAIO","price":1200}]`
1309 assertJSON(t, out, expected, "combined Where with orWhere")
1310 }
1311
1312 func TestJSONQ_CombinedWhereOrWhere_invalid_key(t *testing.T) {
1313 jq := New().FromString(jsonStr).
1314 From("vendor.items").
1315 Where("id", "=", 1).
1316 OrWhere("invalid_key", "=", "Sony VAIO")
1317 out := jq.Get()
1318 expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350}]`
1319 assertJSON(t, out, expected, "combined Where with orWhere containing invalid key")
1320 }
1321
1322 func TestJSONQ_Get_with_Select_method(t *testing.T) {
1323 jq := New().FromString(jsonStr).
1324 From("vendor.items").
1325 Select("id", "name").
1326 Where("price", "=", 1350)
1327 out := jq.Get()
1328 expected := `[{"id":1,"name":"MacBook Pro 13 inch retina"}]`
1329 assertJSON(t, out, expected, "Select method Using Get")
1330 }
1331
1332 func TestJSONQ_Get_with_nested_Select_method(t *testing.T) {
1333 jq := New().FromString(jsonStrUsers).
1334 From("users").
1335 Select("id as uid", "name.first as fname", "name.last")
1336 out := jq.Get()
1337 expected := `[{"fname":"John","last":"Ramboo","uid":1},{"fname":"Ethan","last":"Hunt","uid":2},{"fname":"John","last":"Doe","uid":3}]`
1338 assertJSON(t, out, expected, "nested Select method using alias")
1339 }
1340
1341 func TestJSONQ_Get_with_nested_invalid_property_in_Select_method_expecting_error(t *testing.T) {
1342 jq := New().FromString(jsonStrUsers).
1343 From("users").
1344 Select("id as uid", "name.middle")
1345 out := jq.Get()
1346 if jq.Error() == nil {
1347 t.Error("nested property in Select method failed to catch error")
1348 }
1349 expected := `[{"uid":1},{"uid":2},{"uid":3}]`
1350 assertJSON(t, out, expected, "nested Select method using alias")
1351 }
1352
1353 func TestJSONQ_GetR(t *testing.T) {
1354 jq := New().FromString(jsonStr).
1355 From("vendor.items").Select("name")
1356 result, err := jq.GetR()
1357 if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
1358 t.Error("failed to match Result type")
1359 }
1360 }
1361
1362 func TestJSONQ_GetR_error(t *testing.T) {
1363 jq := New().FromString(jsonStr).
1364 From("invalid_path")
1365 result, err := jq.GetR()
1366 if result != nil && err == nil {
1367 t.Error("failed to catch error")
1368 }
1369 }
1370
1371 func TestJSONQ_Offset_method(t *testing.T) {
1372 jq := New().FromString(jsonStr).
1373 From("vendor.items").
1374 Offset(4)
1375 out := jq.Get()
1376 expected := `[{"id":5,"key":2300,"name":"HP core i5","price":850},{"id":6,"name":"HP core i7","price":950},{"id":null,"name":"HP core i3 SSD","price":850}]`
1377 assertJSON(t, out, expected, "failed to offset records")
1378 }
1379
1380 func TestJSONQ_Offset_Where_method(t *testing.T) {
1381 jq := New().FromString(jsonStr).
1382 From("vendor.items").
1383 Offset(4).WhereNotNil("id")
1384 out := jq.Get()
1385 expected := `[{"id":5,"key":2300,"name":"HP core i5","price":850},{"id":6,"name":"HP core i7","price":950}]`
1386 assertJSON(t, out, expected, "failed to limit records")
1387 }
1388
1389 func TestJSONQ_Offset_greater_than_the_original_value_with_Where_method(t *testing.T) {
1390 jq := New().FromString(jsonStr).
1391 From("vendor.items").
1392 Offset(40).WhereNotNil("id")
1393 out := jq.Get()
1394 expected := `[]`
1395 assertJSON(t, out, expected, "failed to offset records when offset value is greater than the length of orignal result")
1396 }
1397
1398 func TestJSONQ_Limit_method(t *testing.T) {
1399 jq := New().FromString(jsonStr).
1400 From("vendor.items").
1401 Limit(2)
1402 out := jq.Get()
1403 expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
1404 assertJSON(t, out, expected, "failed to limit records")
1405 }
1406
1407 func TestJSONQ_Limit_Where_method(t *testing.T) {
1408 jq := New().FromString(jsonStr).
1409 From("vendor.items").
1410 Limit(2).WhereNotNil("id")
1411 out := jq.Get()
1412 expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
1413 assertJSON(t, out, expected, "failed to limit records")
1414 }
1415
1416 func TestJSONQ_Offset_invalid_number_should_return_error(t *testing.T) {
1417 jq := New().FromString(jsonStr).
1418 From("vendor.items").
1419 Offset(-2)
1420 jq.Get()
1421 if jq.Error() == nil {
1422 t.Error("failed to catch invalid offset error")
1423 }
1424 }
1425 func TestJSONQ_Limit_invalid_number_should_return_error(t *testing.T) {
1426 jq := New().FromString(jsonStr).
1427 From("vendor.items").
1428 Limit(-2)
1429 jq.Get()
1430 if jq.Error() == nil {
1431 t.Error("failed to catch invalid limit error")
1432 }
1433 }
1434
1435 func TestJSONQ_WhereLenEqual(t *testing.T) {
1436 jq := New().FromString(jsonStrUsers).
1437 From("users").
1438 WhereLenEqual("name.first", 4)
1439 expected := `[{"id":1,"name":{"first":"John","last":"Ramboo"}},{"id":3,"name":{"first":"John","last":"Doe"}}]`
1440 out := jq.Get()
1441 assertJSON(t, out, expected, "single WhereLenEqual")
1442 }
1443
1444 func TestJSONQ_WhereLenNotEqual(t *testing.T) {
1445 jq := New().FromString(jsonStrUsers).
1446 From("users").
1447 WhereLenNotEqual("name.first", 4)
1448 expected := `[{"id":2,"name":{"first":"Ethan","last":"Hunt"}}]`
1449 out := jq.Get()
1450 assertJSON(t, out, expected, "single WhereLenEqual")
1451 }
1452
1453 func TestJSONQ_More(t *testing.T) {
1454 jq := New().FromString(jsonStrUsers).From("users")
1455 jq.Where("id", "<", 3)
1456 jq.More() // start query again
1457 jq.Where("id", ">", 1)
1458 expected := `[{"id":2,"name":{"first":"Ethan","last":"Hunt"}}]`
1459 out := jq.Get()
1460 assertJSON(t, out, expected, "More")
1461 }
1462
1463 // ======================== Benchmark ======================== //
1464
1465 func Benchmark_Copy(b *testing.B) {
1466 jq := New().FromString(jsonStr)
1467 for n := 0; n < b.N; n++ {
1468 jq.Copy()
1469 }
1470 }
1471
1472 func Benchmark_Find(b *testing.B) {
1473 jq := New().FromString(jsonStr)
1474 for n := 0; n < b.N; n++ {
1475 jq.Find("name")
1476 }
1477 }
1478
1479 func Benchmark_Get(b *testing.B) {
1480 jq := New().FromString(jsonStr)
1481 for n := 0; n < b.N; n++ {
1482 jq.Get()
1483 }
1484 }
1485
1486 func Benchmark_From_Get(b *testing.B) {
1487 jq := New().FromString(jsonStr)
1488 for n := 0; n < b.N; n++ {
1489 jq.From("vendor.items").Get()
1490 }
1491 }
1492
1493 func Benchmark_From_Where_Get(b *testing.B) {
1494 jq := New().FromString(jsonStr)
1495 for n := 0; n < b.N; n++ {
1496 jq.From("vendor.items").Where("id", "=", 1).Get()
1497 }
1498 }
1499
1500 func Benchmark_From_Where_Select_Get(b *testing.B) {
1501 jq := New().FromString(jsonStr)
1502 for n := 0; n < b.N; n++ {
1503 jq.From("vendor.items").Where("id", "=", 1).Select("id", "name").Get()
1504 }
1505 }
1506
1507 func Benchmark_From_Sum(b *testing.B) {
1508 jq := New().FromString(jsonStr)
1509 for n := 0; n < b.N; n++ {
1510 jq.From("vendor.items").Sum("price")
1511 }
1512 }
1513
1514 func Benchmark_From_Avg(b *testing.B) {
1515 jq := New().FromString(jsonStr)
1516 for n := 0; n < b.N; n++ {
1517 jq.From("vendor.items").Avg("price")
1518 }
1519 }
1520
1521 func Benchmark_From_Count(b *testing.B) {
1522 jq := New().FromString(jsonStr)
1523 for n := 0; n < b.N; n++ {
1524 jq.From("vendor.items").Count()
1525 }
1526 }
1527
1528 func Benchmark_From_First(b *testing.B) {
1529 jq := New().FromString(jsonStr)
1530 for n := 0; n < b.N; n++ {
1531 jq.From("vendor.items").First()
1532 }
1533 }
1534
1535 func Benchmark_From_GroupBy(b *testing.B) {
1536 jq := New().FromString(jsonStr)
1537 for n := 0; n < b.N; n++ {
1538 jq.From("vendor.items").GroupBy("price")
1539 }
1540 }
1541
1542 func Benchmark_From_SortBy(b *testing.B) {
1543 jq := New().FromString(jsonStr)
1544 for n := 0; n < b.N; n++ {
1545 jq.From("vendor.items").SortBy("price")
1546 }
1547 }
1548
1549 func Benchmark_From_Where_nested_element_Get(b *testing.B) {
1550 jq := New().FromString(jsonStrUsers)
1551 for n := 0; n < b.N; n++ {
1552 jq.From("users").WhereEqual("name.first", "John").Get()
1553 }
1554 }
1555
1556 func Benchmark_From_WhereLenEqual_Get(b *testing.B) {
1557 jq := New().FromString(jsonStr)
1558 for n := 0; n < b.N; n++ {
1559 jq.From("vendor.items").WhereLenEqual("name", 10).Get()
1560 }
1561 }