summaryrefslogtreecommitdiff
path: root/tests/test_asyncio/test_json.py
blob: fc530c63c1b37e273fe41f1729f3e2056e362f6e (plain)
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
import pytest

import redis.asyncio as redis
from redis import exceptions
from redis.commands.json.path import Path
from tests.conftest import skip_ifmodversion_lt


@pytest.mark.redismod
async def test_json_setbinarykey(modclient: redis.Redis):
    d = {"hello": "world", b"some": "value"}
    with pytest.raises(TypeError):
        modclient.json().set("somekey", Path.root_path(), d)
    assert await modclient.json().set("somekey", Path.root_path(), d, decode_keys=True)


@pytest.mark.redismod
async def test_json_setgetdeleteforget(modclient: redis.Redis):
    assert await modclient.json().set("foo", Path.root_path(), "bar")
    assert await modclient.json().get("foo") == "bar"
    assert await modclient.json().get("baz") is None
    assert await modclient.json().delete("foo") == 1
    assert await modclient.json().forget("foo") == 0  # second delete
    assert await modclient.exists("foo") == 0


@pytest.mark.redismod
async def test_jsonget(modclient: redis.Redis):
    await modclient.json().set("foo", Path.root_path(), "bar")
    assert await modclient.json().get("foo") == "bar"


@pytest.mark.redismod
async def test_json_get_jset(modclient: redis.Redis):
    assert await modclient.json().set("foo", Path.root_path(), "bar")
    assert "bar" == await modclient.json().get("foo")
    assert await modclient.json().get("baz") is None
    assert 1 == await modclient.json().delete("foo")
    assert await modclient.exists("foo") == 0


@pytest.mark.redismod
async def test_nonascii_setgetdelete(modclient: redis.Redis):
    assert await modclient.json().set("notascii", Path.root_path(), "hyvää-élève")
    assert "hyvää-élève" == await modclient.json().get("notascii", no_escape=True)
    assert 1 == await modclient.json().delete("notascii")
    assert await modclient.exists("notascii") == 0


@pytest.mark.redismod
async def test_jsonsetexistentialmodifiersshouldsucceed(modclient: redis.Redis):
    obj = {"foo": "bar"}
    assert await modclient.json().set("obj", Path.root_path(), obj)

    # Test that flags prevent updates when conditions are unmet
    assert await modclient.json().set("obj", Path("foo"), "baz", nx=True) is None
    assert await modclient.json().set("obj", Path("qaz"), "baz", xx=True) is None

    # Test that flags allow updates when conditions are met
    assert await modclient.json().set("obj", Path("foo"), "baz", xx=True)
    assert await modclient.json().set("obj", Path("qaz"), "baz", nx=True)

    # Test that flags are mutually exlusive
    with pytest.raises(Exception):
        await modclient.json().set("obj", Path("foo"), "baz", nx=True, xx=True)


@pytest.mark.redismod
async def test_mgetshouldsucceed(modclient: redis.Redis):
    await modclient.json().set("1", Path.root_path(), 1)
    await modclient.json().set("2", Path.root_path(), 2)
    assert await modclient.json().mget(["1"], Path.root_path()) == [1]

    assert await modclient.json().mget([1, 2], Path.root_path()) == [1, 2]


@pytest.mark.redismod
@skip_ifmodversion_lt("99.99.99", "ReJSON")  # todo: update after the release
async def test_clear(modclient: redis.Redis):
    await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4])
    assert 1 == await modclient.json().clear("arr", Path.root_path())
    assert [] == await modclient.json().get("arr")


@pytest.mark.redismod
async def test_type(modclient: redis.Redis):
    await modclient.json().set("1", Path.root_path(), 1)
    assert "integer" == await modclient.json().type("1", Path.root_path())
    assert "integer" == await modclient.json().type("1")


@pytest.mark.redismod
async def test_numincrby(modclient):
    await modclient.json().set("num", Path.root_path(), 1)
    assert 2 == await modclient.json().numincrby("num", Path.root_path(), 1)
    assert 2.5 == await modclient.json().numincrby("num", Path.root_path(), 0.5)
    assert 1.25 == await modclient.json().numincrby("num", Path.root_path(), -1.25)


