def test_context(): called = [] class Backend: async def perform_send(self, target_id, message, attachments, kwargs): called.append(("ps", message)) async def perform_api_call(self, method, kwargs): called.append(("pac", method, kwargs)) ctx = Context(backend=Backend()) ctx.default_target_id = 1 asyncio.get_event_loop().run_until_complete(ctx.reply("hey1")) asyncio.get_event_loop().run_until_complete(ctx.send_message(1, "hey2")) asyncio.get_event_loop().run_until_complete(ctx.request("a", v="hey3")) assert called == [ ("ps", "hey1"), ("ps", "hey2"), ("pac", "a", {"v": "hey3"}), ] called.clear() message = "a" * 4096 + "b" * 4096 + "c" * 4096 asyncio.get_event_loop().run_until_complete(ctx.reply(message)) assert called == [ ("ps", message[:4096]), ("ps", message[4096: 4096 * 2]), ("ps", message[4096 * 2:]), ]
def test_lont_message_for_kwargs(): ctx = Context(backend=CoroutineMock(perform_send=CoroutineMock())) ctx.default_target_id = 1 asyncio.get_event_loop().run_until_complete(ctx.reply("a" * (4096 * 2 - 1))) ctx.backend.perform_send.assert_any_await(1, "a" * 4096, (), {}) ctx.backend.perform_send.assert_any_await(1, "a" * 4095, (), {})
def test_reply_no_default_target(): ctx = Context(backend=MagicMock(execute_send=CoroutineMock())) ctx.default_target_id = None with pytest.raises(RuntimeError): asyncio.get_event_loop().run_until_complete(ctx.reply("hey")) ctx.backend.execute_send.assert_not_called()
def test_reply_non_str(): ctx = Context(backend=MagicMock(perform_send=CoroutineMock())) ctx.default_target_id = 1 asyncio.get_event_loop().run_until_complete(ctx.reply(123)) ctx.backend.perform_send.assert_called_with(1, '123', (), {})