Skip to content

fixes issues #44 #45 setDecoder not working, TypeError when reJSON returns None #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 3, 2021
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
16 changes: 6 additions & 10 deletions rejson/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class Client(StrictRedis):
_encoder = None
_encode = None
_decoder = None
_decode = None

def __init__(self, encoder=None, decoder=None, *args, **kwargs):
"""
Expand Down Expand Up @@ -90,7 +89,9 @@ def setDecoder(self, decoder):
self._decoder = json.JSONDecoder()
else:
self._decoder = decoder
self._decode = self._decoder.decode

def _decode(self, s, *args, **kwargs):
return self._decoder.decode(s, *args, **kwargs) if s is not None else None

def jsondel(self, name, path=Path.rootPath()):
"""
Expand All @@ -113,14 +114,9 @@ def jsonget(self, name, *args, no_escape=False):

else:
for p in args:
pieces.append(str_path(p))

# Handle case where key doesn't exist. The JSONDecoder would raise a
# TypeError exception since it can't decode None
try:
return self.execute_command('JSON.GET', *pieces)
except TypeError:
return None
pieces.append(str_path(p))

return self.execute_command('JSON.GET', *pieces)

def jsonmget(self, path, *args):
"""
Expand Down
18 changes: 17 additions & 1 deletion tests/test_rejson.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ def testPipelineShouldSucceed(self):
p = rj.pipeline()
p.jsonset('foo', Path.rootPath(), 'bar')
p.jsonget('foo')
p.jsonget('bar')
p.jsondel('foo')
p.exists('foo')
self.assertListEqual([True, 'bar', 1, False], p.execute())
self.assertListEqual([True, 'bar', None, 1, False], p.execute())

def testCustomEncoderDecoderShouldSucceed(self):
"Test a custom encoder and decoder"
Expand Down Expand Up @@ -209,6 +210,21 @@ def decode(self, obj):
# Check the custom encoder
self.assertTrue(rj.jsonset('cus', Path.rootPath(),
CustomClass('foo', 'bar')))
# Check the custom decoder
obj = rj.jsonget('cus', Path.rootPath())
self.assertIsNotNone(obj)
self.assertEqual(CustomClass, obj.__class__)
self.assertEqual('foo', obj.key)
self.assertEqual('bar', obj.val)

# Test resetting the decoder after the client have been created
rj.setDecoder(json.JSONDecoder())
obj = rj.jsonget('cus', Path.rootPath())
self.assertIsNotNone(obj)
self.assertNotEqual(CustomClass, obj.__class__)

# Test setting the decoder after the client have been created
rj.setDecoder(TestDecoder())
obj = rj.jsonget('cus', Path.rootPath())
self.assertIsNotNone(obj)
self.assertEqual(CustomClass, obj.__class__)
Expand Down