@pytest.mark.redismod
async def test_nummultby(modclient: redis.Redis):
    await modclient.json().set("num", Path.root_path(), 1)

    with pytest.deprecated_call():
        assert 2 == await modclient.json().nummultby("num", Path.root_path(), 2)
        assert 5 == await modclient.json().nummultby("num", Path.root_path(), 2.5)
        assert 2.5 == await modclient.json().nummultby("num", Path.root_path(), 0.5)


@pytest.mark.redismod
@skip_ifmodversion_lt("99.99.99", "ReJSON")  # todo: update after the release
async def test_toggle(modclient: redis.Redis):
    await modclient.json().set("bool", Path.root_path(), False)
    assert await modclient.json().toggle("bool", Path.root_path())
    assert await modclient.json().toggle("bool", Path.root_path()) is False
    # check non-boolean value
    await modclient.json().set("num", Path.root_path(), 1)
    with pytest.raises(exceptions.ResponseError):
        await modclient.json().toggle("num", Path.root_path())


@pytest.mark.redismod
async def test_strappend(modclient: redis.Redis):
    await modclient.json().set("jsonkey", Path.root_path(), "foo")
    assert 6 == await modclient.json().strappend("jsonkey", "bar")
    assert "foobar" == await modclient.json().get("jsonkey", Path.root_path())


@pytest.mark.redismod
async def test_strlen(modclient: redis.Redis):
    await modclient.json().set("str", Path.root_path(), "foo")
    assert 3 == await modclient.json().strlen("str", Path.root_path())
    await modclient.json().strappend("str", "bar", Path.root_path())
    assert 6 == await modclient.json().strlen("str", Path.root_path())
    assert 6 == await modclient.json().strlen("str")


@pytest.mark.redismod
async def test_arrappend(modclient: redis.Redis):
    await modclient.json().set("arr", Path.root_path(), [1])
    assert 2 == await modclient.json().arrappend("arr", Path.root_path(), 2)
    assert 4 == await modclient.json().arrappend("arr", Path.root_path(), 3, 4)
    assert 7 == await modclient.json().arrappend("arr", Path.root_path(), *[5, 6, 7])


@pytest.mark.redismod
async def test_arrindex(modclient: redis.Redis):
    r_path = Path.root_path()
    await modclient.json().set("arr", r_path, [0, 1, 2, 3, 4])
    assert 1 == await modclient.json().arrindex("arr", r_path, 1)
    assert -1 == await modclient.json().arrindex("arr", r_path, 1, 2)
    assert 4 == await modclient.json().arrindex("arr", r_path, 4)
    assert 4 == await modclient.json().arrindex("arr", r_path, 4, start=0)
    assert 4 == await modclient.json().arrindex("arr", r_path, 4, start=0, stop=5000)
    assert -1 == await modclient.json().arrindex("arr", r_path, 4, start=0, stop=-1)
    assert -1 == await modclient.json().arrindex("arr", r_path, 4, start=1, stop=3)


@pytest.mark.redismod
async def test_arrinsert(modclient: redis.Redis):
    await modclient.json().set("arr", Path.root_path(), [0, 4])
    assert 5 - -await modclient.json().arrinsert("arr", Path.root_path(), 1, *[1, 2, 3])
    assert [0, 1, 2, 3, 4] == await modclient.json().get("arr")

    # test prepends
    await modclient.json().set("val2", Path.root_path(), [5, 6, 7, 8, 9])
    await modclient.json().arrinsert("val2", Path.root_path(), 0, ["some", "thing"])
    assert await modclient.json().get("val2") == [["some", "thing"], 5, 6, 7, 8, 9]


@pytest.mark.redismod
async def test_arrlen(modclient: redis.Redis):
    await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4])
    assert 5 == await modclient.json().arrlen("arr", Path.root_path())
    assert 5 == await modclient.json().arrlen("arr")
    assert await modclient.json().arrlen("fakekey") is None


@pytest.mark.redismod
async def test_arrpop(modclient: redis.Redis):
    await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4])
    assert 4 == await modclient.json().arrpop("arr", Path.root_path(), 4)
    assert 3 == await modclient.json().arrpop("arr", Path.root_path(), -1)
    assert 2 == await modclient.json().arrpop("arr", Path.root_path())
    assert 0 == await modclient.json().arrpop("arr", Path.root_path(), 0)
    assert [1] == await modclient.json().get("arr")

    # test out of bounds
    await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4])
    assert 4 == await modclient.json().arrpop("arr", Path.root_path(), 99)

    # none test
    await modclient.json().set("arr", Path.root_path(), [])
    assert await modclient.json().arrpop("arr") is None


