Queue: add handling of Exchange as str to constructor (#904)

To fit with the documentation, Queue can now handle a str argument for
the exchange parameter in the constructor, as well as an actual Exchange
object.
Added relevant unit tests to avoid regressions.

Fixes: https://github.com/celery/kombu/issues/903

Signed-off-by: Antonio Gutierrez <chibby0ne@gmail.com>
This commit is contained in:
Antonio Gutierrez 2018-08-12 04:59:27 +02:00 committed by Asif Saifuddin Auvi
parent 073001e341
commit 1160b92c27
2 changed files with 18 additions and 1 deletions

View File

@ -568,7 +568,10 @@ class Queue(MaybeChannelBound):
**kwargs):
super(Queue, self).__init__(**kwargs)
self.name = name or self.name
self.exchange = exchange or self.exchange
if isinstance(exchange, str):
self.exchange = Exchange(exchange)
elif isinstance(exchange, Exchange):
self.exchange = exchange
self.routing_key = routing_key or self.routing_key
self.bindings = set(bindings or [])
self.on_declared = on_declared

View File

@ -214,6 +214,20 @@ class test_Queue:
def setup(self):
self.exchange = Exchange('foo', 'direct')
def test_constructor_with_actual_exchange(self):
exchange = Exchange('exchange_name', 'direct')
queue = Queue(name='queue_name', exchange=exchange)
assert queue.exchange == exchange
def test_constructor_with_string_exchange(self):
exchange_name = str('exchange_name')
queue = Queue(name='queue_name', exchange=exchange_name)
assert queue.exchange == Exchange(exchange_name)
def test_constructor_with_default_exchange(self):
queue = Queue(name='queue_name')
assert queue.exchange == Exchange('')
def test_hash(self):
assert hash(Queue('a')) == hash(Queue('a'))
assert hash(Queue('a')) != hash(Queue('b'))