-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathtest.py
990 lines (844 loc) · 41.7 KB
/
test.py
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
# -*- coding: utf-8 -*-
import sys
import os
import redis
import json
from RLTest import Env
from includes import *
#----------------------------------------------------------------------------------------------
# Path to JSON test case files
HERE = os.path.abspath(os.path.dirname(__file__))
ROOT = os.path.abspath(os.path.join(HERE, "../.."))
TESTS_ROOT = os.path.abspath(os.path.join(HERE, ".."))
JSON_PATH = os.path.join(TESTS_ROOT, 'files')
#----------------------------------------------------------------------------------------------
# TODO: these are currently not supported so ignore them
json_ignore = [
'pass-json-parser-0002.json', # UTF-8 to Unicode
'pass-json-parser-0005.json', # big numbers
'pass-json-parser-0006.json', # UTF-8 to Unicode
'pass-json-parser-0007.json', # UTF-8 to Unicode
'pass-json-parser-0012.json', # UTF-8 to Unicode
'pass-jsonsl-1.json', # big numbers
'pass-jsonsl-yelp.json', # float precision
]
# Some basic documents to use in the tests
docs = {
'simple': {
'foo': 'bar',
},
'basic': {
'string': 'string value',
'none': None,
'bool': True,
'int': 42,
'num': 4.2,
'arr': [42, None, -1.2, False, ['sub', 'array'], {'subdict': True}],
'dict': {
'a': 1,
'b': '2',
'c': None,
}
},
'scalars': {
'unicode': 'string value',
'NoneType': None,
'bool': True,
'int': 42,
'float': -1.2,
},
'values': {
'str': 'string value',
'NoneType': None,
'bool': True,
'int': 42,
'float': -1.2,
'dict': {},
'list': []
},
'types': {
'null': None,
'boolean': False,
'integer': 42,
'number': 1.2,
'string': 'str',
'object': {},
'array': [],
},
}
#----------------------------------------------------------------------------------------------
# def getCacheInfo(env):
# r = env
# res = r.cmd('JSON._CACHEINFO')
# ret = {}
# for x in range(0, len(res), 2):
# ret[res[x]] = res[x+1]
# return ret
def assertOk(r, x, msg=None):
r.assertOk(x, message=msg)
def assertExists(r, key, msg=None):
r.assertTrue(r.exists(key), message=msg)
def assertNotExists(r, key, msg=None):
r.assertFalse(r.exists(key), message=msg)
#----------------------------------------------------------------------------------------------
def testSetRootWithInvalidJSONValuesShouldFail(env):
"""Test that setting the root of a ReJSON key with invalid JSON values fails"""
r = env
invalid = ['{', '}', '[', ']', '{]', '[}', '\\', '\\\\', '',
' ', '\\"', '\'', '\[', '\x00', '\x0a', '\x0c',
# '\xff' TODO pending https://github.com/RedisLabsModules/redismodule-rs/pull/15
]
for i in invalid:
r.expect('JSON.SET', 'test', '.', i).raiseError()
assertNotExists(r, 'test%s' % i)
def testSetInvalidPathShouldFail(env):
"""Test that invalid paths fail"""
r = env
invalid = ['', ' ', '\x00', '\x0a', '\x0c',
# '\xff' TODO pending https://github.com/RedisLabsModules/redismodule-rs/pull/15
'."', '.\x00', '.\x0a\x0c', '.-foo', '.43',
'.foo\n.bar']
for i in invalid:
r.expect('JSON.SET', 'test', i, 'null').raiseError()
assertNotExists(r, 'test%s' % i)
def testSetRootWithJSONValuesShouldSucceed(env):
"""Test that the root of a JSON key can be set with any valid JSON"""
r = env
for v in ['string', 1, -2, 3.14, None, True, False, [], {}]:
r.cmd('DEL', 'test')
j = json.dumps(v)
r.assertOk(r.execute_command('JSON.SET', 'test', '.', j))
r.assertExists('test')
s = json.loads(r.execute_command('JSON.GET', 'test'))
r.assertEqual(v, s)
def testSetReplaceRootShouldSucceed(env):
"""Test replacing the root of an existing key with a valid object succeeds"""
r = env
r.assertOk(r.execute_command('JSON.SET', 'test', '.', json.dumps(docs['basic'])))
r.assertOk(r.execute_command('JSON.SET', 'test', '.', json.dumps(docs['simple'])))
raw = r.execute_command('JSON.GET', 'test', '.')
r.assertEqual(json.loads(raw), docs['simple'])
for k, v in iter(docs['values'].items()):
r.assertOk(r.execute_command('JSON.SET', 'test', '.', json.dumps(v)))
data = json.loads(r.execute_command('JSON.GET', 'test', '.'))
r.assertEqual(str(type(data)), '<class \'{}\'>'.format(k))
r.assertEqual(data, v)
def testSetGetWholeBasicDocumentShouldBeEqual(env):
"""Test basic JSON.GET/JSON.SET"""
r = env
data = json.dumps(docs['basic'])
r.assertOk(r.execute_command('JSON.SET', 'test', '.', data))
r.assertExists('test')
r.assertEqual(json.dumps(json.loads(r.execute_command('JSON.GET', 'test'))), data)
def testSetBehaviorModifyingSubcommands(env):
"""Test JSON.SET's NX and XX subcommands"""
r = env
# test against the root
r.assertIsNone(r.execute_command('JSON.SET', 'test', '.', '{}', 'XX'))
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{}', 'NX'))
r.assertIsNone(r.execute_command('JSON.SET', 'test', '.', '{}', 'NX'))
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{}', 'XX'))
# test an object key
r.assertIsNone(r.execute_command('JSON.SET', 'test', '.foo', '[]', 'XX'))
r.assertOk(r.execute_command('JSON.SET', 'test', '.foo', '[]', 'NX'))
r.assertIsNone(r.execute_command('JSON.SET', 'test', '.foo', '[]', 'NX'))
r.assertOk(r.execute_command('JSON.SET', 'test', '.foo', '[1]', 'XX'))
# verify failure for arrays
r.expect('JSON.SET', 'test', '.foo[1]', 'null', 'NX').raiseError()
# r.expect('JSON.SET', 'test', '.foo[1]', 'null', 'XX').raiseError()
# Wrong arguments
r.expect('JSON.SET', 'test', '.foo', '[]', '').raiseError()
r.expect('JSON.SET', 'test', '.foo', '[]', 'NN').raiseError()
r.expect('JSON.SET', 'test', '.foo', '[]', 'FORMAT', 'TT').raiseError()
r.expect('JSON.SET', 'test', '.foo', '[]', 'XX', 'FORMAT', '').raiseError()
r.expect('JSON.SET', 'test', '.foo', '[]', 'XX', 'XN').raiseError()
r.expect('JSON.SET', 'test', '.foo', '[]', 'XX', '').raiseError()
def testSetWithBracketNotation(env):
r = env
r.assertOk(r.execute_command('JSON.SET', 'x', '.', '{}'))
r.assertOk(r.execute_command('JSON.SET', 'x', '.["f1"]', '{}')) # Simple bracket notation
r.assertOk(r.execute_command('JSON.SET', 'x', '.["f1"].f2', '[0,0,0]')) # Mixed with dot notation
r.assertOk(r.execute_command('JSON.SET', 'x', '.["f1"].f2[1]', '{}')) # Replace in array
r.assertOk(r.execute_command('JSON.SET', 'x', '.["f1"].f2[1]["f.]$.f"]', '{}')) # Dots and invalid chars in the brackets
r.assertOk(r.execute_command('JSON.SET', 'x', '.["f1"]["f2"][1]["f.]$.f"]', '1')) # Replace existing value
r.assertIsNone(r.execute_command('JSON.SET', 'x', '.["f3"].f2', '1')) # Fail trying to set f2 when f3 doesn't exist
r.assertEqual(json.loads(r.execute_command('JSON.GET', 'x')), {'f1': {'f2': [0, {'f.]$.f': 1}, 0]}}) # Make sure it worked
def testGetWithBracketNotation(env):
r = env
r.assertOk(r.execute_command('JSON.SET', 'x', '.', '[1,2,3]'))
r.assertEqual(json.loads(r.execute_command('JSON.GET', 'x', '.[1]')), 2) # dot notation - single value
r.assertEqual(json.loads(r.execute_command('JSON.GET', 'x', '[1]')), 2) # implicit dot notation - single value
r.assertEqual(json.loads(r.execute_command('JSON.GET', 'x', '$.[1]')), [2]) # dollar notation - array
r.assertEqual(json.loads(r.execute_command('JSON.GET', 'x', '$[1]')), [2]) # dollar notation - array
def testSetWithPathErrors(env):
r = env
r.expect('JSON.SET', 'x', '.', '{}').ok()
# Add to non static path
r.expect('JSON.SET', 'x', '$..f', 1).raiseError()
# r.assertEqual(str(e.exception), 'Err: wrong static path')
# Treat object as array
r.expect('JSON.SET', 'x', '$[0]', 1).raiseError()
# r.assertEqual(str(e.exception), 'Err: path not an object')
def testGetNonExistantPathsFromBasicDocumentShouldFail(env):
"""Test failure of getting non-existing values"""
r = env
r.assertOk(r.execute_command('JSON.SET', 'test', '.', json.dumps(docs['scalars'])))
# Paths that do not exist
paths = ['.foo', 'boo', '.key1[0]', '.key2.bar', '.key5[99]', '.key5["moo"]']
for p in paths:
r.expect('JSON.GET', 'test', p).raiseError()
# TODO uncomment
# # Test failure in multi-path get
# r.expect('JSON.GET', 'test', '.bool', paths[0]).raiseError()
def testGetPartsOfValuesDocumentOneByOne(env):
"""Test type and value returned by JSON.GET"""
r = env
r.expect('JSON.SET', 'test', '.', json.dumps(docs['values'])).ok()
for k, v in iter(docs['values'].items()):
data = json.loads(r.execute_command('JSON.GET', 'test', '.{}'.format(k)))
r.assertEqual(str(type(data)), '<class \'{}\'>'.format(k), message=k)
r.assertEqual(data, v, message=k)
def testGetPartsOfValuesDocumentMultiple(env):
"""Test correctness of an object returned by JSON.GET"""
r = env
r.expect('JSON.SET', 'test', '.', json.dumps(docs['values'])).ok()
data = json.loads(r.execute_command('JSON.GET', 'test', *docs['values'].keys()))
r.assertEqual(data, docs['values'])
def testGetFormatting(env):
r = env
objects_to_test = [
{'obj': {'f': 'v'}},
{'arr': [0, 1]}
]
formatted_objects = [
'{{{newline}{indent}"obj":{space}{{{newline}{indent}{indent}"f":{space}"v"{newline}{indent}}}{newline}}}',
'{{{newline}{indent}"arr":{space}[{newline}{indent}{indent}0,{newline}{indent}{indent}1{newline}{indent}]{newline}}}'
]
for o in objects_to_test:
r.assertOk(r.execute_command('JSON.SET', list(o.keys()).pop(), '$', json.dumps(o)))
for space in ['', ' ', '\t', ' ']:
for indent in ['', ' ', '\t', ' ']:
for newline in ['', '\n', '\r\n']:
for o, f in zip(objects_to_test, formatted_objects):
res = r.execute_command('JSON.GET', list(o.keys()).pop(), 'INDENT', indent, 'NEWLINE', newline, 'SPACE', space)
r.assertEqual(res, f.format(newline=newline, space=space, indent=indent))
def testBackwardRDB(env):
env.skipOnCluster()
dbFileName = env.cmd('config', 'get', 'dbfilename')[1]
dbDir = env.cmd('config', 'get', 'dir')[1]
rdbFilePath = os.path.join(dbDir, dbFileName)
env.stop()
try:
os.unlink(rdbFilePath)
except OSError:
pass
filePath = os.path.join(JSON_PATH, 'backward.rdb')
os.symlink(filePath, rdbFilePath)
env.start()
r = env
data = json.loads(r.execute_command('JSON.GET', 'complex'))
r.assertEqual(data, {"a":{"b":[{"c":{"d":[1,'2'],"e":None}},True],"a":'a'},"b":1,"c":True,"d":None})
def testSetBSON(env):
r = env
bson = open(os.path.join(JSON_PATH , 'bson_bytes_1.bson'), 'rb').read()
r.assertOk(r.execute_command('JSON.SET', 'test', '.', bson, 'FORMAT', 'BSON'))
data = json.loads(r.execute_command('JSON.GET', 'test', *docs['values'].keys()))
def testMgetCommand(env):
"""Test REJSON.MGET command"""
r = env
# Set up a few keys
for d in range(0, 5):
key = 'doc:{}'.format(d)
r.cmd('DEL', key)
r.expect('JSON.SET', key, '.', json.dumps(docs['basic'])).ok()
# Test an MGET that succeeds on all keys
raw = r.execute_command('JSON.MGET', *['doc:{}'.format(d) for d in range(0, 5)] + ['.'])
r.assertEqual(len(raw), 5)
for d in range(0, 5):
key = 'doc:{}'.format(d)
r.assertEqual(json.loads(raw[d]), docs['basic'], d)
# Test an MGET that fails for one key
r.cmd('DEL', 'test')
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{"bool":false}'))
raw = r.execute_command('JSON.MGET', 'test', 'doc:0', 'foo', '.bool')
r.assertEqual(len(raw), 3)
r.assertFalse(json.loads(raw[0]))
r.assertTrue(json.loads(raw[1]))
r.assertEqual(raw[2], None)
# Test that MGET on missing path
raw = r.execute_command('JSON.MGET', 'doc:0', 'doc:1', '42isnotapath')
r.assertEqual(len(raw), 2)
r.assertEqual(raw[0], None)
r.assertEqual(raw[1], None)
# Test that MGET fails on path errors
r.cmd('DEL', 'test')
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{"bull":4.2}'))
raw = r.execute_command('JSON.MGET', 'doc:0', 'test', 'doc:1', '.bool')
r.assertEqual(len(raw), 3)
r.assertTrue(json.loads(raw[0]))
r.assertEqual(raw[1], None)
r.assertTrue(json.loads(raw[2]))
def testToggleCommand(env):
"""Test REJSON.TOGGLE command"""
r = env
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{"foo":true}'))
r.assertEqual(r.execute_command('JSON.TOGGLE','test','.foo'), 'false')
r.assertEqual(r.execute_command('JSON.TOGGLE','test','.foo'), 'true')
# Test Toggeling Empty Path
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{"foo":"bar"}'))
r.expect('JSON.TOGGLE', 'test', '.bar').raiseError()
# Test Toggeling Non Boolean
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{"foo":"bar"}'))
r.expect('JSON.TOGGLE','test','.foo').raiseError()
def testDelCommand(env):
"""Test REJSON.DEL command"""
r = env
# Test deleting an empty object
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{}'))
r.assertEqual(r.execute_command('JSON.DEL', 'test', '.'), 1)
assertNotExists(r, 'test')
# Test deleting an empty object
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{"foo": "bar", "baz": "qux"}'))
r.assertEqual(r.execute_command('JSON.DEL', 'test', '.baz'), 1)
r.assertEqual(r.execute_command('JSON.OBJLEN', 'test', '.'), 1)
r.assertIsNone(r.execute_command('JSON.TYPE', 'test', '.baz'))
r.assertEqual(r.execute_command('JSON.DEL', 'test', '.foo'), 1)
r.assertEqual(r.execute_command('JSON.OBJLEN', 'test', '.'), 0)
r.assertIsNone(r.execute_command('JSON.TYPE', 'test', '.foo'))
r.assertEqual(r.execute_command('JSON.TYPE', 'test', '.'), 'object')
# Test deleting some keys from an object
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{}'))
r.assertOk(r.execute_command('JSON.SET', 'test', '.foo', '"bar"'))
r.assertOk(r.execute_command('JSON.SET', 'test', '.baz', '"qux"'))
r.assertEqual(r.execute_command('JSON.DEL', 'test', '.baz'), 1)
r.assertEqual(r.execute_command('JSON.OBJLEN', 'test', '.'), 1)
r.assertIsNone(r.execute_command('JSON.TYPE', 'test', '.baz'))
r.assertEqual(r.execute_command('JSON.DEL', 'test', '.foo'), 1)
r.assertEqual(r.execute_command('JSON.OBJLEN', 'test', '.'), 0)
r.assertIsNone(r.execute_command('JSON.TYPE', 'test', '.foo'))
r.assertEqual(r.execute_command('JSON.TYPE', 'test', '.'), 'object')
# Test with an array
r.assertOk(r.execute_command('JSON.SET', 'test', '.foo', '"bar"'))
r.assertOk(r.execute_command('JSON.SET', 'test', '.baz', '"qux"'))
r.assertOk(r.execute_command('JSON.SET', 'test', '.arr', '[1.2,1,2]'))
r.assertEqual(r.execute_command('JSON.DEL', 'test', '.arr[1]'), 1)
r.assertEqual(r.execute_command('JSON.OBJLEN', 'test', '.'), 3)
r.assertEqual(r.execute_command('JSON.ARRLEN', 'test', '.arr'), 2)
r.assertEqual(r.execute_command('JSON.TYPE', 'test', '.arr'), 'array')
r.assertEqual(r.execute_command('JSON.DEL', 'test', '.arr'), 1)
r.assertEqual(r.execute_command('JSON.OBJLEN', 'test', '.'), 2)
r.assertEqual(r.execute_command('JSON.DEL', 'test', '.'), 1)
r.assertIsNone(r.execute_command('JSON.GET', 'test'))
def testObjectCRUD(env):
r = env
# Create an object
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{ }'))
r.assertEqual('object', r.execute_command('JSON.TYPE', 'test', '.'))
r.assertEqual(0, r.execute_command('JSON.OBJLEN', 'test', '.'))
raw = r.execute_command('JSON.GET', 'test')
data = json.loads(raw)
r.assertEqual(data, {})
# Test failure to access a non-existing element
r.expect('JSON.GET', 'test', '.foo').raiseError()
# Test setting a key in the oject
r.assertOk(r.execute_command('JSON.SET', 'test', '.foo', '"bar"'))
r.assertEqual(1, r.execute_command('JSON.OBJLEN', 'test', '.'))
raw = r.execute_command('JSON.GET', 'test', '.')
data = json.loads(raw)
r.assertEqual(data, {u'foo': u'bar'})
# Test replacing a key's value in the object
r.assertOk(r.execute_command('JSON.SET', 'test', '.foo', '"baz"'))
raw = r.execute_command('JSON.GET', 'test', '.')
data = json.loads(raw)
r.assertEqual(data, {u'foo': u'baz'})
# Test adding another key to the object
r.assertOk(r.execute_command('JSON.SET', 'test', '.boo', '"far"'))
r.assertEqual(2, r.execute_command('JSON.OBJLEN', 'test', '.'))
raw = r.execute_command('JSON.GET', 'test', '.')
data = json.loads(raw)
r.assertEqual(data, {u'foo': u'baz', u'boo': u'far'})
# Test deleting a key from the object
r.assertEqual(1, r.execute_command('JSON.DEL', 'test', '.foo'))
raw = r.execute_command('JSON.GET', 'test', '.')
data = json.loads(raw)
r.assertEqual(data, {u'boo': u'far'})
# Test replacing the object
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{"foo": "bar"}'))
raw = r.execute_command('JSON.GET', 'test', '.')
data = json.loads(raw)
r.assertEqual(data, {u'foo': u'bar'})
# Test deleting the object
r.assertEqual(1, r.execute_command('JSON.DEL', 'test', '.'))
r.assertIsNone(r.execute_command('JSON.GET', 'test', '.'))
# Test deleting with default (root) path
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{"foo": "bar"}'))
r.assertEqual(1, r.execute_command('JSON.DEL', 'test'))
r.assertIsNone(r.execute_command('JSON.GET', 'test', '.'))
def testClear(env):
"""Test JSON.CLEAR command"""
r = env
multi_content = r'{"n":42,"s":"42","arr":[{"n":44},"s",{"n":{"a":1,"b":2}},{"n2":{"x":3.02,"n":["to","be","cleared",4],"y":4.91}}]}'
r.expect('JSON.SET', 'test', '.', multi_content).ok()
# Test get multi results (using .. recursive descent)
r.expect('JSON.GET', 'test', '$..n').equal(r'[42,44,{"a":1,"b":2},["to","be","cleared",4]]')
# Make sure specific obj content exists before clear
obj_content = r'[{"a":1,"b":2}]'
obj_content_legacy = r'{"a":1,"b":2}'
r.expect('JSON.GET', 'test', '$.arr[2].n').equal(obj_content)
r.expect('JSON.GET', 'test', '.arr[2].n').equal(obj_content_legacy)
# Make sure specific arr content exists before clear
arr_content = r'[["to","be","cleared",4]]'
arr_content_legacy = r'["to","be","cleared",4]'
r.expect('JSON.GET', 'test', '$.arr[3].n2.n').equal(arr_content)
r.expect('JSON.GET', 'test', '.arr[3].n2.n').equal(arr_content_legacy)
# Clear obj and arr with specific paths
r.expect('JSON.CLEAR', 'test', '$.arr[2].n').equal(1)
r.expect('JSON.CLEAR', 'test', '$.arr[3].n2.n').equal(1)
# Fail clear on inappropriate path (not obj or arr)
r.expect('JSON.CLEAR', 'test', '$.arr[1]').equal(0)
# Make sure specific obj content was cleared
r.expect('JSON.GET', 'test', '$.arr[2].n').equal('[{}]')
r.expect('JSON.GET', 'test', '.arr[2].n').equal('{}')
# Make sure specific arr content was cleared
r.expect('JSON.GET', 'test', '$.arr[3].n2.n').equal('[[]]')
r.expect('JSON.GET', 'test', '.arr[3].n2.n').equal('[]')
# Make sure only appropriate content (obj and arr) was cleared - and that errors were printed for inappropriate content (string and numeric)
r.expect('JSON.GET', 'test', '$..n').equal('[42,44,{},[]]')
# Clear root
r.expect('JSON.SET', 'test', '.', r'{"n":42,"s":"42","arr":[{"n":44},"s",{"n":{"a":1,"b":2}},{"n2":{"x":3.02,"n":["to","be","cleared",4],"y":4.91}}]}') \
.ok()
# TODO: switch order of the following paths and expect .equals(2) when supporting multi-paths in JSON.CLEAR
r.expect('JSON.CLEAR', 'test', '$', '$.arr[2].n').equal(1)
r.expect('JSON.GET', 'test', '$').equal('[{}]')
r.expect('JSON.SET', 'test', '$', obj_content_legacy).ok()
r.expect('JSON.CLEAR', 'test').equal(1)
r.expect('JSON.GET', 'test', '$').equal('[{}]')
def testArrayCRUD(env):
"""Test JSON Array CRUDness"""
r = env
# Test creation of an empty array
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '[]'))
r.assertEqual('array', r.execute_command('JSON.TYPE', 'test', '.'))
r.assertEqual(0, r.execute_command('JSON.ARRLEN', 'test', '.'))
# Test failure of setting an element at different positons in an empty array
r.expect('JSON.SET', 'test', '[0]', 0).raiseError()
r.expect('JSON.SET', 'test', '[19]', 0).raiseError()
r.expect('JSON.SET', 'test', '[-1]', 0).raiseError()
# Test appending and inserting elements to the array
r.assertEqual(1, r.execute_command('JSON.ARRAPPEND', 'test', '.', 1))
r.assertEqual(1, r.execute_command('JSON.ARRLEN', 'test', '.'))
r.assertEqual(2, r.execute_command('JSON.ARRINSERT', 'test', '.', 0, -1))
r.assertEqual(2, r.execute_command('JSON.ARRLEN', 'test', '.'))
data = json.loads(r.execute_command('JSON.GET', 'test', '.'))
r.assertListEqual([-1, 1, ], data)
r.assertEqual(3, r.execute_command('JSON.ARRINSERT', 'test', '.', -1, 0))
data = json.loads(r.execute_command('JSON.GET', 'test', '.'))
r.assertListEqual([-1, 0, 1, ], data)
r.assertEqual(5, r.execute_command('JSON.ARRINSERT', 'test', '.', -3, -3, -2))
data = json.loads(r.execute_command('JSON.GET', 'test', '.'))
r.assertListEqual([-3, -2, -1, 0, 1, ], data)
r.assertEqual(7, r.execute_command('JSON.ARRAPPEND', 'test', '.', 2, 3))
data = json.loads(r.execute_command('JSON.GET', 'test', '.'))
r.assertListEqual([-3, -2, -1, 0, 1, 2, 3], data)
# Test replacing elements in the array
r.assertOk(r.execute_command('JSON.SET', 'test', '[0]', '"-inf"'))
r.assertOk(r.execute_command('JSON.SET', 'test', '[-1]', '"+inf"'))
r.assertOk(r.execute_command('JSON.SET', 'test', '[3]', 'null'))
data = json.loads(r.execute_command('JSON.GET', 'test', '.'))
r.assertListEqual([u'-inf', -2, -1, None, 1, 2, u'+inf'], data)
# Test deleting from the array
r.assertEqual(1, r.execute_command('JSON.DEL', 'test', '[1]'))
r.assertEqual(1, r.execute_command('JSON.DEL', 'test', '[-2]'))
data = json.loads(r.execute_command('JSON.GET', 'test', '.'))
r.assertListEqual([u'-inf', -1, None, 1, u'+inf'], data)
# TODO: Should not be needed once DEL works
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '["-inf", -1, null, 1, "+inf"]'))
# Test trimming the array
r.assertEqual(4, r.execute_command('JSON.ARRTRIM', 'test', '.', 1, -1))
data = json.loads(r.execute_command('JSON.GET', 'test', '.'))
r.assertListEqual([-1, None, 1, u'+inf'], data)
r.assertEqual(3, r.execute_command('JSON.ARRTRIM', 'test', '.', 0, -2))
data = json.loads(r.execute_command('JSON.GET', 'test', '.'))
r.assertListEqual([-1, None, 1], data)
r.assertEqual(1, r.execute_command('JSON.ARRTRIM', 'test', '.', 1, 1))
data = json.loads(r.execute_command('JSON.GET', 'test', '.'))
r.assertListEqual([None], data)
# Test replacing the array
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '[true]'))
r.assertEqual('array', r.execute_command('JSON.TYPE', 'test', '.'))
r.assertEqual(1, r.execute_command('JSON.ARRLEN', 'test', '.'))
r.assertEqual('true', r.execute_command('JSON.GET', 'test', '[0]'))
def testArrIndexCommand(env):
"""Test JSON.ARRINDEX command"""
r = env
r.assertOk(r.execute_command('JSON.SET', 'test',
'.', '{ "arr": [0, 1, 2, 3, 2, 1, 0] }'))
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 0), 0)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 3), 3)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 4), -1)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 0, 1), 6)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 0, -1), 6)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 0, 6), 6)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 0, 4, -0), 6)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 0, 5, -1), -1)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 2, -2, 6), -1)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', '"foo"'), -1)
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'test', '.arr', 4, '[4]'), 8)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 3), 3)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 2, 3), 5)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', '[4]'), 4)
def testArrInsertCommand(env):
"""Test JSON.ARRINSERT command"""
r = env
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{ "arr": [] }'))
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'test', '.arr', 0, '1'), 1)
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'test', '.arr', -1, '2'), 2)
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'test', '.arr', -2, '3'), 3)
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'test', '.arr', 3, '4'), 4)
r.assertEqual(r.execute_command('JSON.GET', 'test', '.arr'), "[3,2,1,4]")
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'test', '.arr', 1, '5'), 5)
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'test', '.arr', -2, '6'), 6)
r.assertEqual(r.execute_command('JSON.GET', 'test', '.arr'), "[3,5,2,6,1,4]")
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'test', '.arr', -3, '7', '{"A":"Z"}', '9'), 9)
r.assertEqual(r.execute_command('JSON.GET', 'test', '.arr'), '[3,5,2,7,{"A":"Z"},9,6,1,4]')
r.expect('JSON.ARRINSERT', 'test', '.arr', -10, '10').raiseError()
r.expect('JSON.ARRINSERT', 'test', '.arr', 10, '10').raiseError()
def testArrIndexMixCommand(env):
"""Test JSON.ARRINDEX command with mixed values"""
r = env
r.assertOk(r.execute_command('JSON.SET', 'test',
'.', '{ "arr": [0, 1, 2, 3, 2, 1, 0, {"val": 4}, {"val": 9}, [3,4,8], ["a", "b", 8]] }'))
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 0), 0)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 3), 3)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 4), -1)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 0, 1), 6)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 0, -5), 6)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 0, 6), 6)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 0, 4, -0), 6)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 0, 5, -1), 6)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 2, -2, 6), -1)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', '"foo"'), -1)
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'test', '.arr', 4, '[4]'), 12)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 3), 3)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 2, 3), 5)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', '[4]'), 4)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', '{\"val\":4}'), 8)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', '["a", "b", 8]'), 11)
def testArrTrimCommand(env):
"""Test JSON.ARRTRIM command"""
r = env
r.assertOk(r.execute_command('JSON.SET', 'test',
'.', '{ "arr": [0, 1, 2, 3, 2, 1, 0] }'))
r.assertEqual(r.execute_command('JSON.ARRTRIM', 'test', '.arr', 1, -2), 5)
r.assertListEqual(json.loads(r.execute_command(
'JSON.GET', 'test', '.arr')), [1, 2, 3, 2, 1])
r.assertEqual(r.execute_command('JSON.ARRTRIM', 'test', '.arr', 0, 99), 5)
r.assertListEqual(json.loads(r.execute_command(
'JSON.GET', 'test', '.arr')), [1, 2, 3, 2, 1])
r.assertEqual(r.execute_command('JSON.ARRTRIM', 'test', '.arr', 0, 2), 3)
r.assertListEqual(json.loads(r.execute_command(
'JSON.GET', 'test', '.arr')), [1, 2, 3])
r.assertEqual(r.execute_command('JSON.ARRTRIM', 'test', '.arr', 99, 2), 0)
r.assertListEqual(json.loads(r.execute_command('JSON.GET', 'test', '.arr')), [])
def testArrPopCommand(env):
"""Test JSON.ARRPOP command"""
r = env
r.assertOk(r.execute_command('JSON.SET', 'test',
'.', '[1,2,3,4,5,6,7,8,9]'))
r.assertEqual('9', r.execute_command('JSON.ARRPOP', 'test'))
r.assertEqual('8', r.execute_command('JSON.ARRPOP', 'test', '.'))
r.assertEqual('7', r.execute_command('JSON.ARRPOP', 'test', '.', -1))
r.assertEqual('5', r.execute_command('JSON.ARRPOP', 'test', '.', -2))
r.assertEqual('1', r.execute_command('JSON.ARRPOP', 'test', '.', 0))
r.assertEqual('4', r.execute_command('JSON.ARRPOP', 'test', '.', 2))
r.assertEqual('6', r.execute_command('JSON.ARRPOP', 'test', '.', 99))
r.assertEqual('2', r.execute_command('JSON.ARRPOP', 'test', '.', -99))
r.assertEqual('3', r.execute_command('JSON.ARRPOP', 'test'))
r.assertIsNone(r.execute_command('JSON.ARRPOP', 'test'))
r.assertIsNone(r.execute_command('JSON.ARRPOP', 'test', '.'))
r.assertIsNone(r.execute_command('JSON.ARRPOP', 'test', '.', 2))
def testArrPopErrors(env):
r = env
r.assertOk(r.execute_command('JSON.SET', 'test','.', '1'))
r.expect('JSON.ARRPOP', 'test').error().contains("not an array")
def testArrWrongChars(env):
r = env
r.assertOk(r.execute_command('JSON.SET', 'test','.', '{"arr":[1,2]}'))
r.expect('JSON.ARRINSERT', 'test', '.arr', 0, b'\x80abc').error().contains("Couldn't parse as UTF-8 string")
r.expect('JSON.ARRAPPEND', 'test', '.arr', b'\x80abc').error().contains("Couldn't parse as UTF-8 string")
def testArrTrimErrors(env):
r = env
r.assertOk(r.execute_command('JSON.SET', 'test','.', '1'))
r.expect('JSON.ARRTRIM', 'test', '.', '0', '1').error().contains("not an array")
def testTypeCommand(env):
"""Test JSON.TYPE command"""
r = env
for k, v in iter(docs['types'].items()):
r.cmd('DEL', 'test')
r.assertOk(r.execute_command('JSON.SET', 'test', '.', json.dumps(v)))
reply = r.execute_command('JSON.TYPE', 'test', '.')
r.assertEqual(reply, k)
def testLenCommands(env):
"""Test the JSON.ARRLEN, JSON.OBJLEN and JSON.STRLEN commands"""
r = env
# test that nothing is returned for empty keys
r.assertEqual(r.execute_command('JSON.ARRLEN', 'foo', '.bar'), None)
# test elements with valid lengths
r.assertOk(r.execute_command('JSON.SET', 'test', '.', json.dumps(docs['basic'])))
r.assertEqual(r.execute_command('JSON.STRLEN', 'test', '.string'), 12)
r.assertEqual(r.execute_command('JSON.OBJLEN', 'test', '.dict'), 3)
r.assertEqual(r.execute_command('JSON.ARRLEN', 'test', '.arr'), 6)
# test elements with undefined lengths
r.expect('JSON.ARRLEN', 'test', '.bool').raiseError()
r.expect('JSON.STRLEN', 'test', '.none').raiseError()
r.expect('JSON.OBJLEN', 'test', '.int').raiseError()
r.expect('JSON.STRLEN', 'test', '.num').raiseError()
# test a non existing key
r.expect('JSON.LEN', 'test', '.foo').raiseError()
# test an out of bounds index
r.expect('JSON.LEN', 'test', '.arr[999]').raiseError()
# test an infinite index
r.expect('JSON.LEN', 'test', '.arr[-inf]').raiseError()
def testObjKeysCommand(env):
"""Test JSON.OBJKEYS command"""
r = env
r.expect('JSON.SET', 'test', '.', json.dumps(docs['types'])).ok()
data = r.execute_command('JSON.OBJKEYS', 'test', '.')
r.assertEqual(len(data), len(docs['types']))
for k in data:
r.assertTrue(k in docs['types'], message=k)
# test a wrong type
r.expect('JSON.OBJKEYS', 'test', '.null').raiseError()
def testNumIncrCommand(env):
"""Test JSON.NUMINCRBY command"""
r = env
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{ "foo": 0, "bar": "baz" }'))
r.assertEqual('1', r.execute_command('JSON.NUMINCRBY', 'test', '.foo', 1))
r.assertEqual('1', r.execute_command('JSON.GET', 'test', '.foo'))
r.assertEqual('3', r.execute_command('JSON.NUMINCRBY', 'test', '.foo', 2))
r.assertEqual('3.5', r.execute_command('JSON.NUMINCRBY', 'test', '.foo', .5))
# test a wrong type
r.expect('JSON.NUMINCRBY', 'test', '.bar', 1).raiseError()
#
# # test a missing path
# r.expect('JSON.NUMINCRBY', 'test', '.fuzz', 1).raiseError()
#
# test issue #9
r.assertOk(r.execute_command('JSON.SET', 'num', '.', '0'))
r.assertEqual('1', r.execute_command('JSON.NUMINCRBY', 'num', '.', 1))
r.assertEqual('2.5', r.execute_command('JSON.NUMINCRBY', 'num', '.', 1.5))
# test issue 55
r.assertOk(r.execute_command('JSON.SET', 'foo', '.', '{"foo":0,"bar":42}'))
# Get the document once
r.execute_command('JSON.GET', 'foo', '.')
r.assertEqual('1', r.execute_command('JSON.NUMINCRBY', 'foo', 'foo', 1))
r.assertEqual('84', r.execute_command('JSON.NUMMULTBY', 'foo', 'bar', 2))
res = json.loads(r.execute_command('JSON.GET', 'foo', '.'))
r.assertEqual(1, res['foo'])
r.assertEqual(84, res['bar'])
def testStrCommands(env):
"""Test JSON.STRAPPEND and JSON.STRLEN commands"""
r = env
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '"foo"'))
r.assertEqual('string', r.execute_command('JSON.TYPE', 'test', '.'))
r.assertEqual(3, r.execute_command('JSON.STRLEN', 'test', '.'))
r.assertEqual(6, r.execute_command('JSON.STRAPPEND', 'test', '.', '"bar"'))
r.assertEqual('"foobar"', r.execute_command('JSON.GET', 'test', '.'))
def testRespCommand(env):
"""Test JSON.RESP command"""
r = env
r.assertOk(r.execute_command('JSON.SET', 'test', '.', 'null'))
# r.assertIsNone(r.execute_command('JSON.RESP', 'test'))
# r.assertOk(r.execute_command('JSON.SET', 'test', '.', 'true'))
# r.assertEquals('true', r.execute_command('JSON.RESP', 'test'))
# r.assertOk(r.execute_command('JSON.SET', 'test', '.', 42))
# r.assertEquals(42, r.execute_command('JSON.RESP', 'test'))
# r.assertOk(r.execute_command('JSON.SET', 'test', '.', 2.5))
# r.assertEquals('2.5', r.execute_command('JSON.RESP', 'test'))
# r.assertOk(r.execute_command('JSON.SET', 'test', '.', '"foo"'))
# r.assertEquals('foo', r.execute_command('JSON.RESP', 'test'))
# r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{"foo":"bar"}'))
# resp = r.execute_command('JSON.RESP', 'test')
# r.assertEqual(2, len(resp))
# r.assertEqual('{', resp[0])
# r.assertEqual(2, len(resp[1]))
# r.assertEqual('foo', resp[1][0])
# r.assertEqual('bar', resp[1][1])
# r.assertOk(r.execute_command('JSON.SET', 'test', '.', '[1,2]'))
# resp = r.execute_command('JSON.RESP', 'test')
# r.assertEqual(3, len(resp))
# r.assertEqual('[', resp[0])
# r.assertEqual(1, resp[1])
# r.assertEqual(2, resp[2])
# def testAllJSONCaseFiles(env):
# """Test using all JSON test case files"""
# r.maxDiff = None
# with r.redis() as r:
# r.client_setname(r._testMethodName)
# r.flushdb()
# for jsonfile in os.listdir(JSON_PATH):
# if jsonfile.endswith('.json'):
# path = '{}/{}'.format(JSON_PATH, jsonfile)
# with open(path) as f:
# value = f.read()
# if jsonfile.startswith('pass-'):
# r.assertOk(r.execute_command('JSON.SET', jsonfile, '.', value), path)
# elif jsonfile.startswith('fail-'):
# r.expect('JSON.SET', jsonfile, '.', value).raiseError()
# assertNotExists(r, jsonfile, path)
def testSetGetComparePassJSONCaseFiles(env):
"""Test setting, getting, saving and loading passable JSON test case files"""
r = env
for jsonfile in os.listdir(JSON_PATH):
r.maxDiff = None
if jsonfile.startswith('pass-') and jsonfile.endswith('.json') and jsonfile not in json_ignore:
path = '{}/{}'.format(JSON_PATH, jsonfile)
r.flush()
with open(path) as f:
value = f.read()
r.expect('JSON.SET', jsonfile, '.', value).ok()
d1 = json.loads(value)
for _ in r.retry_with_rdb_reload():
r.assertExists(jsonfile)
raw = r.execute_command('JSON.GET', jsonfile)
d2 = json.loads(raw)
r.assertEqual(d1, d2, message=path)
def testIssue_13(env):
"""https://github.com/RedisJSON/RedisJSON/issues/13"""
r = env
r.assertOk(r.execute_command('JSON.SET', 'test', '.', json.dumps(docs['simple'])))
# This shouldn't crash Redis
r.execute_command('JSON.GET', 'test', 'foo', 'foo')
def testIssue_74(env):
"""https://github.com/RedisJSON/RedisJSON2/issues/74"""
r = env
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{}'))
# This shouldn't crash Redis
r.expect('JSON.SET', 'test', '$a', '12').raiseError()
def testDoubleParse(env):
r = env
r.cmd('JSON.SET', 'dblNum', '.', '[1512060373.222988]')
res = r.cmd('JSON.GET', 'dblNum', '[0]')
r.assertEqual(1512060373.222988, float(res))
r.assertEqual('1512060373.222988', res)
def testIssue_80(env):
"""https://github.com/RedisJSON/RedisJSON2/issues/80"""
r = env
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '[{"code":"1"}, {"code":"2"}]'))
r.execute_command('JSON.GET', 'test', '.[?(@.code=="2")]')
# This shouldn't crash Redis
r.execute_command('JSON.GET', 'test', '$.[?(@.code=="2")]')
def testMultiPathResults(env):
env.expect("JSON.SET", "k", '$', '[1,2,3]').ok()
env.expect("JSON.GET", "k", '$[*]').equal('[1,2,3]')
env.expect("JSON.SET", "k", '$', '{"a":[1,2,3],"b":["c","d","e"],"c":"k"}').ok()
env.expect("JSON.GET", "k", '$.*[0,2]').equal('[1,3,"c","e"]')
# make sure legacy json path returns single result
env.expect("JSON.GET", "k", '.*[0,2]').equal('1')
# class CacheTestCase(BaseReJSONTest):
# @property
# def module_args(env):
# return ['CACHE', 'ON']
#
# def testLruCache(self):
# def cacheItems():
# return getCacheInfo(r)['items']
# def cacheBytes():
# return getCacheInfo(r)['bytes']
#
# r.cmd('JSON.SET', 'myDoc', '.', json.dumps({
# 'foo': 'fooValue',
# 'bar': 'barValue',
# 'baz': 'bazValue',
# 'key\\': 'escapedKey'
# }))
#
# res = r.cmd('JSON.GET', 'myDoc', 'foo')
# r.assertEqual(1, cacheItems())
# r.assertEqual('"fooValue"', res)
# r.assertEqual('"fooValue"', r.cmd('JSON.GET', 'myDoc', 'foo'))
# r.assertEqual('"fooValue"', r.cmd('JSON.GET', 'myDoc', '.foo'))
# # Get it again - item count should be the same
# r.cmd('JSON.GET', 'myDoc', 'foo')
# r.assertEqual(1, cacheItems())
#
# res = r.cmd('JSON.GET', 'myDoc', '.')
# # print repr(json.loads(res))
# r.assertEqual({u'bar': u'barValue', u'foo': u'fooValue', u'baz': u'bazValue', u'key\\': u'escapedKey'},
# json.loads(res))
#
# # Try to issue multiple gets
# r.cmd('JSON.GET', 'myDoc', '.foo')
# r.cmd('JSON.GET', 'myDoc', 'foo')
# r.cmd('JSON.GET', 'myDoc', '.bar')
# r.cmd('JSON.GET', 'myDoc', 'bar')
#
# res = r.cmd('JSON.GET', 'myDoc', '.foo', 'foo', '.bar', 'bar', '["key\\"]')
# # print repr(json.loads(res))
# r.assertEqual({u'.foo': u'fooValue', u'foo': u'fooValue', u'bar': u'barValue', u'.bar': u'barValue', u'["key\\"]': u'escapedKey'}, json.loads(res))
#
# r.cmd('JSON.DEL', 'myDoc', '.')
# r.assertEqual(0, cacheItems())
# r.assertEqual(0, cacheBytes())
#
# # Try with an array document
# r.cmd('JSON.SET', 'arr', '.', '[{}, 1,2,3,4]')
# r.assertEqual('{}', r.cmd('JSON.GET', 'arr', '[0]'))
# r.assertEqual(1, cacheItems())
# r.assertEqual('{}', r.cmd('JSON.GET', 'arr', '[0]'))
# r.assertEqual(1, cacheItems())
# r.assertEqual('{}', r.cmd('JSON.GET', 'arr', '[0]'))
#
# r.assertEqual('[{},1,2,3,4]', r.cmd('JSON.GET', 'arr', '.'))
# r.assertEqual(2, cacheItems())
#
# r.cmd('JSON.SET', 'arr', '[0].key', 'null')
# r.assertEqual(0, cacheItems())
#
# r.assertEqual('null', r.cmd('JSON.GET', 'arr', '[0].key'))
# # NULL is still not cached!
# r.assertEqual(0, cacheItems())
#
# # Try with a document that contains top level object with an array child
# r.cmd('JSON.DEL', 'arr', '.')
# r.cmd('JSON.SET', 'mixed', '.', '{"arr":[{},\"Hello\",2,3,null]}')
# r.assertEqual("\"Hello\"", r.cmd('JSON.GET', 'mixed', '.arr[1]'))
# r.assertEqual(1, cacheItems())
#
# r.cmd('JSON.ARRAPPEND', 'mixed', 'arr', '42')
# r.assertEqual(0, cacheItems())
# r.assertEqual("\"Hello\"", r.cmd('JSON.GET', 'mixed', 'arr[1]'))
#
# # Test cache eviction
# r.cmd('json._cacheinit', 4096, 20, 0)
# keys = ['json_{}'.format(x) for x in range(10)]
# paths = ['path_{}'.format(x) for x in xrange(100)]
# doc = json.dumps({ p: "some string" for p in paths})
#
# # 100k different path/key combinations
# for k in keys:
# r.cmd('JSON.SET', k, '.', doc)
#
# # Now get 'em back all
# for k in keys:
# for p in paths:
# r.cmd('JSON.GET', k, p)
# r.assertEqual(20, cacheItems())
#
# r.cmd('json._cacheinit')
# class NoCacheTestCase(BaseReJSONTest):
# def testNoCache(self):
# def cacheItems():
# return getCacheInfo(r)['items']
# def cacheBytes():
# return getCacheInfo(r)['bytes']
#
# r.cmd('JSON.SET', 'myDoc', '.', json.dumps({
# 'foo': 'fooValue',
# 'bar': 'barValue',
# 'baz': 'bazValue',
# 'key\\': 'escapedKey'
# }))
#
# res = r.cmd('JSON.GET', 'myDoc', 'foo')
# r.assertEqual(0, cacheItems())