@pytest.mark.redismod
async def test_arrtrim(modclient: redis.Redis):
    await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4])
    assert 3 == await modclient.json().arrtrim("arr", Path.root_path(), 1, 3)
    assert [1, 2, 3] == await modclient.json().get("arr")

    # <0 test, should be 0 equivalent
    await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4])
    assert 0 == await modclient.json().arrtrim("arr", Path.root_path(), -1, 3)

    # testing stop > end
    await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4])
    assert 2 == await modclient.json().arrtrim("arr", Path.root_path(), 3, 99)

    # start > array size and stop
    await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4])
    assert 0 == await modclient.json().arrtrim("arr", Path.root_path(), 9, 1)

    # all larger
    await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4])
    assert 0 == await modclient.json().arrtrim("arr", Path.root_path(), 9, 11)


@pytest.mark.redismod
async def test_resp(modclient: redis.Redis):
    obj = {"foo": "bar", "baz": 1, "qaz": True}
    await modclient.json().set("obj", Path.root_path(), obj)
    assert "bar" == await modclient.json().resp("obj", Path("foo"))
    assert 1 == await modclient.json().resp("obj", Path("baz"))
    assert await modclient.json().resp("obj", Path("qaz"))
    assert isinstance(await modclient.json().resp("obj"), list)


@pytest.mark.redismod
async def test_objkeys(modclient: redis.Redis):
    obj = {"foo": "bar", "baz": "qaz"}
    await modclient.json().set("obj", Path.root_path(), obj)
    keys = await modclient.json().objkeys("obj", Path.root_path())
    keys.sort()
    exp = list(obj.keys())
    exp.sort()
    assert exp == keys

    await modclient.json().set("obj", Path.root_path(), obj)
    keys = await modclient.json().objkeys("obj")
    assert keys == list(obj.keys())

    assert await modclient.json().objkeys("fakekey") is None


@pytest.mark.redismod
async def test_objlen(modclient: redis.Redis):
    obj = {"foo": "bar", "baz": "qaz"}
    await modclient.json().set("obj", Path.root_path(), obj)
    assert len(obj) == await modclient.json().objlen("obj", Path.root_path())

    await modclient.json().set("obj", Path.root_path(), obj)
    assert len(obj) == await modclient.json().objlen("obj")


# @pytest.mark.redismod
# async def test_json_commands_in_pipeline(modclient: redis.Redis):
#     async with modclient.json().pipeline() as p:
#         p.set("foo", Path.root_path(), "bar")
#         p.get("foo")
#         p.delete("foo")
#         assert [True, "bar", 1] == await p.execute()
#     assert await modclient.keys() == []
#     assert await modclient.get("foo") is None

#     # now with a true, json object
#     await modclient.flushdb()
#     p = await modclient.json().pipeline()
#     d = {"hello": "world", "oh": "snap"}
#     with pytest.deprecated_call():
#         p.jsonset("foo", Path.root_path(), d)
#         p.jsonget("foo")
#     p.exists("notarealkey")
#     p.delete("foo")
#     assert [True, d, 0, 1] == p.execute()
#     assert await modclient.keys() == []
#     assert await modclient.get("foo") is None


@pytest.mark.redismod
async def test_json_delete_with_dollar(modclient: redis.Redis):
    doc1 = {"a": 1, "nested": {"a": 2, "b": 3}}
    assert await modclient.json().set("doc1", "$", doc1)
    assert await modclient.json().delete("doc1", "$..a") == 2
    r = await modclient.json().get("doc1", "$")
    assert r == [{"nested": {"b": 3}}]

    doc2 = {"a": {"a": 2, "b": 3}, "b": ["a", "b"], "nested": {"b": [True, "a", "b"]}}
    assert await modclient.json().set("doc2", "$", doc2)
    assert await modclient.json().delete("doc2", "$..a") == 1
    res = await modclient.json().get("doc2", "$")
    assert res == [{"nested": {"b": [True, "a", "b"]}, "b": ["a", "b"]}]

    doc3 = [
        {
            "ciao": ["non ancora"],
            "nested": [
                {"ciao": [1, "a"]},
                {"ciao": [2, "a"]},
                {"ciaoc": [3, "non", "ciao"]},
                {"ciao": [4, "a"]},
                {"e": [5, "non", "ciao"]},
            ],
        }
    ]
    assert await modclient.json().set("doc3", "$", doc3)
    assert await modclient.json().delete("doc3", '$.[0]["nested"]..ciao') == 3

    doc3val = [
        [
            {
                "ciao": ["non ancora"],
                "nested": [
                    {},
                    {},
                    {"ciaoc": [3, "non", "ciao"]},
                    {},
                    {"e": [5, "non", "ciao"]},
                ],
            }
        ]
    ]
    res = await modclient.json().get("doc3", "$")
    assert res == doc3val

    # Test async default path
    assert await modclient.json().delete("doc3") == 1
    assert await modclient.json().get("doc3", "$") is None

    await modclient.json().delete("not_a_document", "..a")


