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: 11 additions & 1 deletion src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,14 +782,24 @@ pub fn command_json_mget<M: Manager>(
let path = Path::new(path.try_as_str()?);
let keys = &args[1..args.len() - 1];

let to_string =
|doc: &M::V| KeyValue::new(doc).to_string_multi(path.get_path(), None, None, None);
let to_string_legacy =
|doc: &M::V| KeyValue::new(doc).to_string(path.get_path(), Format::JSON);
let is_legacy = path.is_legacy();

let results: Result<Vec<RedisValue>, RedisError> = keys
.iter()
.map(|key| {
manager
.open_key_read(ctx, key)?
.get_value()?
.map(|doc| {
KeyValue::new(doc).to_string_multi(path.get_path(), None, None, None)
if !is_legacy {
to_string(doc)
} else {
to_string_legacy(doc)
}
})
.transpose()
.map_or_else(|_| Ok(RedisValue::Null), |v| Ok(v.into()))
Expand Down
12 changes: 6 additions & 6 deletions tests/pytest/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,30 +308,30 @@ def testMgetCommand(env):
r.assertEqual(len(raw), 5)
for d in range(0, 5):
key = 'doc:{}'.format(d)
r.assertEqual(json.loads(raw[d]), [docs['basic']], d)
r.assertEqual(json.loads(raw[d]), docs['basic'], d)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this reverts the tests changes?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the previous change ignored the previous behavior. Since the path here starts with a ., the old behavior is now kept.


# 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])[0])
r.assertTrue(json.loads(raw[1])[0])
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], '[]')
r.assertEqual(raw[1], '[]')
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], '[]')
r.assertEqual(raw[1], None)
r.assertTrue(json.loads(raw[2]))

def testToggleCommand(env):
Expand Down
32 changes: 28 additions & 4 deletions tests/pytest/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ def testMGetCommand(env):
"""Test REJSON.MGET command"""
r = env
# Test mget with multi paths
r.assertOk(r.execute_command('JSON.SET', 'doc1', '$', '{"a":1, "b": 2, "nested": {"a": 3}, "c": null, "nested2": {"a": null}} '))
r.assertOk(r.execute_command('JSON.SET', 'doc2', '$', '{"a":4, "b": 5, "nested": {"a": 6}, "c": null, "nested2": {"a": [null]}}'))
r.assertOk(r.execute_command('JSON.SET', 'doc1', '$', '{"a":1, "b": 2, "nested1": {"a": 3}, "c": null, "nested2": {"a": null}}'))
r.assertOk(r.execute_command('JSON.SET', 'doc2', '$', '{"a":4, "b": 5, "nested3": {"a": 6}, "c": null, "nested4": {"a": [null]}}'))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this change?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanted a path that would match only one of the keys (for the missing path/key scenario)

@MeirShpilraien MeirShpilraien Oct 31, 2021 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So why change an existing test and not add a new one?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This had no impact on existing tests (just names of internal paths)
And it is now used in a new test

# Compare also to single JSON.GET
res1 = r.execute_command('JSON.GET', 'doc1', '$..a')
res2 = r.execute_command('JSON.GET', 'doc2', '$..a')
Expand All @@ -172,14 +172,38 @@ def testMGetCommand(env):
r.assertEqual([res1], res)
# Test mget with multi path
res = r.execute_command('JSON.MGET', 'doc1', 'doc2', '$..a')
r.assertEqual(res, [res1,res2])
r.assertEqual(res, [res1, res2])

# Test missing key
# Test missing key/path
res = r.execute_command('JSON.MGET', 'doc1', 'missing_doc', '$..a')
r.assertEqual(res, [res1, None])
res = r.execute_command('JSON.MGET', 'doc1', 'doc2', 'missing_doc', '$.nested1.a')
r.assertEqual(res, [json.dumps([json.loads(res1)[1]]), '[]', None])
res = r.execute_command('JSON.MGET', 'missing_doc1', 'missing_doc2', '$..a')
r.assertEqual(res, [None, None])

# Test missing path
res = r.execute_command('JSON.MGET', 'doc1', 'missing_doc2', '$..niente')
r.assertEqual(res, ['[]', None])

# Test legacy (for each path only the first value is returned as a json string)
# Test mget with single path
res = r.execute_command('JSON.MGET', 'doc1', '..a')
r.assertEqual(res, [json.dumps(json.loads(res1)[0])])
# Test mget with multi path
res = r.execute_command('JSON.MGET', 'doc1', 'doc2', '..a')
r.assertEqual(res, [json.dumps(json.loads(res1)[0]), json.dumps(json.loads(res2)[0])])

# Test missing key/path
res = r.execute_command('JSON.MGET', 'doc1', 'doc2', 'missing_doc', '.nested1.a')
r.assertEqual(res, [json.dumps(json.loads(res1)[1]), None, None])
res = r.execute_command('JSON.MGET', 'missing_doc1', 'missing_doc2', '..a')
r.assertEqual(res, [None, None])

# Test missing path
res = r.execute_command('JSON.MGET', 'doc1', 'missing_doc2', '.niente')
r.assertEqual(res, [None, None])


def testNumByCommands(env):
"""
Expand Down