def test_next_page(self): messages = { 'links': [], 'messages': [{ 'href': '/v1/queues/mine/messages/123123423', 'ttl': 800, 'age': 790, 'body': { 'event': 'ActivateAccount', 'mode': 'active' } }] } with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(messages)) send_method.return_value = resp # NOTE(flaper87): The first iteration will return 1 message # and then call `_next_page` which will use the rel-next link # to get a new set of messages. link = { 'rel': 'next', 'href': "/v1/queues/mine/messages?marker=6244-244224-783" } messages['links'].append(link) iterator = message._MessageIterator(self.queue, messages) iterated = [msg for msg in iterator] self.assertEqual(len(iterated), 2)
def send(self, request): url, method, request = self._prepare(request) # NOTE(flape87): Do not modify # request's headers directly. headers = request.headers.copy() headers['content-type'] = 'application/json' resp = self.client.request(method, url=url, params=request.params, headers=headers, data=request.content) if resp.status_code in self.http_to_marconi: try: msg = json.loads(resp.text)['description'] except Exception: # TODO(flaper87): Log this exception # but don't stop raising the corresponding # exception msg = '' raise self.http_to_marconi[resp.status_code](msg) # NOTE(flaper87): This reads the whole content # and will consume any attempt of streaming. return response.Response(request, resp.text, headers=resp.headers)
def test_queue_stats(self): result = { "messages": { "free": 146929, "claimed": 2409, "total": 149338, "oldest": { "href": "/v1/queues/qq/messages/50b68a50d6f5b8c8a7c62b01", "age": 63, "created": "2013-08-12T20:44:55Z" }, "newest": { "href": "/v1/queues/qq/messages/50b68a50d6f5b8c8a7c62b01", "age": 12, "created": "2013-08-12T20:45:46Z" } } } with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(result)) send_method.return_value = resp stats = self.queue.stats self.assertEqual(result, stats)
def test_queue_exists(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) send_method.return_value = resp self.queue.exists()
def test_queue_metadata(self): test_metadata = {'type': 'Bank Accounts'} with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(test_metadata)) send_method.return_value = resp metadata = self.queue.metadata() self.assertEqual(metadata, test_metadata)
def test_shard_delete(self): shard_data = {'weight': 10, 'uri': 'sqlite://'} with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) send_method.return_value = resp # NOTE(flaper87): This will call # ensure exists in the client instance # since auto_create's default is True shard = self.client.shard('test', **shard_data) shard.delete()
def test_message_get(self): returned = { 'href': '/v1/queues/fizbit/messages/50b68a50d6f5b8c8a7c62b01', 'ttl': 800, 'age': 790, 'body': {'event': 'ActivateAccount', 'mode': 'active'} } with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(returned)) send_method.return_value = resp msgs = self.queue.message('50b68a50d6f5b8c8a7c62b01') self.assertIsInstance(msgs, message.Message)
def test_message_post(self): messages = [{'ttl': 30, 'body': 'Post It!'}] result = { "resources": [ "/v1/queues/fizbit/messages/50b68a50d6f5b8c8a7c62b01" ], "partial": False } with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(result)) send_method.return_value = resp posted = self.queue.post(messages) self.assertEqual(result, posted)
def test_message_list(self): returned = { 'links': [{ 'rel': 'next', 'href': '/v1/queues/fizbit/messages?marker=6244-244224-783' }], 'messages': [{ 'href': '/v1/queues/fizbit/messages/50b68a50d6f5b8c8a7c62b01', 'ttl': 800, 'age': 790, 'body': {'event': 'ActivateAccount', 'mode': 'active'} }] } with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(returned)) send_method.return_value = resp msgs = self.queue.messages(limit=1) self.assertIsInstance(msgs, message._MessageIterator)
def test_health(self): cli = client.Client('http://example.com', VERSION, {}) with mock.patch.object(core, 'health', autospec=True) as core_health: resp = response.Response(None, None) core_health.return_value = resp self.assertIsNotNone(cli.health())