@pytest.mark.redismod
async def test_json_forget_with_dollar(modclient: redis.Redis):
    doc1 = {"a": 1, "nested": {"a": 2, "b": 3}}
    assert await modclient.json().set("doc1", "$", doc1)
    assert await modclient.json().forget("doc1", "$..a") == 2
    r = await modclient.json().get("doc1", "$")
    assert r == [{"nested": {"b": 3}}]

    doc2 = {"a": {"a": 2, "b": 3}, "b": ["a", "b"], "nested": {"b": [True, "a", "b"]}}
    assert await modclient.json().set("doc2", "$", doc2)
    assert await modclient.json().forget("doc2", "$..a") == 1
    res = await modclient.json().get("doc2", "$")
    assert res == [{"nested": {"b": [True, "a", "b"]}, "b": ["a", "b"]}]

    doc3 = [
        {
            "ciao": ["non ancora"],
            "nested": [
                {"ciao": [1, "a"]},
                {"ciao": [2, "a"]},
                {"ciaoc": [3, "non", "ciao"]},
                {"ciao": [4, "a"]},
                {"e": [5, "non", "ciao"]},
            ],
        }
    ]
    assert await modclient.json().set("doc3", "$", doc3)
    assert await modclient.json().forget("doc3", '$.[0]["nested"]..ciao') == 3

    doc3val = [
        [
            {
                "ciao": ["non ancora"],
                "nested": [
                    {},
                    {},
                    {"ciaoc": [3, "non", "ciao"]},
                    {},
                    {"e": [5, "non", "ciao"]},
                ],
            }
        ]
    ]
    res = await modclient.json().get("doc3", "$")
    assert res == doc3val

    # Test async default path
    assert await modclient.json().forget("doc3") == 1
    assert await modclient.json().get("doc3", "$") is None

    await modclient.json().forget("not_a_document", "..a")


@pytest.mark.redismod
async def test_json_mget_dollar(modclient: redis.Redis):
    # Test mget with multi paths
    await modclient.json().set(
        "doc1",
        "$",
        {"a": 1, "b": 2, "nested": {"a": 3}, "c": None, "nested2": {"a": None}},
    )
    await modclient.json().set(
        "doc2",
        "$",
        {"a": 4, "b": 5, "nested": {"a": 6}, "c": None, "nested2": {"a": [None]}},
    )
    # Compare also to single JSON.GET
    assert await modclient.json().get("doc1", "$..a") == [1, 3, None]
    assert await modclient.json().get("doc2", "$..a") == [4, 6, [None]]

    # Test mget with single path
    await modclient.json().mget("doc1", "$..a") == [1, 3, None]
    # Test mget with multi path
    res = await modclient.json().mget(["doc1", "doc2"], "$..a")
    assert res == [[1, 3, None], [4, 6, [None]]]

    # Test missing key
    res = await modclient.json().mget(["doc1", "missing_doc"], "$..a")
    assert res == [[1, 3, None], None]
    res = await modclient.json().mget(["missing_doc1", "missing_doc2"], "$..a")
    assert res == [None, None]


