rustis 0.23.0

Redis async driver for Rust
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
use crate::{
    Result,
    commands::{
        FlushingMode, JsonArrIndexOptions, JsonCommands, JsonFpType, JsonGetFormat, JsonGetOptions,
        JsonSetOptions, ServerCommands, SetCondition,
    },
    resp::Value,
    tests::{TestClient, get_test_client},
};
use serial_test::serial;
use smallvec::SmallVec;

#[tokio::test]
#[serial]
async fn json_arrappend() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"foo":[{"bar":[1,2,3]},{"bar":[3,4,5]},{"bar":12}]}"#,
            None,
        )
        .await?;

    let result: Vec<Option<usize>> = client.json_arrappend("key", "$.fooo", [4, 5]).await?;
    assert_eq!(0, result.len());

    let result: Vec<Option<usize>> = client.json_arrappend("key", "$.foo[*].bar", [4, 5]).await?;
    assert_eq!(3, result.len());
    assert_eq!(Some(5), result[0]);
    assert_eq!(Some(5), result[1]);
    assert_eq!(None, result[2]);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_arrindex() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"foo":[{"bar":[1,2,3]},{"bar":[3,4,5]},{"bar":12}]}"#,
            None,
        )
        .await?;

    let result: Vec<Option<isize>> = client
        .json_arrindex("key", "$.foo[*].bar", "1", JsonArrIndexOptions::default())
        .await?;
    assert_eq!(3, result.len());
    assert_eq!(Some(0), result[0]);
    assert_eq!(Some(-1), result[1]);
    assert_eq!(None, result[2]);

    let result: Vec<Option<isize>> = client
        .json_arrindex("key", "$.foo[*].bar", "3", JsonArrIndexOptions::default())
        .await?;
    assert_eq!(3, result.len());
    assert_eq!(Some(2), result[0]);
    assert_eq!(Some(0), result[1]);
    assert_eq!(None, result[2]);

    let result: Vec<Option<isize>> = client
        .json_arrindex(
            "key",
            "$.foo[0].bar[0].1",
            "3",
            JsonArrIndexOptions::default(),
        )
        .await?;
    assert_eq!(0, result.len());

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_arrinsert() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"foo":[{"bar":[1,2,3]},{"bar":[3,4,5]},{"bar":12}]}"#,
            None,
        )
        .await?;

    let result: Vec<Option<usize>> = client
        .json_arrinsert("key", "$.foo[*].bar", -1, "4")
        .await?;
    assert_eq!(3, result.len());
    assert_eq!(Some(4), result[0]);
    assert_eq!(Some(4), result[1]);
    assert_eq!(None, result[2]);

    let result: Vec<Option<usize>> = client.json_arrinsert("key", "$.foo[0].bar", 1, "5").await?;
    assert_eq!(1, result.len());
    assert_eq!(Some(5), result[0]);

    // not an array
    let result: Vec<Option<usize>> = client
        .json_arrinsert("key", "$.foo[0].bar[0].1", -1, "6")
        .await?;
    assert_eq!(0, result.len());

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_arrlen() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"prop1":12,"prop2":"foo","prop3":["foo","bar"],"prop4":[12,13,14]}"#,
            None,
        )
        .await?;

    let result: Vec<Option<usize>> = client.json_arrlen("key", "$.[*]").await?;
    assert_eq!(4, result.len());
    assert_eq!(None, result[0]);
    assert_eq!(None, result[1]);
    assert_eq!(Some(2), result[2]);
    assert_eq!(Some(3), result[3]);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_arrpop() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"foo":[{"bar":[1,2,3]},{"bar":[3,4,5]}]}"#,
            None,
        )
        .await?;

    let result: Vec<i64> = client.json_arrpop("key", "$.foo[*].bar", -1).await?;
    assert_eq!(2, result.len());
    assert_eq!(3, result[0]);
    assert_eq!(5, result[1]);

    client
        .json_set(
            "key",
            "$",
            r#"{"foo":[{"bar":["a","b","c"]},{"bar":12}]}"#,
            None,
        )
        .await?;

    let result: Vec<Option<String>> = client.json_arrpop("key", "$.foo[*].bar", -1).await?;
    assert_eq!(2, result.len());
    assert_eq!(Some(r#""c""#.to_owned()), result[0]);
    assert_eq!(None, result[1]);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_arrtrim() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"foo":[{"bar":["a","b","c"]},{"bar":12}]}"#,
            None,
        )
        .await?;

    let result: Vec<Option<usize>> = client.json_arrtrim("key", "$.foo[*].bar", 0, -1).await?;
    assert_eq!(2, result.len());
    assert_eq!(Some(3), result[0]);
    assert_eq!(None, result[1]);

    let result: Vec<Option<usize>> = client.json_arrtrim("key", "$.foo[*].bar", 1, 1).await?;
    assert_eq!(2, result.len());
    assert_eq!(Some(1), result[0]);
    assert_eq!(None, result[1]);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_clear() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"foo":[{"bar":["a","b","c"]},{"bar":12}]}"#,
            None,
        )
        .await?;

    let num: usize = client.json_clear("key", "$.foo[*].bar").await?;
    assert_eq!(2, num);

    let json: String = client.json_get("key", JsonGetOptions::default()).await?;
    assert_eq!(r#"{"foo":[{"bar":[]},{"bar":0}]}"#, json);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_debug_memory() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"foo":[{"bar":["a","b","c"]},{"bar":12}]}"#,
            None,
        )
        .await?;

    let result: Vec<usize> = client.json_debug_memory("key", "$.foo[*].bar").await?;
    assert_eq!(2, result.len());
    // The exact byte counts belong to the JSON module allocator. Only their
    // ordering is stable: a three-string array outweighs a single number.
    assert!(result[0] > result[1]);
    assert!(result[1] > 0);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_del() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"foo":[{"bar":["a","b","c"]},{"bar":12}]}"#,
            None,
        )
        .await?;

    let num_deleted = client.json_del("key", "$").await?;
    assert_eq!(1, num_deleted);

    let json: Option<String> = client.json_get("key", JsonGetOptions::default()).await?;
    assert_eq!(None, json);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_forget() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"foo":[{"bar":["a","b","c"]},{"bar":12}]}"#,
            None,
        )
        .await?;

    let num_deleted = client.json_forget("key", "$").await?;
    assert_eq!(1, num_deleted);

    let json: Option<String> = client.json_get("key", JsonGetOptions::default()).await?;
    assert_eq!(None, json);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_get() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"foo":[{"bar":[1,2,3]},{"bar":[3,4,5]},{"bar":12}]}"#,
            None,
        )
        .await?;

    let json: String = client
        .json_get("key", JsonGetOptions::default().path("$.foo[*].bar"))
        .await?;
    assert_eq!("[[1,2,3],[3,4,5],12]", json);

    // `STRING` is the default: the whole reply as one serialized document.
    let json: String = client
        .json_get(
            "key",
            JsonGetOptions::default()
                .path("$.foo[*].bar")
                .format(JsonGetFormat::String),
        )
        .await?;
    assert_eq!("[[1,2,3],[3,4,5],12]", json);

    // `EXPAND1` groups the matches per path and serializes only the containers,
    // leaving scalars native — hence the mixed element types.
    let values: Vec<Vec<Value>> = client
        .json_get(
            "key",
            JsonGetOptions::default()
                .path("$.foo[*].bar")
                .format(JsonGetFormat::Expand1),
        )
        .await?;
    assert_eq!(1, values.len());
    assert_eq!(3, values[0].len());
    assert_eq!("[1,2,3]", values[0][0].to_string());
    assert_eq!(Value::Integer(12), values[0][2]);

    // `EXPAND` goes all the way down to native RESP3 values.
    let values: Vec<Vec<Value>> = client
        .json_get(
            "key",
            JsonGetOptions::default()
                .path("$.foo[*].bar")
                .format(JsonGetFormat::Expand),
        )
        .await?;
    assert_eq!(1, values.len());
    assert_eq!(
        Value::Array(vec![
            Value::Integer(1),
            Value::Integer(2),
            Value::Integer(3)
        ]),
        values[0][0]
    );

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_set_fpha() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    // FPHA forces the storage type of floating-point homogeneous arrays; the
    // integers come back as floats because the array is stored as FP16.
    client
        .json_set(
            "key",
            "$",
            "[[1,2,3,4e3],[5,6.0,7,8]]",
            JsonSetOptions::default().fpha(JsonFpType::Fp16),
        )
        .await?;
    let result: String = client.json_get("key", JsonGetOptions::default()).await?;
    assert_eq!("[[1.0,2.0,3.0,4000.0],[5.0,6.0,7.0,8.0]]", result);

    // A value that does not fit the requested type is rejected.
    let result = client
        .json_set(
            "key2",
            "$",
            "[1e40]",
            JsonSetOptions::default().fpha(JsonFpType::Fp16),
        )
        .await;
    assert!(result.is_err());

    Ok(())
}

