Skip to content

Commit 0402041

Browse files
committed
Fixes reply handlers and completes test coverage
1 parent b703804 commit 0402041

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

rejson/client.py

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,6 @@ def str_path(p):
1212
else:
1313
return p
1414

15-
def float_or_long(n):
16-
"Return a number from a Redis reply"
17-
if isinstance(n, six.string_types):
18-
return float(n)
19-
else:
20-
return long(n)
21-
22-
def long_or_none(r):
23-
"Return a long or None from a Redis reply"
24-
if r:
25-
return long(r)
26-
return r
27-
28-
def json_or_none(d):
29-
"Return a deserialized JSON object or None"
30-
def _f(r):
31-
if r:
32-
return d(r)
33-
return r
34-
return _f
35-
3615
def bulk_of_jsons(d):
3716
"Replace serialized JSON values with objects in a bulk array response (list)"
3817
def _f(b):
@@ -75,20 +54,20 @@ def __init__(self, encoder=None, decoder=None, *args, **kwargs):
7554
# Set the module commands' callbacks
7655
MODULE_CALLBACKS = {
7756
'JSON.DEL': long,
78-
'JSON.GET': json_or_none(self._decode),
57+
'JSON.GET': self._decode,
7958
'JSON.MGET': bulk_of_jsons(self._decode),
8059
'JSON.SET': lambda r: r and nativestr(r) == 'OK',
81-
'JSON.NUMINCRBY': float_or_long,
82-
'JSON.NUMMULTBY': float_or_long,
83-
'JSON.STRAPPEND': long_or_none,
84-
'JSON.STRLEN': long_or_none,
85-
'JSON.ARRAPPEND': long_or_none,
86-
'JSON.ARRINDEX': long_or_none,
87-
'JSON.ARRINSERT': long_or_none,
88-
'JSON.ARRLEN': long_or_none,
89-
'JSON.ARRPOP': json_or_none(self._decode),
90-
'JSON.ARRTRIM': long_or_none,
91-
'JSON.OBJLEN': long_or_none,
60+
'JSON.NUMINCRBY': self._decode,
61+
'JSON.NUMMULTBY': self._decode,
62+
'JSON.STRAPPEND': long,
63+
'JSON.STRLEN': long,
64+
'JSON.ARRAPPEND': long,
65+
'JSON.ARRINDEX': long,
66+
'JSON.ARRINSERT': long,
67+
'JSON.ARRLEN': long,
68+
'JSON.ARRPOP': self._decode,
69+
'JSON.ARRTRIM': long,
70+
'JSON.OBJLEN': long,
9271
}
9372
for k, v in six.iteritems(MODULE_CALLBACKS):
9473
self.set_response_callback(k, v)

tests/test_rejson.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ def testJSONSetGetDelShouldSucceed(self):
2424
self.assertEqual(1, rj.jsondel('foo'))
2525
self.assertFalse(rj.exists('foo'))
2626

27+
def testJSONSetExistentialModifiersShouldSucceed(self):
28+
"Test JSONSet's NX/XX flags"
29+
30+
obj = { 'foo': 'bar' }
31+
self.assertTrue(rj.jsonset('obj', Path.rootPath(), obj))
32+
33+
# Test that flags prevent updates when conditions are unmet
34+
self.assertFalse(rj.jsonset('obj', Path('foo'), 'baz', nx=True))
35+
self.assertFalse(rj.jsonset('obj', Path('qaz'), 'baz', xx=True))
36+
37+
# Test that flags allow updates when conditions are met
38+
self.assertTrue(rj.jsonset('obj', Path('foo'), 'baz', xx=True))
39+
self.assertTrue(rj.jsonset('obj', Path('qaz'), 'baz', nx=True))
40+
41+
# Test that flags are mutually exlusive
42+
with self.assertRaises(Exception) as context:
43+
rj.jsonset('obj', Path('foo'), 'baz', nx=True, xx=True)
44+
2745
def testMGetShouldSucceed(self):
2846
"Test JSONMGet"
2947

0 commit comments

Comments
 (0)