@pytest.mark.redismod
async def test_numby_commands_dollar(modclient: redis.Redis):

    # Test NUMINCRBY
    await modclient.json().set(
        "doc1", "$", {"a": "b", "b": [{"a": 2}, {"a": 5.0}, {"a": "c"}]}
    )
    # Test multi
    assert await modclient.json().numincrby("doc1", "$..a", 2) == [None, 4, 7.0, None]

    res = await modclient.json().numincrby("doc1", "$..a", 2.5)
    assert res == [None, 6.5, 9.5, None]
    # Test single
    assert await modclient.json().numincrby("doc1", "$.b[1].a", 2) == [11.5]

    assert await modclient.json().numincrby("doc1", "$.b[2].a", 2) == [None]
    assert await modclient.json().numincrby("doc1", "$.b[1].a", 3.5) == [15.0]

    # Test NUMMULTBY
    await modclient.json().set(
        "doc1", "$", {"a": "b", "b": [{"a": 2}, {"a": 5.0}, {"a": "c"}]}
    )

    # test list
    with pytest.deprecated_call():
        res = await modclient.json().nummultby("doc1", "$..a", 2)
        assert res == [None, 4, 10, None]
        res = await modclient.json().nummultby("doc1", "$..a", 2.5)
        assert res == [None, 10.0, 25.0, None]

    # Test single
    with pytest.deprecated_call():
        assert await modclient.json().nummultby("doc1", "$.b[1].a", 2) == [50.0]
        assert await modclient.json().nummultby("doc1", "$.b[2].a", 2) == [None]
        assert await modclient.json().nummultby("doc1", "$.b[1].a", 3) == [150.0]

    # test missing keys
    with pytest.raises(exceptions.ResponseError):
        await modclient.json().numincrby("non_existing_doc", "$..a", 2)
        await modclient.json().nummultby("non_existing_doc", "$..a", 2)

    # Test legacy NUMINCRBY
    await modclient.json().set(
        "doc1", "$", {"a": "b", "b": [{"a": 2}, {"a": 5.0}, {"a": "c"}]}
    )
    await modclient.json().numincrby("doc1", ".b[0].a", 3) == 5

    # Test legacy NUMMULTBY
    await modclient.json().set(
        "doc1", "$", {"a": "b", "b": [{"a": 2}, {"a": 5.0}, {"a": "c"}]}
    )

    with pytest.deprecated_call():
        await modclient.json().nummultby("doc1", ".b[0].a", 3) == 6


@pytest.mark.redismod
async def test_strappend_dollar(modclient: redis.Redis):

    await modclient.json().set(
        "doc1", "$", {"a": "foo", "nested1": {"a": "hello"}, "nested2": {"a": 31}}
    )
    # Test multi
    await modclient.json().strappend("doc1", "bar", "$..a") == [6, 8, None]

    await modclient.json().get("doc1", "$") == [
        {"a": "foobar", "nested1": {"a": "hellobar"}, "nested2": {"a": 31}}
    ]
    # Test single
    await modclient.json().strappend("doc1", "baz", "$.nested1.a") == [11]

    await modclient.json().get("doc1", "$") == [
        {"a": "foobar", "nested1": {"a": "hellobarbaz"}, "nested2": {"a": 31}}
    ]

    # Test missing key
    with pytest.raises(exceptions.ResponseError):
        await modclient.json().strappend("non_existing_doc", "$..a", "err")

    # Test multi
    await modclient.json().strappend("doc1", "bar", ".*.a") == 8
    await modclient.json().get("doc1", "$") == [
        {"a": "foo", "nested1": {"a": "hellobar"}, "nested2": {"a": 31}}
    ]

    # Test missing path
    with pytest.raises(exceptions.ResponseError):
        await modclient.json().strappend("doc1", "piu")


@pytest.mark.redismod
async def test_strlen_dollar(modclient: redis.Redis):

    # Test multi
    await modclient.json().set(
        "doc1", "$", {"a": "foo", "nested1": {"a": "hello"}, "nested2": {"a": 31}}
    )
    assert await modclient.json().strlen("doc1", "$..a") == [3, 5, None]

    res2 = await modclient.json().strappend("doc1", "bar", "$..a")
    res1 = await modclient.json().strlen("doc1", "$..a")
    assert res1 == res2

    # Test single
    await modclient.json().strlen("doc1", "$.nested1.a") == [8]
    await modclient.json().strlen("doc1", "$.nested2.a") == [None]

    # Test missing key
    with pytest.raises(exceptions.ResponseError):
        await modclient.json().strlen("non_existing_doc", "$..a")


