Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mocks.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. from __future__ import absolute_import
  2. from itertools import count
  3. import anyjson
  4. from kombu.transport import base
  5. class Message(base.Message):
  6. def __init__(self, *args, **kwargs):
  7. self.throw_decode_error = kwargs.get('throw_decode_error', False)
  8. super(Message, self).__init__(*args, **kwargs)
  9. def decode(self):
  10. if self.throw_decode_error:
  11. raise ValueError("can't decode message")
  12. return super(Message, self).decode()
  13. class Channel(base.StdChannel):
  14. open = True
  15. throw_decode_error = False
  16. _ids = count(1)
  17. def __init__(self, connection):
  18. self.connection = connection
  19. self.called = []
  20. self.deliveries = count(1)
  21. self.to_deliver = []
  22. self.events = {'basic_return': set()}
  23. self.channel_id = next(self._ids)
  24. def _called(self, name):
  25. self.called.append(name)
  26. def __contains__(self, key):
  27. return key in self.called
  28. def exchange_declare(self, *args, **kwargs):
  29. self._called('exchange_declare')
  30. def prepare_message(self, body, priority=0, content_type=None,
  31. content_encoding=None, headers=None, properties={}):
  32. self._called('prepare_message')
  33. return dict(body=body,
  34. headers=headers,
  35. properties=properties,
  36. priority=priority,
  37. content_type=content_type,
  38. content_encoding=content_encoding)
  39. def basic_publish(self, message, exchange='', routing_key='',
  40. mandatory=False, immediate=False, **kwargs):
  41. self._called('basic_publish')
  42. return message, exchange, routing_key
  43. def exchange_delete(self, *args, **kwargs):
  44. self._called('exchange_delete')
  45. def queue_declare(self, *args, **kwargs):
  46. self._called('queue_declare')
  47. def queue_bind(self, *args, **kwargs):
  48. self._called('queue_bind')
  49. def queue_unbind(self, *args, **kwargs):
  50. self._called('queue_unbind')
  51. def queue_delete(self, queue, if_unused=False, if_empty=False, **kwargs):
  52. self._called('queue_delete')
  53. def basic_get(self, *args, **kwargs):
  54. self._called('basic_get')
  55. try:
  56. return self.to_deliver.pop()
  57. except IndexError:
  58. pass
  59. def queue_purge(self, *args, **kwargs):
  60. self._called('queue_purge')
  61. def basic_consume(self, *args, **kwargs):
  62. self._called('basic_consume')
  63. def basic_cancel(self, *args, **kwargs):
  64. self._called('basic_cancel')
  65. def basic_ack(self, *args, **kwargs):
  66. self._called('basic_ack')
  67. def basic_recover(self, requeue=False):
  68. self._called('basic_recover')
  69. def exchange_bind(self, *args, **kwargs):
  70. self._called('exchange_bind')
  71. def exchange_unbind(self, *args, **kwargs):
  72. self._called('exchange_unbind')
  73. def close(self):
  74. self._called('close')
  75. def message_to_python(self, message, *args, **kwargs):
  76. self._called('message_to_python')
  77. return Message(self, body=anyjson.dumps(message),
  78. delivery_tag=next(self.deliveries),
  79. throw_decode_error=self.throw_decode_error,
  80. content_type='application/json',
  81. content_encoding='utf-8')
  82. def flow(self, active):
  83. self._called('flow')
  84. def basic_reject(self, delivery_tag, requeue=False):
  85. if requeue:
  86. return self._called('basic_reject:requeue')
  87. return self._called('basic_reject')
  88. def basic_qos(self, prefetch_size=0, prefetch_count=0,
  89. apply_global=False):
  90. self._called('basic_qos')
  91. class Connection(object):
  92. connected = True
  93. def __init__(self, client):
  94. self.client = client
  95. def channel(self):
  96. return Channel(self)
  97. class Transport(base.Transport):
  98. def establish_connection(self):
  99. return Connection(self.client)
  100. def create_channel(self, connection):
  101. return connection.channel()
  102. def drain_events(self, connection, **kwargs):
  103. return 'event'
  104. def close_connection(self, connection):
  105. connection.connected = False