-
Notifications
You must be signed in to change notification settings - Fork 139
/
jsonq_test.go
1561 lines (1387 loc) · 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gojsonq
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"math"
"reflect"
"strings"
"testing"
)
func TestNew(t *testing.T) {
jq := New()
if reflect.ValueOf(jq).Type().String() != "*gojsonq.JSONQ" {
t.Error("failed to match JSONQ type")
}
}
func TestJSONQ_String(t *testing.T) {
jq := New()
expected := fmt.Sprintf("\nContent: %s\nQueries:%v\n", string(jq.raw), jq.queries)
if out := jq.String(); out != expected {
t.Errorf("Expected: %v\n Got: %v", expected, out)
}
}
func TestJSONQ_decode(t *testing.T) {
testCases := []struct {
tag string
jsonStr string
errExpect bool
}{
{
tag: "valid json",
jsonStr: `{"name": "John Doe", "age": 30}`,
errExpect: false,
},
{
tag: "invalid json should return error",
jsonStr: `{"name": "John Doe", "age": 30, "only_key"}`,
errExpect: true,
},
}
for _, tc := range testCases {
jq := New()
jq.raw = json.RawMessage(tc.jsonStr)
jq.decode()
if err := jq.Error(); err != nil && !tc.errExpect {
t.Errorf("failed %s", tc.tag)
}
}
}
func TestJSONQ_Copy(t *testing.T) {
jq := New()
mp := map[string]int{}
for i := 0; i < 100; i++ {
adr := fmt.Sprintf("%p", jq.Copy())
if _, ok := mp[adr]; ok {
t.Error("failed to copy JSONQ")
} else {
mp[adr] = i
}
}
}
func TestJSONQ_File(t *testing.T) {
filename := "data.json"
path, f := createTestFile(t, filename)
defer f()
testCases := []struct {
tag string
filename string
expectedErr bool
}{
{
tag: "valid file name does not expect error",
filename: path,
expectedErr: false,
},
{
tag: "invalid valid file name expecting error",
filename: "invalid_file.xjson",
expectedErr: true,
},
}
for _, tc := range testCases {
err := New().File(tc.filename).Error()
if tc.expectedErr && err == nil {
t.Errorf("%s", tc.tag)
}
}
}
func TestJSONQ_JSONString(t *testing.T) {
testCases := []struct {
tag string
jsonStr string
errExpect bool
}{
{
tag: "valid json",
jsonStr: `{"name": "John Doe", "age": 30}`,
errExpect: false,
},
{
tag: "invalid json should return error",
jsonStr: `{"name": "John Doe", "age": 30, "only_key"}`,
errExpect: true,
},
}
for _, tc := range testCases {
if err := New().FromString(tc.jsonStr).Error(); err != nil && !tc.errExpect {
t.Errorf("failed %s", tc.tag)
}
}
}
func TestJSONQ_FromString(t *testing.T) {
testCases := []struct {
tag string
inputStr string
errExpect bool
}{
// TODO: Doesn't need to test decoder for input content
{
tag: "valid json",
inputStr: `{"name": "John Doe", "age": 30}`,
errExpect: false,
},
{
tag: "invalid json should return error",
inputStr: `{"name": "John Doe", "age": 30, "only_key"}`,
errExpect: true,
},
}
for _, tc := range testCases {
if err := New().FromString(tc.inputStr).Error(); err != nil && !tc.errExpect {
t.Errorf("failed %s", tc.tag)
}
}
}
func TestJSONQ_Reader(t *testing.T) {
testCases := []struct {
tag string
jsonStr string
errExpect bool
}{
{
tag: "valid json",
jsonStr: `{"name": "John Doe", "age": 30}`,
errExpect: false,
},
{
tag: "invalid json should return error",
jsonStr: `{"name": "John Doe", "age": 30, "only_key"}`,
errExpect: true,
},
}
for _, tc := range testCases {
rdr := strings.NewReader(tc.jsonStr)
if err := New().Reader(rdr).Error(); err != nil && !tc.errExpect {
t.Errorf("failed %s", tc.tag)
}
}
}
type invalidReader string
func (invalidReader) Read(p []byte) (n int, err error) {
return 0, errors.New("this reader always return an error")
}
func TestJSONQ_Reader_expecting_error(t *testing.T) {
var rdr invalidReader
if err := New().Reader(rdr).Error(); err == nil {
t.Errorf("failed to catch Reader error")
}
}
func TestJSONQ_Errors(t *testing.T) {
testCases := []struct {
tag string
jsonStr string
}{
{
tag: "invalid json 1",
jsonStr: `{"name": "John Doe", "age": 30, :""}`,
},
{
tag: "invalid json 2",
jsonStr: `{"name": "John Doe", "age": 30, "only_key"}`,
},
}
for _, tc := range testCases {
if errs := New().FromString(tc.jsonStr).Errors(); len(errs) == 0 {
t.Errorf("failed %s", tc.tag)
}
}
}
func TestJSONQ_Macro(t *testing.T) {
jq := New()
jq.Macro("mac1", func(x, y interface{}) (bool, error) {
return true, nil
})
if _, ok := jq.queryMap["mac1"]; !ok {
t.Error("failed to register macro")
}
jq.Macro("mac1", func(x, y interface{}) (bool, error) {
return true, nil
})
if jq.Error() == nil {
t.Error("failed to throw error for already registered macro")
}
}
func TestJSONQ_From_Set(t *testing.T) {
node := "root.items.[0].name"
jq := New().From(node)
if jq.node != node {
t.Error("failed to set node name")
}
}
func TestJSONQ_Select(t *testing.T) {
jq := New().Select("id", "name")
if len(jq.attributes) != 2 {
t.Error("failed to set properties")
}
}
func TestJSONQ_Offset(t *testing.T) {
jq := New().Offset(3)
if jq.offsetRecords != 3 {
t.Error("failed to set offset records value")
}
}
func TestJSONQ_Limit(t *testing.T) {
jq := New().Limit(12)
if jq.limitRecords != 12 {
t.Error("failed to set limit records value")
}
}
func TestJSONQ_reset(t *testing.T) {
node := "root.items"
jq := New().From(node).Select("name", "age").WhereEqual("price", "1900").WhereEqual("id", 1)
jq.reset()
if len(jq.queries) != 0 ||
len(jq.attributes) != 0 ||
jq.queryIndex != 0 {
t.Error("reset failed")
}
}
func TestJSONQ_Reset(t *testing.T) {
node := "root.items"
jq := New().From(node).WhereEqual("price", "1900").WhereEqual("id", 1)
jq.Reset()
if len(jq.queries) != 0 || jq.queryIndex != 0 || jq.node != "" {
t.Error("reset failed")
}
}
func TestJSONQ_From(t *testing.T) {
testCases := []struct {
tag string
query string
expected string
expectError bool
}{
{
tag: "accessing node",
query: "vendor.name",
expected: `"Star Trek"`,
expectError: false,
},
{
tag: "accessing not existed index",
query: "vendor.items.[0]",
expected: `{"id":1,"name":"MacBook Pro 13 inch retina","price":1350}`,
expectError: false,
},
{
tag: "accessing not existed index",
query: "vendor.items.[10]",
expected: `null`,
expectError: false,
},
{
tag: "accessing invalid index error",
query: "vendor.items.[x]",
expectError: true,
},
}
for _, tc := range testCases {
jq := New().FromString(jsonStr)
out := jq.From(tc.query).Get()
if tc.expectError && jq.Error() == nil {
t.Error("failed to catch error")
}
if !tc.expectError {
assertJSON(t, out, tc.expected, tc.tag)
}
}
jq := New().FromString(jsonStr)
expJSON := `[{"id":3,"name":"Sony VAIO","price":1200}]`
out := jq.From("vendor.items").GroupBy("price").From("1200").Get()
assertJSON(t, out, expJSON, "accessing group by data")
}
func TestJSONQ_FromInterface(t *testing.T) {
var v map[string]interface{}
err := json.Unmarshal([]byte(jsonStr), &v)
if err != nil {
t.Error(err)
}
jq := New().FromInterface(v)
if jq.rootJSONContent == nil || jq.jsonContent == nil {
t.Errorf("failed to assign value using FromInterface method")
}
var customType float64
jq = New().FromInterface(customType)
if jq.Error() == nil {
t.Errorf("failed to set error properly for FromInterface method")
}
}
func TestJSONQ_Where_single_where(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
Where("price", "=", 1700)
expected := `[{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
out := jq.Get()
assertJSON(t, out, expected, "single Where")
}
func TestJSONQ_Where_deep_nested_value(t *testing.T) {
jq := New().FromString(jsonStrUsers).
From("users").
Where("name.first", "=", "John")
expected := `[{"id":1,"name":{"first":"John","last":"Ramboo"}},{"id":3,"name":{"first":"John","last":"Doe"}}]`
out := jq.Get()
assertJSON(t, out, expected, "single Where with nested value")
}
func TestJSONQ_Where_multiple_where_expecting_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
Where("price", "=", 1700).
Where("id", "=", 2)
expected := `[{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
out := jq.Get()
assertJSON(t, out, expected, "multiple Where expecting data")
}
func TestJSONQ_Where_multiple_where_expecting_empty_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
Where("price", "=", 1700).
Where("id", "=", "1700")
expected := `[]`
out := jq.Get()
assertJSON(t, out, expected, "multiple Where expecting empty result")
}
func TestJSONQ_Where_multiple_where_with_invalid_operator_expecting_error(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
Where("price", "invalid_op", 1700)
jq.Get()
if jq.Error() == nil {
t.Error("expecting: invalid operator invalid_op")
}
}
func TestJSONQ_Where_multiple_where_with_invalid_operand_expecting_error(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
Where("price", "contains", 1700)
jq.Get()
if jq.Error() == nil {
t.Error("expecting: invalid operator invalid_op")
}
}
func TestJSONQ_single_WhereEqual(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereEqual("price", 1700)
expected := `[{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
out := jq.Get()
assertJSON(t, out, expected, "single WhereEqual")
}
func TestJSONQ_multiple_WhereEqual_expecting_data(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereEqual("price", 1700).
WhereEqual("id", 2)
expected := `[{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
out := jq.Get()
assertJSON(t, out, expected, "multiple WhereEqual expecting data")
}
func TestJSONQ_multiple_WhereEqual_expecting_empty_data(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereEqual("price", 1700).
WhereEqual("id", "1700")
expected := `[]`
out := jq.Get()
assertJSON(t, out, expected, "multiple WhereEqual expecting empty result")
}
func TestJSONQ_single_WhereNotEqual(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereNotEqual("price", 850)
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}]`
out := jq.Get()
assertJSON(t, out, expected, "single WhereNotEqual")
}
func TestJSONQ_multiple_WhereNotEqual(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereNotEqual("price", 850).
WhereNotEqual("id", 2)
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}]`
out := jq.Get()
assertJSON(t, out, expected, "multiple WhereNotEqual expecting result")
}
func TestJSONQ_WhereNil(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereNil("id")
expected := `[{"id":null,"name":"HP core i3 SSD","price":850}]`
out := jq.Get()
assertJSON(t, out, expected)
}
func TestJSONQ_WhereNotNil(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereNotNil("id")
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}]`
out := jq.Get()
assertJSON(t, out, expected)
}
func TestJSONQ_WhereIn_expecting_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereIn("id", []int{1, 3, 5})
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}]`
out := jq.Get()
assertJSON(t, out, expected, "WhereIn expecting result")
}
func TestJSONQ_WhereIn_expecting_empty_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereIn("id", []int{18, 39, 85})
expected := `[]`
out := jq.Get()
assertJSON(t, out, expected, "WhereIn expecting empty result")
}
func TestJSONQ_WhereNotIn_expecting_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereNotIn("id", []int{1, 3, 5, 6})
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}]`
out := jq.Get()
assertJSON(t, out, expected, "WhereIn expecting result")
}
func TestJSONQ_WhereNotIn_expecting_empty_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereNotIn("price", []float64{850, 950, 1200, 1700, 1350})
expected := `[]`
out := jq.Get()
assertJSON(t, out, expected, "WhereIn expecting empty result")
}
func TestJSONQ_OrWhere(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
OrWhere("price", ">", 1200)
expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
out := jq.Get()
assertJSON(t, out, expected, "OrWhere expecting result")
}
func TestJSONQ_WhereStartsWith_expecting_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereStartsWith("name", "Mac")
expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
out := jq.Get()
assertJSON(t, out, expected, "WhereStartsWith expecting result")
}
func TestJSONQ_WhereStartsWith_expecting_empty_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereStartsWith("name", "xyz")
expected := `[]`
out := jq.Get()
assertJSON(t, out, expected, "WhereStartsWith expecting empty result")
}
func TestJSONQ_WhereEndsWith(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereEndsWith("name", "retina")
expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
out := jq.Get()
assertJSON(t, out, expected, "WhereStartsWith expecting result")
}
func TestJSONQ_WhereEndsWith_empty_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereEndsWith("name", "xyz")
expected := `[]`
out := jq.Get()
assertJSON(t, out, expected, "WhereStartsWith expecting empty result")
}
func TestJSONQ_WhereContains_expecting_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereContains("name", "RetinA")
expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
out := jq.Get()
assertJSON(t, out, expected, "WhereContains expecting result")
}
func TestJSONQ_WhereContains_expecting_empty_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereContains("name", "xyz")
expected := `[]`
out := jq.Get()
assertJSON(t, out, expected, "WhereContains expecting empty result")
}
func TestJSONQ_WhereStrictContains_expecting_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereStrictContains("name", "retina")
expected := `[{"id":1,"name":"MacBook Pro 13 inch retina","price":1350},{"id":2,"name":"MacBook Pro 15 inch retina","price":1700}]`
out := jq.Get()
assertJSON(t, out, expected, "WhereContains expecting result")
}
func TestJSONQ_WhereStrictContains_expecting_empty_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
WhereStrictContains("name", "RetinA")
expected := `[]`
out := jq.Get()
assertJSON(t, out, expected, "WhereContains expecting empty result")
}
func TestJSONQ_GroupBy(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
GroupBy("price")
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}]}`
out := jq.Get()
assertJSON(t, out, expected, "GroupBy expecting result")
}
func TestJSONQ_GroupBy_expecting_error(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
GroupBy("invalid_key")
expected := `{}`
out := jq.Get()
assertJSON(t, out, expected, "GroupBy expecting empty result")
if len(jq.Errors()) == 0 {
t.Error("failed to catch GroupBy error")
}
}
func TestJSONQ_GroupBy_nested_property(t *testing.T) {
jq := New().FromString(jsonStrUsers).
From("users").
GroupBy("name.first")
expected := `{"Ethan":[{"id":2,"name":{"first":"Ethan","last":"Hunt"}}],"John":[{"id":1,"name":{"first":"John","last":"Ramboo"}},{"id":3,"name":{"first":"John","last":"Doe"}}]}`
out := jq.Get()
assertJSON(t, out, expected, "GroupBy nested expecting result")
}
func TestJSONQ_GroupBy_nested_property_expecting_error(t *testing.T) {
jq := New().FromString(jsonStrUsers).
From("users").
GroupBy("name.invalid_key")
out := jq.Get()
expected := `{}`
assertJSON(t, out, expected, "Nsested GroupBy expecting empty result")
if len(jq.errors) == 0 {
t.Error("failed to catch GroupBy nested property error")
}
}
func TestJSONQ_Sort_string_ascending_order(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.names").
Sort()
expected := `["Abby","Jane Doe","Jerry","John Doe","Nicolas","Tom"]`
out := jq.Get()
assertJSON(t, out, expected, "sorting array of string in ascending desc")
}
func TestJSONQ_Sort_float64_descending_order(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.prices").
Sort("desc")
expected := `[2400,2100,1200,400.87,150.1,89.9]`
out := jq.Get()
assertJSON(t, out, expected, "sorting array of float in descending order")
}
func TestJSONQ_Sort_with_two_args_expecting_error(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.prices").
Sort("asc", "desc")
jq.Get()
if jq.Error() == nil {
t.Error("expecting an error")
}
}
func TestJSONQ_SortBy_float_ascending_order(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
SortBy("price")
expected := `[{"id":null,"name":"HP core i3 SSD","price":850},{"id":4,"name":"Fujitsu","price":850},{"id":5,"key":2300,"name":"HP core i5","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}]`
out := jq.Get()
assertJSON(t, out, expected, "sorting array of object by its key (price-float64) in ascending desc")
}
func TestJSONQ_SortBy_float_descending_order(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
SortBy("price", "desc")
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}]`
out := jq.Get()
assertJSON(t, out, expected, "sorting array of object by its key (price-float64) in descending desc")
}
func TestJSONQ_SortBy_string_ascending_order(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
SortBy("name")
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}]`
out := jq.Get()
assertJSON(t, out, expected, "sorting array of object by its key (name-string) in ascending desc")
}
func TestJSONQ_SortBy_string_descending_order(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
SortBy("name", "desc")
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}]`
out := jq.Get()
assertJSON(t, out, expected, "sorting array of object by its key (name-string) in descending desc")
}
func TestJSONQ_SortBy_deep_nested_string_ascending_order(t *testing.T) {
jq := New().FromString(jsonStrUsers).
From("users").
SortBy("name.first")
expected := `[{"id":2,"name":{"first":"Ethan","last":"Hunt"}},{"id":1,"name":{"first":"John","last":"Ramboo"}},{"id":3,"name":{"first":"John","last":"Doe"}}]`
out := jq.Get()
assertJSON(t, out, expected, "sorting array of object by its key (name-string) in descending desc")
}
func TestJSONQ_SortBy_deep_nested_string_invalid_key_should_return_error(t *testing.T) {
jq := New().FromString(jsonStrUsers).
From("users").
SortBy("name.middle")
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
out := jq.Get()
assertJSON(t, out, expected, "sorting array of object by its key (name-string) in descending desc")
if len(jq.errors) == 0 {
t.Error("invalid path should return error/errors in SortBy")
}
}
func TestJSONQ_SortBy_no_argument_expecting_error(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
SortBy()
jq.Get()
if jq.Error() == nil {
t.Error("expecting an error")
}
}
func TestJSONQ_SortBy_more_than_two_argument_expecting_error(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
SortBy("name", "desc", "asc")
jq.Get()
if jq.Error() == nil {
t.Error("expecting an error")
}
}
func TestJSONQ_SortBy_expecting_as_provided_node_is_not_list(t *testing.T) {
jq := New().FromString(jsonStr).
From("name").
SortBy("name", "desc")
out := jq.Get()
expJSON := `"computers"`
assertJSON(t, out, expJSON)
}
func TestJSONQ_SortBy_expecting_empty_as_provided_node_is_not_list(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").Where("price", ">", 2500).
SortBy("name", "desc")
out := jq.Get()
expJSON := `[]`
assertJSON(t, out, expJSON)
}
func TestJSONQ_Distinct(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
Distinct("price")
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}]`
out := jq.Get()
assertJSON(t, out, expected, "Distinct expecting result")
}
func TestJSONQ_Distinct_expecting_error(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
Distinct("invalid_key")
expected := `[]`
out := jq.Get()
assertJSON(t, out, expected, "Distinct expecting empty result")
if len(jq.Errors()) == 0 {
t.Error("failed to catch Distinct error")
}
}
func TestJSONQ_Only(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items")
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}]`
out := jq.Only("id", "price")
assertJSON(t, out, expected)
}
func TestJSONQ_Only_with_distinct(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").Distinct("price")
expected := `[{"id":1,"price":1350},{"id":2,"price":1700},{"id":3,"price":1200},{"id":4,"price":850},{"id":6,"price":950}]`
out := jq.Only("id", "price")
assertJSON(t, out, expected)
}
func TestJSONQ_OnlyR(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items")
result, err := jq.OnlyR("name", "price")
if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
t.Error("failed to match Result type")
}
}
func TestJSONQ_OnlyR_error(t *testing.T) {
jq := New().FromString(jsonStr).
From("invalid_path")
result, err := jq.OnlyR("name", "price")
if result != nil && err == nil {
t.Error("failed to catch error")
}
}
func TestJSONQ_First_expecting_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items")
expected := `{"id":1,"name":"MacBook Pro 13 inch retina","price":1350}`
out := jq.First()
assertJSON(t, out, expected, "First expecting result")
}
func TestJSONQ_First_expecting_empty_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
Where("price", ">", 1800)
expected := `null`
out := jq.First()
assertJSON(t, out, expected, "First expecting empty result")
}
func TestJSONQ_First_distinct_expecting_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").Distinct("price").Where("price", "=", 850)
expected := `{"id":4,"name":"Fujitsu","price":850}`
out := jq.First()
assertJSON(t, out, expected, "First with distinct & where expecting result result")
}
func TestJSONQ_FirstR(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").Distinct("price").Where("price", "=", 850)
result, err := jq.FirstR()
if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
t.Error("failed to match Result type")
}
}
func TestJSONQ_FirstR_error(t *testing.T) {
jq := New().FromString(jsonStr).
From("invalid").Distinct("price").Where("price", "=", 850)
result, err := jq.FirstR()
if result != nil && err == nil {
t.Error("failed to catch error")
}
}
func TestJSONQ_Last_expecting_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items")
expected := `{"id":null,"name":"HP core i3 SSD","price":850}`
out := jq.Last()
assertJSON(t, out, expected, "Last expecting result")
}
func TestJSONQ_Last_expecting_empty_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
Where("price", ">", 1800)
expected := `null`
out := jq.Last()
assertJSON(t, out, expected, "Last expecting empty result")
}
func TestJSONQ_Last_distinct_expecting_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").Distinct("price").Where("price", "=", 850)
expected := `{"id":4,"name":"Fujitsu","price":850}`
out := jq.Last()
assertJSON(t, out, expected, "Last with distinct & where expecting result result")
}
func TestJSONQ_LastR(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").Distinct("price").Where("price", "=", 850)
result, err := jq.LastR()
if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
t.Error("failed to match Result type")
}
}
func TestJSONQ_LastR_error(t *testing.T) {
jq := New().FromString(jsonStr).
From("invalid_path").Distinct("price").Where("price", "=", 850)
result, err := jq.LastR()
if result != nil && err == nil {
t.Error("failed to catch error")
}
}
func TestJSONQ_Nth_expecting_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items")
expected := `{"id":1,"name":"MacBook Pro 13 inch retina","price":1350}`
out := jq.Nth(1)
assertJSON(t, out, expected, "Nth expecting result")
}
func TestJSONQ_Nth_expecting_empty_result_with_error(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
Where("price", ">", 1800)
expected := `null`
out := jq.Nth(1)
assertJSON(t, out, expected, "Nth expecting empty result with an error")
if jq.Error() == nil {
t.Error("expecting an error for empty result nth value")
}
}
func TestJSONQ_Nth_expecting_empty_result_with_error_index_out_of_range(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items")
expected := `null`
out := jq.Nth(100)
assertJSON(t, out, expected, "Nth expecting empty result with an error of index out of range")
if jq.Error() == nil {
t.Error("expecting an error for empty result nth value")
}
}
func TestJSONQ_Nth_expecting_result_from_last_using_negative_index(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items")
expected := `{"id":null,"name":"HP core i3 SSD","price":850}`
out := jq.Nth(-1)
assertJSON(t, out, expected, "Nth expecting result form last when providing -1")
}
func TestJSONQ_Nth_expecting_error_providing_zero_as_index(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").
Where("price", ">", 1800)
jq.Nth(0)
if jq.Error() == nil {
t.Error("expecting error")
}
}
func TestJSONQ_Nth_expecting_empty_result_as_node_is_map(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items.[0]")
out := jq.Nth(0)
expected := `null`
assertJSON(t, out, expected, "Nth expecting empty result if the node is a map")
}
func TestJSONQ_Nth_expecting_empty_result_as_node_is_object(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items.[0]")
out := jq.Nth(1)
expected := `null`
assertJSON(t, out, expected, "Nth expecting empty result if the node is a object")
}
func TestJSONQ_Nth_distinct_expecting_result(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").Distinct("price")
expected := `{"id":1,"name":"MacBook Pro 13 inch retina","price":1350}`
out := jq.Nth(1)
assertJSON(t, out, expected, "Last with distinct & where expecting result result")
}
func TestJSONQ_NthR(t *testing.T) {
jq := New().FromString(jsonStr).
From("vendor.items").Distinct("price").Where("price", "=", 850)
result, err := jq.NthR(1)
if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" && err != nil {
t.Error("failed to match Result type")
}
}
func TestJSONQ_NthR_error(t *testing.T) {
jq := New().FromString(jsonStr).
From("invalid_path").Distinct("price").Where("price", "=", 850)
result, err := jq.NthR(1)
if result != nil && err == nil {
t.Error("failed to catch error")
}
}
func TestJSONQ_Find_simple_property(t *testing.T) {
jq := New().FromString(jsonStr)
out := jq.Find("name")
expected := `"computers"`
assertJSON(t, out, expected, "Find expecting name computers")
}
func TestJSONQ_Find_nested_property(t *testing.T) {
jq := New().FromString(jsonStr)
out := jq.Find("vendor.items.[0]")
expected := `{"id":1,"name":"MacBook Pro 13 inch retina","price":1350}`
assertJSON(t, out, expected, "Find expecting a nested object")