Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bbayles committed Feb 17, 2020
1 parent 9e1d600 commit c57eba1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
5 changes: 3 additions & 2 deletions redis_collections/dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,14 @@ def popitem(self):
a :exc:`KeyError`.
"""
def popitem_trans(pipe):
pipe.multi()
try:
pickled_key = pipe.hkeys(self.key)[0]
pipe.hkeys(self.key)
pickled_key = pipe.execute()[-1][0]
except IndexError:
raise KeyError

# pop its value
pipe.multi()
pipe.hget(self.key, pickled_key)
pipe.hdel(self.key, pickled_key)
pickled_value, __ = pipe.execute()
Expand Down
13 changes: 3 additions & 10 deletions redis_collections/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,18 +449,11 @@ def _insert_left(self, value, pipe=None):
self.cache = {k + 1: v for k, v in self.cache.items()}
self.cache[0] = value

def _insert_middle(self, index, value, pipe=None):
# Insert *value* at *index*.
pipe = self.redis if pipe is None else pipe

def _insert_middle(self, index, value, pipe):
# First, retrieve everything from the index to the end.
__, cache_index = self._normalize_index(index, pipe)

if isinstance(pipe, Pipeline):
pipe.lrange(self.key, cache_index, -1)
right_values = pipe.execute()[-1]
else:
right_values = pipe.lrange(self.key, cache_index, -1)
pipe.lrange(self.key, cache_index, -1)
right_values = pipe.execute()[-1]

# Next, zap everything after the index. Finally, insert the new value
# and then re-insert the items from before.
Expand Down
10 changes: 2 additions & 8 deletions redis_collections/sortedsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,9 @@ def _repr_data(self):

# Magic methods

def __contains__(self, member, pipe=None):
def __contains__(self, member):
"""Return ``True`` if *member* is present, else ``False``."""
pipe = self.redis if pipe is None else pipe
if isinstance(pipe, Pipeline):
pipe.zscore(self.key, self._pickle(member))
score = pipe.execute()[-1]
else:
score = pipe.zscore(self.key, self._pickle(member))

score = self.redis.zscore(self.key, self._pickle(member))
return score is not None

def __iter__(self, pipe=None):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,12 @@ def test_scan_items(self):

self.assertTrue(dict(items), expected_dict)

def test_redis_version(self):
redis_dict = self.create_dict()
actual = '.'.join(str(c) for c in redis_dict.redis_version)
expected = self.redis.info()['redis_version']
self.assertEqual(actual, expected)


class CounterTest(RedisTestCase):

Expand Down
3 changes: 1 addition & 2 deletions tests/test_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,8 @@ def test_scan_elements(self):
expected_elements.add(elem)
redis_set.add(elem)

actual_elements = list(redis_set)
actual_elements = list(redis_set.scan_elements())
self.assertTrue(len(actual_elements) >= len(expected_elements))

self.assertEqual(set(actual_elements), expected_elements)


Expand Down
19 changes: 19 additions & 0 deletions tests/test_sortedsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,21 @@ def create_geodb(self, *args, **kwargs):
kwargs['redis'] = self.redis
return GeoDB(*args, **kwargs)

def test_init(self):
data = {
'St. Louis': {'latitude': 38.6270, 'longitude': -90.1994},
'Sydney': {'latitude': -33.8562, 'longitude': 151.2153},
}
geodb = self.create_geodb(data)

response = geodb.get_location('Sydney')
self.assertAlmostEqual(response['latitude'], -33.8562, places=4)
self.assertAlmostEqual(response['longitude'], 151.2153, places=4)

response = geodb.get_location('St. Louis')
self.assertAlmostEqual(response['latitude'], 38.6270, places=4)
self.assertAlmostEqual(response['longitude'], -90.1994, places=4)

def test_getitem(self):
geodb = self.create_geodb()
geodb.set_location('St. Louis', 38.6270, -90.1994)
Expand Down Expand Up @@ -308,6 +323,7 @@ def test_get_hash(self):
geodb = self.create_geodb()
geodb.set_location('St. Louis', 38.6270, -90.1994)
self.assertEqual(geodb.get_hash('St. Louis'), '9yzgeryf9d0')
self.assertIsNone(geodb.get_hash('Unknown'))

def test_get_set_location(self):
geodb = self.create_geodb()
Expand Down Expand Up @@ -346,6 +362,9 @@ def test_places_within_radius(self):
)
self.assertEqual(response[0]['place'], 'Bahia')

with self.assertRaises(ValueError):
geodb.places_within_radius()

def test_update(self):
geodb_1 = self.create_geodb()
geodb_1.set_location('St. Louis', 38.6270, -90.1994)
Expand Down

0 comments on commit c57eba1

Please sign in to comment.