Skip to content

Fixes / Python 3 compatibility #5

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

Closed
wants to merge 8 commits into from
Closed
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
8 changes: 4 additions & 4 deletions rejson/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from sys import stdout
import six
import json
from redis import StrictRedis, exceptions
from redis import StrictRedis
from redis.client import BasePipeline
from redis._compat import (long, nativestr)
from .path import Path
Expand All @@ -14,7 +14,7 @@ def str_path(p):

def float_or_long(n):
"Return a number from a Redis reply"
if isinstance(n, str):
if isinstance(n, six.string_types):
return float(n)
else:
return long(n)
Expand Down Expand Up @@ -90,7 +90,7 @@ def __init__(self, encoder=None, decoder=None, *args, **kwargs):
'JSON.ARRTRIM': long_or_none,
'JSON.OBJLEN': long_or_none,
}
for k, v in MODULE_CALLBACKS.iteritems():
for k, v in six.iteritems(MODULE_CALLBACKS):
self.set_response_callback(k, v)

def setEncoder(self, encoder):
Expand Down
79 changes: 33 additions & 46 deletions test/test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
from __future__ import print_function
import six
import json
import redis
import unittest
from unittest import TestCase
from rejson import Client, Path


rj = None
port = 6379


class ReJSONTestCase(TestCase):

def setUp(self):
global rj
rj = Client(port=port, decode_responses=True)
rj.flushdb()

def testJSONSetGetDelShouldSucceed(self):
"Test basic JSONSet/Get/Del"
rj = Client()
rj.flushdb()

self.assertTrue(rj.jsonset('foo', Path.rootPath(), 'bar'))
self.assertEqual('bar', rj.jsonget('foo'))
Expand All @@ -16,8 +27,6 @@ def testJSONSetGetDelShouldSucceed(self):

def testMGetShouldSucceed(self):
"Test JSONMGet"
rj = Client()
rj.flushdb()

rj.jsonset('1', Path.rootPath(), 1)
rj.jsonset('2', Path.rootPath(), 2)
Expand All @@ -27,16 +36,12 @@ def testMGetShouldSucceed(self):

def testTypeShouldSucceed(self):
"Test JSONType"
rj = Client()
rj.flushdb()

rj.jsonset('1', Path.rootPath(), 1)
self.assertEqual('integer', rj.jsontype('1'))

def testNumIncrByShouldSucceed(self):
"Test JSONNumIncrBy"
rj = Client()
rj.flushdb()

rj.jsonset('num', Path.rootPath(), 1)
self.assertEqual(2, rj.jsonnumincrby('num', Path.rootPath(), 1))
Expand All @@ -45,8 +50,6 @@ def testNumIncrByShouldSucceed(self):

def testNumMultByShouldSucceed(self):
"Test JSONNumIncrBy"
rj = Client()
rj.flushdb()

rj.jsonset('num', Path.rootPath(), 1)
self.assertEqual(2, rj.jsonnummultby('num', Path.rootPath(), 2))
Expand All @@ -55,17 +58,13 @@ def testNumMultByShouldSucceed(self):

def testStrAppendShouldSucceed(self):
"Test JSONStrAppend"
rj = Client()
rj.flushdb()

rj.jsonset('str', Path.rootPath(), 'foo')
self.assertEqual(6, rj.jsonstrappend('str', 'bar', Path.rootPath()))
self.assertEqual('foobar', rj.jsonget('str', Path.rootPath()))

def testStrLenShouldSucceed(self):
"Test JSONStrLen"
rj = Client()
rj.flushdb()

rj.jsonset('str', Path.rootPath(), 'foo')
self.assertEqual(3, rj.jsonstrlen('str', Path.rootPath()))
Expand All @@ -74,42 +73,33 @@ def testStrLenShouldSucceed(self):

def testArrAppendShouldSucceed(self):
"Test JSONSArrAppend"
rj = Client()
rj.flushdb()

rj.jsonset('arr', Path.rootPath(), [1])
self.assertEqual(2, rj.jsonarrappend('arr', Path.rootPath(), 2))

def testArrIndexShouldSucceed(self):
"Test JSONSArrIndex"
rj = Client()
rj.flushdb()

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

def testArrInsertShouldSucceed(self):
"Test JSONSArrInsert"
rj = Client()
rj.flushdb()

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

def testArrLenShouldSucceed(self):
"Test JSONSArrLen"
rj = Client()
rj.flushdb()

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

def testArrPopShouldSucceed(self):
"Test JSONSArrPop"
rj = Client()
rj.flushdb()