#[test]
fn json_set_args() {
    let cmd = TestClient
        .json_set(
            "key",
            "$",
            "[1.0]",
            JsonSetOptions::default()
                .condition(SetCondition::NX)
                .fpha(JsonFpType::Bf16),
        )
        .command;
    assert_eq!("JSON.SET key $ [1.0] NX FPHA BF16", cmd.to_string());

    let cmd = TestClient.json_set("key", "$", "[1.0]", None).command;
    assert_eq!("JSON.SET key $ [1.0]", cmd.to_string());
}

#[tokio::test]
#[serial]
async fn json_merge() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"a":2, "b":3, "nested":{"a":4, "b":5}}"#,
            None,
        )
        .await?;

    // A merge updates the keys it names and leaves the others alone. Assertions
    // go through paths rather than the whole document, whose key order the
    // server does not preserve across a delete.
    client.json_merge("key", "$", r#"{"b":8}"#).await?;
    let json: String = client
        .json_get("key", JsonGetOptions::default().path("$.b"))
        .await?;
    assert_eq!("[8]", json);
    let json: String = client
        .json_get("key", JsonGetOptions::default().path("$.a"))
        .await?;
    assert_eq!("[2]", json);

    // A null value deletes the key, which is what makes MERGE more than a set.
    client.json_merge("key", "$", r#"{"a":null}"#).await?;
    let json: String = client
        .json_get("key", JsonGetOptions::default().path("$.a"))
        .await?;
    assert_eq!("[]", json);

    // The path can target a nested document, merging in place.
    client.json_merge("key", "$.nested", r#"{"c":6}"#).await?;
    let json: String = client
        .json_get("key", JsonGetOptions::default().path("$.nested"))
        .await?;
    assert_eq!(r#"[{"a":4,"b":5,"c":6}]"#, json);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_mset() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    // JSON.MSET takes key/path/value triplets and creates the documents in one
    // atomic call.
    client
        .json_mset([
            ("key1", "$", r#"{"a":1, "nested": {"a": 3}}"#),
            ("key2", "$", r#"{"a":4, "nested": {"a": 6}}"#),
        ])
        .await?;

    let jsons: SmallVec<[String; 2]> = client.json_mget(["key1", "key2"], "$..a").await?;
    assert_eq!(2, jsons.len());
    assert_eq!("[1,3]", jsons[0]);
    assert_eq!("[4,6]", jsons[1]);

    // A path inside an existing document updates just that member.
    client.json_mset([("key1", "$.a", "2")]).await?;

    let json: String = client.json_get("key1", JsonGetOptions::default()).await?;
    assert_eq!(r#"{"a":2,"nested":{"a":3}}"#, json);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_mget() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key1",
            "$",
            r#"{"a":1, "b": 2, "nested": {"a": 3}, "c": null}"#,
            None,
        )
        .await?;

    client
        .json_set(
            "key2",
            "$",
            r#"{"a":4, "b": 5, "nested": {"a": 6}, "c": null}"#,
            None,
        )
        .await?;

    let jsons: SmallVec<[String; 2]> = client.json_mget(["key1", "key2"], "$..a").await?;
    assert_eq!(2, jsons.len());
    assert_eq!("[1,3]", jsons[0]);
    assert_eq!("[4,6]", jsons[1]);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_numincrby() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"a":"b","b":[{"a":2}, {"a":5}, {"a":"c"}]}"#,
            None,
        )
        .await?;

    let response: Vec<Option<i32>> = client.json_numincrby("key", "$.a", 2).await?;
    assert_eq!(vec![None], response);

    let response: Vec<Option<i32>> = client.json_numincrby("key", "$..a", 2).await?;
    assert_eq!(vec![None, Some(4), Some(7), None], response);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_nummultby() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"a":"b","b":[{"a":2}, {"a":5}, {"a":"c"}]}"#,
            None,
        )
        .await?;

    let response: Vec<Option<i32>> = client.json_nummultby("key", "$.a", 2).await?;
    assert_eq!(vec![None], response);

    let response: Vec<Option<i32>> = client.json_nummultby("key", "$..a", 2).await?;
    assert_eq!(vec![None, Some(4), Some(10), None], response);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_objkeys() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"a":[3], "nested": {"a": {"b":2, "c": 1}}}"#,
            None,
        )
        .await?;

    let result: Vec<Vec<String>> = client.json_objkeys("key", "$..a").await?;
    assert_eq!(2, result.len());
    assert_eq!(0, result[0].len());
    assert_eq!(2, result[1].len());
    assert_eq!("b", result[1][0]);
    assert_eq!("c", result[1][1]);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_objlen() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"a":[3], "nested": {"a": {"b":2, "c": 1}}}"#,
            None,
        )
        .await?;

    let result: Vec<Option<usize>> = client.json_objlen("key", "$..a").await?;
    assert_eq!(2, result.len());
    assert_eq!(None, result[0]);
    assert_eq!(Some(2), result[1]);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_resp() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"prop1":12,"prop2":"foo","prop3": true,"prop4":null,"prop5":["foo","bar"],"prop6":{"prop1": "foo", "prop2": 12}}"#,
            None,
        )
        .await?;

    let mut result: Vec<Value> = client.json_resp("key", "$").await?;
    assert_eq!(1, result.len());
    let values: Vec<Value> = result.pop().unwrap().into()?;
    assert_eq!(13, values.len());
    let mut iter = values.into_iter();
    assert_eq!("{", iter.next().unwrap().into::<String>()?);
    assert_eq!("prop1", iter.next().unwrap().into::<String>()?);
    assert_eq!(12, iter.next().unwrap().into::<i64>()?);
    assert_eq!("prop2", iter.next().unwrap().into::<String>()?);
    assert_eq!("foo", iter.next().unwrap().into::<String>()?);
    assert_eq!("prop3", iter.next().unwrap().into::<String>()?);
    assert_eq!("true", iter.next().unwrap().into::<String>()?);
    assert_eq!("prop4", iter.next().unwrap().into::<String>()?);
    assert_eq!("", iter.next().unwrap().into::<String>()?);
    assert_eq!("prop5", iter.next().unwrap().into::<String>()?);
    let prop5_values: Vec<Value> = iter.next().unwrap().into()?;
    assert_eq!(3, prop5_values.len());
    let mut iter_prop5 = prop5_values.into_iter();
    assert_eq!("[", iter_prop5.next().unwrap().into::<String>()?);
    assert_eq!("foo", iter_prop5.next().unwrap().into::<String>()?);
    assert_eq!("bar", iter_prop5.next().unwrap().into::<String>()?);
    assert_eq!("prop6", iter.next().unwrap().into::<String>()?);
    let prop6_values: Vec<Value> = iter.next().unwrap().into()?;
    assert_eq!(5, prop6_values.len());
    let mut iter_prop6 = prop6_values.into_iter();
    assert_eq!("{", iter_prop6.next().unwrap().into::<String>()?);
    assert_eq!("prop1", iter_prop6.next().unwrap().into::<String>()?);
    assert_eq!("foo", iter_prop6.next().unwrap().into::<String>()?);
    assert_eq!("prop2", iter_prop6.next().unwrap().into::<String>()?);
    assert_eq!(12, iter_prop6.next().unwrap().into::<i64>()?);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_strappend() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}}"#,
            None,
        )
        .await?;

    let result: Vec<Option<usize>> = client.json_strappend("key", "$..a", r#""baz""#).await?;
    assert_eq!(3, result.len());
    assert_eq!(Some(6), result[0]);
    assert_eq!(Some(8), result[1]);
    assert_eq!(None, result[2]);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_strlen() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}}"#,
            None,
        )
        .await?;

    let result: Vec<Option<usize>> = client.json_strlen("key", "$..a").await?;
    assert_eq!(3, result.len());
    assert_eq!(Some(3), result[0]);
    assert_eq!(Some(5), result[1]);
    assert_eq!(None, result[2]);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_toggle() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set("key", "$", r#"{"foo":[{"bar":true},{"bar":12}]}"#, None)
        .await?;

    let result: Vec<Option<usize>> = client.json_toggle("key", "$.foo[*].bar").await?;
    assert_eq!(2, result.len());
    assert_eq!(Some(0), result[0]);
    assert_eq!(None, result[1]);

    let json: String = client.json_get("key", JsonGetOptions::default()).await?;
    assert_eq!(r#"{"foo":[{"bar":false},{"bar":12}]}"#, json);

    let result: Vec<Option<usize>> = client.json_toggle("key", "$.foo[*].bar").await?;
    assert_eq!(2, result.len());
    assert_eq!(Some(1), result[0]);
    assert_eq!(None, result[1]);

    let json: String = client.json_get("key", JsonGetOptions::default()).await?;
    assert_eq!(r#"{"foo":[{"bar":true},{"bar":12}]}"#, json);

    Ok(())
}

#[tokio::test]
#[serial]
async fn json_type() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set(
            "key",
            "$",
            r#"{"a":2, "nested": {"a": true}, "foo": "bar"}"#,
            None,
        )
        .await?;

    let result: Vec<String> = client.json_type("key", ".foo").await?;
    assert_eq!(1, result.len());
    assert_eq!("string", result[0]);

    let result: Vec<Vec<String>> = client.json_type("key", "$..foo").await?;
    assert_eq!(1, result.len());
    assert_eq!(1, result[0].len());
    assert_eq!("string", result[0][0]);

    let result: Vec<Vec<String>> = client.json_type("key", "$..a").await?;
    assert_eq!(1, result.len());
    assert_eq!(2, result[0].len());
    assert_eq!("integer", result[0][0]);
    assert_eq!("boolean", result[0][1]);

    let result: Vec<Vec<String>> = client.json_type("key", "$..dummy").await?;
    assert_eq!(1, result.len());
    assert_eq!(0, result[0].len());

    Ok(())
}

/// `JSON.GET key [INDENT indent] [NEWLINE newline] [SPACE space] [path ...]`.
/// The three formatting options are the server's own pretty-printer: INDENT per
/// nesting level, NEWLINE at each line end, SPACE between a key and its value.
#[tokio::test]
#[serial]
async fn json_get_formatting() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set("key", "$", r#"{"a":1,"b":2}"#, None)
        .await?;

    let json: String = client
        .json_get(
            "key",
            JsonGetOptions::default()
                .indent("--")
                .newline("|")
                .space("_")
                .path("$.a"),
        )
        .await?;
    assert_eq!("[|--1|]", json);

    // Each option is independently visible: dropping NEWLINE drops the line
    // breaks but keeps nothing else.
    let json: String = client
        .json_get(
            "key",
            JsonGetOptions::default().indent("--").space("_").path("$"),
        )
        .await?;
    assert_eq!(r#"[--{----"a":_1,----"b":_2--}]"#, json);

    Ok(())
}

/// `JSON.ARRINDEX key path value [start [stop]]`. start and stop are positional
/// and slice the searched range; stop is exclusive except that 0 means "to the
/// end".
#[tokio::test]
#[serial]
async fn json_arrindex_stop() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set("key", "$", r#"{"b":[1,2,3,4,5]}"#, None)
        .await?;

    // 4 sits at index 3, inside [0, 4).
    let result: Vec<Option<isize>> = client
        .json_arrindex(
            "key",
            "$.b",
            "4",
            JsonArrIndexOptions::default().start(0).stop(4),
        )
        .await?;
    assert_eq!(vec![Some(3)], result);

    // Outside [0, 3) it is not found.
    let result: Vec<Option<isize>> = client
        .json_arrindex(
            "key",
            "$.b",
            "4",
            JsonArrIndexOptions::default().start(0).stop(3),
        )
        .await?;
    assert_eq!(vec![Some(-1)], result);

    // A negative stop counts from the end and stays exclusive, so -1 drops the
    // last element; stop 0 is the one value that means "to the end".
    let result: Vec<Option<isize>> = client
        .json_arrindex(
            "key",
            "$.b",
            "5",
            JsonArrIndexOptions::default().start(0).stop(-1),
        )
        .await?;
    assert_eq!(vec![Some(-1)], result);

    let result: Vec<Option<isize>> = client
        .json_arrindex(
            "key",
            "$.b",
            "5",
            JsonArrIndexOptions::default().start(0).stop(0),
        )
        .await?;
    assert_eq!(vec![Some(4)], result);

    Ok(())
}

/// A negative `start` counts from the end, like `stop` and like every other
/// index in this family.
#[tokio::test]
#[serial]
async fn json_arrindex_negative_start() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set("key", "$", r#"{"b":[1,2,3,2,1]}"#, None)
        .await?;

    // Searching the whole array finds the first 2, at index 1.
    let result: Vec<Option<isize>> = client
        .json_arrindex("key", "$.b", "2", JsonArrIndexOptions::default())
        .await?;
    assert_eq!(vec![Some(1)], result);

    // -3 starts the search at index 2, so the second 2 is the first match.
    let result: Vec<Option<isize>> = client
        .json_arrindex("key", "$.b", "2", JsonArrIndexOptions::default().start(-3))
        .await?;
    assert_eq!(vec![Some(3)], result);

    Ok(())
}

/// `JSON.SET key path value [NX | XX]`. The value-comparison conditions the
/// string `SET` accepts are a syntax error here, which is why `condition` says
/// so.
#[tokio::test]
#[serial]
async fn json_set_condition() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    // XX on a missing key writes nothing and answers nil.
    client
        .json_set(
            "key",
            "$",
            "1",
            JsonSetOptions::default().condition(SetCondition::XX),
        )
        .await?;
    let types: Vec<Option<String>> = client.json_type("key", "$").await?;
    assert_eq!(vec![None], types);

    // Once the key exists, XX overwrites it and NX leaves it alone.
    client.json_set("key", "$", "1", None).await?;
    client
        .json_set(
            "key",
            "$",
            "2",
            JsonSetOptions::default().condition(SetCondition::XX),
        )
        .await?;
    let value: String = client.json_get("key", JsonGetOptions::default()).await?;
    assert_eq!("2", value);

    client
        .json_set(
            "key",
            "$",
            "3",
            JsonSetOptions::default().condition(SetCondition::NX),
        )
        .await?;
    let value: String = client.json_get("key", JsonGetOptions::default()).await?;
    assert_eq!("2", value);

    // The `From<SetCondition>` shorthand takes the same path as the builder.
    client
        .json_set("key", "$", "4", JsonSetOptions::from(SetCondition::XX))
        .await?;
    let value: String = client.json_get("key", JsonGetOptions::default()).await?;
    assert_eq!("4", value);

    // The value-comparison conditions are rejected by the server.
    let result = client
        .json_set(
            "key",
            "$",
            "5",
            JsonSetOptions::default().condition(SetCondition::IFEQ("4")),
        )
        .await;
    assert!(result.is_err());

    Ok(())
}

/// Every `FPHA` storage type is accepted and keeps the precision it declares.
#[tokio::test]
#[serial]
async fn json_set_fpha_types() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    // FP32 keeps a value FP16 cannot hold.
    client
        .json_set(
            "fp32",
            "$",
            "[1e30,2e30]",
            JsonSetOptions::default().fpha(JsonFpType::Fp32),
        )
        .await?;
    let value: String = client.json_get("fp32", JsonGetOptions::default()).await?;
    assert!(value.starts_with("[1e30"), "unexpected value: {value}");

    // FP64 keeps a value FP32 cannot hold.
    client
        .json_set(
            "fp64",
            "$",
            "[1e300,2e300]",
            JsonSetOptions::default().fpha(JsonFpType::Fp64),
        )
        .await?;
    let value: String = client.json_get("fp64", JsonGetOptions::default()).await?;
    assert!(value.starts_with("[1e300"), "unexpected value: {value}");

    let result = client
        .json_set(
            "fp32_overflow",
            "$",
            "[1e300]",
            JsonSetOptions::default().fpha(JsonFpType::Fp32),
        )
        .await;
    assert!(result.is_err());

    Ok(())
}

/// `path` accumulates: `JSON.GET` takes several paths and answers a map keyed by
/// path.
#[tokio::test]
#[serial]
async fn json_get_multiple_paths() -> Result<()> {
    let client = get_test_client().await?;
    client.flushall(FlushingMode::Sync).await?;

    client
        .json_set("key", "$", r#"{"a":1,"b":2,"c":3}"#, None)
        .await?;

    let json: String = client
        .json_get("key", JsonGetOptions::default().path("$.a").path("$.c"))
        .await?;
    // The reply is a map keyed by path; the server does not order it.
    assert!(json.contains(r#""$.a":[1]"#), "unexpected reply: {json}");
    assert!(json.contains(r#""$.c":[3]"#), "unexpected reply: {json}");
    assert!(!json.contains(r#""$.b""#), "unexpected reply: {json}");

    // NEWLINE alone is the only formatting option applied.
    let json: String = client
        .json_get("key", JsonGetOptions::default().newline("|").path("$.a"))
        .await?;
    assert_eq!("[|1|]", json);

    Ok(())
}