diff --git a/rejson/client.py b/rejson/client.py index f7c1836..fbe3ab4 100644 --- a/rejson/client.py +++ b/rejson/client.py @@ -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 @@ -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) @@ -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): diff --git a/test/test.py b/test/test.py index d994571..2559c1e 100644 --- a/test/test.py +++ b/test/test.py @@ -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')) @@ -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) @@ -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)) @@ -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)) @@ -55,8 +58,6 @@ 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())) @@ -64,8 +65,6 @@ def testStrAppendShouldSucceed(self): def testStrLenShouldSucceed(self): "Test JSONStrLen" - rj = Client() - rj.flushdb() rj.jsonset('str', Path.rootPath(), 'foo') self.assertEqual(3, rj.jsonstrlen('str', Path.rootPath())) @@ -74,16 +73,12 @@ 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)) @@ -91,25 +86,20 @@ def testArrIndexShouldSucceed(self): 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)) @@ -120,8 +110,6 @@ 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)) @@ -129,44 +117,39 @@ def testArrTrimShouldSucceed(self): 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 @@ -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 @@ -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__) @@ -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 = { @@ -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) @@ -236,5 +222,6 @@ def testUsageExampleShouldSucceed(self): jp.jsonset('baz', Path.rootPath(), 'qaz') jp.execute() + if __name__ == '__main__': unittest.main()