kombu/t/mocks.py

205 lines
5.5 KiB
Python
Raw Normal View History

from __future__ import annotations
2010-06-29 15:31:56 +00:00
from itertools import count
from typing import TYPE_CHECKING
2020-08-15 20:51:02 +00:00
from unittest.mock import Mock
2012-03-20 14:53:00 +00:00
from kombu.transport import base
2014-05-20 20:49:47 +00:00
from kombu.utils import json
2010-06-29 15:31:56 +00:00
if TYPE_CHECKING:
from types import TracebackType
2010-06-29 15:31:56 +00:00
class _ContextMock(Mock):
"""Dummy class implementing __enter__ and __exit__
as the :keyword:`with` statement requires these to be implemented
in the class, not just the instance."""
def __enter__(self):
return self
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None
) -> None:
pass
def ContextMock(*args, **kwargs):
"""Mock that mocks :keyword:`with` statement contexts."""
obj = _ContextMock(*args, **kwargs)
obj.attach_mock(_ContextMock(), '__enter__')
obj.attach_mock(_ContextMock(), '__exit__')
obj.__enter__.return_value = obj
# if __exit__ return a value the exception is ignored,
# so it must return None here.
obj.__exit__.return_value = None
return obj
def PromiseMock(*args, **kwargs):
m = Mock(*args, **kwargs)
def on_throw(exc=None, *args, **kwargs):
if exc:
raise exc
raise
m.throw.side_effect = on_throw
m.set_error_state.side_effect = on_throw
m.throw1.side_effect = on_throw
return m
2020-07-13 13:58:06 +00:00
class MockPool:
def __init__(self, value=None):
self.value = value or ContextMock()
def acquire(self, **kwargs):
return self.value
class Message(base.Message):
2010-06-29 15:31:56 +00:00
def __init__(self, *args, **kwargs):
2012-06-15 17:32:40 +00:00
self.throw_decode_error = kwargs.get('throw_decode_error', False)
2020-07-13 13:58:06 +00:00
super().__init__(*args, **kwargs)
2010-06-29 15:31:56 +00:00
def decode(self):
if self.throw_decode_error:
raise ValueError("can't decode message")
2020-07-13 13:58:06 +00:00
return super().decode()
2010-06-29 15:31:56 +00:00
2011-06-29 11:08:48 +00:00
class Channel(base.StdChannel):
2010-06-29 15:31:56 +00:00
open = True
throw_decode_error = False
2013-02-12 16:37:01 +00:00
_ids = count(1)
2010-06-29 15:31:56 +00:00
def __init__(self, connection):
self.connection = connection
2010-06-29 15:31:56 +00:00
self.called = []
2013-02-12 16:37:01 +00:00
self.deliveries = count(1)
2010-11-11 13:41:55 +00:00
self.to_deliver = []
self.events = {'basic_return': set()}
2013-02-12 16:37:01 +00:00
self.channel_id = next(self._ids)
2010-06-29 15:31:56 +00:00
def _called(self, name):
self.called.append(name)
def __contains__(self, key):
return key in self.called
def exchange_declare(self, *args, **kwargs):
2012-06-15 17:32:40 +00:00
self._called('exchange_declare')
2010-06-29 15:31:56 +00:00
def prepare_message(self, body, priority=0, content_type=None,
2013-01-17 13:50:01 +00:00
content_encoding=None, headers=None, properties={}):
2012-06-15 17:32:40 +00:00
self._called('prepare_message')
return {'body': body,
'headers': headers,
'properties': properties,
'priority': priority,
'content_type': content_type,
'content_encoding': content_encoding}
2010-06-29 15:31:56 +00:00
2012-06-15 17:32:40 +00:00
def basic_publish(self, message, exchange='', routing_key='',
2013-01-17 13:50:01 +00:00
mandatory=False, immediate=False, **kwargs):
2012-06-15 17:32:40 +00:00
self._called('basic_publish')
2010-06-29 15:31:56 +00:00
return message, exchange, routing_key
def exchange_delete(self, *args, **kwargs):
2012-06-15 17:32:40 +00:00
self._called('exchange_delete')
2010-06-29 15:31:56 +00:00
def queue_declare(self, *args, **kwargs):
2012-06-15 17:32:40 +00:00
self._called('queue_declare')
2010-06-29 15:31:56 +00:00
def queue_bind(self, *args, **kwargs):
2012-06-15 17:32:40 +00:00
self._called('queue_bind')
2010-06-29 15:31:56 +00:00
2010-11-11 11:52:57 +00:00
def queue_unbind(self, *args, **kwargs):
2012-06-15 17:32:40 +00:00
self._called('queue_unbind')
2010-11-11 11:52:57 +00:00
2010-07-31 14:41:35 +00:00
def queue_delete(self, queue, if_unused=False, if_empty=False, **kwargs):
2012-06-15 17:32:40 +00:00
self._called('queue_delete')
2010-06-29 15:31:56 +00:00
def basic_get(self, *args, **kwargs):
2012-06-15 17:32:40 +00:00
self._called('basic_get')
2010-11-11 13:41:55 +00:00
try:
return self.to_deliver.pop()
except IndexError:
pass
2010-06-29 15:31:56 +00:00
def queue_purge(self, *args, **kwargs):
2012-06-15 17:32:40 +00:00
self._called('queue_purge')
2010-06-29 15:31:56 +00:00
def basic_consume(self, *args, **kwargs):
2012-06-15 17:32:40 +00:00
self._called('basic_consume')
2010-06-29 15:31:56 +00:00
def basic_cancel(self, *args, **kwargs):
2012-06-15 17:32:40 +00:00
self._called('basic_cancel')
2010-06-29 15:31:56 +00:00
def basic_ack(self, *args, **kwargs):
2012-06-15 17:32:40 +00:00
self._called('basic_ack')
2010-06-29 15:31:56 +00:00
def basic_recover(self, requeue=False):
2012-06-15 17:32:40 +00:00
self._called('basic_recover')
2010-06-29 15:31:56 +00:00
2012-09-21 13:13:48 +00:00
def exchange_bind(self, *args, **kwargs):
self._called('exchange_bind')
def exchange_unbind(self, *args, **kwargs):
self._called('exchange_unbind')
2010-11-11 13:41:55 +00:00
def close(self):
2012-06-15 17:32:40 +00:00
self._called('close')
2010-11-11 13:41:55 +00:00
2010-06-29 15:31:56 +00:00
def message_to_python(self, message, *args, **kwargs):
2012-06-15 17:32:40 +00:00
self._called('message_to_python')
return Message(body=json.dumps(message),
channel=self,
2013-02-12 16:37:01 +00:00
delivery_tag=next(self.deliveries),
2013-01-17 13:50:01 +00:00
throw_decode_error=self.throw_decode_error,
content_type='application/json',
content_encoding='utf-8')
2010-06-29 15:31:56 +00:00
def flow(self, active):
2012-06-15 17:32:40 +00:00
self._called('flow')
2010-06-29 15:31:56 +00:00
def basic_reject(self, delivery_tag, requeue=False):
if requeue:
2012-06-15 17:32:40 +00:00
return self._called('basic_reject:requeue')
return self._called('basic_reject')
2010-06-29 15:31:56 +00:00
def basic_qos(self, prefetch_size=0, prefetch_count=0,
2013-01-17 13:50:01 +00:00
apply_global=False):
2012-06-15 17:32:40 +00:00
self._called('basic_qos')
2010-06-29 15:31:56 +00:00
2020-07-13 13:58:06 +00:00
class Connection:
2010-06-29 15:31:56 +00:00
connected = True
def __init__(self, client):
self.client = client
2010-06-29 15:31:56 +00:00
def channel(self):
return Channel(self)
2010-06-29 15:31:56 +00:00
class Transport(base.Transport):
2010-06-29 15:31:56 +00:00
def establish_connection(self):
return Connection(self.client)
2010-06-29 15:31:56 +00:00
def create_channel(self, connection):
return connection.channel()
2010-07-31 14:41:35 +00:00
def drain_events(self, connection, **kwargs):
2012-06-15 17:32:40 +00:00
return 'event'
2010-06-29 15:31:56 +00:00
def close_connection(self, connection):
connection.connected = False