diff --git a/Makefile b/Makefile index 6a2f442a..d4cac3ac 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,9 @@ help: @echo "Tests:" @echo " test: Run tests" @echo " test-coverage: Run tests with HTML reporting (results + coverage)" - @echo " test-coverage-report: Open coverage report in default system web browser" + @echo " test-coverage-report: Generate HTML report of coverage" + @echo " test-coverage-view: Open coverage report in default system web browser" + @echo " test-coverage-and-view: Run tests with HTML report, and open it in default system web browser" @echo "" @echo "Documentation:" @echo " docs: Build docs" @@ -68,8 +70,12 @@ clean: # Code quality # ================================================================================================= +.PHONE: isort_seed_config +isort_update_known_libraries: + $(py) seed-isort-config + .PHONY: isort -isort: +isort: isort_update_known_libraries # update config in $(py) isort -rc aiogram tests .PHONY: black @@ -78,7 +84,7 @@ black: .PHONY: flake8 flake8: - $(py) flake8 aiogram + $(py) flake8 --config=.flake8 aiogram tests .PHONY: flake8-report flake8-report: @@ -115,10 +121,12 @@ test-coverage-report: $(py) coverage html -d $(reports_dir)/coverage .PHONY: test-coverage-view -test-coverage-view: - $(py) coverage html -d $(reports_dir)/coverage +test-coverage-view: test-coverage-report python -c "import webbrowser; webbrowser.open('file://$(shell pwd)/reports/coverage/index.html')" +.PHONY: test-coverage-and-view +test-coverage-and-view: test-coverage test-coverage-view + # ================================================================================================= # Docs # ================================================================================================= diff --git a/aiogram/api/types/chat.py b/aiogram/api/types/chat.py index 2e3163b2..7354d641 100644 --- a/aiogram/api/types/chat.py +++ b/aiogram/api/types/chat.py @@ -2,6 +2,7 @@ from __future__ import annotations from typing import TYPE_CHECKING, Optional +from ...utils import helper from .base import TelegramObject if TYPE_CHECKING: # pragma: no cover @@ -51,3 +52,20 @@ class Chat(TelegramObject): """For supergroups, name of group sticker set. Returned only in getChat.""" can_set_sticker_set: Optional[bool] = None """True, if the bot can change the group sticker set. Returned only in getChat.""" + + +class ChatType(helper.Helper): + """ + List of chat types + :key: PRIVATE + :key: GROUP + :key: SUPER_GROUP + :key: CHANNEL + """ + + mode = helper.HelperMode.lowercase + + PRIVATE = helper.Item() # private + GROUP = helper.Item() # group + SUPER_GROUP = helper.Item() # supergroup + CHANNEL = helper.Item() # channel diff --git a/poetry.lock b/poetry.lock index d81c8b2f..730cd588 100644 --- a/poetry.lock +++ b/poetry.lock @@ -65,6 +65,17 @@ version = "1.1.2" aiohttp = ">=3.1.0,<4.0.0" pytest-asyncio = "*" +[[package]] +category = "dev" +description = "Utilities for refactoring imports in python-like syntax." +name = "aspy.refactor-imports" +optional = false +python-versions = ">=3.6.1" +version = "2.1.1" + +[package.dependencies] +cached-property = "*" + [[package]] category = "main" description = "Simple lru_cache for asyncio" @@ -172,6 +183,14 @@ version = ">=0.9" filecache = ["lockfile (>=0.9)"] redis = ["redis (>=2.10.5)"] +[[package]] +category = "dev" +description = "A decorator for caching properties in classes." +name = "cached-property" +optional = false +python-versions = "*" +version = "1.5.1" + [[package]] category = "dev" description = "Cachy provides a simple yet effective caching library." @@ -308,6 +327,29 @@ optional = false python-versions = "*" version = "0.3.0" +[[package]] +category = "dev" +description = "A versatile test fixtures replacement based on thoughtbot's factory_bot for Ruby." +name = "factory-boy" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" +version = "2.12.0" + +[package.dependencies] +Faker = ">=0.7.0" + +[[package]] +category = "dev" +description = "Faker is a Python package that generates fake data for you." +name = "faker" +optional = false +python-versions = ">=3.4" +version = "4.1.1" + +[package.dependencies] +python-dateutil = ">=2.4" +text-unidecode = "1.3" + [[package]] category = "dev" description = "A platform independent file lock." @@ -1074,13 +1116,13 @@ description = "Pytest support for asyncio." name = "pytest-asyncio" optional = false python-versions = ">= 3.5" -version = "0.10.0" +version = "0.14.0" [package.dependencies] -pytest = ">=3.0.6" +pytest = ">=5.4.0" [package.extras] -testing = ["async-generator (>=1.3)", "coverage", "hypothesis (>=3.64)"] +testing = ["async-generator (>=1.3)", "coverage", "hypothesis (>=5.7.1)"] [[package]] category = "dev" @@ -1155,6 +1197,17 @@ version = ">=0.700" python = ">=3.5" version = ">=2.8" +[[package]] +category = "dev" +description = "Extensions to the standard Python datetime module" +name = "python-dateutil" +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7" +version = "2.8.1" + +[package.dependencies] +six = ">=1.5" + [[package]] category = "main" description = "World timezone definitions, modern and historical" @@ -1177,7 +1230,7 @@ category = "dev" description = "YAML parser and emitter for Python" name = "pyyaml" optional = false -python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +python-versions = "*" version = "5.3.1" [[package]] @@ -1230,6 +1283,17 @@ version = "3.1.2" cryptography = "*" jeepney = ">=0.4.2" +[[package]] +category = "dev" +description = "Statically populate the `known_third_party` `isort` setting." +name = "seed-isort-config" +optional = false +python-versions = ">=3.6.1" +version = "2.2.0" + +[package.dependencies] +"aspy.refactor-imports" = "*" + [[package]] category = "dev" description = "Tool to Detect Surrounding Shell" @@ -1246,6 +1310,14 @@ optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*" version = "1.15.0" +[[package]] +category = "dev" +description = "The most basic Text::Unidecode port" +name = "text-unidecode" +optional = false +python-versions = "*" +version = "1.3" + [[package]] category = "dev" description = "Python Library for Tom's Obvious, Minimal Language" @@ -1403,7 +1475,7 @@ fast = ["uvloop"] proxy = ["aiohttp-socks"] [metadata] -content-hash = "152bb9b155a00baadd3c8b9fa21f08af719180bddccb8ad6c3dd6548c3e71e3e" +content-hash = "ad330c030959fb1ada05c2a4173a3d5725dbbc9051cc658859c9a32551ea59d5" python-versions = "^3.7" [metadata.files] @@ -1441,6 +1513,10 @@ aresponses = [ {file = "aresponses-1.1.2-py3-none-any.whl", hash = "sha256:639defa70730a2a0d5c7af5e42e88b7558e444ba0d43c44917efc57fb0d3b60e"}, {file = "aresponses-1.1.2.tar.gz", hash = "sha256:20a63536d86af6f31f9b0720c561bdc595b6bfe071940e347ab58b11caff9e1b"}, ] +"aspy.refactor-imports" = [ + {file = "aspy.refactor_imports-2.1.1-py2.py3-none-any.whl", hash = "sha256:9df76bf19ef81620068b785a386740ab3c8939fcbdcebf20c4a4e0057230d782"}, + {file = "aspy.refactor_imports-2.1.1.tar.gz", hash = "sha256:eec8d1a73bedf64ffb8b589ad919a030c1fb14acf7d1ce0ab192f6eedae895c5"}, +] async-lru = [ {file = "async_lru-1.0.2.tar.gz", hash = "sha256:baa898027619f5cc31b7966f96f00e4fc0df43ba206a8940a5d1af5336a477cb"}, ] @@ -1476,6 +1552,10 @@ cachecontrol = [ {file = "CacheControl-0.12.6-py2.py3-none-any.whl", hash = "sha256:10d056fa27f8563a271b345207402a6dcce8efab7e5b377e270329c62471b10d"}, {file = "CacheControl-0.12.6.tar.gz", hash = "sha256:be9aa45477a134aee56c8fac518627e1154df063e85f67d4f83ce0ccc23688e8"}, ] +cached-property = [ + {file = "cached-property-1.5.1.tar.gz", hash = "sha256:9217a59f14a5682da7c4b8829deadbfc194ac22e9908ccf7c8820234e80a1504"}, + {file = "cached_property-1.5.1-py2.py3-none-any.whl", hash = "sha256:3a026f1a54135677e7da5ce819b0c690f156f37976f3e30c5430740725203d7f"}, +] cachy = [ {file = "cachy-0.3.0-py2.py3-none-any.whl", hash = "sha256:338ca09c8860e76b275aff52374330efedc4d5a5e45dc1c5b539c1ead0786fe7"}, {file = "cachy-0.3.0.tar.gz", hash = "sha256:186581f4ceb42a0bbe040c407da73c14092379b1e4c0e327fdb72ae4a9b269b1"}, @@ -1599,6 +1679,14 @@ decorator = [ distlib = [ {file = "distlib-0.3.0.zip", hash = "sha256:2e166e231a26b36d6dfe35a48c4464346620f8645ed0ace01ee31822b288de21"}, ] +factory-boy = [ + {file = "factory_boy-2.12.0-py2.py3-none-any.whl", hash = "sha256:728df59b372c9588b83153facf26d3d28947fc750e8e3c95cefa9bed0e6394ee"}, + {file = "factory_boy-2.12.0.tar.gz", hash = "sha256:faf48d608a1735f0d0a3c9cbf536d64f9132b547dae7ba452c4d99a79e84a370"}, +] +faker = [ + {file = "Faker-4.1.1-py3-none-any.whl", hash = "sha256:1290f589648bc470b8d98fff1fdff773fe3f46b4ca2cac73ac74668b12cf008e"}, + {file = "Faker-4.1.1.tar.gz", hash = "sha256:c006b3664c270a2cfd4785c5e41ff263d48101c4e920b5961cf9c237131d8418"}, +] filelock = [ {file = "filelock-3.0.12-py3-none-any.whl", hash = "sha256:929b7d63ec5b7d6b71b0fa5ac14e030b3f70b75747cef1b10da9b879fef15836"}, {file = "filelock-3.0.12.tar.gz", hash = "sha256:18d82244ee114f543149c66a6e0c14e9c4f8a1044b5cdaadd0f82159d6a6ff59"}, @@ -1743,6 +1831,11 @@ markupsafe = [ {file = "MarkupSafe-1.1.1-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:ba59edeaa2fc6114428f1637ffff42da1e311e29382d81b339c1817d37ec93c6"}, {file = "MarkupSafe-1.1.1-cp37-cp37m-win32.whl", hash = "sha256:b00c1de48212e4cc9603895652c5c410df699856a2853135b3967591e4beebc2"}, {file = "MarkupSafe-1.1.1-cp37-cp37m-win_amd64.whl", hash = "sha256:9bf40443012702a1d2070043cb6291650a0841ece432556f784f004937f0f32c"}, + {file = "MarkupSafe-1.1.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:6788b695d50a51edb699cb55e35487e430fa21f1ed838122d722e0ff0ac5ba15"}, + {file = "MarkupSafe-1.1.1-cp38-cp38-manylinux1_i686.whl", hash = "sha256:cdb132fc825c38e1aeec2c8aa9338310d29d337bebbd7baa06889d09a60a1fa2"}, + {file = "MarkupSafe-1.1.1-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:13d3144e1e340870b25e7b10b98d779608c02016d5184cfb9927a9f10c689f42"}, + {file = "MarkupSafe-1.1.1-cp38-cp38-win32.whl", hash = "sha256:596510de112c685489095da617b5bcbbac7dd6384aeebeda4df6025d0256a81b"}, + {file = "MarkupSafe-1.1.1-cp38-cp38-win_amd64.whl", hash = "sha256:e8313f01ba26fbbe36c7be1966a7b7424942f670f38e666995b88d012765b9be"}, {file = "MarkupSafe-1.1.1.tar.gz", hash = "sha256:29872e92839765e546828bb7754a68c418d927cd064fd4708fab9fe9c8bb116b"}, ] mccabe = [ @@ -1936,8 +2029,8 @@ pytest = [ {file = "pytest-5.4.3.tar.gz", hash = "sha256:7979331bfcba207414f5e1263b5a0f8f521d0f457318836a7355531ed1a4c7d8"}, ] pytest-asyncio = [ - {file = "pytest-asyncio-0.10.0.tar.gz", hash = "sha256:9fac5100fd716cbecf6ef89233e8590a4ad61d729d1732e0a96b84182df1daaf"}, - {file = "pytest_asyncio-0.10.0-py3-none-any.whl", hash = "sha256:d734718e25cfc32d2bf78d346e99d33724deeba774cc4afdf491530c6184b63b"}, + {file = "pytest-asyncio-0.14.0.tar.gz", hash = "sha256:9882c0c6b24429449f5f969a5158b528f39bde47dc32e85b9f0403965017e700"}, + {file = "pytest_asyncio-0.14.0-py3-none-any.whl", hash = "sha256:2eae1e34f6c68fc0a9dc12d4bea190483843ff4708d24277c41568d6b6044f1d"}, ] pytest-cov = [ {file = "pytest-cov-2.10.0.tar.gz", hash = "sha256:1a629dc9f48e53512fcbfda6b07de490c374b0c83c55ff7a1720b3fccff0ac87"}, @@ -1959,6 +2052,10 @@ pytest-mypy = [ {file = "pytest-mypy-0.4.2.tar.gz", hash = "sha256:5a5338cecff17f005b181546a13e282761754b481225df37f33d37f86ac5b304"}, {file = "pytest_mypy-0.4.2-py3-none-any.whl", hash = "sha256:3b7b56912d55439d5f447cc609f91caac7f74f0f1c89f1379d04f06bac777c32"}, ] +python-dateutil = [ + {file = "python-dateutil-2.8.1.tar.gz", hash = "sha256:73ebfe9dbf22e832286dafa60473e4cd239f8592f699aa5adaf10050e6e1823c"}, + {file = "python_dateutil-2.8.1-py2.py3-none-any.whl", hash = "sha256:75bb3f31ea686f1197762692a9ee6a7550b59fc6ca3a1f4b5d7e32fb98e2da2a"}, +] pytz = [ {file = "pytz-2020.1-py2.py3-none-any.whl", hash = "sha256:a494d53b6d39c3c6e44c3bec237336e14305e4f29bbf800b599253057fbb79ed"}, {file = "pytz-2020.1.tar.gz", hash = "sha256:c35965d010ce31b23eeb663ed3cc8c906275d6be1a34393a1d73a41febf4a048"}, @@ -2004,6 +2101,7 @@ regex = [ {file = "regex-2020.6.8.tar.gz", hash = "sha256:e9b64e609d37438f7d6e68c2546d2cb8062f3adb27e6336bc129b51be20773ac"}, ] requests = [ + {file = "requests-2.23.0-py2.7.egg", hash = "sha256:5d2d0ffbb515f39417009a46c14256291061ac01ba8f875b90cad137de83beb4"}, {file = "requests-2.23.0-py2.py3-none-any.whl", hash = "sha256:43999036bfa82904b6af1d99e4882b560e5e2c68e5c4b0aa03b655f3d7d73fee"}, {file = "requests-2.23.0.tar.gz", hash = "sha256:b3f43d496c6daba4493e7c431722aeb7dbc6288f52a6e04e7b6023b0247817e6"}, ] @@ -2015,6 +2113,10 @@ secretstorage = [ {file = "SecretStorage-3.1.2-py3-none-any.whl", hash = "sha256:b5ec909dde94d4ae2fa26af7c089036997030f0cf0a5cb372b4cccabd81c143b"}, {file = "SecretStorage-3.1.2.tar.gz", hash = "sha256:15da8a989b65498e29be338b3b279965f1b8f09b9668bd8010da183024c8bff6"}, ] +seed-isort-config = [ + {file = "seed_isort_config-2.2.0-py2.py3-none-any.whl", hash = "sha256:8601fb715a5a4aac39256bbf73c2da6a81f964da9c9d9897ab9074db3663526f"}, + {file = "seed_isort_config-2.2.0.tar.gz", hash = "sha256:be4cfef8f9a3fe8ea1817069c6b624538ac0b429636ec746edeb27e98ed628c8"}, +] shellingham = [ {file = "shellingham-1.3.2-py2.py3-none-any.whl", hash = "sha256:7f6206ae169dc1a03af8a138681b3f962ae61cc93ade84d0585cca3aaf770044"}, {file = "shellingham-1.3.2.tar.gz", hash = "sha256:576c1982bea0ba82fb46c36feb951319d7f42214a82634233f58b40d858a751e"}, @@ -2023,6 +2125,10 @@ six = [ {file = "six-1.15.0-py2.py3-none-any.whl", hash = "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced"}, {file = "six-1.15.0.tar.gz", hash = "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259"}, ] +text-unidecode = [ + {file = "text-unidecode-1.3.tar.gz", hash = "sha256:bad6603bb14d279193107714b288be206cac565dfa49aa5b105294dd5c4aab93"}, + {file = "text_unidecode-1.3-py2.py3-none-any.whl", hash = "sha256:1311f10e8b895935241623731c2ba64f4c455287888b18189350b67134a822e8"}, +] toml = [ {file = "toml-0.10.1-py2.py3-none-any.whl", hash = "sha256:bda89d5935c2eac546d648028b9901107a595863cb36bae0c73ac804a9b4ce88"}, {file = "toml-0.10.1.tar.gz", hash = "sha256:926b612be1e5ce0634a2ca03470f95169cf16f939018233a670519cb4ac58b0f"}, diff --git a/pyproject.toml b/pyproject.toml index 22efdce3..e6e7323f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,7 +45,7 @@ aiohttp-socks = {version = "^0.3.8", optional = true} uvloop = {version = "^0.14.0", markers = "sys_platform == 'darwin' or sys_platform == 'linux'"} pytest = "^5.3" pytest-html = "^2.0" -pytest-asyncio = "^0.10.0" +pytest-asyncio = "^0.14.0" pytest-mypy = "^0.4.2" pytest-mock = "^2.0" pytest-cov = "^2.8" @@ -69,6 +69,8 @@ pre-commit = "^2.3.0" packaging = "^20.3" typing-extensions = "^3.7.4" poetry = "^1.0.5" +factory_boy = "^2.12.0" +seed-isort-config = "^2.2.0" [tool.poetry.extras] fast = ["uvloop"] @@ -95,7 +97,7 @@ include_trailing_comma = true force_grid_wrap = 0 use_parentheses = true line_length = 99 -known_third_party = ["aiofiles", "aiohttp", "aiohttp_socks", "aresponses", "async_lru", "packaging", "pkg_resources", "pydantic", "pytest"] +known_third_party = ["aiofiles", "aiohttp", "aiohttp_socks", "aresponses", "async_lru", "factory", "packaging", "poetry", "pydantic", "pytest", "typing_extensions"] [build-system] requires = ["poetry>=0.12"] diff --git a/tests/conftest.py b/tests/conftest.py index 60d9d0fe..3bb41292 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,7 @@ import pytest from aiogram import Bot +from tests.factories.chat import ChatFactory from tests.mocked_bot import MockedBot @@ -11,3 +12,8 @@ def bot(): yield bot Bot.reset_current(token) bot.me.invalidate(bot) + + +@pytest.fixture() +def private_chat(): + return ChatFactory() diff --git a/tests/factories/__init__.py b/tests/factories/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/factories/chat.py b/tests/factories/chat.py new file mode 100644 index 00000000..77b0f607 --- /dev/null +++ b/tests/factories/chat.py @@ -0,0 +1,35 @@ +import factory + +from aiogram.api.types import Chat +from aiogram.api.types.chat import ChatType +from tests.factories import sequences + + +class ChatFactory(factory.Factory): + class Meta: + model = Chat + + id = None # lazy attribute + first_name = sequences.first_name + last_name = sequences.last_name + username = sequences.username + type = ChatType.PRIVATE + + @factory.lazy_attribute_sequence + def id(self, n): + _id = n + if self.type is ChatType.CHANNEL: + _id = -_id + return _id + + @factory.lazy_attribute_sequence + def title(self, n): + if self.type is ChatType.CHANNEL: + return f"Title #{n}" + + def __new__(cls, *args, **kwargs) -> "ChatFactory.Meta.model": + """ + This is a dirty hack for correct type hints + See https://github.com/FactoryBoy/factory_boy/issues/468#issuecomment-505646794 + """ + return super().__new__(*args, **kwargs) diff --git a/tests/factories/chat_member.py b/tests/factories/chat_member.py new file mode 100644 index 00000000..8202faa4 --- /dev/null +++ b/tests/factories/chat_member.py @@ -0,0 +1,20 @@ +import factory + +from aiogram.api.types.chat_member import ChatMember, ChatMemberStatus +from tests.factories.user import UserFactory + + +class ChatMemberFactory(factory.Factory): + class Meta: + model = ChatMember + + user = factory.SubFactory(UserFactory) + + status = ChatMemberStatus.ADMINISTRATOR + + def __new__(cls, *args, **kwargs) -> "ChatMemberFactory.Meta.model": + """ + This is a dirty hack for correct type hints + See https://github.com/FactoryBoy/factory_boy/issues/468#issuecomment-505646794 + """ + return super().__new__(*args, **kwargs) diff --git a/tests/factories/inline_query.py b/tests/factories/inline_query.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/factories/message.py b/tests/factories/message.py new file mode 100644 index 00000000..c4eec6f4 --- /dev/null +++ b/tests/factories/message.py @@ -0,0 +1,27 @@ +from datetime import datetime + +import factory + +from aiogram.api.types import Message +from tests.factories import sequences +from tests.factories.chat import ChatFactory +from tests.factories.user import UserFactory + + +class MessageFactory(factory.Factory): + class Meta: + model = Message + + message_id = sequences.id_ + from_user = factory.SubFactory(UserFactory) + chat = factory.SubFactory(ChatFactory) + text = factory.Sequence(lambda n: f"Message text #{n}") + + date = factory.LazyFunction(lambda: datetime.now().toordinal()) + + def __new__(cls, *args, **kwargs) -> "MessageFactory.Meta.model": + """ + This is a dirty hack for correct type hints + See https://github.com/FactoryBoy/factory_boy/issues/468#issuecomment-505646794 + """ + return super().__new__(*args, **kwargs) diff --git a/tests/factories/sequences.py b/tests/factories/sequences.py new file mode 100644 index 00000000..0d4de041 --- /dev/null +++ b/tests/factories/sequences.py @@ -0,0 +1,6 @@ +import factory + +id_ = factory.Sequence(lambda n: n) +first_name = factory.Sequence(lambda n: f"First name #{n}") +last_name = factory.Sequence(lambda n: f"Last name #{n}") +username = factory.Sequence(lambda n: f"Username #{n}") diff --git a/tests/factories/user.py b/tests/factories/user.py new file mode 100644 index 00000000..b52efe77 --- /dev/null +++ b/tests/factories/user.py @@ -0,0 +1,20 @@ +import factory + +from aiogram.api.types import User +from tests.factories import sequences + + +class UserFactory(factory.Factory): + class Meta: + model = User + + id = sequences.id_ + first_name = factory.Sequence(lambda n: f"First name #{n}") + is_bot = False + + def __new__(cls, *args, **kwargs) -> "UserFactory.Meta.model": + """ + This is a dirty hack for correct type hints + See https://github.com/FactoryBoy/factory_boy/issues/468#issuecomment-505646794 + """ + return super().__new__(*args, **kwargs) diff --git a/tests/test_api/test_methods/test_edit_message_caption.py b/tests/test_api/test_methods/test_edit_message_caption.py index 50fcf838..648959a6 100644 --- a/tests/test_api/test_methods/test_edit_message_caption.py +++ b/tests/test_api/test_methods/test_edit_message_caption.py @@ -1,26 +1,17 @@ -import datetime from typing import Union import pytest from aiogram.api.methods import EditMessageCaption, Request -from aiogram.api.types import Chat, Message +from aiogram.api.types import Message +from tests.factories.message import MessageFactory from tests.mocked_bot import MockedBot class TestEditMessageCaption: @pytest.mark.asyncio async def test_method(self, bot: MockedBot): - prepare_result = bot.add_result_for( - EditMessageCaption, - ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - text="text", - chat=Chat(id=42, type="private"), - ), - ) + prepare_result = bot.add_result_for(EditMessageCaption, ok=True, result=MessageFactory()) response: Union[Message, bool] = await EditMessageCaption() request: Request = bot.get_request() @@ -29,16 +20,7 @@ class TestEditMessageCaption: @pytest.mark.asyncio async def test_bot_method(self, bot: MockedBot): - prepare_result = bot.add_result_for( - EditMessageCaption, - ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - text="text", - chat=Chat(id=42, type="private"), - ), - ) + prepare_result = bot.add_result_for(EditMessageCaption, ok=True, result=MessageFactory()) response: Union[Message, bool] = await bot.edit_message_caption() request: Request = bot.get_request() diff --git a/tests/test_api/test_methods/test_edit_message_media.py b/tests/test_api/test_methods/test_edit_message_media.py index 882ae98a..3c9d243c 100644 --- a/tests/test_api/test_methods/test_edit_message_media.py +++ b/tests/test_api/test_methods/test_edit_message_media.py @@ -3,7 +3,7 @@ from typing import Union import pytest from aiogram.api.methods import EditMessageMedia, Request -from aiogram.api.types import BufferedInputFile, InputMedia, InputMediaPhoto, Message +from aiogram.api.types import BufferedInputFile, InputMediaPhoto, Message from tests.mocked_bot import MockedBot diff --git a/tests/test_api/test_methods/test_forward_message.py b/tests/test_api/test_methods/test_forward_message.py index 3e2ae0d9..15d4557b 100644 --- a/tests/test_api/test_methods/test_forward_message.py +++ b/tests/test_api/test_methods/test_forward_message.py @@ -4,43 +4,30 @@ import pytest from aiogram.api.methods import ForwardMessage, Request from aiogram.api.types import Chat, Message +from tests.factories.message import MessageFactory from tests.mocked_bot import MockedBot class TestForwardMessage: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): - prepare_result = bot.add_result_for( - ForwardMessage, - ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - chat=Chat(id=42, title="chat", type="private"), - text="text", - ), - ) + async def test_method(self, bot: MockedBot, private_chat: Chat): + prepare_result = bot.add_result_for(ForwardMessage, ok=True, result=MessageFactory()) - response: Message = await ForwardMessage(chat_id=42, from_chat_id=42, message_id=42) + response: Message = await ForwardMessage( + chat_id=private_chat.id, from_chat_id=private_chat.id, message_id=42 + ) request: Request = bot.get_request() assert request.method == "forwardMessage" # assert request.data == {} assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): - prepare_result = bot.add_result_for( - ForwardMessage, - ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - chat=Chat(id=42, title="chat", type="private"), - text="text", - ), - ) + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): + prepare_result = bot.add_result_for(ForwardMessage, ok=True, result=MessageFactory()) - response: Message = await bot.forward_message(chat_id=42, from_chat_id=42, message_id=42) + response: Message = await bot.forward_message( + chat_id=private_chat.id, from_chat_id=private_chat.id, message_id=42 + ) request: Request = bot.get_request() assert request.method == "forwardMessage" # assert request.data == {} diff --git a/tests/test_api/test_methods/test_get_chat.py b/tests/test_api/test_methods/test_get_chat.py index d88b5261..41b5aea8 100644 --- a/tests/test_api/test_methods/test_get_chat.py +++ b/tests/test_api/test_methods/test_get_chat.py @@ -2,28 +2,28 @@ import pytest from aiogram.api.methods import GetChat, Request from aiogram.api.types import Chat +from aiogram.api.types.chat import ChatType +from tests.factories.chat import ChatFactory from tests.mocked_bot import MockedBot class TestGetChat: @pytest.mark.asyncio async def test_method(self, bot: MockedBot): - prepare_result = bot.add_result_for( - GetChat, ok=True, result=Chat(id=-42, type="channel", title="chat") - ) + channel = ChatFactory(type=ChatType.CHANNEL) + prepare_result = bot.add_result_for(GetChat, ok=True, result=channel) - response: Chat = await GetChat(chat_id=-42) + response: Chat = await GetChat(chat_id=channel.id) request: Request = bot.get_request() assert request.method == "getChat" assert response == prepare_result.result @pytest.mark.asyncio async def test_bot_method(self, bot: MockedBot): - prepare_result = bot.add_result_for( - GetChat, ok=True, result=Chat(id=-42, type="channel", title="chat") - ) + channel = ChatFactory(type=ChatType.CHANNEL) + prepare_result = bot.add_result_for(GetChat, ok=True, result=channel) - response: Chat = await bot.get_chat(chat_id=-42) + response: Chat = await bot.get_chat(chat_id=channel.id) request: Request = bot.get_request() assert request.method == "getChat" assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_get_chat_administrators.py b/tests/test_api/test_methods/test_get_chat_administrators.py index f5e76c2e..7fc58f23 100644 --- a/tests/test_api/test_methods/test_get_chat_administrators.py +++ b/tests/test_api/test_methods/test_get_chat_administrators.py @@ -3,7 +3,8 @@ from typing import List import pytest from aiogram.api.methods import GetChatAdministrators, Request -from aiogram.api.types import ChatMember, User +from aiogram.api.types import ChatMember +from tests.factories.chat_member import ChatMemberFactory from tests.mocked_bot import MockedBot @@ -11,11 +12,7 @@ class TestGetChatAdministrators: @pytest.mark.asyncio async def test_method(self, bot: MockedBot): prepare_result = bot.add_result_for( - GetChatAdministrators, - ok=True, - result=[ - ChatMember(user=User(id=42, is_bot=False, first_name="User"), status="creator") - ], + GetChatAdministrators, ok=True, result=[ChatMemberFactory(status="creator")] ) response: List[ChatMember] = await GetChatAdministrators(chat_id=-42) @@ -26,11 +23,7 @@ class TestGetChatAdministrators: @pytest.mark.asyncio async def test_bot_method(self, bot: MockedBot): prepare_result = bot.add_result_for( - GetChatAdministrators, - ok=True, - result=[ - ChatMember(user=User(id=42, is_bot=False, first_name="User"), status="creator") - ], + GetChatAdministrators, ok=True, result=[ChatMemberFactory(status="creator")] ) response: List[ChatMember] = await bot.get_chat_administrators(chat_id=-42) request: Request = bot.get_request() diff --git a/tests/test_api/test_methods/test_get_chat_member.py b/tests/test_api/test_methods/test_get_chat_member.py index d63bdbd0..5ab28e77 100644 --- a/tests/test_api/test_methods/test_get_chat_member.py +++ b/tests/test_api/test_methods/test_get_chat_member.py @@ -1,7 +1,8 @@ import pytest from aiogram.api.methods import GetChatMember, Request -from aiogram.api.types import ChatMember, User +from aiogram.api.types import ChatMember +from tests.factories.chat_member import ChatMemberFactory from tests.mocked_bot import MockedBot @@ -9,9 +10,7 @@ class TestGetChatMember: @pytest.mark.asyncio async def test_method(self, bot: MockedBot): prepare_result = bot.add_result_for( - GetChatMember, - ok=True, - result=ChatMember(user=User(id=42, is_bot=False, first_name="User"), status="creator"), + GetChatMember, ok=True, result=ChatMemberFactory(status="creator") ) response: ChatMember = await GetChatMember(chat_id=-42, user_id=42) @@ -22,9 +21,7 @@ class TestGetChatMember: @pytest.mark.asyncio async def test_bot_method(self, bot: MockedBot): prepare_result = bot.add_result_for( - GetChatMember, - ok=True, - result=ChatMember(user=User(id=42, is_bot=False, first_name="User"), status="creator"), + GetChatMember, ok=True, result=ChatMemberFactory(status="creator") ) response: ChatMember = await bot.get_chat_member(chat_id=-42, user_id=42) diff --git a/tests/test_api/test_methods/test_send_animation.py b/tests/test_api/test_methods/test_send_animation.py index b02bad5f..da869fd4 100644 --- a/tests/test_api/test_methods/test_send_animation.py +++ b/tests/test_api/test_methods/test_send_animation.py @@ -4,46 +4,41 @@ import pytest from aiogram.api.methods import Request, SendAnimation from aiogram.api.types import Animation, Chat, Message +from tests.factories.message import MessageFactory from tests.mocked_bot import MockedBot class TestSendAnimation: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendAnimation, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), + result=MessageFactory( animation=Animation( file_id="file id", width=42, height=42, duration=0, file_unique_id="file id" - ), - chat=Chat(id=42, type="private"), + ) ), ) - response: Message = await SendAnimation(chat_id=42, animation="file id") + response: Message = await SendAnimation(chat_id=private_chat.id, animation="file id") request: Request = bot.get_request() assert request.method == "sendAnimation" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendAnimation, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), + result=MessageFactory( animation=Animation( file_id="file id", width=42, height=42, duration=0, file_unique_id="file id" - ), - chat=Chat(id=42, type="private"), + ) ), ) - response: Message = await bot.send_animation(chat_id=42, animation="file id") + response: Message = await bot.send_animation(chat_id=private_chat.id, animation="file id") request: Request = bot.get_request() assert request.method == "sendAnimation" assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_send_audio.py b/tests/test_api/test_methods/test_send_audio.py index fdc06bdb..a5305a3f 100644 --- a/tests/test_api/test_methods/test_send_audio.py +++ b/tests/test_api/test_methods/test_send_audio.py @@ -3,43 +3,38 @@ import datetime import pytest from aiogram.api.methods import Request, SendAudio -from aiogram.api.types import Audio, Chat, File, Message +from aiogram.api.types import Audio, Chat, Message +from tests.factories.message import MessageFactory from tests.mocked_bot import MockedBot class TestSendAudio: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendAudio, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - audio=Audio(file_id="file id", duration=42, file_unique_id="file id"), - chat=Chat(id=42, type="private"), + result=MessageFactory( + audio=Audio(file_id="file id", duration=42, file_unique_id="file id") ), ) - response: Message = await SendAudio(chat_id=42, audio="file id") + response: Message = await SendAudio(chat_id=private_chat.id, audio="file id") request: Request = bot.get_request() assert request.method == "sendAudio" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendAudio, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - audio=Audio(file_id="file id", duration=42, file_unique_id="file id"), - chat=Chat(id=42, type="private"), + result=MessageFactory( + audio=Audio(file_id="file id", duration=42, file_unique_id="file id") ), ) - response: Message = await bot.send_audio(chat_id=42, audio="file id") + response: Message = await bot.send_audio(chat_id=private_chat.id, audio="file id") request: Request = bot.get_request() assert request.method == "sendAudio" assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_send_contact.py b/tests/test_api/test_methods/test_send_contact.py index 44435d2d..a6370d84 100644 --- a/tests/test_api/test_methods/test_send_contact.py +++ b/tests/test_api/test_methods/test_send_contact.py @@ -1,46 +1,37 @@ -import datetime - import pytest from aiogram.api.methods import Request, SendContact from aiogram.api.types import Chat, Contact, Message +from tests.factories.message import MessageFactory from tests.mocked_bot import MockedBot class TestSendContact: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendContact, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - contact=Contact(phone_number="911", first_name="911"), - chat=Chat(id=42, type="private"), - ), + result=MessageFactory(contact=Contact(phone_number="911", first_name="911")), ) - response: Message = await SendContact(chat_id=42, phone_number="911", first_name="911") + response: Message = await SendContact( + chat_id=private_chat.id, phone_number="911", first_name="911" + ) request: Request = bot.get_request() assert request.method == "sendContact" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendContact, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - contact=Contact(phone_number="911", first_name="911"), - chat=Chat(id=42, type="private"), - ), + result=MessageFactory(contact=Contact(phone_number="911", first_name="911")), ) response: Message = await bot.send_contact( - chat_id=42, phone_number="911", first_name="911" + chat_id=private_chat.id, phone_number="911", first_name="911" ) request: Request = bot.get_request() assert request.method == "sendContact" diff --git a/tests/test_api/test_methods/test_send_document.py b/tests/test_api/test_methods/test_send_document.py index 5ddd2e6a..cfc2921c 100644 --- a/tests/test_api/test_methods/test_send_document.py +++ b/tests/test_api/test_methods/test_send_document.py @@ -4,42 +4,33 @@ import pytest from aiogram.api.methods import Request, SendDocument from aiogram.api.types import Chat, Document, Message +from tests.factories.message import MessageFactory from tests.mocked_bot import MockedBot class TestSendDocument: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendDocument, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - document=Document(file_id="file id", file_unique_id="file id"), - chat=Chat(id=42, type="private"), - ), + result=MessageFactory(document=Document(file_id="file id", file_unique_id="file id")), ) - response: Message = await SendDocument(chat_id=42, document="file id") + response: Message = await SendDocument(chat_id=private_chat.id, document="file id") request: Request = bot.get_request() assert request.method == "sendDocument" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendDocument, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - document=Document(file_id="file id", file_unique_id="file id"), - chat=Chat(id=42, type="private"), - ), + result=MessageFactory(document=Document(file_id="file id", file_unique_id="file id")), ) - response: Message = await bot.send_document(chat_id=42, document="file id") + response: Message = await bot.send_document(chat_id=private_chat.id, document="file id") request: Request = bot.get_request() assert request.method == "sendDocument" assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_send_game.py b/tests/test_api/test_methods/test_send_game.py index 7728c079..704bc0d9 100644 --- a/tests/test_api/test_methods/test_send_game.py +++ b/tests/test_api/test_methods/test_send_game.py @@ -4,54 +4,49 @@ import pytest from aiogram.api.methods import Request, SendGame from aiogram.api.types import Chat, Game, Message, PhotoSize +from tests.factories.message import MessageFactory from tests.mocked_bot import MockedBot class TestSendGame: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendGame, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), + result=MessageFactory( game=Game( title="title", description="description", photo=[ PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id") ], - ), - chat=Chat(id=42, type="private"), + ) ), ) - response: Message = await SendGame(chat_id=42, game_short_name="game") + response: Message = await SendGame(chat_id=private_chat.id, game_short_name="game") request: Request = bot.get_request() assert request.method == "sendGame" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendGame, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), + result=MessageFactory( game=Game( title="title", description="description", photo=[ PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id") ], - ), - chat=Chat(id=42, type="private"), + ) ), ) - response: Message = await bot.send_game(chat_id=42, game_short_name="game") + response: Message = await bot.send_game(chat_id=private_chat.id, game_short_name="game") request: Request = bot.get_request() assert request.method == "sendGame" assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_send_invoice.py b/tests/test_api/test_methods/test_send_invoice.py index eb6c83b6..e4809d9a 100644 --- a/tests/test_api/test_methods/test_send_invoice.py +++ b/tests/test_api/test_methods/test_send_invoice.py @@ -1,34 +1,30 @@ -import datetime - import pytest from aiogram.api.methods import Request, SendInvoice from aiogram.api.types import Chat, Invoice, LabeledPrice, Message +from tests.factories.message import MessageFactory from tests.mocked_bot import MockedBot class TestSendInvoice: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendInvoice, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), + result=MessageFactory( invoice=Invoice( title="test", description="test", start_parameter="brilliant", currency="BTC", total_amount=1, - ), - chat=Chat(id=42, type="private"), + ) ), ) response: Message = await SendInvoice( - chat_id=42, + chat_id=private_chat.id, title="test", description="test", payload="payload", @@ -42,26 +38,23 @@ class TestSendInvoice: assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendInvoice, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), + result=MessageFactory( invoice=Invoice( title="test", description="test", start_parameter="brilliant", currency="BTC", total_amount=1, - ), - chat=Chat(id=42, type="private"), + ) ), ) response: Message = await bot.send_invoice( - chat_id=42, + chat_id=private_chat.id, title="test", description="test", payload="payload", diff --git a/tests/test_api/test_methods/test_send_location.py b/tests/test_api/test_methods/test_send_location.py index d833733c..1078fa4c 100644 --- a/tests/test_api/test_methods/test_send_location.py +++ b/tests/test_api/test_methods/test_send_location.py @@ -4,42 +4,37 @@ import pytest from aiogram.api.methods import Request, SendLocation from aiogram.api.types import Chat, Location, Message +from tests.factories.message import MessageFactory from tests.mocked_bot import MockedBot class TestSendLocation: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendLocation, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - location=Location(longitude=3.14, latitude=3.14), - chat=Chat(id=42, type="private"), - ), + result=MessageFactory(location=Location(longitude=3.14, latitude=3.14)), ) - response: Message = await SendLocation(chat_id=42, latitude=3.14, longitude=3.14) + response: Message = await SendLocation( + chat_id=private_chat.id, latitude=3.14, longitude=3.14 + ) request: Request = bot.get_request() assert request.method == "sendLocation" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendLocation, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - location=Location(longitude=3.14, latitude=3.14), - chat=Chat(id=42, type="private"), - ), + result=MessageFactory(location=Location(longitude=3.14, latitude=3.14)), ) - response: Message = await bot.send_location(chat_id=42, latitude=3.14, longitude=3.14) + response: Message = await bot.send_location( + chat_id=private_chat.id, latitude=3.14, longitude=3.14 + ) request: Request = bot.get_request() assert request.method == "sendLocation" assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_send_media_group.py b/tests/test_api/test_methods/test_send_media_group.py index 14a30ccb..00a666e3 100644 --- a/tests/test_api/test_methods/test_send_media_group.py +++ b/tests/test_api/test_methods/test_send_media_group.py @@ -13,28 +13,24 @@ from aiogram.api.types import ( PhotoSize, Video, ) +from tests.factories.message import MessageFactory from tests.mocked_bot import MockedBot class TestSendMediaGroup: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendMediaGroup, ok=True, result=[ - Message( - message_id=42, - date=datetime.datetime.now(), + MessageFactory( photo=[ PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id") ], media_group_id="media group", - chat=Chat(id=42, type="private"), ), - Message( - message_id=43, - date=datetime.datetime.now(), + MessageFactory( video=Video( file_id="file id", width=42, @@ -43,13 +39,12 @@ class TestSendMediaGroup: file_unique_id="file id", ), media_group_id="media group", - chat=Chat(id=42, type="private"), ), ], ) response: List[Message] = await SendMediaGroup( - chat_id=42, + chat_id=private_chat.id, media=[ InputMediaPhoto(media="file id"), InputMediaVideo(media=BufferedInputFile(b"", "video.mp4")), @@ -60,23 +55,18 @@ class TestSendMediaGroup: assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendMediaGroup, ok=True, result=[ - Message( - message_id=42, - date=datetime.datetime.now(), + MessageFactory( photo=[ PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id") ], media_group_id="media group", - chat=Chat(id=42, type="private"), ), - Message( - message_id=43, - date=datetime.datetime.now(), + MessageFactory( video=Video( file_id="file id", width=42, @@ -85,13 +75,12 @@ class TestSendMediaGroup: file_unique_id="file id", ), media_group_id="media group", - chat=Chat(id=42, type="private"), ), ], ) response: List[Message] = await bot.send_media_group( - chat_id=42, + chat_id=private_chat.id, media=[ InputMediaPhoto(media="file id"), InputMediaVideo(media=BufferedInputFile(b"", "video.mp4")), diff --git a/tests/test_api/test_methods/test_send_message.py b/tests/test_api/test_methods/test_send_message.py index 9c09284e..c5749d81 100644 --- a/tests/test_api/test_methods/test_send_message.py +++ b/tests/test_api/test_methods/test_send_message.py @@ -4,22 +4,14 @@ import pytest from aiogram.api.methods import Request, SendMessage from aiogram.api.types import Chat, Message +from tests.factories.message import MessageFactory from tests.mocked_bot import MockedBot class TestSendMessage: @pytest.mark.asyncio async def test_method(self, bot: MockedBot): - prepare_result = bot.add_result_for( - SendMessage, - ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - text="test", - chat=Chat(id=42, type="private"), - ), - ) + prepare_result = bot.add_result_for(SendMessage, ok=True, result=MessageFactory()) response: Message = await SendMessage(chat_id=42, text="test") request: Request = bot.get_request() @@ -28,16 +20,7 @@ class TestSendMessage: @pytest.mark.asyncio async def test_bot_method(self, bot: MockedBot): - prepare_result = bot.add_result_for( - SendMessage, - ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - text="test", - chat=Chat(id=42, type="private"), - ), - ) + prepare_result = bot.add_result_for(SendMessage, ok=True, result=MessageFactory()) response: Message = await bot.send_message(chat_id=42, text="test") request: Request = bot.get_request() diff --git a/tests/test_api/test_methods/test_send_photo.py b/tests/test_api/test_methods/test_send_photo.py index 2ff2b7c6..cc789d57 100644 --- a/tests/test_api/test_methods/test_send_photo.py +++ b/tests/test_api/test_methods/test_send_photo.py @@ -1,9 +1,8 @@ -import datetime - import pytest from aiogram.api.methods import Request, SendPhoto -from aiogram.api.types import Chat, Message, PhotoSize +from aiogram.api.types import Message, PhotoSize +from tests.factories.message import MessageFactory from tests.mocked_bot import MockedBot @@ -13,13 +12,8 @@ class TestSendPhoto: prepare_result = bot.add_result_for( SendPhoto, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - photo=[ - PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id") - ], - chat=Chat(id=42, type="private"), + result=MessageFactory( + photo=[PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id")] ), ) @@ -33,13 +27,8 @@ class TestSendPhoto: prepare_result = bot.add_result_for( SendPhoto, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), - photo=[ - PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id") - ], - chat=Chat(id=42, type="private"), + result=MessageFactory( + photo=[PhotoSize(file_id="file id", width=42, height=42, file_unique_id="file id")] ), ) diff --git a/tests/test_api/test_methods/test_send_poll.py b/tests/test_api/test_methods/test_send_poll.py index 5e2b6c28..ac15cadd 100644 --- a/tests/test_api/test_methods/test_send_poll.py +++ b/tests/test_api/test_methods/test_send_poll.py @@ -4,18 +4,18 @@ import pytest from aiogram.api.methods import Request, SendPoll from aiogram.api.types import Chat, Message, Poll, PollOption +from tests.conftest import private_chat +from tests.factories.message import MessageFactory from tests.mocked_bot import MockedBot class TestSendPoll: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendPoll, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), + result=MessageFactory( poll=Poll( id="QA", question="Q", @@ -30,25 +30,27 @@ class TestSendPoll: total_voter_count=0, correct_option_id=0, ), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) response: Message = await SendPoll( - chat_id=42, question="Q?", options=["A", "B"], correct_option_id=0, type="quiz" + chat_id=private_chat.id, + question="Q?", + options=["A", "B"], + correct_option_id=0, + type="quiz", ) request: Request = bot.get_request() assert request.method == "sendPoll" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendPoll, ok=True, - result=Message( - message_id=42, - date=datetime.datetime.now(), + result=MessageFactory( poll=Poll( id="QA", question="Q", @@ -63,12 +65,16 @@ class TestSendPoll: total_voter_count=0, correct_option_id=0, ), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) response: Message = await bot.send_poll( - chat_id=42, question="Q?", options=["A", "B"], correct_option_id=0, type="quiz" + chat_id=private_chat.id, + question="Q?", + options=["A", "B"], + correct_option_id=0, + type="quiz", ) request: Request = bot.get_request() assert request.method == "sendPoll" diff --git a/tests/test_api/test_methods/test_send_sticker.py b/tests/test_api/test_methods/test_send_sticker.py index 8475581e..e881aded 100644 --- a/tests/test_api/test_methods/test_send_sticker.py +++ b/tests/test_api/test_methods/test_send_sticker.py @@ -9,7 +9,7 @@ from tests.mocked_bot import MockedBot class TestSendSticker: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendSticker, ok=True, @@ -23,17 +23,17 @@ class TestSendSticker: is_animated=False, file_unique_id="file id", ), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) - response: Message = await SendSticker(chat_id=42, sticker="file id") + response: Message = await SendSticker(chat_id=private_chat.id, sticker="file id") request: Request = bot.get_request() assert request.method == "sendSticker" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendSticker, ok=True, @@ -47,11 +47,11 @@ class TestSendSticker: is_animated=False, file_unique_id="file id", ), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) - response: Message = await bot.send_sticker(chat_id=42, sticker="file id") + response: Message = await bot.send_sticker(chat_id=private_chat.id, sticker="file id") request: Request = bot.get_request() assert request.method == "sendSticker" assert response == prepare_result.result diff --git a/tests/test_api/test_methods/test_send_video_note.py b/tests/test_api/test_methods/test_send_video_note.py index bb235bed..0dff6a30 100644 --- a/tests/test_api/test_methods/test_send_video_note.py +++ b/tests/test_api/test_methods/test_send_video_note.py @@ -9,7 +9,7 @@ from tests.mocked_bot import MockedBot class TestSendVideoNote: @pytest.mark.asyncio - async def test_method(self, bot: MockedBot): + async def test_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendVideoNote, ok=True, @@ -19,19 +19,19 @@ class TestSendVideoNote: video_note=VideoNote( file_id="file id", length=0, duration=0, file_unique_id="file id" ), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) response: Message = await SendVideoNote( - chat_id=42, video_note="file id", thumb=BufferedInputFile(b"", "file.png") + chat_id=private_chat.id, video_note="file id", thumb=BufferedInputFile(b"", "file.png") ) request: Request = bot.get_request() assert request.method == "sendVideoNote" assert response == prepare_result.result @pytest.mark.asyncio - async def test_bot_method(self, bot: MockedBot): + async def test_bot_method(self, bot: MockedBot, private_chat: Chat): prepare_result = bot.add_result_for( SendVideoNote, ok=True, @@ -41,12 +41,12 @@ class TestSendVideoNote: video_note=VideoNote( file_id="file id", length=0, duration=0, file_unique_id="file id" ), - chat=Chat(id=42, type="private"), + chat=private_chat, ), ) response: Message = await bot.send_video_note( - chat_id=42, video_note="file id", thumb=BufferedInputFile(b"", "file.png") + chat_id=private_chat.id, video_note="file id", thumb=BufferedInputFile(b"", "file.png") ) request: Request = bot.get_request() assert request.method == "sendVideoNote" diff --git a/tests/test_api/test_methods/test_send_voice.py b/tests/test_api/test_methods/test_send_voice.py index 6dcab09d..22688bfe 100644 --- a/tests/test_api/test_methods/test_send_voice.py +++ b/tests/test_api/test_methods/test_send_voice.py @@ -3,7 +3,8 @@ import datetime import pytest from aiogram.api.methods import Request, SendVoice -from aiogram.api.types import Chat, Message, Voice +from aiogram.api.types import Message, Voice +from tests.factories.chat import ChatFactory from tests.mocked_bot import MockedBot @@ -17,7 +18,7 @@ class TestSendVoice: message_id=42, date=datetime.datetime.now(), voice=Voice(file_id="file id", duration=0, file_unique_id="file id"), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), ), ) @@ -35,7 +36,7 @@ class TestSendVoice: message_id=42, date=datetime.datetime.now(), voice=Voice(file_id="file id", duration=0, file_unique_id="file id"), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), ), ) diff --git a/tests/test_api/test_methods/test_set_chat_administrator_custom_title.py b/tests/test_api/test_methods/test_set_chat_administrator_custom_title.py index e4372c3d..11452c40 100644 --- a/tests/test_api/test_methods/test_set_chat_administrator_custom_title.py +++ b/tests/test_api/test_methods/test_set_chat_administrator_custom_title.py @@ -1,6 +1,6 @@ import pytest -from aiogram.api.methods import Request, SetChatAdministratorCustomTitle, SetChatTitle +from aiogram.api.methods import Request, SetChatAdministratorCustomTitle from tests.mocked_bot import MockedBot diff --git a/tests/test_api/test_methods/test_set_chat_photo.py b/tests/test_api/test_methods/test_set_chat_photo.py index 5517c66b..affaf6c1 100644 --- a/tests/test_api/test_methods/test_set_chat_photo.py +++ b/tests/test_api/test_methods/test_set_chat_photo.py @@ -1,7 +1,7 @@ import pytest from aiogram.api.methods import Request, SetChatPhoto -from aiogram.api.types import BufferedInputFile, InputFile +from aiogram.api.types import BufferedInputFile from tests.mocked_bot import MockedBot diff --git a/tests/test_api/test_types/test_callback_query.py b/tests/test_api/test_types/test_callback_query.py index 792cca68..64b97399 100644 --- a/tests/test_api/test_types/test_callback_query.py +++ b/tests/test_api/test_types/test_callback_query.py @@ -1,12 +1,11 @@ from aiogram.api.methods import AnswerCallbackQuery -from aiogram.api.types import CallbackQuery, User +from aiogram.api.types import CallbackQuery +from tests.factories.user import UserFactory class TestCallbackQuery: def test_answer_alias(self): - callback_query = CallbackQuery( - id="id", from_user=User(id=42, is_bot=False, first_name="name"), chat_instance="chat" - ) + callback_query = CallbackQuery(id="id", from_user=UserFactory(), chat_instance="chat") kwargs = dict(text="foo", show_alert=True, url="https://foo.bar/", cache_time=123) diff --git a/tests/test_api/test_types/test_chat_member.py b/tests/test_api/test_types/test_chat_member.py index bb88e690..85216ed2 100644 --- a/tests/test_api/test_types/test_chat_member.py +++ b/tests/test_api/test_types/test_chat_member.py @@ -1,8 +1,6 @@ import pytest -from aiogram.api.types import ChatMember, User - -user = User(id=42, is_bot=False, first_name="User", last_name=None) +from tests.factories.chat_member import ChatMemberFactory class TestChatMember: @@ -10,7 +8,7 @@ class TestChatMember: "status,result", [["administrator", True], ["creator", True], ["member", False]] ) def test_is_chat_admin(self, status: str, result: bool): - chat_member = ChatMember(user=user, status=status) + chat_member = ChatMemberFactory(status=status) assert chat_member.is_chat_admin == result @pytest.mark.parametrize( @@ -25,5 +23,5 @@ class TestChatMember: ], ) def test_is_chat_member(self, status: str, result: bool): - chat_member = ChatMember(user=user, status=status) + chat_member = ChatMemberFactory(status=status) assert chat_member.is_chat_member == result diff --git a/tests/test_api/test_types/test_inline_query.py b/tests/test_api/test_types/test_inline_query.py index c828c17a..269b75cf 100644 --- a/tests/test_api/test_types/test_inline_query.py +++ b/tests/test_api/test_types/test_inline_query.py @@ -1,15 +1,11 @@ from aiogram.api.methods import AnswerInlineQuery -from aiogram.api.types import InlineQuery, User +from aiogram.api.types import InlineQuery +from tests.factories.user import UserFactory class TestInlineQuery: def test_answer_alias(self): - inline_query = InlineQuery( - id="id", - from_user=User(id=42, is_bot=False, first_name="name"), - query="query", - offset="", - ) + inline_query = InlineQuery(id="id", from_user=UserFactory(), query="query", offset="",) kwargs = dict( results=[], diff --git a/tests/test_api/test_types/test_message.py b/tests/test_api/test_types/test_message.py index f386e191..02502647 100644 --- a/tests/test_api/test_types/test_message.py +++ b/tests/test_api/test_types/test_message.py @@ -25,7 +25,6 @@ from aiogram.api.methods import ( from aiogram.api.types import ( Animation, Audio, - Chat, Contact, Dice, Document, @@ -39,13 +38,15 @@ from aiogram.api.types import ( PollOption, Sticker, SuccessfulPayment, - User, Venue, Video, VideoNote, Voice, ) from aiogram.api.types.message import ContentType, Message +from tests.factories.chat import ChatFactory +from tests.factories.message import MessageFactory +from tests.factories.user import UserFactory class TestMessage: @@ -53,12 +54,7 @@ class TestMessage: "message,content_type", [ [ - Message( - message_id=42, - date=datetime.datetime.now(), - text="test", - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + MessageFactory( ), ContentType.TEXT, ], @@ -67,8 +63,8 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), audio=Audio(file_id="file id", file_unique_id="file id", duration=42), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.AUDIO, ], @@ -83,8 +79,8 @@ class TestMessage: height=42, duration=0, ), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.ANIMATION, ], @@ -93,8 +89,8 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), document=Document(file_id="file id", file_unique_id="file id"), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.DOCUMENT, ], @@ -111,8 +107,8 @@ class TestMessage: ) ], ), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.GAME, ], @@ -123,8 +119,8 @@ class TestMessage: photo=[ PhotoSize(file_id="file id", file_unique_id="file id", width=42, height=42) ], - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.PHOTO, ], @@ -139,8 +135,8 @@ class TestMessage: height=42, is_animated=False, ), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.STICKER, ], @@ -155,8 +151,8 @@ class TestMessage: height=42, duration=0, ), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.VIDEO, ], @@ -167,8 +163,8 @@ class TestMessage: video_note=VideoNote( file_id="file id", file_unique_id="file id", length=0, duration=0 ), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.VIDEO_NOTE, ], @@ -177,8 +173,8 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), voice=Voice(file_id="file id", file_unique_id="file id", duration=0), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.VOICE, ], @@ -187,8 +183,8 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), contact=Contact(phone_number="911", first_name="911"), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.CONTACT, ], @@ -202,8 +198,8 @@ class TestMessage: address="Under the stairs, 4 Privet Drive, " "Little Whinging, Surrey, England, Great Britain", ), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.VENUE, ], @@ -212,8 +208,8 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), location=Location(longitude=3.14, latitude=3.14), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.LOCATION, ], @@ -221,9 +217,9 @@ class TestMessage: Message( message_id=42, date=datetime.datetime.now(), - new_chat_members=[User(id=42, is_bot=False, first_name="Test")], - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + new_chat_members=[UserFactory()], + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.NEW_CHAT_MEMBERS, ], @@ -231,9 +227,9 @@ class TestMessage: Message( message_id=42, date=datetime.datetime.now(), - left_chat_member=User(id=42, is_bot=False, first_name="Test"), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + left_chat_member=UserFactory(), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.LEFT_CHAT_MEMBER, ], @@ -248,8 +244,8 @@ class TestMessage: currency="BTC", total_amount=1, ), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.INVOICE, ], @@ -264,8 +260,8 @@ class TestMessage: telegram_payment_charge_id="charge", provider_payment_charge_id="payment", ), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.SUCCESSFUL_PAYMENT, ], @@ -274,8 +270,8 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), connected_website="token", - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.CONNECTED_WEBSITE, ], @@ -284,8 +280,8 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), migrate_from_chat_id=42, - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.MIGRATE_FROM_CHAT_ID, ], @@ -294,8 +290,8 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), migrate_to_chat_id=42, - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.MIGRATE_TO_CHAT_ID, ], @@ -307,11 +303,11 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), text="pinned", - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.PINNED_MESSAGE, ], @@ -320,8 +316,8 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), new_chat_title="test", - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.NEW_CHAT_TITLE, ], @@ -332,8 +328,8 @@ class TestMessage: new_chat_photo=[ PhotoSize(file_id="file id", file_unique_id="file id", width=42, height=42) ], - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.NEW_CHAT_PHOTO, ], @@ -342,8 +338,8 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), delete_chat_photo=True, - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.DELETE_CHAT_PHOTO, ], @@ -352,8 +348,8 @@ class TestMessage: message_id=42, date=datetime.datetime.now(), group_chat_created=True, - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.GROUP_CHAT_CREATED, ], @@ -365,8 +361,8 @@ class TestMessage: data=[], credentials=EncryptedCredentials(data="test", hash="test", secret="test"), ), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.PASSPORT_DATA, ], @@ -388,8 +384,8 @@ class TestMessage: total_voter_count=0, correct_option_id=1, ), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.POLL, ], @@ -397,9 +393,9 @@ class TestMessage: Message( message_id=42, date=datetime.datetime.now(), - chat=Chat(id=42, type="private"), + chat=ChatFactory(), dice=Dice(value=6, emoji="X"), - from_user=User(id=42, is_bot=False, first_name="Test"), + from_user=UserFactory(), ), ContentType.DICE, ], @@ -407,8 +403,8 @@ class TestMessage: Message( message_id=42, date=datetime.datetime.now(), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), + chat=ChatFactory(), + from_user=UserFactory(), ), ContentType.UNKNOWN, ], @@ -484,9 +480,7 @@ class TestMessage: ] ], ): - message = Message( - message_id=42, chat=Chat(id=42, type="private"), date=datetime.datetime.now() - ) + message = Message(message_id=42, chat=ChatFactory(), date=datetime.datetime.now()) alias_name = "_".join(item for item in [alias_type, alias_for_method] if item) alias = getattr(message, alias_name) diff --git a/tests/test_api/test_types/test_pre_checkout_query.py b/tests/test_api/test_types/test_pre_checkout_query.py index 1bef6cff..37e64391 100644 --- a/tests/test_api/test_types/test_pre_checkout_query.py +++ b/tests/test_api/test_types/test_pre_checkout_query.py @@ -1,12 +1,13 @@ from aiogram.api.methods import AnswerPreCheckoutQuery -from aiogram.api.types import PreCheckoutQuery, User +from aiogram.api.types import PreCheckoutQuery +from tests.factories.user import UserFactory class TestPreCheckoutQuery: def test_answer_alias(self): pre_checkout_query = PreCheckoutQuery( id="id", - from_user=User(id=42, is_bot=False, first_name="name"), + from_user=UserFactory(), currency="currency", total_amount=123, invoice_payload="payload", diff --git a/tests/test_api/test_types/test_shipping_query.py b/tests/test_api/test_types/test_shipping_query.py index 939bb6c5..d43bf648 100644 --- a/tests/test_api/test_types/test_shipping_query.py +++ b/tests/test_api/test_types/test_shipping_query.py @@ -1,12 +1,13 @@ from aiogram.api.methods import AnswerShippingQuery -from aiogram.api.types import LabeledPrice, ShippingAddress, ShippingOption, ShippingQuery, User +from aiogram.api.types import LabeledPrice, ShippingAddress, ShippingOption, ShippingQuery +from tests.factories.user import UserFactory class TestInlineQuery: def test_answer_alias(self): shipping_query = ShippingQuery( id="id", - from_user=User(id=42, is_bot=False, first_name="name"), + from_user=UserFactory(), invoice_payload="payload", shipping_address=ShippingAddress( country_code="foo", diff --git a/tests/test_api/test_types/test_user.py b/tests/test_api/test_types/test_user.py index ed09b97c..21cf3672 100644 --- a/tests/test_api/test_types/test_user.py +++ b/tests/test_api/test_types/test_user.py @@ -1,11 +1,11 @@ import pytest -from aiogram.api.types import User +from tests.factories.user import UserFactory class TestUser: @pytest.mark.parametrize( - "first,last,result", + "first_name,last_name,result", [ ["User", None, "User"], ["", None, ""], @@ -15,6 +15,6 @@ class TestUser: [" ", " ", " "], ], ) - def test_full_name(self, first: str, last: str, result: bool): - user = User(id=42, is_bot=False, first_name=first, last_name=last) + def test_full_name(self, first_name: str, last_name: str, result: bool): + user = UserFactory(first_name=first_name, last_name=last_name) assert user.full_name == result diff --git a/tests/test_dispatcher/test_dispatcher.py b/tests/test_dispatcher/test_dispatcher.py index e5b9b50f..0a9eb4b3 100644 --- a/tests/test_dispatcher/test_dispatcher.py +++ b/tests/test_dispatcher/test_dispatcher.py @@ -9,7 +9,6 @@ from aiogram import Bot from aiogram.api.methods import GetMe, GetUpdates, SendMessage from aiogram.api.types import Chat, Message, Update, User from aiogram.dispatcher.dispatcher import Dispatcher -from aiogram.dispatcher.event.bases import NOT_HANDLED from aiogram.dispatcher.router import Router from tests.mocked_bot import MockedBot diff --git a/tests/test_dispatcher/test_event/test_handler.py b/tests/test_dispatcher/test_event/test_handler.py index 3db676db..eec15670 100644 --- a/tests/test_dispatcher/test_event/test_handler.py +++ b/tests/test_dispatcher/test_event/test_handler.py @@ -5,7 +5,6 @@ import pytest from aiogram.api.types import Update from aiogram.dispatcher.event.handler import CallableMixin, FilterObject, HandlerObject -from aiogram.dispatcher.filters import Text from aiogram.dispatcher.filters.base import BaseFilter from aiogram.dispatcher.handler.base import BaseHandler diff --git a/tests/test_dispatcher/test_filters/test_text.py b/tests/test_dispatcher/test_filters/test_text.py index 3e0bc479..717e924f 100644 --- a/tests/test_dispatcher/test_filters/test_text.py +++ b/tests/test_dispatcher/test_filters/test_text.py @@ -5,9 +5,10 @@ from typing import Sequence, Type import pytest from pydantic import ValidationError -from aiogram.api.types import CallbackQuery, Chat, InlineQuery, Message, Poll, PollOption, User +from aiogram.api.types import CallbackQuery, Chat, InlineQuery, Poll, PollOption, User, Message from aiogram.dispatcher.filters import BUILTIN_FILTERS from aiogram.dispatcher.filters.text import Text +from tests.factories.message import MessageFactory class TestText: @@ -55,116 +56,33 @@ class TestText: @pytest.mark.parametrize( "argument,ignore_case,input_value,update_type,result", [ + ["text", False, "test", MessageFactory(text=""), False,], [ "text", False, "test", - Message( - message_id=42, - date=datetime.datetime.now(), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), - ), - False, - ], - [ - "text", - False, - "test", - Message( + MessageFactory( message_id=42, date=datetime.datetime.now(), + text="", caption="test", chat=Chat(id=42, type="private"), from_user=User(id=42, is_bot=False, first_name="Test"), ), True, ], - [ - "text", - False, - "test", - Message( - message_id=42, - date=datetime.datetime.now(), - text="test", - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), - ), - True, - ], - [ - "text", - True, - "TEst", - Message( - message_id=42, - date=datetime.datetime.now(), - text="tesT", - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), - ), - True, - ], - [ - "text", - False, - "TEst", - Message( - message_id=42, - date=datetime.datetime.now(), - text="tesT", - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), - ), - False, - ], - [ - "text_startswith", - False, - "test", - Message( - message_id=42, - date=datetime.datetime.now(), - text="test case", - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), - ), - True, - ], - [ - "text_endswith", - False, - "case", - Message( - message_id=42, - date=datetime.datetime.now(), - text="test case", - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), - ), - True, - ], - [ - "text_contains", - False, - " ", - Message( - message_id=42, - date=datetime.datetime.now(), - text="test case", - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), - ), - True, - ], + ["text", False, "test", MessageFactory(text="test"), True,], + ["text", True, "TEst", MessageFactory(text="tesT"), True,], + ["text", False, "TEst", MessageFactory(text="tesT"), False,], + ["text_startswith", False, "test", MessageFactory(text="test case"), True,], + ["text_endswith", False, "case", MessageFactory(text="test case"), True,], + ["text_contains", False, " ", MessageFactory(text="test case"), True,], [ "text_startswith", True, "question", - Message( - message_id=42, - date=datetime.datetime.now(), + MessageFactory( + text="", poll=Poll( id="poll id", question="Question?", @@ -175,8 +93,6 @@ class TestText: allows_multiple_answers=False, total_voter_count=0, ), - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), ), True, ], diff --git a/tests/test_dispatcher/test_handler/test_chosen_inline_result.py b/tests/test_dispatcher/test_handler/test_chosen_inline_result.py index 66ccde90..1b080c45 100644 --- a/tests/test_dispatcher/test_handler/test_chosen_inline_result.py +++ b/tests/test_dispatcher/test_handler/test_chosen_inline_result.py @@ -2,7 +2,7 @@ from typing import Any import pytest -from aiogram.api.types import CallbackQuery, ChosenInlineResult, User +from aiogram.api.types import ChosenInlineResult, User from aiogram.dispatcher.handler import ChosenInlineResultHandler diff --git a/tests/test_dispatcher/test_handler/test_error.py b/tests/test_dispatcher/test_handler/test_error.py index 093d9b0a..a83d96a4 100644 --- a/tests/test_dispatcher/test_handler/test_error.py +++ b/tests/test_dispatcher/test_handler/test_error.py @@ -2,16 +2,7 @@ from typing import Any import pytest -from aiogram.api.types import ( - CallbackQuery, - InlineQuery, - Poll, - PollOption, - ShippingAddress, - ShippingQuery, - User, -) -from aiogram.dispatcher.handler import ErrorHandler, PollHandler +from aiogram.dispatcher.handler import ErrorHandler class TestErrorHandler: diff --git a/tests/test_dispatcher/test_handler/test_inline_query.py b/tests/test_dispatcher/test_handler/test_inline_query.py index 19c454da..f62efbbe 100644 --- a/tests/test_dispatcher/test_handler/test_inline_query.py +++ b/tests/test_dispatcher/test_handler/test_inline_query.py @@ -2,7 +2,7 @@ from typing import Any import pytest -from aiogram.api.types import CallbackQuery, InlineQuery, User +from aiogram.api.types import InlineQuery, User from aiogram.dispatcher.handler import InlineQueryHandler diff --git a/tests/test_dispatcher/test_handler/test_message.py b/tests/test_dispatcher/test_handler/test_message.py index d4cc4818..18455708 100644 --- a/tests/test_dispatcher/test_handler/test_message.py +++ b/tests/test_dispatcher/test_handler/test_message.py @@ -1,11 +1,10 @@ -import datetime from typing import Any import pytest -from aiogram.api.types import Chat, Message, User from aiogram.dispatcher.filters import CommandObject from aiogram.dispatcher.handler.message import MessageHandler, MessageHandlerCommandMixin +from tests.factories.message import MessageFactory class MyHandler(MessageHandler): @@ -16,13 +15,7 @@ class MyHandler(MessageHandler): class TestClassBasedMessageHandler: @pytest.mark.asyncio async def test_message_handler(self): - event = Message( - message_id=42, - date=datetime.datetime.now(), - text="test", - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), - ) + event = MessageFactory() handler = MyHandler(event=event) assert handler.from_user == event.from_user @@ -37,13 +30,7 @@ class HandlerWithCommand(MessageHandlerCommandMixin, MessageHandler): class TestBaseMessageHandlerCommandMixin: def test_command_accessible(self): handler = HandlerWithCommand( - Message( - message_id=42, - date=datetime.datetime.now(), - text="/test args", - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), - ), + MessageFactory(text="/test args"), command=CommandObject(prefix="/", command="command", args="args"), ) @@ -51,14 +38,6 @@ class TestBaseMessageHandlerCommandMixin: assert handler.command.command == "command" def test_command_not_presented(self): - handler = HandlerWithCommand( - Message( - message_id=42, - date=datetime.datetime.now(), - text="test", - chat=Chat(id=42, type="private"), - from_user=User(id=42, is_bot=False, first_name="Test"), - ) - ) + handler = HandlerWithCommand(MessageFactory()) assert handler.command is None diff --git a/tests/test_dispatcher/test_handler/test_poll.py b/tests/test_dispatcher/test_handler/test_poll.py index 5908b2fc..c892d3ea 100644 --- a/tests/test_dispatcher/test_handler/test_poll.py +++ b/tests/test_dispatcher/test_handler/test_poll.py @@ -2,15 +2,7 @@ from typing import Any import pytest -from aiogram.api.types import ( - CallbackQuery, - InlineQuery, - Poll, - PollOption, - ShippingAddress, - ShippingQuery, - User, -) +from aiogram.api.types import Poll, PollOption from aiogram.dispatcher.handler import PollHandler diff --git a/tests/test_dispatcher/test_handler/test_shipping_query.py b/tests/test_dispatcher/test_handler/test_shipping_query.py index 0df94218..b44606ea 100644 --- a/tests/test_dispatcher/test_handler/test_shipping_query.py +++ b/tests/test_dispatcher/test_handler/test_shipping_query.py @@ -2,7 +2,7 @@ from typing import Any import pytest -from aiogram.api.types import CallbackQuery, InlineQuery, ShippingAddress, ShippingQuery, User +from aiogram.api.types import ShippingAddress, ShippingQuery, User from aiogram.dispatcher.handler import ShippingQueryHandler