@pytest.mark.redismod
async def test_arrappend_dollar(modclient: redis.Redis):
    await modclient.json().set(
        "doc1",
        "$",
        {
            "a": ["foo"],
            "nested1": {"a": ["hello", None, "world"]},
            "nested2": {"a": 31},
        },
    )
    # Test multi
    await modclient.json().arrappend("doc1", "$..a", "bar", "racuda") == [3, 5, None]
    assert await modclient.json().get("doc1", "$") == [
        {
            "a": ["foo", "bar", "racuda"],
            "nested1": {"a": ["hello", None, "world", "bar", "racuda"]},
            "nested2": {"a": 31},
        }
    ]

    # Test single
    assert await modclient.json().arrappend("doc1", "$.nested1.a", "baz") == [6]
    assert await modclient.json().get("doc1", "$") == [
        {
            "a": ["foo", "bar", "racuda"],
            "nested1": {"a": ["hello", None, "world", "bar", "racuda", "baz"]},
            "nested2": {"a": 31},
        }
    ]

    # Test missing key
    with pytest.raises(exceptions.ResponseError):
        await modclient.json().arrappend("non_existing_doc", "$..a")

    # Test legacy
    await modclient.json().set(
        "doc1",
        "$",
        {
            "a": ["foo"],
            "nested1": {"a": ["hello", None, "world"]},
            "nested2": {"a": 31},
        },
    )
    # Test multi (all paths are updated, but return result of last path)
    assert await modclient.json().arrappend("doc1", "..a", "bar", "racuda") == 5

    assert await modclient.json().get("doc1", "$") == [
        {
            "a": ["foo", "bar", "racuda"],
            "nested1": {"a": ["hello", None, "world", "bar", "racuda"]},
            "nested2": {"a": 31},
        }
    ]
    # Test single
    assert await modclient.json().arrappend("doc1", ".nested1.a", "baz") == 6
    assert await modclient.json().get("doc1", "$") == [
        {
            "a": ["foo", "bar", "racuda"],
            "nested1": {"a": ["hello", None, "world", "bar", "racuda", "baz"]},
            "nested2": {"a": 31},
        }
    ]

    # Test missing key
    with pytest.raises(exceptions.ResponseError):
        await modclient.json().arrappend("non_existing_doc", "$..a")


@pytest.mark.redismod
async def test_arrinsert_dollar(modclient: redis.Redis):
    await modclient.json().set(
        "doc1",
        "$",
        {
            "a": ["foo"],
            "nested1": {"a": ["hello", None, "world"]},
            "nested2": {"a": 31},
        },
    )
    # Test multi
    res = await modclient.json().arrinsert("doc1", "$..a", "1", "bar", "racuda")
    assert res == [3, 5, None]

    assert await modclient.json().get("doc1", "$") == [
        {
            "a": ["foo", "bar", "racuda"],
            "nested1": {"a": ["hello", "bar", "racuda", None, "world"]},
            "nested2": {"a": 31},
        }
    ]
    # Test single
    assert await modclient.json().arrinsert("doc1", "$.nested1.a", -2, "baz") == [6]
    assert await modclient.json().get("doc1", "$") == [
        {
            "a": ["foo", "bar", "racuda"],
            "nested1": {"a": ["hello", "bar", "racuda", "baz", None, "world"]},
            "nested2": {"a": 31},
        }
    ]

    # Test missing key
    with pytest.raises(exceptions.ResponseError):
        await modclient.json().arrappend("non_existing_doc", "$..a")


@pytest.mark.redismod
async def test_arrlen_dollar(modclient: redis.Redis):

    await modclient.json().set(
        "doc1",
        "$",
        {
            "a": ["foo"],
            "nested1": {"a": ["hello", None, "world"]},
            "nested2": {"a": 31},
        },
    )

    # Test multi
    assert await modclient.json().arrlen("doc1", "$..a") == [1, 3, None]
    res = await modclient.json().arrappend("doc1", "$..a", "non", "abba", "stanza")
    assert res == [4, 6, None]

    await modclient.json().clear("doc1", "$.a")
    assert await modclient.json().arrlen("doc1", "$..a") == [0, 6, None]
    # Test single
    assert await modclient.json().arrlen("doc1", "$.nested1.a") == [6]

    # Test missing key
    with pytest.raises(exceptions.ResponseError):
        await modclient.json().arrappend("non_existing_doc", "$..a")

    await modclient.json().set(
        "doc1",
        "$",
        {
            "a": ["foo"],
            "nested1": {"a": ["hello", None, "world"]},
            "nested2": {"a": 31},
        },
    )
    # Test multi (return result of last path)
    assert await modclient.json().arrlen("doc1", "$..a") == [1, 3, None]
    assert await modclient.json().arrappend("doc1", "..a", "non", "abba", "stanza") == 6

    # Test single
    assert await modclient.json().arrlen("doc1", ".nested1.a") == 6

    # Test missing key
    assert await modclient.json().arrlen("non_existing_doc", "..a") is None


