-
-
Notifications
You must be signed in to change notification settings - Fork 98
fix: TaskiqAdminMiddleware doesn't work with dataclasses #554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import pytest | ||
| from aiohttp import web | ||
| from aiohttp.test_utils import TestServer | ||
| from typing_extensions import AsyncGenerator | ||
|
|
||
| from taskiq.brokers.inmemory_broker import InMemoryBroker | ||
| from taskiq.brokers.shared_broker import async_shared_broker | ||
| from taskiq.middlewares import TaskiqAdminMiddleware | ||
| from tests.middlewares.admin_middleware.dto import ( | ||
| DataclassDTO, | ||
| PydanticDTO, | ||
| TypedDictDTO, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def admin_api_server() -> AsyncGenerator[TestServer, None]: | ||
| async def handle_queued(request: web.Request) -> web.Response: | ||
| return web.json_response({"status": "ok"}, status=200) | ||
|
|
||
| async def handle_started(request: web.Request) -> web.Response: | ||
| return web.json_response({"status": "ok"}, status=200) | ||
|
|
||
danfimov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| async def handle_executed(request: web.Request) -> web.Response: | ||
| return web.json_response({"status": "ok"}, status=200) | ||
|
|
||
| app = web.Application() | ||
| app.router.add_post("/api/tasks/{task_id}/queued", handle_queued) | ||
danfimov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| app.router.add_post("/api/tasks/{task_id}/started", handle_started) | ||
| app.router.add_post("/api/tasks/{task_id}/executed", handle_executed) | ||
|
|
||
| server = TestServer(app) | ||
| await server.start_server() | ||
danfimov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| yield server | ||
| await server.close() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
danfimov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| async def broker_with_admin_middleware( | ||
| admin_api_server: TestServer, | ||
| ) -> AsyncGenerator[InMemoryBroker, None]: | ||
| broker = InMemoryBroker(await_inplace=True).with_middlewares( | ||
| TaskiqAdminMiddleware( | ||
| str(admin_api_server.make_url("/")), # URL тестового сервера | ||
| "supersecret", | ||
| taskiq_broker_name="InMemory", | ||
| ), | ||
| ) | ||
|
|
||
| broker.register_task(task_with_dataclass, task_name="task_with_dataclass") | ||
| broker.register_task(task_with_typed_dict, task_name="task_with_typed_dict") | ||
| broker.register_task(task_with_pydantic_model, task_name="task_with_pydantic_model") | ||
| async_shared_broker.default_broker(broker) | ||
|
|
||
| await broker.startup() | ||
| yield broker | ||
| await broker.shutdown() | ||
|
|
||
|
|
||
| async def task_with_dataclass(dto: DataclassDTO) -> None: | ||
| assert dto | ||
|
|
||
|
|
||
| async def task_with_typed_dict(dto: TypedDictDTO) -> None: | ||
| assert dto | ||
|
|
||
|
|
||
| async def task_with_pydantic_model(dto: PydanticDTO) -> None: | ||
| assert dto | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from dataclasses import dataclass | ||
|
|
||
| import pydantic | ||
| from typing_extensions import TypedDict | ||
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
| class DataclassNestedDTO: | ||
| id: int | ||
| name: str | ||
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
danfimov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| class DataclassDTO: | ||
| nested: DataclassNestedDTO | ||
| recipients: list[str] | ||
| subject: str | ||
| attachments: list[str] | None = None | ||
| text: str | None = None | ||
| html: str | None = None | ||
|
|
||
|
|
||
| class PydanticDTO(pydantic.BaseModel): | ||
| number: int | ||
| text: str | ||
| flag: bool | ||
| list: list[float] | ||
| dictionary: dict[str, str] | None = None | ||
|
|
||
|
|
||
| class TypedDictDTO(TypedDict): | ||
| id: int | ||
| name: str | ||
| active: bool | ||
| scores: list[int] | ||
| metadata: dict[str, str] | None | ||
53 changes: 53 additions & 0 deletions
53
tests/middlewares/admin_middleware/test_arguments_formatting.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import pytest | ||
| from polyfactory.factories import BaseFactory, DataclassFactory, TypedDictFactory | ||
| from polyfactory.factories.pydantic_factory import ModelFactory | ||
|
|
||
| from taskiq.brokers.inmemory_broker import InMemoryBroker | ||
| from tests.middlewares.admin_middleware.dto import ( | ||
| DataclassDTO, | ||
| PydanticDTO, | ||
| TypedDictDTO, | ||
| ) | ||
|
|
||
|
|
||
| class DataclassDTOFactory(DataclassFactory[DataclassDTO]): | ||
| __model__ = DataclassDTO | ||
|
|
||
|
|
||
| class TypedDictDTOFactory(TypedDictFactory[TypedDictDTO]): | ||
| __model__ = TypedDictDTO | ||
|
|
||
|
|
||
| class PydanticDTOFactory(ModelFactory[PydanticDTO]): | ||
| __model__ = PydanticDTO | ||
|
|
||
|
|
||
| # @pytest.mark.skip | ||
| class TestArgumentsFormattingInAdminMiddleware: | ||
| @pytest.mark.parametrize( | ||
| "dto_factory, task_name", | ||
| [ | ||
| pytest.param(DataclassDTOFactory, "task_with_dataclass", id="dataclass"), | ||
| pytest.param(TypedDictDTOFactory, "task_with_typed_dict", id="typeddict"), | ||
| pytest.param(PydanticDTOFactory, "task_with_pydantic_model", id="pydantic"), | ||
danfimov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ], | ||
| ) | ||
| async def test_when_task_dto_passed__then_middleware_successfully_send_request( | ||
| self, | ||
| broker_with_admin_middleware: InMemoryBroker, | ||
| dto_factory: type[BaseFactory], # type: ignore[type-arg] | ||
| task_name: str, | ||
| ) -> None: | ||
| # given | ||
| task_arguments = dto_factory.build() | ||
| task = broker_with_admin_middleware.find_task(task_name) | ||
| assert task is not None, f"Task {task_name} should be registered in the broker" | ||
|
|
||
| # when | ||
| kicked_task = await task.kiq(task_arguments) | ||
danfimov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| await broker_with_admin_middleware.wait_all() | ||
|
|
||
| # then | ||
| result = await kicked_task.get_result() | ||
| # we just expect no errors during post_send/pre_execute/post_execute | ||
| assert result.error is None | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.