Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ pub fn command_json_str_len<M: Manager>(
) -> RedisResult {
let mut args = args.into_iter().skip(1);
let key = args.next_arg()?;
let path = Path::new(args.next_str()?);
let path = Path::new(args.next_str().unwrap_or(JSON_ROOT_PATH_LEGACY));

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

Expand Down Expand Up @@ -1449,7 +1449,7 @@ pub fn command_json_arr_len<M: Manager>(
) -> RedisResult {
let mut args = args.into_iter().skip(1);
let key = args.next_arg()?;
let path = Path::new(args.next_str()?);
let path = Path::new(args.next_str().unwrap_or(JSON_ROOT_PATH_LEGACY));
let is_legacy = path.is_legacy();
let key = manager.open_key_read(ctx, &key)?;
let root = match key.get_value()? {
Expand Down Expand Up @@ -1666,7 +1666,7 @@ pub fn command_json_obj_keys<M: Manager>(
) -> RedisResult {
let mut args = args.into_iter().skip(1);
let key = args.next_arg()?;
let path = Path::new(args.next_str()?);
let path = Path::new(args.next_str().unwrap_or(JSON_ROOT_PATH_LEGACY));

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

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

let key = args.next_arg()?;
let path = match args.next() {
None => Path::new(JSON_ROOT_PATH),
None => Path::new(JSON_ROOT_PATH_LEGACY),
Some(s) => Path::new(s.try_as_str()?),
};

Expand Down
50 changes: 47 additions & 3 deletions tests/pytest/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def testForgetCommand(env):

def testSetAndGetCommands(env):
"""Test REJSON.SET command"""
"""Test REJSON.GET command"""

r = env
# Test set and get on large nested key
r.assertIsNone(r.execute_command('JSON.SET', 'doc1', '$', nested_large_key, 'XX'))
Expand Down Expand Up @@ -154,6 +156,12 @@ def testSetAndGetCommands(env):
r.expect('JSON.GET', 'doc2', '.a', '.nested.b', '.back_in_nov', '.ttyl').raiseError()
r.expect('JSON.GET', 'doc2', '.back_in_nov').raiseError()

# Test missing path (defaults to root)
r.assertOk(r.execute_command('JSON.SET', 'doc1', '$', '"inizio"'))
res = r.execute_command('JSON.GET', 'doc1')
r.assertEqual(res, '"inizio"')



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

# Test missing path
r.expect('JSON.STRAPPEND', 'doc1', '"piu"').raiseError()
# Test missing path (defaults to root)
r.assertOk(r.execute_command('JSON.SET', 'doc1', '$', '"abcd"'))
res = r.execute_command('JSON.STRAPPEND', 'doc1', '"piu"')
r.assertEqual(res, 7)


def testStrLenCommand(env):
Expand Down Expand Up @@ -342,6 +352,11 @@ def testStrLenCommand(env):
res1 = r.execute_command('JSON.STRLEN', 'doc1', '..a')
r.assertEqual(res1, 6)

# Test missing path
r.assertOk(r.execute_command('JSON.SET', 'doc1', '$', '"kantele"'))
res = r.execute_command('JSON.STRLEN', 'doc1')
r.assertEqual(res, 7)


def testArrAppendCommand(env):
"""
Expand Down Expand Up @@ -455,6 +470,11 @@ def testArrLenCommand(env):
# Test missing key
r.assertEqual(r.execute_command('JSON.ARRLEN', 'non_existing_doc', '..a'), None)

# Test missing path (defaults to root)
r.assertOk(r.execute_command('JSON.SET', 'doc1', '$', '[0, 1, 2, 3, 4]'))
res = r.execute_command('JSON.ARRLEN', 'doc1')
r.assertEqual(res, 5)

def testArrPopCommand(env):
"""
Test REJSON.ARRPOP command
Expand Down Expand Up @@ -489,14 +509,25 @@ def testArrPopCommand(env):
res = r.execute_command('JSON.GET', 'doc1', '$')
r.assertEqual(json.loads(res), [{"a": [], "nested1": {"a": ["hello", "world"]}, "nested2": {"a": 31}}])
# Test single
res = r.execute_command('JSON.ARRPOP', 'doc1', '.nested1.a', -2, '"baz"')
res = r.execute_command('JSON.ARRPOP', 'doc1', '.nested1.a', -2)
Comment thread
MeirShpilraien marked this conversation as resolved.
Resolved
r.assertEqual(res, '"hello"')
res = r.execute_command('JSON.GET', 'doc1', '$')
r.assertEqual(json.loads(res), [{"a": [], "nested1": {"a": ["world"]}, "nested2": {"a": 31}}])

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

# Test default path/index
r.assertOk(r.execute_command('JSON.SET', 'doc1', '$', '[0, 1, 2]'))
res = r.execute_command('JSON.ARRPOP', 'doc1')
r.assertEqual(res, '2')
res = r.execute_command('JSON.ARRPOP', 'doc1', '$')
r.assertEqual(res, ['1'])
res = r.execute_command('JSON.ARRPOP', 'doc1', '.')
r.assertEqual(res, '0')



def testArrTrimCommand(env):
"""
Test REJSON.ARRTRIM command
Expand Down Expand Up @@ -565,6 +596,10 @@ def testObjKeysCommand(env):
# Test missing key
r.expect('JSON.OBJKEYS', 'doc1', '$.nowhere').raiseError()

# Test default path
res = r.execute_command('JSON.OBJKEYS', 'doc1')
r.assertEqual(res, ["nested1", "a", "nested2"])


def testObjLenCommand(env):
"""Test JSON.OBJLEN command"""
Expand Down Expand Up @@ -600,6 +635,10 @@ def testObjLenCommand(env):
# Test missing path
r.expect('JSON.OBJLEN', 'doc1', '.nowhere').raiseError()

# Test default path
res = r.execute_command('JSON.OBJLEN', 'doc1')
r.assertEqual(res, 3)


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

# Test default path
r.assertOk(r.execute_command('JSON.SET', 'doc1', '$', '[[1],[2]]'))
res = r.execute_command('JSON.RESP', 'doc1')
r.assertEqual(res, ['[', ['[', 1], ['[', 2]])

def testArrIndexCommand(env):
"""Test JSON.ARRINDEX command"""
r = env
Expand Down