rj.jsonset('arr', Path.rootPath(), [0, 1, 2, 3, 4])
self.assertEqual(4, rj.jsonarrpop('arr', Path.rootPath(), 4))
Expand All @@ -120,53 +110,46 @@ def testArrPopShouldSucceed(self):

def testArrTrimShouldSucceed(self):
"Test JSONSArrPop"
rj = Client()
rj.flushdb()

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

def testObjKeysShouldSucceed(self):
"Test JSONSObjKeys"
rj = Client()
rj.flushdb()

obj = { 'foo': 'bar', 'baz': 'qaz' }
obj = {'foo': 'bar', 'baz': 'qaz'}
rj.jsonset('obj', Path.rootPath(), obj)
keys = rj.jsonobjkeys('obj', Path.rootPath())
keys.sort()
exp = [k for k in obj.iterkeys()]
exp = [k for k in six.iterkeys(obj)]
exp.sort()
self.assertListEqual(exp, keys)

def testObjLenShouldSucceed(self):
"Test JSONSObjLen"
rj = Client()
rj.flushdb()

obj = { 'foo': 'bar', 'baz': 'qaz' }
obj = {'foo': 'bar', 'baz': 'qaz'}
rj.jsonset('obj', Path.rootPath(), obj)
self.assertEqual(len(obj), rj.jsonobjlen('obj', Path.rootPath()))

def testPipelineShouldSucceed(self):
"Test pipeline"
rj = Client()
rj.flushdb()

p = rj.pipeline()
p.jsonset('foo', Path.rootPath(), 'bar')
p.jsonget('foo')
p.jsondel('foo')
p.exists('foo')
self.assertListEqual([ True, 'bar', 1, False ], p.execute())
self.assertListEqual([True, 'bar', 1, False], p.execute())

def testCustomEncoderDecoderShouldSucceed(self):
"Test a custom encoder and decoder"

class CustomClass(object):
key = ''
val = ''

def __init__(self, k='', v=''):
self.key = k
self.val = v
Expand All @@ -180,12 +163,14 @@ def default(self, obj):
class TestDecoder(json.JSONDecoder):
def decode(self, obj):
d = json.JSONDecoder.decode(self, obj)
if isinstance(d, basestring) and d.startswith('CustomClass:'):
if isinstance(d, six.string_types) and \
d.startswith('CustomClass:'):
s = d.split(':')
return CustomClass(k=s[1], v=s[2])
return d

rj = Client(encoder=TestEncoder(), decoder=TestDecoder())
rj = Client(encoder=TestEncoder(), decoder=TestDecoder(),
port=port, decode_responses=True)
rj.flushdb()

# Check a regular string
Expand All @@ -194,7 +179,8 @@ def decode(self, obj):
self.assertEqual('bar', rj.jsonget('foo', Path.rootPath()))

# Check the custom encoder
self.assertTrue(rj.jsonset('cus', Path.rootPath(), CustomClass('foo', 'bar')))
self.assertTrue(rj.jsonset('cus', Path.rootPath(),
CustomClass('foo', 'bar')))
obj = rj.jsonget('cus', Path.rootPath())
self.assertIsNotNone(obj)
self.assertEqual(CustomClass, obj.__class__)
Expand All @@ -205,7 +191,7 @@ def testUsageExampleShouldSucceed(self):
"Test the usage example"

# Create a new rejson-py client
rj = Client(host='localhost', port=6379)
rj = Client(host='localhost', port=port, decode_responses=True)

# Set the key `obj` to some object
obj = {
Expand All @@ -218,14 +204,14 @@ def testUsageExampleShouldSucceed(self):
rj.jsonset('obj', Path.rootPath(), obj)

# Get something
print 'Is there anybody... {}?'.format(
print('Is there anybody... {}?'.format(
rj.jsonget('obj', Path('.truth.coord'))
)
))

# Delete something (or perhaps nothing), append something and pop it
rj.jsondel('obj', Path('.arr[0]'))
rj.jsonarrappend('obj', Path('.arr'), 'something')
print '{} popped!'.format(rj.jsonarrpop('obj', Path('.arr')))
print('{} popped!'.format(rj.jsonarrpop('obj', Path('.arr'))))

# Update something else
rj.jsonset('obj', Path('.answer'), 2.17)
Expand All @@ -236,5 +222,6 @@ def testUsageExampleShouldSucceed(self):
jp.jsonset('baz', Path.rootPath(), 'qaz')
jp.execute()


if __name__ == '__main__':
unittest.main()