@pytest.mark.redismod
async def test_arrpop_dollar(modclient: redis.Redis):
    await modclient.json().set(
        "doc1",
        "$",
        {
            "a": ["foo"],
            "nested1": {"a": ["hello", None, "world"]},
            "nested2": {"a": 31},
        },
    )

    # # # Test multi
    assert await modclient.json().arrpop("doc1", "$..a", 1) == ['"foo"', None, None]

    assert await modclient.json().get("doc1", "$") == [
        {"a": [], "nested1": {"a": ["hello", "world"]}, "nested2": {"a": 31}}
    ]

    # Test missing key
    with pytest.raises(exceptions.ResponseError):
        await modclient.json().arrpop("non_existing_doc", "..a")

    # # Test legacy
    await modclient.json().set(
        "doc1",
        "$",
        {
            "a": ["foo"],
            "nested1": {"a": ["hello", None, "world"]},
            "nested2": {"a": 31},
        },
    )
    # Test multi (all paths are updated, but return result of last path)
    await modclient.json().arrpop("doc1", "..a", "1") is None
    assert await modclient.json().get("doc1", "$") == [
        {"a": [], "nested1": {"a": ["hello", "world"]}, "nested2": {"a": 31}}
    ]

    # # Test missing key
    with pytest.raises(exceptions.ResponseError):
        await modclient.json().arrpop("non_existing_doc", "..a")


@pytest.mark.redismod
async def test_arrtrim_dollar(modclient: redis.Redis):

    await modclient.json().set(
        "doc1",
        "$",
        {
            "a": ["foo"],
            "nested1": {"a": ["hello", None, "world"]},
            "nested2": {"a": 31},
        },
    )
    # Test multi
    assert await modclient.json().arrtrim("doc1", "$..a", "1", -1) == [0, 2, None]
    assert await modclient.json().get("doc1", "$") == [
        {"a": [], "nested1": {"a": [None, "world"]}, "nested2": {"a": 31}}
    ]

    assert await modclient.json().arrtrim("doc1", "$..a", "1", "1") == [0, 1, None]
    assert await modclient.json().get("doc1", "$") == [
        {"a": [], "nested1": {"a": ["world"]}, "nested2": {"a": 31}}
    ]
    # Test single
    assert await modclient.json().arrtrim("doc1", "$.nested1.a", 1, 0) == [0]
    assert await modclient.json().get("doc1", "$") == [
        {"a": [], "nested1": {"a": []}, "nested2": {"a": 31}}
    ]

    # Test missing key
    with pytest.raises(exceptions.ResponseError):
        await modclient.json().arrtrim("non_existing_doc", "..a", "0", 1)

    # Test legacy
    await modclient.json().set(
        "doc1",
        "$",
        {
            "a": ["foo"],
            "nested1": {"a": ["hello", None, "world"]},
            "nested2": {"a": 31},
        },
    )

    # Test multi (all paths are updated, but return result of last path)
    assert await modclient.json().arrtrim("doc1", "..a", "1", "-1") == 2

    # Test single
    assert await modclient.json().arrtrim("doc1", ".nested1.a", "1", "1") == 1
    assert await modclient.json().get("doc1", "$") == [
        {"a": [], "nested1": {"a": ["world"]}, "nested2": {"a": 31}}
    ]

    # Test missing key
    with pytest.raises(exceptions.ResponseError):
        await modclient.json().arrtrim("non_existing_doc", "..a", 1, 1)


@pytest.mark.redismod
async def test_objkeys_dollar(modclient: redis.Redis):
    await modclient.json().set(
        "doc1",
        "$",
        {
            "nested1": {"a": {"foo": 10, "bar": 20}},
            "a": ["foo"],
            "nested2": {"a": {"baz": 50}},
        },
    )

    # Test single
    assert await modclient.json().objkeys("doc1", "$.nested1.a") == [["foo", "bar"]]

    # Test legacy
    assert await modclient.json().objkeys("doc1", ".*.a") == ["foo", "bar"]
    # Test single
    assert await modclient.json().objkeys("doc1", ".nested2.a") == ["baz"]

    # Test missing key
    assert await modclient.json().objkeys("non_existing_doc", "..a") is None

    # Test non existing doc
    with pytest.raises(exceptions.ResponseError):
        assert await modclient.json().objkeys("non_existing_doc", "$..a") == []

    assert await modclient.json().objkeys("doc1", "$..nowhere") == []


