async def test_respond_mxc(self): gif_bytes = (b"GIF89a\x01\x00\x01\x00\x00\xff\x00," b"\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x00;") image = Image(url="mxc://aurl") with amock.patch(api_string.format( "send_content")) as patched_send, amock.patch( "opsdroid.events.Image.get_file_bytes") as patched_bytes: patched_bytes.return_value = asyncio.Future() patched_bytes.return_value.set_result(gif_bytes) patched_send.return_value = asyncio.Future() patched_send.return_value.set_result(None) await self.connector.send(image) patched_send.assert_called_once_with( "#test:localhost", "mxc://aurl", "opsdroid_upload", "m.image", { "w": 1, "h": 1, "mimetype": "image/gif", "size": 26 }, )
async def get_graph(self, image, date=None): """Gets info from db and saves as graph.""" if not date: time = datetime.datetime.now() date = "{}-{}-{}".format(time.day, time.month, time.year) status = await self.opsdroid.memory.get("status") labels = ["Time", "CPU", "RAM"] graph = pd.DataFrame.from_records(status[date], columns=labels) ax = plt.gca() graph.plot(kind='line', x='Time', y="RAM", ax=ax) graph.plot(kind='line', x='Time', y="CPU", ax=ax) byio = io.BytesIO() plt.savefig(byio) graph_image = Image(file_bytes=byio.getbuffer()) graph_image.name = 'daily_graph_{}'.format(date) await image.respond(graph_image) plt.close()
async def test_respond_image_failure(self): post_response = amock.Mock() post_response.status = 400 gif_bytes = (b"GIF89a\x01\x00\x01\x00\x00\xff\x00," b"\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x00;") image = Image(file_bytes=gif_bytes, target={"id": '123'}) with amock.patch.object(self.connector.session, 'post') \ as patched_request: patched_request.return_value = asyncio.Future() patched_request.return_value.set_result(post_response) await self.connector.send_image(image) self.assertLogs("_LOOGER", "debug")
async def test_respond_image(self): post_response = amock.Mock() post_response.status = 200 gif_bytes = (b"GIF89a\x01\x00\x01\x00\x00\xff\x00," b"\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x00;") image = Image(file_bytes=gif_bytes, target={"id": "123"}) with amock.patch.object(self.connector.session, "post") as patched_request: patched_request.return_value = asyncio.Future() patched_request.return_value.set_result(post_response) await self.connector.send_image(image) self.assertTrue(patched_request.called)
async def test_respond_mxc(self): gif_bytes = (b"GIF89a\x01\x00\x01\x00\x00\xff\x00," b"\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x00;") image = Image(url="mxc://aurl") with amock.patch(api_string.format("send_content")) as patched_send, \ amock.patch("opsdroid.events.Image.get_file_bytes") as patched_bytes: patched_bytes.return_value = asyncio.Future() patched_bytes.return_value.set_result(gif_bytes) patched_send.return_value = asyncio.Future() patched_send.return_value.set_result(None) await self.connector.send(image) patched_send.assert_called_once_with( '#test:localhost', 'mxc://aurl', 'opsdroid_upload', 'm.image', {'w': 1, 'h': 1, 'mimetype': 'image/gif', 'size': 26})