Skip to content

Commit 7c3d52e

Browse files
committed
tests with global setUp method to setup the client for a test, verbose tests modified
1 parent 9b712c1 commit 7c3d52e

File tree

1 file changed

+44
-49
lines changed

1 file changed

+44
-49
lines changed

tests/test_rejson.py

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1+
from __future__ import print_function
2+
import six
13
import json
2-
import redis
4+
import unittest
35
from unittest import TestCase
46
from rejson import Client, Path
57

8+
9+
rj = None
10+
port = 6378
11+
12+
613
class ReJSONTestCase(TestCase):
14+
15+
def setUp(self):
16+
global rj
17+
rj = Client(port=port, decode_responses=True)
18+
rj.flushdb()
19+
720
def testJSONSetGetDelShouldSucceed(self):
821
"Test basic JSONSet/Get/Del"
9-
rj = Client()
10-
rj.flushdb()
1122

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

1728
def testMGetShouldSucceed(self):
1829
"Test JSONMGet"
19-
rj = Client()
20-
rj.flushdb()
2130

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

2837
def testTypeShouldSucceed(self):
2938
"Test JSONType"
30-
rj = Client()
31-
rj.flushdb()
3239

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

3643
def testNumIncrByShouldSucceed(self):
3744
"Test JSONNumIncrBy"
38-
rj = Client()
39-
rj.flushdb()
4045

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

4651
def testNumMultByShouldSucceed(self):
4752
"Test JSONNumIncrBy"
48-
rj = Client()
49-
rj.flushdb()
5053

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

5659
def testStrAppendShouldSucceed(self):
5760
"Test JSONStrAppend"
58-
rj = Client()
59-
rj.flushdb()
6061

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

6566
def testStrLenShouldSucceed(self):
6667
"Test JSONStrLen"
67-
rj = Client()
68-
rj.flushdb()
6968

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

7574
def testArrAppendShouldSucceed(self):
7675
"Test JSONSArrAppend"
77-
rj = Client()
78-
rj.flushdb()
7976

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

8380
def testArrIndexShouldSucceed(self):
8481
"Test JSONSArrIndex"
85-
rj = Client()
86-
rj.flushdb()
8782

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

9287
def testArrInsertShouldSucceed(self):
9388
"Test JSONSArrInsert"
94-
rj = Client()
95-
rj.flushdb()
9689

9790
rj.jsonset('arr', Path.rootPath(), [0, 4])
98-
self.assertEqual(5, rj.jsonarrinsert('arr', Path.rootPath(), 1, *[1, 2, 3,]))
91+
self.assertEqual(5, rj.jsonarrinsert('arr',
92+
Path.rootPath(), 1, *[1, 2, 3, ]))
9993
self.assertListEqual([0, 1, 2, 3, 4], rj.jsonget('arr'))
10094

10195
def testArrLenShouldSucceed(self):
10296
"Test JSONSArrLen"
103-
rj = Client()
104-
rj.flushdb()
10597

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

109101
def testArrPopShouldSucceed(self):
110102
"Test JSONSArrPop"
111-
rj = Client()
112-
rj.flushdb()
113103

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

121111
def testArrTrimShouldSucceed(self):
122112
"Test JSONSArrPop"
123-
rj = Client()
124-
rj.flushdb()
125113

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

130118
def testObjKeysShouldSucceed(self):
131119
"Test JSONSObjKeys"
132-
rj = Client()
133-
rj.flushdb()
134120

135-
obj = { 'foo': 'bar', 'baz': 'qaz' }
121+
obj = {'foo': 'bar', 'baz': 'qaz'}
136122
rj.jsonset('obj', Path.rootPath(), obj)
137123
keys = rj.jsonobjkeys('obj', Path.rootPath())
138124
keys.sort()
139-
exp = [k for k in obj.iterkeys()]
125+
exp = [k for k in six.iterkeys(obj)]
140126
exp.sort()
141127
self.assertListEqual(exp, keys)
142128

143129
def testObjLenShouldSucceed(self):
144130
"Test JSONSObjLen"
145-
rj = Client()
146-
rj.flushdb()
147131

