summaryrefslogtreecommitdiff
path: root/kombu/tests/mocks.py
blob: 8d81bc9071a049660d490de9ad30d864c6acf805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from __future__ import absolute_import

from itertools import count

import anyjson

from kombu.transport import base


class Message(base.Message):

    def __init__(self, *args, **kwargs):
        self.throw_decode_error = kwargs.get("throw_decode_error", False)
        super(Message, self).__init__(*args, **kwargs)

    def decode(self):
        if self.throw_decode_error:
            raise ValueError("can't decode message")
        return super(Message, self).decode()


class Channel(base.StdChannel):
    open = True
    throw_decode_error = False

    def __init__(self, connection):
        self.connection = connection
        self.called = []
        self.deliveries = count(1).next
        self.to_deliver = []
        self.events = {"basic_return": []}

    def _called(self, name):
        self.called.append(name)

    def __contains__(self, key):
        return key in self.called

    def exchange_declare(self, *args, **kwargs):
        self._called("exchange_declare")

    def prepare_message(self, message_data, properties={}, priority=0,
            content_type=None, content_encoding=None, headers=None):
        self._called("prepare_message")
        return dict(body=message_data,
                    headers=headers,
                    properties=properties,
                    priority=priority,
                    content_type=content_type,
                    content_encoding=content_encoding)

    def basic_publish(self, message, exchange="", routing_key="",
            mandatory=False, immediate=False, **kwargs):
        self._called("basic_publish")
        return message, exchange, routing_key

    def exchange_delete(self, *args, **kwargs):
        self._called("exchange_delete")

    def queue_declare(self, *args, **kwargs):
        self._called("queue_declare")

    def queue_bind(self, *args, **kwargs):
        self._called("queue_bind")

    def queue_unbind(self, *args, **kwargs):
        self._called("queue_unbind")

    def queue_delete(self, queue, if_unused=False, if_empty=False, **kwargs):
        self._called("queue_delete")

    def basic_get(self, *args, **kwargs):
        self._called("basic_get")
        try:
            return self.to_deliver.pop()
        except IndexError:
            pass

    def queue_purge(self, *args, **kwargs):
        self._called("queue_purge")

    def basic_consume(self, *args, **kwargs):
        self._called("basic_consume")

    def basic_cancel(self, *args, **kwargs):
        self._called("basic_cancel")

    def basic_ack(self, *args, **kwargs):
        self._called("basic_ack")

    def basic_recover(self, requeue=False):
        self._called("basic_recover")

    def close(self):
        self._called("close")

    def message_to_python(self, message, *args, **kwargs):
        self._called("message_to_python")
        return Message(self, body=anyjson.dumps(message),
                delivery_tag=self.deliveries(),
                throw_decode_error=self.throw_decode_error,
                content_type="application/json", content_encoding="utf-8")

    def flow(self, active):
        self._called("flow")

    def basic_reject(self, delivery_tag, requeue=False):
        if requeue:
            return self._called("basic_reject:requeue")
        return self._called("basic_reject")

    def basic_qos(self, prefetch_size=0, prefetch_count=0,
            apply_global=False):
        self._called("basic_qos")


class Connection(object):
    connected = True

    def __init__(self, client):
        self.client = client

    def channel(self):
        return Channel(self)


class Transport(base.Transport):

    def establish_connection(self):
        return Connection(self.client)

    def create_channel(self, connection):
        return connection.channel()

    def drain_events(self, connection, **kwargs):
        return "event"

    def close_connection(self, connection):
        connection.connected = False