diff --git a/docs/docs/en/api/faststream/asyncapi/schema/info/EmailStr.md b/docs/docs/en/api/faststream/asyncapi/schema/info/EmailStr.md deleted file mode 100644 index 553df26149..0000000000 --- a/docs/docs/en/api/faststream/asyncapi/schema/info/EmailStr.md +++ /dev/null @@ -1,11 +0,0 @@ ---- -# 0.5 - API -# 2 - Release -# 3 - Contributing -# 5 - Template Page -# 10 - Default -search: - boost: 0.5 ---- - -::: faststream.asyncapi.schema.info.EmailStr diff --git a/docs/docs/en/getting-started/integrations/fastapi/index.md b/docs/docs/en/getting-started/integrations/fastapi/index.md index 814b144bdb..9ece2eab5e 100644 --- a/docs/docs/en/getting-started/integrations/fastapi/index.md +++ b/docs/docs/en/getting-started/integrations/fastapi/index.md @@ -89,23 +89,8 @@ To test your **FastAPI StreamRouter**, you can still use it with the *TestClient ## Multiple Routers -Using **FastStream** as a **FastAPI** plugin you are still able to separate messages processing logic between different routers (like with a regular `HTTPRouter`). But it can be confusing - how you should include multiple routers, if we have to setup `router.lifespan_context` as a **FastAPI** object lifespan. +Using **FastStream** as a **FastAPI** plugin you are still able to separate messages processing logic between different routers (like with a regular `HTTPRouter`). But it can be confusing - **StreamRouter** patches a **FastAPI** object lifespan. -You can make it in a two ways, depends on you reminds. +Fortunately, you can use regular **FastStream** routers and include them to the **FastAPI** integration one like in the regular broker object. Also, it can be helpful to reuse your endpoints between **FastAPI** integration and regular **FastStream** app. -### Routers nesting - -If you want to use the **SAME CONNECTION** for all of you routers you should nest them each other and finally use only the core router to include it into **FastAPI** object. - -{! includes/getting_started/integrations/fastapi/multiple.md !} - -This way the core router collects all nested routers publishers and subscribers and stores it like its own. - -### Custom lifespan - -Otherwise, if you want to has multiple connections to different broker instances, you can just include them separately to the app (each router has own broker and connection in this case) - -{! includes/getting_started/integrations/fastapi/multiple_lifespan.md !} - -!!! warning - This way you lose AsyncAPI schema, but we are working on it. +{! includes/getting_started/integrations/fastapi/routers.md !} diff --git a/docs/docs_src/integrations/fastapi/confluent/multiple_lifespan.py b/docs/docs_src/integrations/fastapi/confluent/multiple_lifespan.py deleted file mode 100644 index d4fb359d5b..0000000000 --- a/docs/docs_src/integrations/fastapi/confluent/multiple_lifespan.py +++ /dev/null @@ -1,11 +0,0 @@ -from fastapi import FastAPI -from faststream.confluent.fastapi import KafkaRouter - -one_router = KafkaRouter() -another_router = KafkaRouter() - -... - -app = FastAPI() -app.include_router(one_router) -app.include_router(another_router) diff --git a/docs/docs_src/integrations/fastapi/kafka/multiple.py b/docs/docs_src/integrations/fastapi/confluent/router.py similarity index 67% rename from docs/docs_src/integrations/fastapi/kafka/multiple.py rename to docs/docs_src/integrations/fastapi/confluent/router.py index 6bd9f8b94c..db350a9593 100644 --- a/docs/docs_src/integrations/fastapi/kafka/multiple.py +++ b/docs/docs_src/integrations/fastapi/confluent/router.py @@ -1,7 +1,8 @@ from fastapi import FastAPI -from faststream.kafka.fastapi import KafkaRouter +from faststream.confluent import KafkaRouter +from faststream.confluent.fastapi import KafkaRouter as StreamRouter -core_router = KafkaRouter() +core_router = StreamRouter() nested_router = KafkaRouter() @core_router.subscriber("core-topic") diff --git a/docs/docs_src/integrations/fastapi/kafka/multiple_lifespan.py b/docs/docs_src/integrations/fastapi/kafka/multiple_lifespan.py deleted file mode 100644 index abf30d352c..0000000000 --- a/docs/docs_src/integrations/fastapi/kafka/multiple_lifespan.py +++ /dev/null @@ -1,11 +0,0 @@ -from fastapi import FastAPI -from faststream.kafka.fastapi import KafkaRouter - -one_router = KafkaRouter() -another_router = KafkaRouter() - -... - -app = FastAPI() -app.include_router(one_router) -app.include_router(another_router) diff --git a/docs/docs_src/integrations/fastapi/confluent/multiple.py b/docs/docs_src/integrations/fastapi/kafka/router.py similarity index 68% rename from docs/docs_src/integrations/fastapi/confluent/multiple.py rename to docs/docs_src/integrations/fastapi/kafka/router.py index ae65011564..5ca84a70a9 100644 --- a/docs/docs_src/integrations/fastapi/confluent/multiple.py +++ b/docs/docs_src/integrations/fastapi/kafka/router.py @@ -1,7 +1,8 @@ from fastapi import FastAPI -from faststream.confluent.fastapi import KafkaRouter +from faststream.kafka import KafkaRouter +from faststream.kafka.fastapi import KafkaRouter as StreamRouter -core_router = KafkaRouter() +core_router = StreamRouter() nested_router = KafkaRouter() @core_router.subscriber("core-topic") diff --git a/docs/docs_src/integrations/fastapi/nats/multiple_lifespan.py b/docs/docs_src/integrations/fastapi/nats/multiple_lifespan.py deleted file mode 100644 index 7ea737ca09..0000000000 --- a/docs/docs_src/integrations/fastapi/nats/multiple_lifespan.py +++ /dev/null @@ -1,11 +0,0 @@ -from fastapi import FastAPI -from faststream.nats.fastapi import NatsRouter - -one_router = NatsRouter() -another_router = NatsRouter() - -... - -app = FastAPI() -app.include_router(one_router) -app.include_router(another_router) diff --git a/docs/docs_src/integrations/fastapi/nats/multiple.py b/docs/docs_src/integrations/fastapi/nats/router.py similarity index 69% rename from docs/docs_src/integrations/fastapi/nats/multiple.py rename to docs/docs_src/integrations/fastapi/nats/router.py index 6743e6fbcf..6bc03ac0d7 100644 --- a/docs/docs_src/integrations/fastapi/nats/multiple.py +++ b/docs/docs_src/integrations/fastapi/nats/router.py @@ -1,7 +1,8 @@ from fastapi import FastAPI -from faststream.nats.fastapi import NatsRouter +from faststream.nats import NatsRouter +from faststream.nats.fastapi import NatsRouter as StreamRouter -core_router = NatsRouter() +core_router = StreamRouter() nested_router = NatsRouter() @core_router.subscriber("core-subject") diff --git a/docs/docs_src/integrations/fastapi/rabbit/multiple_lifespan.py b/docs/docs_src/integrations/fastapi/rabbit/multiple_lifespan.py deleted file mode 100644 index 465b436989..0000000000 --- a/docs/docs_src/integrations/fastapi/rabbit/multiple_lifespan.py +++ /dev/null @@ -1,11 +0,0 @@ -from fastapi import FastAPI -from faststream.rabbit.fastapi import RabbitRouter - -one_router = RabbitRouter() -another_router = RabbitRouter() - -... - -app = FastAPI() -app.include_router(one_router) -app.include_router(another_router) diff --git a/docs/docs_src/integrations/fastapi/rabbit/multiple.py b/docs/docs_src/integrations/fastapi/rabbit/router.py similarity index 68% rename from docs/docs_src/integrations/fastapi/rabbit/multiple.py rename to docs/docs_src/integrations/fastapi/rabbit/router.py index f7b3b37d48..8c1cd5afe3 100644 --- a/docs/docs_src/integrations/fastapi/rabbit/multiple.py +++ b/docs/docs_src/integrations/fastapi/rabbit/router.py @@ -1,7 +1,8 @@ from fastapi import FastAPI -from faststream.rabbit.fastapi import RabbitRouter +from faststream.rabbit import RabbitRouter +from faststream.rabbit.fastapi import RabbitRouter as StreamRouter -core_router = RabbitRouter() +core_router = StreamRouter() nested_router = RabbitRouter() @core_router.subscriber("core-queue") diff --git a/docs/docs_src/integrations/fastapi/redis/multiple_lifespan.py b/docs/docs_src/integrations/fastapi/redis/multiple_lifespan.py deleted file mode 100644 index ad6ce39fbc..0000000000 --- a/docs/docs_src/integrations/fastapi/redis/multiple_lifespan.py +++ /dev/null @@ -1,11 +0,0 @@ -from fastapi import FastAPI -from faststream.redis.fastapi import RedisRouter - -one_router = RedisRouter() -another_router = RedisRouter() - -... - -app = FastAPI() -app.include_router(one_router) -app.include_router(another_router) diff --git a/docs/docs_src/integrations/fastapi/redis/multiple.py b/docs/docs_src/integrations/fastapi/redis/router.py similarity index 69% rename from docs/docs_src/integrations/fastapi/redis/multiple.py rename to docs/docs_src/integrations/fastapi/redis/router.py index 7092c93a3d..0ec0c2e4b7 100644 --- a/docs/docs_src/integrations/fastapi/redis/multiple.py +++ b/docs/docs_src/integrations/fastapi/redis/router.py @@ -1,7 +1,8 @@ from fastapi import FastAPI -from faststream.redis.fastapi import RedisRouter +from faststream.redis import RedisRouter +from faststream.redis.fastapi import RedisRouter as StreamRouter -core_router = RedisRouter() +core_router = StreamRouter() nested_router = RedisRouter() @core_router.subscriber("core-channel") diff --git a/docs/includes/getting_started/integrations/fastapi/multiple.md b/docs/includes/getting_started/integrations/fastapi/multiple.md deleted file mode 100644 index f713d0ff08..0000000000 --- a/docs/includes/getting_started/integrations/fastapi/multiple.md +++ /dev/null @@ -1,29 +0,0 @@ -=== "AIOKafka" - - ```python linenums="1" hl_lines="4-5 15 17-18" - {!> docs_src/integrations/fastapi/kafka/multiple.py !} - ``` - -=== "Confluent" - - ```python linenums="1" hl_lines="4-5 15 17-18" - {!> docs_src/integrations/fastapi/confluent/multiple.py !} - ``` - -=== "RabbitMQ" - - ```python linenums="1" hl_lines="4-5 15 17-18" - {!> docs_src/integrations/fastapi/rabbit/multiple.py !} - ``` - -=== "NATS" - - ```python linenums="1" hl_lines="4-5 15 17-18" - {!> docs_src/integrations/fastapi/nats/multiple.py !} - ``` - -=== "Redis" - - ```python linenums="1" hl_lines="4-5 15 17-18" - {!> docs_src/integrations/fastapi/redis/multiple.py !} - ``` diff --git a/docs/includes/getting_started/integrations/fastapi/multiple_lifespan.md b/docs/includes/getting_started/integrations/fastapi/multiple_lifespan.md deleted file mode 100644 index 3867657ac7..0000000000 --- a/docs/includes/getting_started/integrations/fastapi/multiple_lifespan.md +++ /dev/null @@ -1,29 +0,0 @@ -=== "AIOKafka" - - ```python linenums="1" hl_lines="9-11" - {!> docs_src/integrations/fastapi/kafka/multiple_lifespan.py !} - ``` - -=== "Confluent" - - ```python linenums="1" hl_lines="9-11" - {!> docs_src/integrations/fastapi/confluent/multiple_lifespan.py !} - ``` - -=== "RabbitMQ" - - ```python linenums="1" hl_lines="9-11" - {!> docs_src/integrations/fastapi/rabbit/multiple_lifespan.py !} - ``` - -=== "NATS" - - ```python linenums="1" hl_lines="9-11" - {!> docs_src/integrations/fastapi/nats/multiple_lifespan.py !} - ``` - -=== "Redis" - - ```python linenums="1" hl_lines="9-11" - {!> docs_src/integrations/fastapi/redis/multiple_lifespan.py !} - ``` diff --git a/docs/includes/getting_started/integrations/fastapi/routers.md b/docs/includes/getting_started/integrations/fastapi/routers.md new file mode 100644 index 0000000000..0e92e3424e --- /dev/null +++ b/docs/includes/getting_started/integrations/fastapi/routers.md @@ -0,0 +1,29 @@ +=== "AIOKafka" + + ```python linenums="1" hl_lines="2-3 6 12-14 16" + {!> docs_src/integrations/fastapi/kafka/router.py !} + ``` + +=== "Confluent" + + ```python linenums="1" hl_lines="2-3 6 12-14 16" + {!> docs_src/integrations/fastapi/confluent/router.py !} + ``` + +=== "RabbitMQ" + + ```python linenums="1" hl_lines="2-3 6 12-14 16" + {!> docs_src/integrations/fastapi/rabbit/router.py !} + ``` + +=== "NATS" + + ```python linenums="1" hl_lines="2-3 6 12-14 16" + {!> docs_src/integrations/fastapi/nats/router.py !} + ``` + +=== "Redis" + + ```python linenums="1" hl_lines="2-3 6 12-14 16" + {!> docs_src/integrations/fastapi/redis/router.py !} + ``` diff --git a/faststream/broker/fastapi/router.py b/faststream/broker/fastapi/router.py index d5313c22ee..baf7ceff53 100644 --- a/faststream/broker/fastapi/router.py +++ b/faststream/broker/fastapi/router.py @@ -35,6 +35,7 @@ from faststream.broker.fastapi.get_dependant import get_fastapi_dependant from faststream.broker.fastapi.route import wrap_callable_to_fastapi_compatible from faststream.broker.middlewares import BaseMiddleware +from faststream.broker.router import BrokerRouter from faststream.broker.types import ( MsgType, P_HandlerParams, @@ -479,9 +480,9 @@ def serve_asyncapi_schema( docs_router.get(f"{schema_url}.yaml")(download_app_yaml_schema) return docs_router - def include_router( + def include_router( # type: ignore[override] self, - router: "APIRouter", + router: Union["StreamRouter[MsgType]", "BrokerRouter[MsgType]"], *, prefix: str = "", tags: Optional[List[Union[str, Enum]]] = None, @@ -496,6 +497,24 @@ def include_router( ), ) -> None: """Includes a router in the API.""" + if isinstance(router, BrokerRouter): + for sub in router._subscribers.values(): + sub._call_decorators = ( # type: ignore[attr-defined] + self._add_api_mq_route( + dependencies=(), + response_model=Default(None), + response_model_include=None, + response_model_exclude=None, + response_model_by_alias=True, + response_model_exclude_unset=False, + response_model_exclude_defaults=False, + response_model_exclude_none=False, + ), + ) + + self.broker.include_router(router) + return + if isinstance(router, StreamRouter): # pragma: no branch router.lifespan_context = fake_context self.broker.include_router(router.broker) diff --git a/tests/a_docs/integration/fastapi/test_multiple_lifespan.py b/tests/a_docs/integration/fastapi/test_multiple_lifespan.py deleted file mode 100644 index 35cc66ddfa..0000000000 --- a/tests/a_docs/integration/fastapi/test_multiple_lifespan.py +++ /dev/null @@ -1,104 +0,0 @@ -import pytest -from fastapi.testclient import TestClient - -from tests.marks import ( - require_aiokafka, - require_aiopika, - require_confluent, - require_nats, - require_redis, -) - - -class BaseCase: - def test_running(self, data): - app, core_router, nested_router = data - - @core_router.subscriber("test1") - async def handler(): ... - - @nested_router.subscriber("test2") - async def handler2(): ... - - handlers1 = core_router.broker._subscribers.values() - handlers2 = nested_router.broker._subscribers.values() - - assert len(handlers1) == 1 - assert len(handlers2) == 1 - - for h in (handlers := (*handlers1, *handlers2)): - assert not h.running - - with TestClient(app): - for h in handlers: - assert h.running - - -@pytest.mark.kafka -@require_aiokafka -class TestKafka(BaseCase): - @pytest.fixture(scope="class") - def data(self): - from docs.docs_src.integrations.fastapi.kafka.multiple_lifespan import ( - another_router, - app, - one_router, - ) - - return (app, one_router, another_router) - - -@pytest.mark.confluent -@require_confluent -class TestConfluent(BaseCase): - @pytest.fixture(scope="class") - def data(self): - from docs.docs_src.integrations.fastapi.confluent.multiple_lifespan import ( - another_router, - app, - one_router, - ) - - return (app, one_router, another_router) - - -@pytest.mark.nats -@require_nats -class TestNats(BaseCase): - @pytest.fixture(scope="class") - def data(self): - from docs.docs_src.integrations.fastapi.nats.multiple_lifespan import ( - another_router, - app, - one_router, - ) - - return (app, one_router, another_router) - - -@pytest.mark.rabbit -@require_aiopika -class TestRabbit(BaseCase): - @pytest.fixture(scope="class") - def data(self): - from docs.docs_src.integrations.fastapi.rabbit.multiple_lifespan import ( - another_router, - app, - one_router, - ) - - return (app, one_router, another_router) - - -@pytest.mark.redis -@require_redis -class TestRedis(BaseCase): - @pytest.fixture(scope="class") - def data(self): - from docs.docs_src.integrations.fastapi.redis.multiple_lifespan import ( - another_router, - app, - one_router, - ) - - return (app, one_router, another_router) diff --git a/tests/a_docs/integration/fastapi/test_multiple.py b/tests/a_docs/integration/fastapi/test_routers.py similarity index 77% rename from tests/a_docs/integration/fastapi/test_multiple.py rename to tests/a_docs/integration/fastapi/test_routers.py index b025aeeb5d..c8122b0414 100644 --- a/tests/a_docs/integration/fastapi/test_multiple.py +++ b/tests/a_docs/integration/fastapi/test_routers.py @@ -30,7 +30,7 @@ def test_running(self, data): class TestKafka(BaseCase): @pytest.fixture(scope="class") def data(self): - from docs.docs_src.integrations.fastapi.kafka.multiple import app, core_router + from docs.docs_src.integrations.fastapi.kafka.router import app, core_router return (app, core_router.broker) @@ -40,7 +40,7 @@ def data(self): class TestConfluent(BaseCase): @pytest.fixture(scope="class") def data(self): - from docs.docs_src.integrations.fastapi.confluent.multiple import ( + from docs.docs_src.integrations.fastapi.confluent.router import ( app, core_router, ) @@ -53,7 +53,7 @@ def data(self): class TestNats(BaseCase): @pytest.fixture(scope="class") def data(self): - from docs.docs_src.integrations.fastapi.nats.multiple import app, core_router + from docs.docs_src.integrations.fastapi.nats.router import app, core_router return (app, core_router.broker) @@ -63,7 +63,7 @@ def data(self): class TestRabbit(BaseCase): @pytest.fixture(scope="class") def data(self): - from docs.docs_src.integrations.fastapi.rabbit.multiple import app, core_router + from docs.docs_src.integrations.fastapi.rabbit.router import app, core_router return (app, core_router.broker) @@ -73,6 +73,6 @@ def data(self): class TestRedis(BaseCase): @pytest.fixture(scope="class") def data(self): - from docs.docs_src.integrations.fastapi.redis.multiple import app, core_router + from docs.docs_src.integrations.fastapi.redis.router import app, core_router return (app, core_router.broker) diff --git a/tests/brokers/base/fastapi.py b/tests/brokers/base/fastapi.py index 69ed8af4dc..f20dd61866 100644 --- a/tests/brokers/base/fastapi.py +++ b/tests/brokers/base/fastapi.py @@ -1,6 +1,6 @@ import asyncio from contextlib import asynccontextmanager -from typing import Callable, Type, TypeVar +from typing import Any, Callable, Type, TypeVar from unittest.mock import Mock import pytest @@ -12,6 +12,7 @@ from faststream.broker.core.usecase import BrokerUsecase from faststream.broker.fastapi.context import Context from faststream.broker.fastapi.router import StreamRouter +from faststream.broker.router import BrokerRouter from faststream.types import AnyCallable from .basic import BaseTestcaseConfig @@ -22,6 +23,7 @@ @pytest.mark.asyncio class FastAPITestcase(BaseTestcaseConfig): router_class: Type[StreamRouter[BrokerUsecase]] + broker_router_class: Type[BrokerRouter[Any]] async def test_base_real(self, mock: Mock, queue: str, event: asyncio.Event): router = self.router_class() @@ -461,7 +463,7 @@ async def m(): async def test_include(self, queue: str): router = self.router_class() - router2 = self.router_class() + router2 = self.broker_router_class() app = FastAPI() diff --git a/tests/brokers/confluent/test_fastapi.py b/tests/brokers/confluent/test_fastapi.py index 164009ff12..0de5bb7311 100644 --- a/tests/brokers/confluent/test_fastapi.py +++ b/tests/brokers/confluent/test_fastapi.py @@ -4,7 +4,8 @@ import pytest -from faststream.confluent.fastapi import KafkaRouter +from faststream.confluent import KafkaRouter +from faststream.confluent.fastapi import KafkaRouter as StreamRouter from faststream.confluent.testing import TestKafkaBroker, build_message from tests.brokers.base.fastapi import FastAPILocalTestcase, FastAPITestcase @@ -13,7 +14,8 @@ @pytest.mark.confluent class TestConfluentRouter(ConfluentTestcaseConfig, FastAPITestcase): - router_class = KafkaRouter + router_class = StreamRouter + broker_router_class = KafkaRouter async def test_batch_real( self, @@ -21,7 +23,7 @@ async def test_batch_real( queue: str, event: asyncio.Event, ): - router = KafkaRouter() + router = self.router_class() args, kwargs = self.get_subscriber_params(queue, batch=True) @@ -45,7 +47,8 @@ async def hello(msg: List[str]): class TestRouterLocal(ConfluentTestcaseConfig, FastAPILocalTestcase): - router_class = KafkaRouter + router_class = StreamRouter + broker_router_class = KafkaRouter broker_test = staticmethod(TestKafkaBroker) build_message = staticmethod(build_message) @@ -55,7 +58,7 @@ async def test_batch_testclient( queue: str, event: asyncio.Event, ): - router = KafkaRouter() + router = self.router_class() args, kwargs = self.get_subscriber_params(queue, batch=True) diff --git a/tests/brokers/kafka/test_fastapi.py b/tests/brokers/kafka/test_fastapi.py index 1b9748924f..509466bc65 100644 --- a/tests/brokers/kafka/test_fastapi.py +++ b/tests/brokers/kafka/test_fastapi.py @@ -4,14 +4,16 @@ import pytest -from faststream.kafka.fastapi import KafkaRouter +from faststream.kafka import KafkaRouter +from faststream.kafka.fastapi import KafkaRouter as StreamRouter from faststream.kafka.testing import TestKafkaBroker, build_message from tests.brokers.base.fastapi import FastAPILocalTestcase, FastAPITestcase @pytest.mark.kafka class TestKafkaRouter(FastAPITestcase): - router_class = KafkaRouter + router_class = StreamRouter + broker_router_class = KafkaRouter async def test_batch_real( self, @@ -19,7 +21,7 @@ async def test_batch_real( queue: str, event: asyncio.Event, ): - router = KafkaRouter() + router = self.router_class() @router.subscriber(queue, batch=True) async def hello(msg: List[str]): @@ -41,7 +43,8 @@ async def hello(msg: List[str]): class TestRouterLocal(FastAPILocalTestcase): - router_class = KafkaRouter + router_class = StreamRouter + broker_router_class = KafkaRouter broker_test = staticmethod(TestKafkaBroker) build_message = staticmethod(build_message) @@ -51,7 +54,7 @@ async def test_batch_testclient( queue: str, event: asyncio.Event, ): - router = KafkaRouter() + router = self.router_class() @router.subscriber(queue, batch=True) async def hello(msg: List[str]): diff --git a/tests/brokers/nats/test_fastapi.py b/tests/brokers/nats/test_fastapi.py index 5c19684890..518fdcd637 100644 --- a/tests/brokers/nats/test_fastapi.py +++ b/tests/brokers/nats/test_fastapi.py @@ -4,15 +4,16 @@ import pytest -from faststream.nats import JStream, PullSub -from faststream.nats.fastapi import NatsRouter +from faststream.nats import JStream, NatsRouter, PullSub +from faststream.nats.fastapi import NatsRouter as StreamRouter from faststream.nats.testing import TestNatsBroker, build_message from tests.brokers.base.fastapi import FastAPILocalTestcase, FastAPITestcase @pytest.mark.nats class TestRouter(FastAPITestcase): - router_class = NatsRouter + router_class = StreamRouter + broker_router_class = NatsRouter async def test_path( self, @@ -20,7 +21,7 @@ async def test_path( event: asyncio.Event, mock: MagicMock, ): - router = NatsRouter() + router = self.router_class() @router.subscriber("in.{name}") def subscriber(msg: str, name: str): @@ -47,7 +48,7 @@ async def test_consume_batch( event: asyncio.Event, mock: MagicMock, ): - router = NatsRouter() + router = self.router_class() @router.subscriber( queue, @@ -73,7 +74,8 @@ def subscriber(m: List[str]): class TestRouterLocal(FastAPILocalTestcase): - router_class = NatsRouter + router_class = StreamRouter + broker_router_class = NatsRouter broker_test = staticmethod(TestNatsBroker) build_message = staticmethod(build_message) @@ -84,7 +86,7 @@ async def test_consume_batch( event: asyncio.Event, mock: MagicMock, ): - router = NatsRouter() + router = self.router_class() @router.subscriber( queue, diff --git a/tests/brokers/rabbit/test_fastapi.py b/tests/brokers/rabbit/test_fastapi.py index b355d4c3f5..18fff70dc6 100644 --- a/tests/brokers/rabbit/test_fastapi.py +++ b/tests/brokers/rabbit/test_fastapi.py @@ -3,15 +3,16 @@ import pytest -from faststream.rabbit import ExchangeType, RabbitExchange, RabbitQueue -from faststream.rabbit.fastapi import RabbitRouter +from faststream.rabbit import ExchangeType, RabbitExchange, RabbitQueue, RabbitRouter +from faststream.rabbit.fastapi import RabbitRouter as StreamRouter from faststream.rabbit.testing import TestRabbitBroker, build_message from tests.brokers.base.fastapi import FastAPILocalTestcase, FastAPITestcase @pytest.mark.rabbit class TestRouter(FastAPITestcase): - router_class = RabbitRouter + router_class = StreamRouter + broker_router_class = RabbitRouter @pytest.mark.asyncio async def test_path( @@ -20,7 +21,7 @@ async def test_path( event: asyncio.Event, mock: MagicMock, ): - router = RabbitRouter() + router = self.router_class() @router.subscriber( RabbitQueue( @@ -54,7 +55,8 @@ def subscriber(msg: str, name: str): @pytest.mark.asyncio class TestRouterLocal(FastAPILocalTestcase): - router_class = RabbitRouter + router_class = StreamRouter + broker_router_class = RabbitRouter broker_test = staticmethod(TestRabbitBroker) build_message = staticmethod(build_message) diff --git a/tests/brokers/redis/test_fastapi.py b/tests/brokers/redis/test_fastapi.py index fb910464ed..56ef879e7c 100644 --- a/tests/brokers/redis/test_fastapi.py +++ b/tests/brokers/redis/test_fastapi.py @@ -4,15 +4,16 @@ import pytest -from faststream.redis import ListSub, StreamSub -from faststream.redis.fastapi import RedisRouter +from faststream.redis import ListSub, RedisRouter, StreamSub +from faststream.redis.fastapi import RedisRouter as StreamRouter from faststream.redis.testing import TestRedisBroker, build_message from tests.brokers.base.fastapi import FastAPILocalTestcase, FastAPITestcase @pytest.mark.redis class TestRouter(FastAPITestcase): - router_class = RedisRouter + router_class = StreamRouter + broker_router_class = RedisRouter async def test_path( self, @@ -20,7 +21,7 @@ async def test_path( event: asyncio.Event, mock: Mock, ): - router = RedisRouter() + router = self.router_class() @router.subscriber("in.{name}") def subscriber(msg: str, name: str): @@ -41,7 +42,7 @@ def subscriber(msg: str, name: str): mock.assert_called_once_with(msg="hello", name="john") async def test_connection_params(self, settings): - broker = RedisRouter( + broker = self.router_class( host="fake-host", port=6377 ).broker # kwargs will be ignored await broker.connect( @@ -57,7 +58,7 @@ async def test_batch_real( queue: str, event: asyncio.Event, ): - router = RedisRouter() + router = self.router_class() @router.subscriber(list=ListSub(queue, batch=True, max_records=1)) async def hello(msg: List[str]): @@ -84,7 +85,7 @@ async def test_consume_stream( mock: Mock, queue, ): - router = RedisRouter() + router = self.router_class() @router.subscriber(stream=StreamSub(queue, polling_interval=10)) async def handler(msg): @@ -112,7 +113,7 @@ async def test_consume_stream_batch( mock: Mock, queue, ): - router = RedisRouter() + router = self.router_class() @router.subscriber(stream=StreamSub(queue, polling_interval=10, batch=True)) async def handler(msg: List[str]): @@ -135,7 +136,8 @@ async def handler(msg: List[str]): class TestRouterLocal(FastAPILocalTestcase): - router_class = RedisRouter + router_class = StreamRouter + broker_router_class = RedisRouter broker_test = staticmethod(TestRedisBroker) build_message = staticmethod(build_message) @@ -145,7 +147,7 @@ async def test_batch_testclient( queue: str, event: asyncio.Event, ): - router = RedisRouter() + router = self.router_class() @router.subscriber(list=ListSub(queue, batch=True, max_records=1)) async def hello(msg: List[str]): @@ -170,7 +172,7 @@ async def test_stream_batch_testclient( queue: str, event: asyncio.Event, ): - router = RedisRouter() + router = self.router_class() @router.subscriber(stream=StreamSub(queue, batch=True)) async def hello(msg: List[str]):