diff --git a/tests/mocked_bot.py b/tests/mocked_bot.py index 680e4883..af86098f 100644 --- a/tests/mocked_bot.py +++ b/tests/mocked_bot.py @@ -35,7 +35,7 @@ class MockedSession(BaseSession): self.requests.append(method) response: Response[TelegramType] = self.responses.pop() self.check_response( - method=method, status_code=response.error_code, content=response.json() + bot=bot, method=method, status_code=response.error_code, content=response.json() ) return response.result # type: ignore diff --git a/tests/test_api/test_client/test_context_controller.py b/tests/test_api/test_client/test_context_controller.py new file mode 100644 index 00000000..25643b27 --- /dev/null +++ b/tests/test_api/test_client/test_context_controller.py @@ -0,0 +1,36 @@ +from aiogram.client.context_controller import BotContextController +from tests.mocked_bot import MockedBot + + +class MyModel(BotContextController): + id: int + + +class TestBotContextController: + def test_via_model_validate(self, bot: MockedBot): + my_model = MyModel.model_validate({"id": 1}, context={"bot": bot}) + assert my_model.id == 1 + assert my_model._bot == bot + + def test_via_model_validate_none(self): + my_model = MyModel.model_validate({"id": 1}, context={}) + assert my_model.id == 1 + assert my_model._bot is None + + def test_as(self, bot: MockedBot): + my_model = MyModel(id=1).as_(bot) + assert my_model.id == 1 + assert my_model._bot == bot + + def test_as_none(self): + my_model = MyModel(id=1).as_(None) + assert my_model.id == 1 + assert my_model._bot is None + + def test_replacement(self, bot: MockedBot): + my_model = MyModel(id=1).as_(bot) + assert my_model.id == 1 + assert my_model._bot == bot + my_model = my_model.as_(None) + assert my_model.id == 1 + assert my_model._bot is None diff --git a/tests/test_api/test_client/test_session/test_base_session.py b/tests/test_api/test_client/test_session/test_base_session.py index 5d1ebef1..3793f22a 100644 --- a/tests/test_api/test_client/test_session/test_base_session.py +++ b/tests/test_api/test_client/test_session/test_base_session.py @@ -170,9 +170,11 @@ class TestBaseSession: ) def test_check_response(self, status_code, content, error): session = CustomSession() + bot = MockedBot() method = DeleteMessage(chat_id=42, message_id=42) if error is None: session.check_response( + bot=bot, method=method, status_code=status_code, content=content, @@ -180,6 +182,7 @@ class TestBaseSession: else: with pytest.raises(error) as exc_info: session.check_response( + bot=bot, method=method, status_code=status_code, content=content, @@ -191,10 +194,12 @@ class TestBaseSession: def test_check_response_json_decode_error(self): session = CustomSession() + bot = MockedBot() method = DeleteMessage(chat_id=42, message_id=42) with pytest.raises(ClientDecodeError, match="JSONDecodeError"): session.check_response( + bot=bot, method=method, status_code=200, content="is not a JSON object", @@ -202,10 +207,12 @@ class TestBaseSession: def test_check_response_validation_error(self): session = CustomSession() + bot = MockedBot() method = DeleteMessage(chat_id=42, message_id=42) with pytest.raises(ClientDecodeError, match="ValidationError"): session.check_response( + bot=bot, method=method, status_code=200, content='{"ok": "test"}', diff --git a/tests/test_api/test_methods/test_base.py b/tests/test_api/test_methods/test_base.py index f2351d40..9626c9b7 100644 --- a/tests/test_api/test_methods/test_base.py +++ b/tests/test_api/test_methods/test_base.py @@ -22,6 +22,14 @@ class TestTelegramMethodRemoveUnset: class TestTelegramMethodCall: + async def test_async_emit_unsuccessful(self, bot: MockedBot): + with pytest.raises( + RuntimeError, + match="This method is not mounted to a any bot instance.+", + ): + await GetMe() + async def test_async_emit(self, bot: MockedBot): bot.add_result_for(GetMe, ok=True, result=User(id=42, is_bot=True, first_name="Test")) - assert isinstance(await GetMe(), User) + method = GetMe().as_(bot) + assert isinstance(await method, User)