Skip to content

Commit 8733934

Browse files
authored
Multipath: handle default arguments (RedisJSON#503)
* Multipath: arrlen - handle missing path * Multipath: handle/add tests for default args (strlen objkeys objlen resp get strappend arrpop)
1 parent dde2450 commit 8733934

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

src/commands.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ pub fn command_json_str_len<M: Manager>(
11611161
) -> RedisResult {
11621162
let mut args = args.into_iter().skip(1);
11631163
let key = args.next_arg()?;
1164-
let path = Path::new(args.next_str()?);
1164+
let path = Path::new(args.next_str().unwrap_or(JSON_ROOT_PATH_LEGACY));
11651165

11661166
let key = manager.open_key_read(ctx, &key)?;
11671167

@@ -1449,7 +1449,7 @@ pub fn command_json_arr_len<M: Manager>(
14491449
) -> RedisResult {
14501450
let mut args = args.into_iter().skip(1);
14511451
let key = args.next_arg()?;
1452-
let path = Path::new(args.next_str()?);
1452+
let path = Path::new(args.next_str().unwrap_or(JSON_ROOT_PATH_LEGACY));
14531453
let is_legacy = path.is_legacy();
14541454
let key = manager.open_key_read(ctx, &key)?;
14551455
let root = match key.get_value()? {
@@ -1666,7 +1666,7 @@ pub fn command_json_obj_keys<M: Manager>(
16661666
) -> RedisResult {
16671667
let mut args = args.into_iter().skip(1);
16681668
let key = args.next_arg()?;
1669-
let path = Path::new(args.next_str()?);
1669+
let path = Path::new(args.next_str().unwrap_or(JSON_ROOT_PATH_LEGACY));
16701670

16711671
let mut key = manager.open_key_read(ctx, &key)?;
16721672
if !path.is_legacy() {
@@ -1720,7 +1720,7 @@ pub fn command_json_obj_len<M: Manager>(
17201720
) -> RedisResult {
17211721
let mut args = args.into_iter().skip(1);
17221722
let key = args.next_arg()?;
1723-
let path = Path::new(args.next_str()?);
1723+
let path = Path::new(args.next_str().unwrap_or(JSON_ROOT_PATH_LEGACY));
17241724

17251725
let key = manager.open_key_read(ctx, &key)?;
17261726
if !path.is_legacy() {
@@ -1858,7 +1858,7 @@ pub fn command_json_resp<M: Manager>(
18581858

18591859
let key = args.next_arg()?;
18601860
let path = match args.next() {
1861-
None => Path::new(JSON_ROOT_PATH),
1861+
None => Path::new(JSON_ROOT_PATH_LEGACY),
18621862
Some(s) => Path::new(s.try_as_str()?),
18631863
};
18641864

tests/pytest/test_multi.py

+47-3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ def testForgetCommand(env):
9898

9999
def testSetAndGetCommands(env):
100100
"""Test REJSON.SET command"""
101+
"""Test REJSON.GET command"""
102+
101103
r = env
102104
# Test set and get on large nested key
103105
r.assertIsNone(r.execute_command('JSON.SET', 'doc1', '$', nested_large_key, 'XX'))
@@ -154,6 +156,12 @@ def testSetAndGetCommands(env):
154156
r.expect('JSON.GET', 'doc2', '.a', '.nested.b', '.back_in_nov', '.ttyl').raiseError()
155157
r.expect('JSON.GET', 'doc2', '.back_in_nov').raiseError()
156158

159+
# Test missing path (defaults to root)
160+
r.assertOk(r.execute_command('JSON.SET', 'doc1', '$', '"inizio"'))
161+
res = r.execute_command('JSON.GET', 'doc1')
162+
r.assertEqual(res, '"inizio"')
163+
164+
157165

158166
def testMGetCommand(env):
159167
"""Test REJSON.MGET command"""
@@ -310,8 +318,10 @@ def testStrAppendCommand(env):
310318
res = r.execute_command('JSON.GET', 'doc1', '$')
311319
r.assertEqual(res, '[{"a":"foo","nested1":{"a":"hellobar"},"nested2":{"a":31}}]')
312320

313-
# Test missing path
314-
r.expect('JSON.STRAPPEND', 'doc1', '"piu"').raiseError()
321+
# Test missing path (defaults to root)
322+
r.assertOk(r.execute_command('JSON.SET', 'doc1', '$', '"abcd"'))
323+
res = r.execute_command('JSON.STRAPPEND', 'doc1', '"piu"')
324+
r.assertEqual(res, 7)
315325

316326

317327
def testStrLenCommand(env):
@@ -342,6 +352,11 @@ def testStrLenCommand(env):
342352
res1 = r.execute_command('JSON.STRLEN', 'doc1', '..a')
343353
r.assertEqual(res1, 6)
344354

355+
# Test missing path
356+
r.assertOk(r.execute_command('JSON.SET', 'doc1', '$', '"kantele"'))
357+
res = r.execute_command('JSON.STRLEN', 'doc1')
358+
r.assertEqual(res, 7)
359+
345360

346361
def testArrAppendCommand(env):
347362
"""
@@ -455,6 +470,11 @@ def testArrLenCommand(env):
455470
# Test missing key
456471
r.assertEqual(r.execute_command('JSON.ARRLEN', 'non_existing_doc', '..a'), None)
457472

473+
# Test missing path (defaults to root)
474+
r.assertOk(r.execute_command('JSON.SET', 'doc1', '$', '[0, 1, 2, 3, 4]'))
475+
res = r.execute_command('JSON.ARRLEN', 'doc1')
476+
r.assertEqual(res, 5)
477+
458478
def testArrPopCommand(env):
459479
"""
460480
Test REJSON.ARRPOP command
@@ -489,14 +509,25 @@ def testArrPopCommand(env):
489509
res = r.execute_command('JSON.GET', 'doc1', '$')
490510
r.assertEqual(json.loads(res), [{"a": [], "nested1": {"a": ["hello", "world"]}, "nested2": {"a": 31}}])
491511
# Test single
492-
res = r.execute_command('JSON.ARRPOP', 'doc1', '.nested1.a', -2, '"baz"')
512+
res = r.execute_command('JSON.ARRPOP', 'doc1', '.nested1.a', -2)
493513
r.assertEqual(res, '"hello"')
494514
res = r.execute_command('JSON.GET', 'doc1', '$')
495515
r.assertEqual(json.loads(res), [{"a": [], "nested1": {"a": ["world"]}, "nested2": {"a": 31}}])
496516

497517
# Test missing key
498518
r.expect('JSON.ARRPOP', 'non_existing_doc', '..a').raiseError()
499519

520+
# Test default path/index
521+
r.assertOk(r.execute_command('JSON.SET', 'doc1', '$', '[0, 1, 2]'))
522+
res = r.execute_command('JSON.ARRPOP', 'doc1')
523+
r.assertEqual(res, '2')
524+
res = r.execute_command('JSON.ARRPOP', 'doc1', '$')
525+
r.assertEqual(res, ['1'])
526+
res = r.execute_command('JSON.ARRPOP', 'doc1', '.')
527+
r.assertEqual(res, '0')
528+
529+
530+
500531
def testArrTrimCommand(env):
501532
"""
502533
Test REJSON.ARRTRIM command
@@ -565,6 +596,10 @@ def testObjKeysCommand(env):
565596
# Test missing key
566597
r.expect('JSON.OBJKEYS', 'doc1', '$.nowhere').raiseError()
567598

599+
# Test default path
600+
res = r.execute_command('JSON.OBJKEYS', 'doc1')
601+
r.assertEqual(res, ["nested1", "a", "nested2"])
602+
568603

569604
def testObjLenCommand(env):
570605
"""Test JSON.OBJLEN command"""
@@ -600,6 +635,10 @@ def testObjLenCommand(env):
600635
# Test missing path
601636
r.expect('JSON.OBJLEN', 'doc1', '.nowhere').raiseError()
602637

638+
# Test default path
639+
res = r.execute_command('JSON.OBJLEN', 'doc1')
640+
r.assertEqual(res, 3)
641+
603642

604643
def load_types_data(nested_key_name):
605644
types_data = {
@@ -781,6 +820,11 @@ def testRespCommand(env):
781820
res = r.execute_command('JSON.RESP', 'doc1', '.L1.a')
782821
r.assertEqual([res], resSingle)
783822

823+
# Test default path
824+
r.assertOk(r.execute_command('JSON.SET', 'doc1', '$', '[[1],[2]]'))
825+
res = r.execute_command('JSON.RESP', 'doc1')
826+
r.assertEqual(res, ['[', ['[', 1], ['[', 2]])
827+
784828
def testArrIndexCommand(env):
785829
"""Test JSON.ARRINDEX command"""
786830
r = env

0 commit comments

Comments
 (0)