2024-04-06 03:26:51 +07:00
|
|
|
from typing import Any, Dict
|
2023-03-11 20:46:36 +02:00
|
|
|
from unittest.mock import sentinel
|
2019-12-26 01:35:21 +02:00
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
2024-04-06 03:26:51 +07:00
|
|
|
from aiogram.client.default import Default
|
|
|
|
|
from aiogram.methods import GetMe, SendMessage, TelegramMethod
|
|
|
|
|
from aiogram.types import LinkPreviewOptions, TelegramObject, User
|
2020-06-14 18:18:29 +03:00
|
|
|
from tests.mocked_bot import MockedBot
|
2019-12-26 01:35:21 +02:00
|
|
|
|
|
|
|
|
|
2023-03-11 20:46:36 +02:00
|
|
|
class TestTelegramMethodRemoveUnset:
|
2019-12-26 01:35:21 +02:00
|
|
|
@pytest.mark.parametrize(
|
2023-03-11 20:46:36 +02:00
|
|
|
"values,names",
|
2019-12-26 01:35:21 +02:00
|
|
|
[
|
2023-03-11 20:46:36 +02:00
|
|
|
[{}, set()],
|
|
|
|
|
[{"foo": "bar"}, {"foo"}],
|
|
|
|
|
[{"foo": "bar", "baz": sentinel.DEFAULT}, {"foo"}],
|
2019-12-26 01:35:21 +02:00
|
|
|
],
|
|
|
|
|
)
|
2023-08-29 02:01:54 +03:00
|
|
|
@pytest.mark.parametrize("obj", [TelegramMethod, TelegramObject])
|
|
|
|
|
def test_remove_unset(self, values, names, obj):
|
2024-05-20 16:05:39 +07:00
|
|
|
validated = obj.remove_unset(values)
|
2023-03-11 20:46:36 +02:00
|
|
|
assert set(validated.keys()) == names
|
2019-12-26 01:35:21 +02:00
|
|
|
|
2023-08-29 02:01:54 +03:00
|
|
|
@pytest.mark.parametrize("obj", [TelegramMethod, TelegramObject])
|
|
|
|
|
def test_remove_unset_non_dict(self, obj):
|
2024-05-20 16:05:39 +07:00
|
|
|
assert obj.remove_unset("") == ""
|
2023-08-29 02:01:54 +03:00
|
|
|
|
2019-12-26 01:35:21 +02:00
|
|
|
|
2024-05-20 15:55:06 +07:00
|
|
|
class TestTelegramMethodModelDumpJson:
|
2024-04-06 03:26:51 +07:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"obj",
|
|
|
|
|
[
|
|
|
|
|
SendMessage(
|
|
|
|
|
chat_id=1,
|
|
|
|
|
text="test",
|
|
|
|
|
),
|
|
|
|
|
LinkPreviewOptions(),
|
|
|
|
|
],
|
|
|
|
|
)
|
2024-05-20 15:55:06 +07:00
|
|
|
def test_model_dump_json(self, obj):
|
2024-04-06 03:26:51 +07:00
|
|
|
def has_defaults(dump: Dict[str, Any]) -> bool:
|
|
|
|
|
return any(isinstance(value, Default) for value in dump.values())
|
|
|
|
|
|
|
|
|
|
assert has_defaults(obj.model_dump())
|
|
|
|
|
assert not has_defaults(obj.model_dump(mode="json"))
|
|
|
|
|
|
|
|
|
|
|
2023-03-11 20:46:36 +02:00
|
|
|
class TestTelegramMethodCall:
|
2023-07-11 23:17:26 +03:00
|
|
|
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()
|
|
|
|
|
|
2023-03-11 20:46:36 +02:00
|
|
|
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"))
|
2023-07-11 23:17:26 +03:00
|
|
|
method = GetMe().as_(bot)
|
|
|
|
|
assert isinstance(await method, User)
|