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()
async def _(msg: Message, ctx: Context): """ Asks user for his nickname """ user_info = await get_user_info(ctx) await asyncio.wait((ctx.set_state(user_state='name'), ctx.reply(await translate("Введите своё имя", "ru", user_info.language))))
def test_replace_method(): calls = [] async def replacement(self, message, attachments=(), **kwargs): calls.append((message, attachments, kwargs)) ctx = Context() ctx.replace_method("reply", replacement) asyncio.get_event_loop().run_until_complete(ctx.reply("hey")) assert calls == [("hey", (), {})] with pytest.raises(ValueError): ctx.replace_method("a" * 64, lambda: 0)
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', (), {})