Skip to content

Commit

Permalink
Code refactor of redis.py and test_redis.py
Browse files Browse the repository at this point in the history
  • Loading branch information
matusvalo committed Mar 3, 2021
1 parent ea4c26e commit 5b9263e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
3 changes: 1 addition & 2 deletions kombu/transport/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,9 +816,8 @@ def _size(self, queue):
queue_name = self._queue_for_priority(queue, pri)
pipe = pipe.llen(queue_name)
sizes = pipe.execute()
size = sum(s for s in sizes
return sum(s for s in sizes
if isinstance(s, numbers.Integral))
return size

def _queue_for_priority(self, queue, pri):
queue_priority = self.priority(pri)
Expand Down
29 changes: 13 additions & 16 deletions t/unit/transport/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,13 +1101,10 @@ def test_transport_driver_version(self):
assert kombu_redis.Transport.driver_version(self.connection.transport)

def test_transport_get_errors_when_InvalidData_used(self):
try:
errors = kombu_redis.Transport._get_errors(
self.connection.transport)
assert errors
assert redis.exceptions.DataError in errors[1]
except Exception:
raise
errors = kombu_redis.Transport._get_errors(
self.connection.transport)
assert errors
assert redis.exceptions.DataError in errors[1]

def test_empty_queues_key(self):
channel = self.channel
Expand Down Expand Up @@ -1175,10 +1172,10 @@ def test_rediss_connection(self):
)

def test_sep_transport_option(self):
conn_kwargs = dict(
transport=FakeRedisKombuTransport,
transport_options={'sep': ':'})
with Connection(**conn_kwargs) as conn:
with Connection(
transport=FakeRedisKombuTransport,
transport_options={'sep': ':'}
) as conn:
key = conn.default_channel.keyprefix_queue % 'celery'
conn.default_channel.client.sadd(key, 'celery::celery')
assert conn.default_channel.sep == ':'
Expand Down Expand Up @@ -1285,8 +1282,8 @@ def test_check_at_least_we_try_to_connect_and_fail(self):

def test_redis_queue_lookup_gets_queue_when_exchange_doesnot_exist(self):
# Given: A test redis client and channel
fake_redis_client = _get_fake_redis_client()
redis_channel = self.connection.default_channel
fake_redis_client = self.connection.default_channel.client
# Given: The default queue is set:
default_queue = 'default_queue'
redis_channel.deadletter_queue = default_queue
Expand All @@ -1301,8 +1298,8 @@ def test_redis_queue_lookup_gets_queue_when_exchange_doesnot_exist(self):

def test_redis_queue_lookup_gets_default_when_route_doesnot_exist(self):
# Given: A test redis client and channel
fake_redis_client = _get_fake_redis_client()
redis_channel = self.connection.default_channel
fake_redis_client = self.connection.default_channel.client
# Given: The default queue is set:
default_queue = 'default_queue'
redis_channel.deadletter_queue = default_queue
Expand All @@ -1316,9 +1313,9 @@ def test_redis_queue_lookup_gets_default_when_route_doesnot_exist(self):
assert lookup_queue_result == [default_queue]

def test_redis_queue_lookup_client_raises_key_error_gets_default(self):
fake_redis_client = _get_fake_redis_client()
fake_redis_client.smembers = Mock(side_effect=KeyError)
redis_channel = self.connection.default_channel
fake_redis_client = self.connection.default_channel.client
fake_redis_client.smembers = Mock(side_effect=KeyError)
routing_key = redis_channel.keyprefix_queue % self.exchange
redis_channel.queue_bind(routing_key, self.exchange_name, routing_key)
fake_redis_client.sadd(routing_key, routing_key)
Expand All @@ -1330,7 +1327,7 @@ def test_redis_queue_lookup_client_raises_key_error_gets_default(self):
assert lookup_queue_result == [default_queue_name]

def test_redis_queue_lookup_client_raises_key_error_gets_deadletter(self):
fake_redis_client = _get_fake_redis_client()
fake_redis_client = self.connection.default_channel.client
fake_redis_client.smembers = Mock(side_effect=KeyError)
redis_channel = self.connection.default_channel
routing_key = redis_channel.keyprefix_queue % self.exchange
Expand Down

0 comments on commit 5b9263e

Please sign in to comment.