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
12 changes: 8 additions & 4 deletions src/redisjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,15 @@ impl RedisJSON {
let res = self.get_first(&path.fixed)?;
Ok(self.serialize_json(res, &indent, &newline, &space).into())
} else {
let values = Value::Array(self.get_values(&path.fixed)?.drain(..).fold(
Vec::new(),
|mut acc, value| {
acc.push((*value).clone());
acc
},
));
Ok(self
.get_values(&path.fixed)?
.drain(..)
.map(|v| self.serialize_json(v, &indent, &newline, &space))
.collect::<Vec<String>>()
.serialize_json(&values, &indent, &newline, &space)
.into())
}
}
Expand Down
44 changes: 25 additions & 19 deletions tests/pytest/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,19 +430,22 @@ def testClear(env):
"""Test JSON.CLEAR command"""

r = env
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()
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)
# TODO: Enable when supporting multi results (and not only the first)
#r.expect('JSON.GET', 'test', '$..n').equal([42,44,{"a": 1,"b": 2},["to","be","cleared",4]])
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}'
r.expect('JSON.GET', 'test', '$.arr[2].n').equal([obj_content])
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]'
r.expect('JSON.GET', 'test', '$.arr[3].n2.n').equal([arr_content])
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)
Expand All @@ -452,22 +455,25 @@ def testClear(env):
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('[{}]')
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('[[]]')
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)
# TODO: Enable when supporting multi results (and not only the first)
#r.expect('JSON.GET', 'test', '$..n').equal([42, 44, {}, []])
r.expect('JSON.GET', 'test', '$..n').equal('[42,44,{},[]]')

# Clear root
# TODO: switch order of the following paths and expect .equals(2) when supporting multi-paths
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.GET', 'test', '$').equal('[{}]')

r.expect('JSON.SET', 'test', '$', obj_content).ok()
r.expect('JSON.SET', 'test', '$', obj_content_legacy).ok()
r.expect('JSON.CLEAR', 'test').equal(1)
r.expect('JSON.GET', 'test', '$').equal(['{}'])
r.expect('JSON.GET', 'test', '$').equal('[{}]')

def testArrayCRUD(env):
"""Test JSON Array CRUDness"""
Expand Down Expand Up @@ -831,9 +837,9 @@ def testIssue_80(env):

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"'])
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')
Expand Down
2 changes: 1 addition & 1 deletion tests/pytest/test_keyspace_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_keyspace_set(env):
env.assertEqual(8, r.execute_command('JSON.STRLEN', 'test_key', '$.foo'))
env.assertEqual(None, pubsub.get_message())

env.assertEqual(['"gogototo"'], r.execute_command('JSON.GET', 'test_key', '$.foo'))
env.assertEqual('["gogototo"]', r.execute_command('JSON.GET', 'test_key', '$.foo'))
env.assertEqual(None, pubsub.get_message())

env.assertEqual(['"gogototo"', None], r.execute_command('JSON.MGET', 'test_key', 'test_key1', '$.foo'))
Expand Down