@pytest.mark.redismod
async def test_objlen_dollar(modclient: redis.Redis):
    await modclient.json().set(
        "doc1",
        "$",
        {
            "nested1": {"a": {"foo": 10, "bar": 20}},
            "a": ["foo"],
            "nested2": {"a": {"baz": 50}},
        },
    )
    # Test multi
    assert await modclient.json().objlen("doc1", "$..a") == [None, 2, 1]
    # Test single
    assert await modclient.json().objlen("doc1", "$.nested1.a") == [2]

    # Test missing key, and path
    with pytest.raises(exceptions.ResponseError):
        await modclient.json().objlen("non_existing_doc", "$..a")

    assert await modclient.json().objlen("doc1", "$.nowhere") == []

    # Test legacy
    assert await modclient.json().objlen("doc1", ".*.a") == 2

    # Test single
    assert await modclient.json().objlen("doc1", ".nested2.a") == 1

    # Test missing key
    assert await modclient.json().objlen("non_existing_doc", "..a") is None

    # Test missing path
    # with pytest.raises(exceptions.ResponseError):
    await modclient.json().objlen("doc1", ".nowhere")


@pytest.mark.redismod
def load_types_data(nested_key_name):
    td = {
        "object": {},
        "array": [],
        "string": "str",
        "integer": 42,
        "number": 1.2,
        "boolean": False,
        "null": None,
    }
    jdata = {}
    types = []
    for i, (k, v) in zip(range(1, len(td) + 1), iter(td.items())):
        jdata["nested" + str(i)] = {nested_key_name: v}
        types.append(k)

    return jdata, types


@pytest.mark.redismod
async def test_type_dollar(modclient: redis.Redis):
    jdata, jtypes = load_types_data("a")
    await modclient.json().set("doc1", "$", jdata)
    # Test multi
    assert await modclient.json().type("doc1", "$..a") == jtypes

    # Test single
    assert await modclient.json().type("doc1", "$.nested2.a") == [jtypes[1]]

    # Test missing key
    assert await modclient.json().type("non_existing_doc", "..a") is None


@pytest.mark.redismod
async def test_clear_dollar(modclient: redis.Redis):

    await modclient.json().set(
        "doc1",
        "$",
        {
            "nested1": {"a": {"foo": 10, "bar": 20}},
            "a": ["foo"],
            "nested2": {"a": "claro"},
            "nested3": {"a": {"baz": 50}},
        },
    )

    # Test multi
    assert await modclient.json().clear("doc1", "$..a") == 3

    assert await modclient.json().get("doc1", "$") == [
        {"nested1": {"a": {}}, "a": [], "nested2": {"a": "claro"}, "nested3": {"a": {}}}
    ]

    # Test single
    await modclient.json().set(
        "doc1",
        "$",
        {
            "nested1": {"a": {"foo": 10, "bar": 20}},
            "a": ["foo"],
            "nested2": {"a": "claro"},
            "nested3": {"a": {"baz": 50}},
        },
    )
    assert await modclient.json().clear("doc1", "$.nested1.a") == 1
    assert await modclient.json().get("doc1", "$") == [
        {
            "nested1": {"a": {}},
            "a": ["foo"],
            "nested2": {"a": "claro"},
            "nested3": {"a": {"baz": 50}},
        }
    ]

    # Test missing path (async defaults to root)
    assert await modclient.json().clear("doc1") == 1
    assert await modclient.json().get("doc1", "$") == [{}]

    # Test missing key
    with pytest.raises(exceptions.ResponseError):
        await modclient.json().clear("non_existing_doc", "$..a")


@pytest.mark.redismod
async def test_toggle_dollar(modclient: redis.Redis):
    await modclient.json().set(
        "doc1",
        "$",
        {
            "a": ["foo"],
            "nested1": {"a": False},
            "nested2": {"a": 31},
            "nested3": {"a": True},
        },
    )
    # Test multi
    assert await modclient.json().toggle("doc1", "$..a") == [None, 1, None, 0]
    assert await modclient.json().get("doc1", "$") == [
        {
            "a": ["foo"],
            "nested1": {"a": True},
            "nested2": {"a": 31},
            "nested3": {"a": False},
        }
    ]

    # Test missing key
    with pytest.raises(exceptions.ResponseError):
        await modclient.json().toggle("non_existing_doc", "$..a")