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
26 changes: 25 additions & 1 deletion rejson/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ def __init__(self, encoder=None, decoder=None, *args, **kwargs):
# Set the module commands' callbacks
MODULE_CALLBACKS = {
'JSON.DEL': long,
'JSON.CLEAR': long,
'JSON.GET': self._decode,
'JSON.MGET': bulk_of_jsons(self._decode),
'JSON.SET': lambda r: r and nativestr(r) == 'OK',
'JSON.NUMINCRBY': self._decode,
'JSON.NUMMULTBY': self._decode,
'JSON.TOGGLE': lambda b: b == 'true',
'JSON.STRAPPEND': long,
'JSON.STRLEN': long,
'JSON.ARRAPPEND': long,
Expand All @@ -65,10 +67,11 @@ def __init__(self, encoder=None, decoder=None, *args, **kwargs):
'JSON.ARRPOP': self._decode,
'JSON.ARRTRIM': long,
'JSON.OBJLEN': long,
'JSON.DEBUG': long,
}
for k, v in six.iteritems(MODULE_CALLBACKS):
self.set_response_callback(k, v)

def setEncoder(self, encoder):
"""
Sets the client's encoder
Expand Down Expand Up @@ -99,6 +102,14 @@ def jsondel(self, name, path=Path.rootPath()):
"""
return self.execute_command('JSON.DEL', name, str_path(path))

def jsonclear(self, name, path=Path.rootPath()):
"""
Emptying arrays and objects (to have zero slots/keys without
deleting the array/object) returning the count of cleared paths
(ignoring non-array and non-objects paths)
"""
return self.execute_command('JSON.CLEAR', name, str_path(path))

def jsonget(self, name, *args, no_escape=False):
"""
Get the object stored as a JSON value at key ``name``
Expand Down Expand Up @@ -166,6 +177,13 @@ def jsonnummultby(self, name, path, number):
"""
return self.execute_command('JSON.NUMMULTBY', name, str_path(path), self._encode(number))

def jsontoggle(self, name, path=Path.rootPath()):
"""
Toggle boolean value under ``path`` at key ``name``,
Returning the new value.
"""
return self.execute_command('JSON.TOGGLE', name, str_path(path))

def jsonstrappend(self, name, string, path=Path.rootPath()):
"""
Appends to the string JSON value under ``path`` at key ``name`` the
Expand Down Expand Up @@ -243,6 +261,12 @@ def jsonobjlen(self, name, path=Path.rootPath()):
"""
return self.execute_command('JSON.OBJLEN', name, str_path(path))

def jsondebugmemory(self, name, path=Path.rootPath()):
"""
Returns the memory usage in bytes of a value under ``path`` from key ``name``.
"""
return self.execute_command("JSON.DEBUG", "MEMORY", name, str_path(path))

def pipeline(self, transaction=True, shard_hint=None):
"""
Return a new pipeline object that can queue multiple commands for
Expand Down
27 changes: 26 additions & 1 deletion tests/test_rejson.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import redis
import six
import json
import unittest
Expand Down Expand Up @@ -36,7 +37,7 @@ def testJSONSetGetDelNonAsciiShouldSucceed(self):
def testJSONSetExistentialModifiersShouldSucceed(self):
"Test JSONSet's NX/XX flags"

obj = { 'foo': 'bar' }
obj = {'foo': 'bar'}
self.assertTrue(rj.jsonset('obj', Path.rootPath(), obj))

# Test that flags prevent updates when conditions are unmet
Expand All @@ -60,6 +61,13 @@ def testMGetShouldSucceed(self):
e = [1, 2]
self.assertListEqual(e, r)

def testClearShouldSucceed(self):
"Test JSONClear"

rj.jsonset('arr', Path.rootPath(), [0, 1, 2, 3, 4])
self.assertEqual(1, rj.jsonclear('arr', Path.rootPath()))
self.assertEqual([], rj.jsonget('arr'))

def testTypeShouldSucceed(self):
"Test JSONType"

Expand All @@ -82,6 +90,17 @@ def testNumMultByShouldSucceed(self):
self.assertEqual(5, rj.jsonnummultby('num', Path.rootPath(), 2.5))
self.assertEqual(2.5, rj.jsonnummultby('num', Path.rootPath(), 0.5))

def testToggleShouldSucceed(self):
"Test JSONToggle"

rj.jsonset('bool', Path.rootPath(), False)
self.assertTrue(rj.jsontoggle('bool', Path.rootPath()))
self.assertFalse(rj.jsontoggle('bool', Path.rootPath()))
# check non-boolean value
rj.jsonset('num', Path.rootPath(), 1)
with self.assertRaises(redis.exceptions.ResponseError):
rj.jsontoggle('num', Path.rootPath())

def testStrAppendShouldSucceed(self):
"Test JSONStrAppend"

Expand Down Expand Up @@ -161,6 +180,12 @@ def testObjLenShouldSucceed(self):
rj.jsonset('obj', Path.rootPath(), obj)
self.assertEqual(len(obj), rj.jsonobjlen('obj', Path.rootPath()))

def testDebugMemoryShouldSucceed(self):
"Test JSONDebug"

rj.jsonset('str', Path.rootPath(), 'foo')
self.assertEqual(24, rj.jsondebugmemory('str', Path.rootPath()))

def testPipelineShouldSucceed(self):
"Test pipeline"

Expand Down