148-
obj = { 'foo': 'bar', 'baz': 'qaz' }
132+
obj = {'foo': 'bar', 'baz': 'qaz'}
149133
rj.jsonset('obj', Path.rootPath(), obj)
150134
self.assertEqual(len(obj), rj.jsonobjlen('obj', Path.rootPath()))
151135

152136
def testPipelineShouldSucceed(self):
153137
"Test pipeline"
154-
rj = Client()
155-
rj.flushdb()
156138

157139
p = rj.pipeline()
158140
p.jsonset('foo', Path.rootPath(), 'bar')
159141
p.jsonget('foo')
160142
p.jsondel('foo')
161143
p.exists('foo')
162-
self.assertListEqual([ True, 'bar', 1, False ], p.execute())
144+
self.assertListEqual([True, 'bar', 1, False], p.execute())
163145

164146
def testCustomEncoderDecoderShouldSucceed(self):
165147
"Test a custom encoder and decoder"
166-
148+
167149
class CustomClass(object):
168150
key = ''
169151
val = ''
152+
170153
def __init__(self, k='', v=''):
171154
self.key = k
172155
self.val = v
@@ -180,12 +163,14 @@ def default(self, obj):
180163
class TestDecoder(json.JSONDecoder):
181164
def decode(self, obj):
182165
d = json.JSONDecoder.decode(self, obj)
183-
if isinstance(d, basestring) and d.startswith('CustomClass:'):
166+
if isinstance(d, six.string_types) and \
167+
d.startswith('CustomClass:'):
184168
s = d.split(':')
185169
return CustomClass(k=s[1], v=s[2])
186170
return d
187171

188-
rj = Client(encoder=TestEncoder(), decoder=TestDecoder())
172+
rj = Client(encoder=TestEncoder(), decoder=TestDecoder(),
173+
port=port, decode_responses=True)
189174
rj.flushdb()
190175

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

196181
# Check the custom encoder
197-
self.assertTrue(rj.jsonset('cus', Path.rootPath(), CustomClass('foo', 'bar')))
182+
self.assertTrue(rj.jsonset('cus', Path.rootPath(),
183+
CustomClass('foo', 'bar')))
198184
obj = rj.jsonget('cus', Path.rootPath())
199185
self.assertIsNotNone(obj)
200186
self.assertEqual(CustomClass, obj.__class__)
@@ -205,7 +191,7 @@ def testUsageExampleShouldSucceed(self):
205191
"Test the usage example"
206192

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

210196
# Set the key `obj` to some object
211197
obj = {
@@ -218,23 +204,32 @@ def testUsageExampleShouldSucceed(self):
218204
rj.jsonset('obj', Path.rootPath(), obj)
219205

220206
# Get something
221-
print 'Is there anybody... {}?'.format(
222-
rj.jsonget('obj', Path('.truth.coord'))
223-
)
207+
rv = rj.jsonget('obj', Path('.truth.coord'))
208+
self.assertEqual(obj['truth']['coord'], rv)
224209

225210
# Delete something (or perhaps nothing), append something and pop it
211+
value = "something"
226212
rj.jsondel('obj', Path('.arr[0]'))
227-
rj.jsonarrappend('obj', Path('.arr'), 'something')
228-
print '{} popped!'.format(rj.jsonarrpop('obj', Path('.arr')))
213+
rj.jsonarrappend('obj', Path('.arr'), value)
214+
rv = rj.jsonarrpop('obj', Path('.arr'))
215+
self.assertEqual(value, rv)
229216

230217
# Update something else
231-
rj.jsonset('obj', Path('.answer'), 2.17)
218+
value = 2.17
219+
rj.jsonset('obj', Path('.answer'), value)
220+
rv = rj.jsonget('obj', Path('.answer'))
221+
self.assertEqual(value, rv)
232222

233223
# And use just like the regular redis-py client
234224
jp = rj.pipeline()
235225
jp.set('foo', 'bar')
236226
jp.jsonset('baz', Path.rootPath(), 'qaz')
237227
jp.execute()
228+
rv1 = rj.get('foo')
229+
self.assertEqual('bar', rv1)
230+
rv2 = rj.jsonget('baz')
231+
self.assertEqual('qaz', rv2)
232+
238233

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

0 commit comments

Comments
 (0)