From 536026b4023f8fdaffe63bdd416c6730f089e200 Mon Sep 17 00:00:00 2001 From: Nikita Pastukhov Date: Tue, 21 May 2024 22:16:07 +0300 Subject: [PATCH] chore: merge main --- docs/docs/en/release.md | 2 +- faststream/confluent/subscriber/asyncapi.py | 4 +- faststream/confluent/subscriber/factory.py | 15 +- faststream/kafka/subscriber/asyncapi.py | 8 +- faststream/kafka/subscriber/factory.py | 38 ++++- faststream/nats/broker/broker.py | 28 +--- faststream/nats/fastapi/fastapi.py | 2 +- faststream/nats/opentelemetry/provider.py | 9 +- faststream/nats/parser.py | 3 +- faststream/nats/router.py | 2 +- faststream/nats/subscriber/asyncapi.py | 8 +- faststream/nats/subscriber/subscription.py | 14 +- faststream/opentelemetry/middleware.py | 14 +- tests/brokers/nats/test_consume.py | 39 +++-- tests/brokers/nats/test_kv_declarer_cache.py | 9 +- tests/brokers/nats/test_os_declarer_cache.py | 9 +- tests/cli/test_publish.py | 114 ++++++------- .../getting_started/cli/confluent/__init__.py | 3 + .../{ => confluent}/test_confluent_context.py | 0 .../getting_started/cli/kafka/__init__.py | 3 + .../cli/{ => kafka}/test_kafka_context.py | 0 .../docs/getting_started/cli/nats/__init__.py | 3 + .../cli/{ => nats}/test_nats_context.py | 0 .../getting_started/cli/rabbit/__init__.py | 3 + .../cli/{ => rabbit}/test_rabbit_context.py | 0 .../getting_started/cli/redis/__init__.py | 3 + .../cli/{ => redis}/test_redis_context.py | 0 .../getting_started/context/test_annotated.py | 24 ++- .../docs/getting_started/context/test_base.py | 22 ++- .../docs/getting_started/context/test_cast.py | 22 ++- .../context/test_custom_global.py | 22 ++- .../context/test_custom_local.py | 22 ++- .../context/test_default_arguments.py | 22 ++- .../context/test_existed_context.py | 22 ++- .../context/test_fields_access.py | 22 ++- .../getting_started/context/test_initial.py | 14 +- .../context/test_manual_local_context.py | 22 ++- .../getting_started/context/test_nested.py | 2 + .../dependencies/basic/test_depends.py | 22 ++- .../dependencies/basic/test_nested_depends.py | 22 ++- .../docs/getting_started/index/test_basic.py | 22 ++- .../getting_started/lifespan/test_basic.py | 29 +++- .../docs/getting_started/lifespan/test_ml.py | 22 ++- .../lifespan/test_ml_context.py | 22 ++- .../getting_started/lifespan/test_testing.py | 83 ++++++--- .../getting_started/publishing/test_broker.py | 22 ++- .../publishing/test_broker_context.py | 22 ++- .../publishing/test_decorator.py | 22 ++- .../getting_started/publishing/test_direct.py | 78 ++++++--- .../getting_started/publishing/test_object.py | 78 ++++++--- .../docs/getting_started/routers/test_base.py | 22 ++- .../getting_started/routers/test_delay.py | 22 ++- .../routers/test_delay_equal.py | 22 ++- .../serialization/test_parser.py | 22 ++- .../subscription/test_annotated.py | 52 +++--- .../subscription/test_filter.py | 22 ++- .../subscription/test_pydantic.py | 22 ++- .../getting_started/subscription/test_real.py | 150 ++++++++++++----- .../subscription/test_testing.py | 158 +++++++++++++----- tests/docs/index/test_basic.py | 22 ++- tests/docs/index/test_pydantic.py | 114 ++++++++++--- tests/docs/integration/fastapi/test_base.py | 22 ++- .../docs/integration/fastapi/test_depends.py | 22 ++- .../docs/integration/fastapi/test_multiple.py | 13 ++ .../fastapi/test_multiple_lifespan.py | 13 ++ tests/docs/integration/fastapi/test_send.py | 22 ++- .../docs/integration/fastapi/test_startup.py | 22 ++- tests/docs/integration/fastapi/test_test.py | 62 +++++-- tests/marks.py | 65 +++++++ tests/opentelemetry/__init__.py | 2 +- 70 files changed, 1352 insertions(+), 512 deletions(-) create mode 100644 tests/docs/getting_started/cli/confluent/__init__.py rename tests/docs/getting_started/cli/{ => confluent}/test_confluent_context.py (100%) create mode 100644 tests/docs/getting_started/cli/kafka/__init__.py rename tests/docs/getting_started/cli/{ => kafka}/test_kafka_context.py (100%) create mode 100644 tests/docs/getting_started/cli/nats/__init__.py rename tests/docs/getting_started/cli/{ => nats}/test_nats_context.py (100%) create mode 100644 tests/docs/getting_started/cli/rabbit/__init__.py rename tests/docs/getting_started/cli/{ => rabbit}/test_rabbit_context.py (100%) create mode 100644 tests/docs/getting_started/cli/redis/__init__.py rename tests/docs/getting_started/cli/{ => redis}/test_redis_context.py (100%) diff --git a/docs/docs/en/release.md b/docs/docs/en/release.md index ce6828b2fa..8fac6fd678 100644 --- a/docs/docs/en/release.md +++ b/docs/docs/en/release.md @@ -39,7 +39,7 @@ broker = NatsBroker( app = FastStream(broker) ``` -To find detailt information just visit our documentation aboout [telemetry](https://faststream.airt.ai/latest/getting-started/opentelemetry/) +To find detailt information just visit our documentation about [telemetry](https://faststream.airt.ai/latest/getting-started/opentelemetry/) P.S. The release includes basic OpenTelemetry support - messages tracing & basic metrics. Baggage support and correct spans linking in batch processing case will be added soon. diff --git a/faststream/confluent/subscriber/asyncapi.py b/faststream/confluent/subscriber/asyncapi.py index 8eacb40ee8..7ec3ffb965 100644 --- a/faststream/confluent/subscriber/asyncapi.py +++ b/faststream/confluent/subscriber/asyncapi.py @@ -22,9 +22,6 @@ if TYPE_CHECKING: from confluent_kafka import Message as ConfluentMsg - from fast_depends.dependencies import Depends - from faststream.broker.types import BrokerMiddleware - from faststream.types import AnyDict class AsyncAPISubscriber(LogicSubscriber[MsgType]): @@ -59,6 +56,7 @@ def get_schema(self) -> Dict[str, Channel]: return channels + class AsyncAPIDefaultSubscriber( DefaultSubscriber, AsyncAPISubscriber["ConfluentMsg"], diff --git a/faststream/confluent/subscriber/factory.py b/faststream/confluent/subscriber/factory.py index c76cb2ad80..b7b6b6ca61 100644 --- a/faststream/confluent/subscriber/factory.py +++ b/faststream/confluent/subscriber/factory.py @@ -1,6 +1,5 @@ from typing import ( TYPE_CHECKING, - Callable, Iterable, Literal, Optional, @@ -19,7 +18,7 @@ from fast_depends.dependencies import Depends from faststream.broker.types import BrokerMiddleware - from faststream.confluent.client import AsyncConfluentConsumer + from faststream.types import AnyDict @overload @@ -30,7 +29,7 @@ def create_subscriber( max_records: Optional[int], # Kafka information group_id: Optional[str], - builder: Callable[..., "AsyncConfluentConsumer"], + connection_data: "AnyDict", is_manual: bool, # Subscriber args no_ack: bool, @@ -52,7 +51,7 @@ def create_subscriber( max_records: Optional[int], # Kafka information group_id: Optional[str], - builder: Callable[..., "AsyncConfluentConsumer"], + connection_data: "AnyDict", is_manual: bool, # Subscriber args no_ack: bool, @@ -74,7 +73,7 @@ def create_subscriber( max_records: Optional[int], # Kafka information group_id: Optional[str], - builder: Callable[..., "AsyncConfluentConsumer"], + connection_data: "AnyDict", is_manual: bool, # Subscriber args no_ack: bool, @@ -100,7 +99,7 @@ def create_subscriber( max_records: Optional[int], # Kafka information group_id: Optional[str], - builder: Callable[..., "AsyncConfluentConsumer"], + connection_data: "AnyDict", is_manual: bool, # Subscriber args no_ack: bool, @@ -123,7 +122,7 @@ def create_subscriber( batch_timeout_ms=batch_timeout_ms, max_records=max_records, group_id=group_id, - builder=builder, + connection_data=connection_data, is_manual=is_manual, no_ack=no_ack, retry=retry, @@ -137,7 +136,7 @@ def create_subscriber( return AsyncAPIDefaultSubscriber( *topics, group_id=group_id, - builder=builder, + connection_data=connection_data, is_manual=is_manual, no_ack=no_ack, retry=retry, diff --git a/faststream/kafka/subscriber/asyncapi.py b/faststream/kafka/subscriber/asyncapi.py index cef8f9e11c..9adb8dad3c 100644 --- a/faststream/kafka/subscriber/asyncapi.py +++ b/faststream/kafka/subscriber/asyncapi.py @@ -14,7 +14,6 @@ from faststream.asyncapi.schema.bindings import kafka from faststream.asyncapi.utils import resolve_payloads from faststream.broker.types import MsgType -from faststream.exceptions import SetupError from faststream.kafka.subscriber.usecase import ( BatchSubscriber, DefaultSubscriber, @@ -23,12 +22,6 @@ if TYPE_CHECKING: from aiokafka import ConsumerRecord - from aiokafka import ConsumerRecord, TopicPartition - from aiokafka.abc import ConsumerRebalanceListener - from fast_depends.dependencies import Depends - - from faststream.broker.types import BrokerMiddleware - from faststream.types import AnyDict class AsyncAPISubscriber(LogicSubscriber[MsgType]): @@ -63,6 +56,7 @@ def get_schema(self) -> Dict[str, Channel]: return channels + class AsyncAPIDefaultSubscriber( DefaultSubscriber, AsyncAPISubscriber["ConsumerRecord"], diff --git a/faststream/kafka/subscriber/factory.py b/faststream/kafka/subscriber/factory.py index aa3c90c567..fb5de4bf1a 100644 --- a/faststream/kafka/subscriber/factory.py +++ b/faststream/kafka/subscriber/factory.py @@ -1,6 +1,5 @@ from typing import ( TYPE_CHECKING, - Callable, Iterable, Literal, Optional, @@ -9,17 +8,19 @@ overload, ) +from faststream.exceptions import SetupError from faststream.kafka.subscriber.asyncapi import ( AsyncAPIBatchSubscriber, AsyncAPIDefaultSubscriber, ) if TYPE_CHECKING: - from aiokafka import AIOKafkaConsumer, ConsumerRecord + from aiokafka import ConsumerRecord, TopicPartition from aiokafka.abc import ConsumerRebalanceListener from fast_depends.dependencies import Depends from faststream.broker.types import BrokerMiddleware + from faststream.types import AnyDict @overload @@ -32,7 +33,8 @@ def create_subscriber( group_id: Optional[str], listener: Optional["ConsumerRebalanceListener"], pattern: Optional[str], - builder: Callable[..., "AIOKafkaConsumer"], + connection_args: "AnyDict", + partitions: Iterable["TopicPartition"], is_manual: bool, # Subscriber args no_ack: bool, @@ -56,7 +58,8 @@ def create_subscriber( group_id: Optional[str], listener: Optional["ConsumerRebalanceListener"], pattern: Optional[str], - builder: Callable[..., "AIOKafkaConsumer"], + connection_args: "AnyDict", + partitions: Iterable["TopicPartition"], is_manual: bool, # Subscriber args no_ack: bool, @@ -80,7 +83,8 @@ def create_subscriber( group_id: Optional[str], listener: Optional["ConsumerRebalanceListener"], pattern: Optional[str], - builder: Callable[..., "AIOKafkaConsumer"], + connection_args: "AnyDict", + partitions: Iterable["TopicPartition"], is_manual: bool, # Subscriber args no_ack: bool, @@ -108,7 +112,8 @@ def create_subscriber( group_id: Optional[str], listener: Optional["ConsumerRebalanceListener"], pattern: Optional[str], - builder: Callable[..., "AIOKafkaConsumer"], + connection_args: "AnyDict", + partitions: Iterable["TopicPartition"], is_manual: bool, # Subscriber args no_ack: bool, @@ -125,6 +130,20 @@ def create_subscriber( "AsyncAPIDefaultSubscriber", "AsyncAPIBatchSubscriber", ]: + if is_manual and not group_id: + raise SetupError("You must use `group_id` with manual commit mode.") + + if not topics and not partitions and not pattern: + raise SetupError( + "You should provide either `topics` or `partitions` or `pattern`." + ) + elif topics and partitions: + raise SetupError("You can't provide both `topics` and `partitions`.") + elif topics and pattern: + raise SetupError("You can't provide both `topics` and `pattern`.") + elif partitions and pattern: + raise SetupError("You can't provide both `partitions` and `pattern`.") + if batch: return AsyncAPIBatchSubscriber( *topics, @@ -133,7 +152,8 @@ def create_subscriber( group_id=group_id, listener=listener, pattern=pattern, - builder=builder, + connection_args=connection_args, + partitions=partitions, is_manual=is_manual, no_ack=no_ack, retry=retry, @@ -143,13 +163,15 @@ def create_subscriber( description_=description_, include_in_schema=include_in_schema, ) + else: return AsyncAPIDefaultSubscriber( *topics, group_id=group_id, listener=listener, pattern=pattern, - builder=builder, + connection_args=connection_args, + partitions=partitions, is_manual=is_manual, no_ack=no_ack, retry=retry, diff --git a/faststream/nats/broker/broker.py b/faststream/nats/broker/broker.py index da4da2b394..e6806172c3 100644 --- a/faststream/nats/broker/broker.py +++ b/faststream/nats/broker/broker.py @@ -631,8 +631,6 @@ async def start(self) -> None: ) except BadRequestError as e: # noqa: PERF203 - old_config = (await self.stream.stream_info(stream.name)).config - log_context = AsyncAPISubscriber.build_log_context( message=None, subject="", @@ -644,6 +642,8 @@ async def start(self) -> None: e.description == "stream name already in use with a different configuration" ): + old_config = (await self.stream.stream_info(stream.name)).config + self._log(str(e), logging.WARNING, log_context) await self.stream.update_stream( config=stream.config, @@ -655,27 +655,9 @@ async def start(self) -> None: else: # pragma: no cover self._log(str(e), logging.ERROR, log_context, exc_info=e) - except BadRequestError as e: - if ( - e.description - == "stream name already in use with a different configuration" - ): - old_config = (await self.stream.stream_info(stream.name)).config - - self._log(str(e), logging.WARNING, log_context) - await self.stream.update_stream( - config=stream.config, - subjects=tuple( - set(old_config.subjects or ()).union(stream.subjects) - ), - ) - - else: # pragma: no cover - self._log(str(e), logging.ERROR, log_context, exc_info=e) - - finally: - # prevent from double declaration - stream.declare = False + finally: + # prevent from double declaration + stream.declare = False # TODO: filter by already running handlers after TestClient refactor for handler in self._subscribers.values(): diff --git a/faststream/nats/fastapi/fastapi.py b/faststream/nats/fastapi/fastapi.py index 6d8a1af3fe..7ea3a2a5df 100644 --- a/faststream/nats/fastapi/fastapi.py +++ b/faststream/nats/fastapi/fastapi.py @@ -66,7 +66,7 @@ SubscriberMiddleware, ) from faststream.nats.message import NatsBatchMessage, NatsMessage - from faststream.nats.schemas import JStream, PullSub, KvWatch, ObjWatch + from faststream.nats.schemas import JStream, KvWatch, ObjWatch, PullSub from faststream.security import BaseSecurity from faststream.types import AnyDict, LoggerProto diff --git a/faststream/nats/opentelemetry/provider.py b/faststream/nats/opentelemetry/provider.py index 7aefafed2c..3c8d00854e 100644 --- a/faststream/nats/opentelemetry/provider.py +++ b/faststream/nats/opentelemetry/provider.py @@ -1,5 +1,6 @@ from typing import TYPE_CHECKING, List, Optional, Sequence, Union, overload +from nats.aio.msg import Msg from opentelemetry.semconv.trace import SpanAttributes from faststream.__about__ import SERVICE_NAME @@ -8,8 +9,6 @@ from faststream.opentelemetry.consts import MESSAGING_DESTINATION_PUBLISH_NAME if TYPE_CHECKING: - from nats.aio.msg import Msg - from faststream.broker.message import StreamMessage from faststream.types import AnyDict @@ -107,8 +106,12 @@ def telemetry_attributes_provider_factory( ) -> Union[ NatsTelemetrySettingsProvider, NatsBatchTelemetrySettingsProvider, + None, ]: if isinstance(msg, Sequence): return NatsBatchTelemetrySettingsProvider() - else: + elif isinstance(msg, Msg): return NatsTelemetrySettingsProvider() + else: + # KeyValue and Object Storage watch cases + return None diff --git a/faststream/nats/parser.py b/faststream/nats/parser.py index 4824d84716..ef18834eca 100644 --- a/faststream/nats/parser.py +++ b/faststream/nats/parser.py @@ -1,5 +1,4 @@ -from typing import TYPE_CHECKING, Any, List, Optional -from typing import TYPE_CHECKING, Dict, List, Optional +from typing import TYPE_CHECKING, Any, Dict, List, Optional from faststream.broker.message import StreamMessage, decode_message, gen_cor_id from faststream.nats.message import ( diff --git a/faststream/nats/router.py b/faststream/nats/router.py index 679010773a..74215d3e78 100644 --- a/faststream/nats/router.py +++ b/faststream/nats/router.py @@ -28,7 +28,7 @@ SubscriberMiddleware, ) from faststream.nats.message import NatsBatchMessage, NatsMessage - from faststream.nats.schemas import JStream, PullSub, KvWatch, ObjWatch + from faststream.nats.schemas import JStream, KvWatch, ObjWatch, PullSub from faststream.types import SendableMessage diff --git a/faststream/nats/subscriber/asyncapi.py b/faststream/nats/subscriber/asyncapi.py index ce5cfdb6ee..ad0edb0bca 100644 --- a/faststream/nats/subscriber/asyncapi.py +++ b/faststream/nats/subscriber/asyncapi.py @@ -103,16 +103,10 @@ def get_schema(self) -> Dict[str, Channel]: class AsyncAPIObjStoreWatchSubscriber(AsyncAPISubscriber, ObjStoreWatchSubscriber): """ObjStoreWatch consumer with AsyncAPI methods.""" -class AsyncAPIDefaultSubscriber(DefaultHandler, AsyncAPISubscriber): - """One-message consumer with AsyncAPI methods.""" - @override def get_name(self) -> str: return "" + @override def get_schema(self) -> Dict[str, Channel]: return {} - -class AsyncAPIBatchSubscriber(BatchHandler, AsyncAPISubscriber): - """Batch-message consumer with AsyncAPI methods.""" - diff --git a/faststream/nats/subscriber/subscription.py b/faststream/nats/subscriber/subscription.py index 3a51b78455..4bc994842b 100644 --- a/faststream/nats/subscriber/subscription.py +++ b/faststream/nats/subscriber/subscription.py @@ -1,23 +1,25 @@ -from typing import Generic, Protocol, TypeVar +from typing import Any, Generic, Optional, Protocol, TypeVar class Unsubscriptable(Protocol): async def unsubscribe(self) -> None: ... -class Stopable(Protocol): +class Watchable(Protocol): async def stop(self) -> None: ... + async def updates(self, timeout: float) -> Optional[Any]: ... -StopableT = TypeVar("StopableT", bound=Stopable) +WatchableT = TypeVar("WatchableT", bound=Watchable) -class UnsubscribeAdapter(Unsubscriptable, Generic[StopableT]): + +class UnsubscribeAdapter(Unsubscriptable, Generic[WatchableT]): __slots__ = ("obj",) - obj: StopableT + obj: WatchableT - def __init__(self, subscription: StopableT): + def __init__(self, subscription: WatchableT) -> None: self.obj = subscription async def unsubscribe(self) -> None: diff --git a/faststream/opentelemetry/middleware.py b/faststream/opentelemetry/middleware.py index 9a4ad34c10..7bb0519c68 100644 --- a/faststream/opentelemetry/middleware.py +++ b/faststream/opentelemetry/middleware.py @@ -102,7 +102,9 @@ def __init__( self, *, tracer: "Tracer", - settings_provider_factory: Callable[[Any], TelemetrySettingsProvider[Any]], + settings_provider_factory: Callable[ + [Any], Optional[TelemetrySettingsProvider[Any]] + ], metrics_container: _MetricsContainer, msg: Optional[Any] = None, ) -> None: @@ -121,7 +123,8 @@ async def publish_scope( *args: Any, **kwargs: Any, ) -> Any: - provider = self.__settings_provider + if (provider := self.__settings_provider) is None: + return await call_next(msg, *args, **kwargs) headers = kwargs.pop("headers", {}) or {} current_context = context.get_current() @@ -182,7 +185,8 @@ async def consume_scope( call_next: "AsyncFuncAny", msg: "StreamMessage[Any]", ) -> Any: - provider = self.__settings_provider + if (provider := self.__settings_provider) is None: + return await call_next(msg) current_context = propagate.extract(msg.headers) destination_name = provider.get_consume_destination_name(msg) @@ -258,7 +262,9 @@ class TelemetryMiddleware: def __init__( self, *, - settings_provider_factory: Callable[[Any], TelemetrySettingsProvider[Any]], + settings_provider_factory: Callable[ + [Any], Optional[TelemetrySettingsProvider[Any]] + ], tracer_provider: Optional["TracerProvider"] = None, meter_provider: Optional["MeterProvider"] = None, meter: Optional["Meter"] = None, diff --git a/tests/brokers/nats/test_consume.py b/tests/brokers/nats/test_consume.py index 733797c150..60ac90a7f3 100644 --- a/tests/brokers/nats/test_consume.py +++ b/tests/brokers/nats/test_consume.py @@ -277,28 +277,31 @@ def subscriber(m, msg: NatsMessage): ) assert event.is_set() + mock.assert_called_once_with(True) @pytest.mark.asyncio() async def test_consume_kv( self, queue: str, - full_broker: NatsBroker, event: asyncio.Event, mock, ): - @full_broker.subscriber("hello", kv_watch="test") + consume_broker = self.get_broker(apply_types=True) + + @consume_broker.subscriber(queue, kv_watch=queue + "1") async def handler(m): mock(m) event.set() - async with full_broker: - await full_broker.start() - bucket = await full_broker.key_value("test") + async with self.patch_broker(consume_broker) as br: + await br.start() + bucket = await br.key_value(queue + "1") + await asyncio.wait( ( asyncio.create_task( bucket.put( - "hello", + queue, b"world", ) ), @@ -312,17 +315,22 @@ async def handler(m): @pytest.mark.asyncio() async def test_consume_os( - self, queue: str, full_broker: NatsBroker, event: asyncio.Event + self, + queue: str, + event: asyncio.Event, + mock, ): - @full_broker.subscriber("test", obj_watch=True) + consume_broker = self.get_broker(apply_types=True) + + @consume_broker.subscriber(queue, obj_watch=True) async def handler(filename: str): - if filename == "hello": - event.set() + event.set() + mock(filename) + + async with self.patch_broker(consume_broker) as br: + await br.start() + bucket = await br.object_storage(queue) - async with full_broker: - await full_broker.start() - bucket = await full_broker.object_storage("test") - await bucket.watch() await asyncio.wait( ( asyncio.create_task( @@ -337,5 +345,4 @@ async def handler(filename: str): ) assert event.is_set() - assert event.is_set() - mock.assert_called_once_with(True) + mock.assert_called_once_with("hello") diff --git a/tests/brokers/nats/test_kv_declarer_cache.py b/tests/brokers/nats/test_kv_declarer_cache.py index e8a200a5c8..e7caf072bb 100644 --- a/tests/brokers/nats/test_kv_declarer_cache.py +++ b/tests/brokers/nats/test_kv_declarer_cache.py @@ -1,7 +1,8 @@ -import pytest from unittest.mock import patch +import pytest from nats.js import JetStreamContext + from faststream.nats import NatsBroker from tests.tools import spy_decorator @@ -10,7 +11,11 @@ async def test_kv_storage_cache(): broker = NatsBroker() await broker.connect() - with patch.object(JetStreamContext, "create_key_value", spy_decorator(JetStreamContext.create_key_value)) as m: + with patch.object( + JetStreamContext, + "create_key_value", + spy_decorator(JetStreamContext.create_key_value), + ) as m: await broker.key_value("test") await broker.key_value("test") assert broker._kv_declarer.buckets["test"] diff --git a/tests/brokers/nats/test_os_declarer_cache.py b/tests/brokers/nats/test_os_declarer_cache.py index 875c36b31a..9a3778105b 100644 --- a/tests/brokers/nats/test_os_declarer_cache.py +++ b/tests/brokers/nats/test_os_declarer_cache.py @@ -1,7 +1,8 @@ -import pytest from unittest.mock import patch +import pytest from nats.js import JetStreamContext + from faststream.nats import NatsBroker from tests.tools import spy_decorator @@ -11,7 +12,11 @@ async def test_object_storage_cache(): broker = NatsBroker() await broker.connect() - with patch.object(JetStreamContext, "create_object_store", spy_decorator(JetStreamContext.create_object_store)) as m: + with patch.object( + JetStreamContext, + "create_object_store", + spy_decorator(JetStreamContext.create_object_store), + ) as m: await broker.object_storage("test") await broker.object_storage("test") assert broker._os_declarer.buckets["test"] diff --git a/tests/cli/test_publish.py b/tests/cli/test_publish.py index 980dfb4a8a..7e2aa367ea 100644 --- a/tests/cli/test_publish.py +++ b/tests/cli/test_publish.py @@ -1,55 +1,38 @@ from unittest.mock import AsyncMock, patch -import pytest from dirty_equals import IsPartialDict -from typer.testing import CliRunner from faststream import FastStream from faststream.cli.main import cli as faststream_app -from faststream.confluent import KafkaBroker as ConfluentBroker -from faststream.confluent.publisher.producer import AsyncConfluentFastProducer -from faststream.kafka import KafkaBroker -from faststream.kafka.publisher.producer import AioKafkaFastProducer -from faststream.nats import NatsBroker -from faststream.nats.publisher.producer import NatsFastProducer -from faststream.rabbit import RabbitBroker -from faststream.rabbit.publisher.producer import AioPikaFastProducer -from faststream.redis import RedisBroker -from faststream.redis.publisher.producer import RedisFastProducer - -# Initialize the CLI runner -runner = CliRunner() - - -@pytest.fixture() -def mock_app(request): - app = FastStream() - broker_type = request.param["broker_type"] - producer_type = request.param["producer_type"] +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) + +def get_mock_app(broker_type, producer_type) -> FastStream: broker = broker_type() broker.connect = AsyncMock() - mock_producer = AsyncMock(spec=producer_type) mock_producer.publish = AsyncMock() broker._producer = mock_producer + return FastStream(broker) - app.broker = broker - return app +@require_redis +def test_publish_command_with_redis_options(runner): + from faststream.redis import RedisBroker + from faststream.redis.publisher.producer import RedisFastProducer -@pytest.mark.parametrize( - "mock_app", - [ - { - "broker_type": RedisBroker, - "producer_type": RedisFastProducer, - } - ], - indirect=True, -) -def test_publish_command_with_redis_options(mock_app): - with patch("faststream.cli.main.import_from_string", return_value=(None, mock_app)): + mock_app = get_mock_app(RedisBroker, RedisFastProducer) + + with patch( + "faststream.cli.main.import_from_string", + return_value=(None, mock_app), + ): result = runner.invoke( faststream_app, [ @@ -82,17 +65,13 @@ def test_publish_command_with_redis_options(mock_app): ) -@pytest.mark.parametrize( - "mock_app", - [ - { - "broker_type": ConfluentBroker, - "producer_type": AsyncConfluentFastProducer, - } - ], - indirect=True, -) -def test_publish_command_with_confluent_options(mock_app): +@require_confluent +def test_publish_command_with_confluent_options(runner): + from faststream.confluent import KafkaBroker as ConfluentBroker + from faststream.confluent.publisher.producer import AsyncConfluentFastProducer + + mock_app = get_mock_app(ConfluentBroker, AsyncConfluentFastProducer) + with patch("faststream.cli.main.import_from_string", return_value=(None, mock_app)): result = runner.invoke( faststream_app, @@ -116,12 +95,13 @@ def test_publish_command_with_confluent_options(mock_app): ) -@pytest.mark.parametrize( - "mock_app", - [{"broker_type": KafkaBroker, "producer_type": AioKafkaFastProducer}], - indirect=True, -) -def test_publish_command_with_kafka_options(mock_app): +@require_aiokafka +def test_publish_command_with_kafka_options(runner): + from faststream.kafka import KafkaBroker + from faststream.kafka.publisher.producer import AioKafkaFastProducer + + mock_app = get_mock_app(KafkaBroker, AioKafkaFastProducer) + with patch("faststream.cli.main.import_from_string", return_value=(None, mock_app)): result = runner.invoke( faststream_app, @@ -145,12 +125,13 @@ def test_publish_command_with_kafka_options(mock_app): ) -@pytest.mark.parametrize( - "mock_app", - [{"broker_type": NatsBroker, "producer_type": NatsFastProducer}], - indirect=True, -) -def test_publish_command_with_nats_options(mock_app): +@require_nats +def test_publish_command_with_nats_options(runner): + from faststream.nats import NatsBroker + from faststream.nats.publisher.producer import NatsFastProducer + + mock_app = get_mock_app(NatsBroker, NatsFastProducer) + with patch("faststream.cli.main.import_from_string", return_value=(None, mock_app)): result = runner.invoke( faststream_app, @@ -178,12 +159,13 @@ def test_publish_command_with_nats_options(mock_app): ) -@pytest.mark.parametrize( - "mock_app", - [{"broker_type": RabbitBroker, "producer_type": AioPikaFastProducer}], - indirect=True, -) -def test_publish_command_with_rabbit_options(mock_app: AsyncMock): +@require_aiopika +def test_publish_command_with_rabbit_options(runner): + from faststream.rabbit import RabbitBroker + from faststream.rabbit.publisher.producer import AioPikaFastProducer + + mock_app = get_mock_app(RabbitBroker, AioPikaFastProducer) + with patch("faststream.cli.main.import_from_string", return_value=(None, mock_app)): result = runner.invoke( faststream_app, diff --git a/tests/docs/getting_started/cli/confluent/__init__.py b/tests/docs/getting_started/cli/confluent/__init__.py new file mode 100644 index 0000000000..c4a1803708 --- /dev/null +++ b/tests/docs/getting_started/cli/confluent/__init__.py @@ -0,0 +1,3 @@ +import pytest + +pytest.importorskip("confluent_kafka") diff --git a/tests/docs/getting_started/cli/test_confluent_context.py b/tests/docs/getting_started/cli/confluent/test_confluent_context.py similarity index 100% rename from tests/docs/getting_started/cli/test_confluent_context.py rename to tests/docs/getting_started/cli/confluent/test_confluent_context.py diff --git a/tests/docs/getting_started/cli/kafka/__init__.py b/tests/docs/getting_started/cli/kafka/__init__.py new file mode 100644 index 0000000000..bd6bc708fc --- /dev/null +++ b/tests/docs/getting_started/cli/kafka/__init__.py @@ -0,0 +1,3 @@ +import pytest + +pytest.importorskip("aiokafka") diff --git a/tests/docs/getting_started/cli/test_kafka_context.py b/tests/docs/getting_started/cli/kafka/test_kafka_context.py similarity index 100% rename from tests/docs/getting_started/cli/test_kafka_context.py rename to tests/docs/getting_started/cli/kafka/test_kafka_context.py diff --git a/tests/docs/getting_started/cli/nats/__init__.py b/tests/docs/getting_started/cli/nats/__init__.py new file mode 100644 index 0000000000..87ead90ee6 --- /dev/null +++ b/tests/docs/getting_started/cli/nats/__init__.py @@ -0,0 +1,3 @@ +import pytest + +pytest.importorskip("nats") diff --git a/tests/docs/getting_started/cli/test_nats_context.py b/tests/docs/getting_started/cli/nats/test_nats_context.py similarity index 100% rename from tests/docs/getting_started/cli/test_nats_context.py rename to tests/docs/getting_started/cli/nats/test_nats_context.py diff --git a/tests/docs/getting_started/cli/rabbit/__init__.py b/tests/docs/getting_started/cli/rabbit/__init__.py new file mode 100644 index 0000000000..ebec43fcd5 --- /dev/null +++ b/tests/docs/getting_started/cli/rabbit/__init__.py @@ -0,0 +1,3 @@ +import pytest + +pytest.importorskip("aio_pika") diff --git a/tests/docs/getting_started/cli/test_rabbit_context.py b/tests/docs/getting_started/cli/rabbit/test_rabbit_context.py similarity index 100% rename from tests/docs/getting_started/cli/test_rabbit_context.py rename to tests/docs/getting_started/cli/rabbit/test_rabbit_context.py diff --git a/tests/docs/getting_started/cli/redis/__init__.py b/tests/docs/getting_started/cli/redis/__init__.py new file mode 100644 index 0000000000..4752ef19b1 --- /dev/null +++ b/tests/docs/getting_started/cli/redis/__init__.py @@ -0,0 +1,3 @@ +import pytest + +pytest.importorskip("redis") diff --git a/tests/docs/getting_started/cli/test_redis_context.py b/tests/docs/getting_started/cli/redis/test_redis_context.py similarity index 100% rename from tests/docs/getting_started/cli/test_redis_context.py rename to tests/docs/getting_started/cli/redis/test_redis_context.py diff --git a/tests/docs/getting_started/context/test_annotated.py b/tests/docs/getting_started/context/test_annotated.py index b800ad1956..b85bc816bc 100644 --- a/tests/docs/getting_started/context/test_annotated.py +++ b/tests/docs/getting_started/context/test_annotated.py @@ -1,20 +1,24 @@ import pytest -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker -from tests.marks import python39 +from tests.marks import ( + python39, + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @python39 @pytest.mark.asyncio() +@require_aiokafka async def test_annotated_kafka(): from docs.docs_src.getting_started.context.kafka.annotated import ( base_handler, broker, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker) as br: await br.publish("Hi!", "test") @@ -24,11 +28,13 @@ async def test_annotated_kafka(): @python39 @pytest.mark.asyncio() +@require_confluent async def test_annotated_confluent(): from docs.docs_src.getting_started.context.confluent.annotated import ( base_handler, broker, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker) as br: await br.publish("Hi!", "test") @@ -38,11 +44,13 @@ async def test_annotated_confluent(): @python39 @pytest.mark.asyncio() +@require_aiopika async def test_annotated_rabbit(): from docs.docs_src.getting_started.context.rabbit.annotated import ( base_handler, broker, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker) as br: await br.publish("Hi!", "test") @@ -52,11 +60,13 @@ async def test_annotated_rabbit(): @python39 @pytest.mark.asyncio() +@require_nats async def test_annotated_nats(): from docs.docs_src.getting_started.context.nats.annotated import ( base_handler, broker, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker) as br: await br.publish("Hi!", "test") @@ -66,11 +76,13 @@ async def test_annotated_nats(): @python39 @pytest.mark.asyncio() +@require_redis async def test_annotated_redis(): from docs.docs_src.getting_started.context.redis.annotated import ( base_handler, broker, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker) as br: await br.publish("Hi!", "test") diff --git a/tests/docs/getting_started/context/test_base.py b/tests/docs/getting_started/context/test_base.py index 03e9984f24..5b24fdd827 100644 --- a/tests/docs/getting_started/context/test_base.py +++ b/tests/docs/getting_started/context/test_base.py @@ -1,15 +1,19 @@ import pytest -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_base_kafka(): from docs.docs_src.getting_started.context.kafka.base import base_handler, broker + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker) as br: await br.publish("Hi!", "test") @@ -18,11 +22,13 @@ async def test_base_kafka(): @pytest.mark.asyncio() +@require_confluent async def test_base_confluent(): from docs.docs_src.getting_started.context.confluent.base import ( base_handler, broker, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker) as br: await br.publish("Hi!", "test") @@ -31,8 +37,10 @@ async def test_base_confluent(): @pytest.mark.asyncio() +@require_aiopika async def test_base_rabbit(): from docs.docs_src.getting_started.context.rabbit.base import base_handler, broker + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker) as br: await br.publish("Hi!", "test") @@ -41,8 +49,10 @@ async def test_base_rabbit(): @pytest.mark.asyncio() +@require_nats async def test_base_nats(): from docs.docs_src.getting_started.context.nats.base import base_handler, broker + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker) as br: await br.publish("Hi!", "test") @@ -51,8 +61,10 @@ async def test_base_nats(): @pytest.mark.asyncio() +@require_redis async def test_base_redis(): from docs.docs_src.getting_started.context.redis.base import base_handler, broker + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker) as br: await br.publish("Hi!", "test") diff --git a/tests/docs/getting_started/context/test_cast.py b/tests/docs/getting_started/context/test_cast.py index 6ade96b33e..f99769603e 100644 --- a/tests/docs/getting_started/context/test_cast.py +++ b/tests/docs/getting_started/context/test_cast.py @@ -1,19 +1,23 @@ import pytest -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_cast_kafka(): from docs.docs_src.getting_started.context.kafka.cast import ( broker, handle, handle_int, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker) as br: await br.publish("Hi!", "test-topic") @@ -26,12 +30,14 @@ async def test_cast_kafka(): @pytest.mark.asyncio() +@require_confluent async def test_cast_confluent(): from docs.docs_src.getting_started.context.confluent.cast import ( broker, handle, handle_int, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker) as br: await br.publish("Hi!", "test-topic") @@ -44,12 +50,14 @@ async def test_cast_confluent(): @pytest.mark.asyncio() +@require_aiopika async def test_cast_rabbit(): from docs.docs_src.getting_started.context.rabbit.cast import ( broker, handle, handle_int, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker) as br: await br.publish("Hi!", "test-queue") @@ -62,12 +70,14 @@ async def test_cast_rabbit(): @pytest.mark.asyncio() +@require_nats async def test_cast_nats(): from docs.docs_src.getting_started.context.nats.cast import ( broker, handle, handle_int, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker) as br: await br.publish("Hi!", "test-subject") @@ -80,12 +90,14 @@ async def test_cast_nats(): @pytest.mark.asyncio() +@require_redis async def test_cast_redis(): from docs.docs_src.getting_started.context.redis.cast import ( broker, handle, handle_int, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker) as br: await br.publish("Hi!", "test-channel") diff --git a/tests/docs/getting_started/context/test_custom_global.py b/tests/docs/getting_started/context/test_custom_global.py index b9ec555cfe..0541980245 100644 --- a/tests/docs/getting_started/context/test_custom_global.py +++ b/tests/docs/getting_started/context/test_custom_global.py @@ -1,20 +1,24 @@ import pytest from faststream import TestApp -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_custom_global_context_kafka(): from docs.docs_src.getting_started.context.kafka.custom_global_context import ( app, broker, handle, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker) as br, TestApp(app): await br.publish("Hi!", "test-topic") @@ -23,12 +27,14 @@ async def test_custom_global_context_kafka(): @pytest.mark.asyncio() +@require_confluent async def test_custom_global_context_confluent(): from docs.docs_src.getting_started.context.confluent.custom_global_context import ( app, broker, handle, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker) as br, TestApp(app): await br.publish("Hi!", "test-topic") @@ -37,12 +43,14 @@ async def test_custom_global_context_confluent(): @pytest.mark.asyncio() +@require_aiopika async def test_custom_global_context_rabbit(): from docs.docs_src.getting_started.context.rabbit.custom_global_context import ( app, broker, handle, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker) as br, TestApp(app): await br.publish("Hi!", "test-queue") @@ -51,12 +59,14 @@ async def test_custom_global_context_rabbit(): @pytest.mark.asyncio() +@require_nats async def test_custom_global_context_nats(): from docs.docs_src.getting_started.context.nats.custom_global_context import ( app, broker, handle, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker) as br, TestApp(app): await br.publish("Hi!", "test-subject") @@ -65,12 +75,14 @@ async def test_custom_global_context_nats(): @pytest.mark.asyncio() +@require_redis async def test_custom_global_context_redis(): from docs.docs_src.getting_started.context.redis.custom_global_context import ( app, broker, handle, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker) as br, TestApp(app): await br.publish("Hi!", "test-channel") diff --git a/tests/docs/getting_started/context/test_custom_local.py b/tests/docs/getting_started/context/test_custom_local.py index 8672c6dd65..e2cf2421c0 100644 --- a/tests/docs/getting_started/context/test_custom_local.py +++ b/tests/docs/getting_started/context/test_custom_local.py @@ -1,18 +1,22 @@ import pytest -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_custom_local_context_kafka(): from docs.docs_src.getting_started.context.kafka.custom_local_context import ( broker, handle, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker) as br: await br.publish("Hi!", "test-topic") @@ -21,11 +25,13 @@ async def test_custom_local_context_kafka(): @pytest.mark.asyncio() +@require_confluent async def test_custom_local_context_confluent(): from docs.docs_src.getting_started.context.confluent.custom_local_context import ( broker, handle, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker) as br: await br.publish("Hi!", "test-topic") @@ -34,11 +40,13 @@ async def test_custom_local_context_confluent(): @pytest.mark.asyncio() +@require_aiopika async def test_custom_local_context_rabbit(): from docs.docs_src.getting_started.context.rabbit.custom_local_context import ( broker, handle, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker) as br: await br.publish("Hi!", "test-queue") @@ -47,11 +55,13 @@ async def test_custom_local_context_rabbit(): @pytest.mark.asyncio() +@require_nats async def test_custom_local_context_nats(): from docs.docs_src.getting_started.context.nats.custom_local_context import ( broker, handle, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker) as br: await br.publish("Hi!", "test-subject") @@ -60,11 +70,13 @@ async def test_custom_local_context_nats(): @pytest.mark.asyncio() +@require_redis async def test_custom_local_context_redis(): from docs.docs_src.getting_started.context.redis.custom_local_context import ( broker, handle, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker) as br: await br.publish("Hi!", "test-channel") diff --git a/tests/docs/getting_started/context/test_default_arguments.py b/tests/docs/getting_started/context/test_default_arguments.py index f583f09f9b..969baf881f 100644 --- a/tests/docs/getting_started/context/test_default_arguments.py +++ b/tests/docs/getting_started/context/test_default_arguments.py @@ -1,18 +1,22 @@ import pytest -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_default_arguments_kafka(): from docs.docs_src.getting_started.context.kafka.default_arguments import ( broker, handle, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker) as br: await br.publish("Hi!", "test-topic") @@ -21,11 +25,13 @@ async def test_default_arguments_kafka(): @pytest.mark.asyncio() +@require_confluent async def test_default_arguments_confluent(): from docs.docs_src.getting_started.context.confluent.default_arguments import ( broker, handle, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker) as br: await br.publish("Hi!", "test-topic") @@ -34,11 +40,13 @@ async def test_default_arguments_confluent(): @pytest.mark.asyncio() +@require_aiopika async def test_default_arguments_rabbit(): from docs.docs_src.getting_started.context.rabbit.default_arguments import ( broker, handle, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker) as br: await br.publish("Hi!", "test-queue") @@ -47,11 +55,13 @@ async def test_default_arguments_rabbit(): @pytest.mark.asyncio() +@require_nats async def test_default_arguments_nats(): from docs.docs_src.getting_started.context.nats.default_arguments import ( broker, handle, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker) as br: await br.publish("Hi!", "test-subject") @@ -60,11 +70,13 @@ async def test_default_arguments_nats(): @pytest.mark.asyncio() +@require_redis async def test_default_arguments_redis(): from docs.docs_src.getting_started.context.redis.default_arguments import ( broker, handle, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker) as br: await br.publish("Hi!", "test-channel") diff --git a/tests/docs/getting_started/context/test_existed_context.py b/tests/docs/getting_started/context/test_existed_context.py index 5b13128849..e0b0eebafc 100644 --- a/tests/docs/getting_started/context/test_existed_context.py +++ b/tests/docs/getting_started/context/test_existed_context.py @@ -1,17 +1,21 @@ import pytest -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_existed_context_kafka(): from docs.docs_src.getting_started.context.kafka.existed_context import ( broker_object, ) + from faststream.kafka import TestKafkaBroker @broker_object.subscriber("response") async def resp(): ... @@ -24,10 +28,12 @@ async def resp(): ... @pytest.mark.asyncio() +@require_confluent async def test_existed_context_confluent(): from docs.docs_src.getting_started.context.confluent.existed_context import ( broker_object, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker @broker_object.subscriber("response") async def resp(): ... @@ -40,10 +46,12 @@ async def resp(): ... @pytest.mark.asyncio() +@require_aiopika async def test_existed_context_rabbit(): from docs.docs_src.getting_started.context.rabbit.existed_context import ( broker_object, ) + from faststream.rabbit import TestRabbitBroker @broker_object.subscriber("response") async def resp(): ... @@ -56,10 +64,12 @@ async def resp(): ... @pytest.mark.asyncio() +@require_nats async def test_existed_context_nats(): from docs.docs_src.getting_started.context.nats.existed_context import ( broker_object, ) + from faststream.nats import TestNatsBroker @broker_object.subscriber("response") async def resp(): ... @@ -72,10 +82,12 @@ async def resp(): ... @pytest.mark.asyncio() +@require_redis async def test_existed_context_redis(): from docs.docs_src.getting_started.context.redis.existed_context import ( broker_object, ) + from faststream.redis import TestRedisBroker @broker_object.subscriber("response") async def resp(): ... diff --git a/tests/docs/getting_started/context/test_fields_access.py b/tests/docs/getting_started/context/test_fields_access.py index ab8a47d65b..f584a86089 100644 --- a/tests/docs/getting_started/context/test_fields_access.py +++ b/tests/docs/getting_started/context/test_fields_access.py @@ -1,18 +1,22 @@ import pytest -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_fields_access_kafka(): from docs.docs_src.getting_started.context.kafka.fields_access import ( broker, handle, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker) as br: await br.publish("Hi!", "test-topic", headers={"user": "John"}) @@ -21,11 +25,13 @@ async def test_fields_access_kafka(): @pytest.mark.asyncio() +@require_confluent async def test_fields_access_confluent(): from docs.docs_src.getting_started.context.confluent.fields_access import ( broker, handle, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker) as br: await br.publish("Hi!", "test-topic", headers={"user": "John"}) @@ -34,11 +40,13 @@ async def test_fields_access_confluent(): @pytest.mark.asyncio() +@require_aiopika async def test_fields_access_rabbit(): from docs.docs_src.getting_started.context.rabbit.fields_access import ( broker, handle, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker) as br: await br.publish("Hi!", "test-queue", headers={"user": "John"}) @@ -47,11 +55,13 @@ async def test_fields_access_rabbit(): @pytest.mark.asyncio() +@require_nats async def test_fields_access_nats(): from docs.docs_src.getting_started.context.nats.fields_access import ( broker, handle, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker) as br: await br.publish("Hi!", "test-subject", headers={"user": "John"}) @@ -60,11 +70,13 @@ async def test_fields_access_nats(): @pytest.mark.asyncio() +@require_redis async def test_fields_access_redis(): from docs.docs_src.getting_started.context.redis.fields_access import ( broker, handle, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker) as br: await br.publish("Hi!", "test-channel", headers={"user": "John"}) diff --git a/tests/docs/getting_started/context/test_initial.py b/tests/docs/getting_started/context/test_initial.py index b4a4a9dbbe..2ea77e2ebd 100644 --- a/tests/docs/getting_started/context/test_initial.py +++ b/tests/docs/getting_started/context/test_initial.py @@ -1,11 +1,19 @@ import pytest from faststream import context -from tests.marks import python39 +from tests.marks import ( + python39, + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() @python39 +@require_aiokafka async def test_kafka(): from docs.docs_src.getting_started.context.kafka.initial import broker from faststream.kafka import TestKafkaBroker @@ -20,6 +28,7 @@ async def test_kafka(): @pytest.mark.asyncio() @python39 +@require_confluent async def test_confluent(): from docs.docs_src.getting_started.context.confluent.initial import broker from faststream.confluent import TestKafkaBroker @@ -34,6 +43,7 @@ async def test_confluent(): @pytest.mark.asyncio() @python39 +@require_aiopika async def test_rabbit(): from docs.docs_src.getting_started.context.rabbit.initial import broker from faststream.rabbit import TestRabbitBroker @@ -48,6 +58,7 @@ async def test_rabbit(): @pytest.mark.asyncio() @python39 +@require_nats async def test_nats(): from docs.docs_src.getting_started.context.nats.initial import broker from faststream.nats import TestNatsBroker @@ -62,6 +73,7 @@ async def test_nats(): @pytest.mark.asyncio() @python39 +@require_redis async def test_redis(): from docs.docs_src.getting_started.context.redis.initial import broker from faststream.redis import TestRedisBroker diff --git a/tests/docs/getting_started/context/test_manual_local_context.py b/tests/docs/getting_started/context/test_manual_local_context.py index 9442d1166c..f7829575b9 100644 --- a/tests/docs/getting_started/context/test_manual_local_context.py +++ b/tests/docs/getting_started/context/test_manual_local_context.py @@ -1,18 +1,22 @@ import pytest -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_manual_local_context_kafka(): from docs.docs_src.getting_started.context.kafka.manual_local_context import ( broker, handle, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker) as br: await br.publish("Hi!", "test-topic") @@ -21,11 +25,13 @@ async def test_manual_local_context_kafka(): @pytest.mark.asyncio() +@require_confluent async def test_manual_local_context_confluent(): from docs.docs_src.getting_started.context.confluent.manual_local_context import ( broker, handle, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker) as br: await br.publish("Hi!", "test-topic") @@ -34,11 +40,13 @@ async def test_manual_local_context_confluent(): @pytest.mark.asyncio() +@require_aiopika async def test_manual_local_context_rabbit(): from docs.docs_src.getting_started.context.rabbit.manual_local_context import ( broker, handle, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker) as br: await br.publish("Hi!", "test-queue") @@ -47,11 +55,13 @@ async def test_manual_local_context_rabbit(): @pytest.mark.asyncio() +@require_nats async def test_manual_local_context_nats(): from docs.docs_src.getting_started.context.nats.manual_local_context import ( broker, handle, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker) as br: await br.publish("Hi!", "test-subject") @@ -60,11 +70,13 @@ async def test_manual_local_context_nats(): @pytest.mark.asyncio() +@require_redis async def test_manual_local_context_redis(): from docs.docs_src.getting_started.context.redis.manual_local_context import ( broker, handle, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker) as br: await br.publish("Hi!", "test-channel") diff --git a/tests/docs/getting_started/context/test_nested.py b/tests/docs/getting_started/context/test_nested.py index 11e6932084..7e460e555b 100644 --- a/tests/docs/getting_started/context/test_nested.py +++ b/tests/docs/getting_started/context/test_nested.py @@ -1,9 +1,11 @@ import pytest from faststream.rabbit import TestRabbitBroker +from tests.marks import require_aiopika @pytest.mark.asyncio() +@require_aiopika async def test(): from docs.docs_src.getting_started.context.nested import broker, handler diff --git a/tests/docs/getting_started/dependencies/basic/test_depends.py b/tests/docs/getting_started/dependencies/basic/test_depends.py index 724fab875c..0c46d6a967 100644 --- a/tests/docs/getting_started/dependencies/basic/test_depends.py +++ b/tests/docs/getting_started/dependencies/basic/test_depends.py @@ -1,18 +1,22 @@ import pytest -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_depends_kafka(): from docs.docs_src.getting_started.dependencies.basic.kafka.depends import ( broker, handler, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker): await broker.publish({}, "test") @@ -20,11 +24,13 @@ async def test_depends_kafka(): @pytest.mark.asyncio() +@require_confluent async def test_depends_confluent(): from docs.docs_src.getting_started.dependencies.basic.confluent.depends import ( broker, handler, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker): await broker.publish({}, "test") @@ -32,11 +38,13 @@ async def test_depends_confluent(): @pytest.mark.asyncio() +@require_aiopika async def test_depends_rabbit(): from docs.docs_src.getting_started.dependencies.basic.rabbit.depends import ( broker, handler, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker): await broker.publish({}, "test") @@ -44,11 +52,13 @@ async def test_depends_rabbit(): @pytest.mark.asyncio() +@require_nats async def test_depends_nats(): from docs.docs_src.getting_started.dependencies.basic.nats.depends import ( broker, handler, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker): await broker.publish({}, "test") @@ -56,11 +66,13 @@ async def test_depends_nats(): @pytest.mark.asyncio() +@require_redis async def test_depends_redis(): from docs.docs_src.getting_started.dependencies.basic.redis.depends import ( broker, handler, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker): await broker.publish({}, "test") diff --git a/tests/docs/getting_started/dependencies/basic/test_nested_depends.py b/tests/docs/getting_started/dependencies/basic/test_nested_depends.py index 51578c24a9..e2ffabd2f1 100644 --- a/tests/docs/getting_started/dependencies/basic/test_nested_depends.py +++ b/tests/docs/getting_started/dependencies/basic/test_nested_depends.py @@ -1,18 +1,22 @@ import pytest -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_nested_depends_kafka(): from docs.docs_src.getting_started.dependencies.basic.kafka.nested_depends import ( broker, handler, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker): await broker.publish({}, "test") @@ -20,11 +24,13 @@ async def test_nested_depends_kafka(): @pytest.mark.asyncio() +@require_confluent async def test_nested_depends_confluent(): from docs.docs_src.getting_started.dependencies.basic.confluent.nested_depends import ( broker, handler, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker): await broker.publish({}, "test") @@ -32,11 +38,13 @@ async def test_nested_depends_confluent(): @pytest.mark.asyncio() +@require_aiopika async def test_nested_depends_rabbit(): from docs.docs_src.getting_started.dependencies.basic.rabbit.nested_depends import ( broker, handler, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker): await broker.publish({}, "test") @@ -44,11 +52,13 @@ async def test_nested_depends_rabbit(): @pytest.mark.asyncio() +@require_nats async def test_nested_depends_nats(): from docs.docs_src.getting_started.dependencies.basic.nats.nested_depends import ( broker, handler, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker): await broker.publish({}, "test") @@ -56,11 +66,13 @@ async def test_nested_depends_nats(): @pytest.mark.asyncio() +@require_redis async def test_nested_depends_redis(): from docs.docs_src.getting_started.dependencies.basic.redis.nested_depends import ( broker, handler, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker): await broker.publish({}, "test") diff --git a/tests/docs/getting_started/index/test_basic.py b/tests/docs/getting_started/index/test_basic.py index 945ae94971..5341408c8d 100644 --- a/tests/docs/getting_started/index/test_basic.py +++ b/tests/docs/getting_started/index/test_basic.py @@ -1,15 +1,19 @@ import pytest -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_quickstart_index_kafka(): from docs.docs_src.getting_started.index.base_kafka import base_handler, broker + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker) as br: await br.publish("", "test") @@ -18,8 +22,10 @@ async def test_quickstart_index_kafka(): @pytest.mark.asyncio() +@require_confluent async def test_quickstart_index_confluent(): from docs.docs_src.getting_started.index.base_confluent import base_handler, broker + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker) as br: await br.publish("", "test") @@ -28,8 +34,10 @@ async def test_quickstart_index_confluent(): @pytest.mark.asyncio() +@require_aiopika async def test_quickstart_index_rabbit(): from docs.docs_src.getting_started.index.base_rabbit import base_handler, broker + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker) as br: await br.publish("", "test") @@ -38,8 +46,10 @@ async def test_quickstart_index_rabbit(): @pytest.mark.asyncio() +@require_nats async def test_quickstart_index_nats(): from docs.docs_src.getting_started.index.base_nats import base_handler, broker + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker) as br: await br.publish("", "test") @@ -48,8 +58,10 @@ async def test_quickstart_index_nats(): @pytest.mark.asyncio() +@require_redis async def test_quickstart_index_redis(): from docs.docs_src.getting_started.index.base_redis import base_handler, broker + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker) as br: await br.publish("", "test") diff --git a/tests/docs/getting_started/lifespan/test_basic.py b/tests/docs/getting_started/lifespan/test_basic.py index 21ba14a72e..82f0227e59 100644 --- a/tests/docs/getting_started/lifespan/test_basic.py +++ b/tests/docs/getting_started/lifespan/test_basic.py @@ -1,18 +1,23 @@ import pytest from faststream import TestApp, context -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker -from tests.marks import pydantic_v2 +from tests.marks import ( + pydantic_v2, + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) from tests.mocks import mock_pydantic_settings_env @pydantic_v2 @pytest.mark.asyncio() +@require_aiopika async def test_rabbit_basic_lifespan(): + from faststream.rabbit import TestRabbitBroker + with mock_pydantic_settings_env({"host": "localhost"}): from docs.docs_src.getting_started.lifespan.rabbit.basic import app, broker @@ -22,7 +27,10 @@ async def test_rabbit_basic_lifespan(): @pydantic_v2 @pytest.mark.asyncio() +@require_aiokafka async def test_kafka_basic_lifespan(): + from faststream.kafka import TestKafkaBroker + with mock_pydantic_settings_env({"host": "localhost"}): from docs.docs_src.getting_started.lifespan.kafka.basic import app, broker @@ -32,7 +40,10 @@ async def test_kafka_basic_lifespan(): @pydantic_v2 @pytest.mark.asyncio() +@require_confluent async def test_confluent_basic_lifespan(): + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker + with mock_pydantic_settings_env({"host": "localhost"}): from docs.docs_src.getting_started.lifespan.confluent.basic import app, broker @@ -42,7 +53,10 @@ async def test_confluent_basic_lifespan(): @pydantic_v2 @pytest.mark.asyncio() +@require_nats async def test_nats_basic_lifespan(): + from faststream.nats import TestNatsBroker + with mock_pydantic_settings_env({"host": "localhost"}): from docs.docs_src.getting_started.lifespan.nats.basic import app, broker @@ -52,7 +66,10 @@ async def test_nats_basic_lifespan(): @pydantic_v2 @pytest.mark.asyncio() +@require_redis async def test_redis_basic_lifespan(): + from faststream.redis import TestRedisBroker + with mock_pydantic_settings_env({"host": "localhost"}): from docs.docs_src.getting_started.lifespan.redis.basic import app, broker diff --git a/tests/docs/getting_started/lifespan/test_ml.py b/tests/docs/getting_started/lifespan/test_ml.py index 8c4502c856..f63d6bbed7 100644 --- a/tests/docs/getting_started/lifespan/test_ml.py +++ b/tests/docs/getting_started/lifespan/test_ml.py @@ -1,16 +1,20 @@ import pytest from faststream import TestApp -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiopika async def test_rabbit_ml_lifespan(): from docs.docs_src.getting_started.lifespan.rabbit.ml import app, broker, predict + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker), TestApp(app): assert {"result": 42.0} == await broker.publish(1.0, "test", rpc=True) @@ -19,8 +23,10 @@ async def test_rabbit_ml_lifespan(): @pytest.mark.asyncio() +@require_aiokafka async def test_kafka_ml_lifespan(): from docs.docs_src.getting_started.lifespan.kafka.ml import app, broker, predict + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker), TestApp(app): assert {"result": 42.0} == await broker.publish(1.0, "test", rpc=True) @@ -29,8 +35,10 @@ async def test_kafka_ml_lifespan(): @pytest.mark.asyncio() +@require_confluent async def test_confluent_ml_lifespan(): from docs.docs_src.getting_started.lifespan.confluent.ml import app, broker, predict + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker), TestApp(app): assert {"result": 42.0} == await broker.publish(1.0, "test", rpc=True) @@ -39,8 +47,10 @@ async def test_confluent_ml_lifespan(): @pytest.mark.asyncio() +@require_nats async def test_nats_ml_lifespan(): from docs.docs_src.getting_started.lifespan.nats.ml import app, broker, predict + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker), TestApp(app): assert {"result": 42.0} == await broker.publish(1.0, "test", rpc=True) @@ -49,8 +59,10 @@ async def test_nats_ml_lifespan(): @pytest.mark.asyncio() +@require_redis async def test_redis_ml_lifespan(): from docs.docs_src.getting_started.lifespan.redis.ml import app, broker, predict + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker), TestApp(app): assert {"result": 42.0} == await broker.publish(1.0, "test", rpc=True) diff --git a/tests/docs/getting_started/lifespan/test_ml_context.py b/tests/docs/getting_started/lifespan/test_ml_context.py index 612fcd2c91..dc7cb57d6a 100644 --- a/tests/docs/getting_started/lifespan/test_ml_context.py +++ b/tests/docs/getting_started/lifespan/test_ml_context.py @@ -1,20 +1,24 @@ import pytest from faststream import TestApp -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiopika async def test_rabbit_ml_lifespan(): from docs.docs_src.getting_started.lifespan.rabbit.ml_context import ( app, broker, predict, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker), TestApp(app): assert {"result": 42.0} == await broker.publish(1.0, "test", rpc=True) @@ -23,12 +27,14 @@ async def test_rabbit_ml_lifespan(): @pytest.mark.asyncio() +@require_aiokafka async def test_kafka_ml_lifespan(): from docs.docs_src.getting_started.lifespan.kafka.ml_context import ( app, broker, predict, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker), TestApp(app): assert {"result": 42.0} == await broker.publish(1.0, "test", rpc=True) @@ -37,12 +43,14 @@ async def test_kafka_ml_lifespan(): @pytest.mark.asyncio() +@require_confluent async def test_confluent_ml_lifespan(): from docs.docs_src.getting_started.lifespan.confluent.ml_context import ( app, broker, predict, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker), TestApp(app): assert {"result": 42.0} == await broker.publish(1.0, "test", rpc=True) @@ -51,12 +59,14 @@ async def test_confluent_ml_lifespan(): @pytest.mark.asyncio() +@require_nats async def test_nats_ml_lifespan(): from docs.docs_src.getting_started.lifespan.nats.ml_context import ( app, broker, predict, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker), TestApp(app): assert {"result": 42.0} == await broker.publish(1.0, "test", rpc=True) @@ -65,12 +75,14 @@ async def test_nats_ml_lifespan(): @pytest.mark.asyncio() +@require_redis async def test_redis_ml_lifespan(): from docs.docs_src.getting_started.lifespan.redis.ml_context import ( app, broker, predict, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker), TestApp(app): assert {"result": 42.0} == await broker.publish(1.0, "test", rpc=True) diff --git a/tests/docs/getting_started/lifespan/test_testing.py b/tests/docs/getting_started/lifespan/test_testing.py index cfe19fca37..42dbf0dd58 100644 --- a/tests/docs/getting_started/lifespan/test_testing.py +++ b/tests/docs/getting_started/lifespan/test_testing.py @@ -1,22 +1,65 @@ -from docs.docs_src.getting_started.lifespan.confluent.testing import ( - test_lifespan as _test_lifespan_confluent, -) -from docs.docs_src.getting_started.lifespan.kafka.testing import ( - test_lifespan as _test_lifespan_k, -) -from docs.docs_src.getting_started.lifespan.nats.testing import ( - test_lifespan as _test_lifespan_n, -) -from docs.docs_src.getting_started.lifespan.rabbit.testing import ( - test_lifespan as _test_lifespan_r, -) -from docs.docs_src.getting_started.lifespan.redis.testing import ( - test_lifespan as _test_lifespan_red, +import pytest + +from tests.marks import ( + python39, + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, ) -from tests.marks import python39 -test_lifespan_red = python39(_test_lifespan_red) -test_lifespan_r = python39(_test_lifespan_r) -test_lifespan_n = python39(_test_lifespan_n) -test_lifespan_k = python39(_test_lifespan_k) -test_lifespan_confluent = python39(_test_lifespan_confluent) + +@pytest.mark.asyncio() +@python39 +@require_redis +async def test_lifespan_redis(): + from docs.docs_src.getting_started.lifespan.redis.testing import ( + test_lifespan as _test_lifespan_red, + ) + + await _test_lifespan_red() + + +@pytest.mark.asyncio() +@python39 +@require_confluent +async def test_lifespan_confluent(): + from docs.docs_src.getting_started.lifespan.confluent.testing import ( + test_lifespan as _test_lifespan_confluent, + ) + + await _test_lifespan_confluent() + + +@pytest.mark.asyncio() +@python39 +@require_aiokafka +async def test_lifespan_kafka(): + from docs.docs_src.getting_started.lifespan.kafka.testing import ( + test_lifespan as _test_lifespan_k, + ) + + await _test_lifespan_k() + + +@pytest.mark.asyncio() +@python39 +@require_aiopika +async def test_lifespan_rabbit(): + from docs.docs_src.getting_started.lifespan.rabbit.testing import ( + test_lifespan as _test_lifespan_r, + ) + + await _test_lifespan_r() + + +@pytest.mark.asyncio() +@python39 +@require_nats +async def test_lifespan_nats(): + from docs.docs_src.getting_started.lifespan.nats.testing import ( + test_lifespan as _test_lifespan_n, + ) + + await _test_lifespan_n() diff --git a/tests/docs/getting_started/publishing/test_broker.py b/tests/docs/getting_started/publishing/test_broker.py index 2b28895b92..794564df71 100644 --- a/tests/docs/getting_started/publishing/test_broker.py +++ b/tests/docs/getting_started/publishing/test_broker.py @@ -1,14 +1,17 @@ import pytest from faststream import TestApp -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_broker_kafka(): from docs.docs_src.getting_started.publishing.kafka.broker import ( app, @@ -16,6 +19,7 @@ async def test_broker_kafka(): handle, handle_next, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker), TestApp(app): handle.mock.assert_called_once_with("") @@ -23,6 +27,7 @@ async def test_broker_kafka(): @pytest.mark.asyncio() +@require_confluent async def test_broker_confluent(): from docs.docs_src.getting_started.publishing.confluent.broker import ( app, @@ -30,6 +35,7 @@ async def test_broker_confluent(): handle, handle_next, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker), TestApp(app): handle.mock.assert_called_once_with("") @@ -37,6 +43,7 @@ async def test_broker_confluent(): @pytest.mark.asyncio() +@require_aiopika async def test_broker_rabbit(): from docs.docs_src.getting_started.publishing.rabbit.broker import ( app, @@ -44,6 +51,7 @@ async def test_broker_rabbit(): handle, handle_next, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker), TestApp(app): handle.mock.assert_called_once_with("") @@ -51,6 +59,7 @@ async def test_broker_rabbit(): @pytest.mark.asyncio() +@require_nats async def test_broker_nats(): from docs.docs_src.getting_started.publishing.nats.broker import ( app, @@ -58,6 +67,7 @@ async def test_broker_nats(): handle, handle_next, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker), TestApp(app): handle.mock.assert_called_once_with("") @@ -65,6 +75,7 @@ async def test_broker_nats(): @pytest.mark.asyncio() +@require_redis async def test_broker_redis(): from docs.docs_src.getting_started.publishing.redis.broker import ( app, @@ -72,6 +83,7 @@ async def test_broker_redis(): handle, handle_next, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker), TestApp(app): handle.mock.assert_called_once_with("") diff --git a/tests/docs/getting_started/publishing/test_broker_context.py b/tests/docs/getting_started/publishing/test_broker_context.py index 60deb460e9..aa8d0f194b 100644 --- a/tests/docs/getting_started/publishing/test_broker_context.py +++ b/tests/docs/getting_started/publishing/test_broker_context.py @@ -1,21 +1,25 @@ import pytest from faststream import TestApp -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() @pytest.mark.kafka() +@require_aiokafka async def test_broker_context_kafka(): from docs.docs_src.getting_started.publishing.kafka.broker_context import ( app, broker, handle, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker, with_real=True), TestApp(app): await handle.wait_call(3) @@ -24,12 +28,14 @@ async def test_broker_context_kafka(): @pytest.mark.asyncio() @pytest.mark.confluent() +@require_confluent async def test_broker_context_confluent(): from docs.docs_src.getting_started.publishing.confluent.broker_context import ( app, broker, handle, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker, with_real=True), TestApp(app): await handle.wait_call(5) @@ -38,12 +44,14 @@ async def test_broker_context_confluent(): @pytest.mark.asyncio() @pytest.mark.nats() +@require_nats async def test_broker_context_nats(): from docs.docs_src.getting_started.publishing.nats.broker_context import ( app, broker, handle, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker, with_real=True), TestApp(app): await handle.wait_call(3) @@ -52,12 +60,14 @@ async def test_broker_context_nats(): @pytest.mark.asyncio() @pytest.mark.rabbit() +@require_aiopika async def test_broker_context_rabbit(): from docs.docs_src.getting_started.publishing.rabbit.broker_context import ( app, broker, handle, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker, with_real=True), TestApp(app): await handle.wait_call(3) @@ -66,12 +76,14 @@ async def test_broker_context_rabbit(): @pytest.mark.asyncio() @pytest.mark.redis() +@require_redis async def test_broker_context_redis(): from docs.docs_src.getting_started.publishing.redis.broker_context import ( app, broker, handle, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker, with_real=True), TestApp(app): await handle.wait_call(3) diff --git a/tests/docs/getting_started/publishing/test_decorator.py b/tests/docs/getting_started/publishing/test_decorator.py index e4ad61195c..66e860e234 100644 --- a/tests/docs/getting_started/publishing/test_decorator.py +++ b/tests/docs/getting_started/publishing/test_decorator.py @@ -1,14 +1,17 @@ import pytest from faststream import TestApp -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_decorator_kafka(): from docs.docs_src.getting_started.publishing.kafka.decorator import ( app, @@ -16,6 +19,7 @@ async def test_decorator_kafka(): handle, handle_next, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker), TestApp(app): handle.mock.assert_called_once_with("") @@ -24,6 +28,7 @@ async def test_decorator_kafka(): @pytest.mark.asyncio() +@require_confluent async def test_decorator_confluent(): from docs.docs_src.getting_started.publishing.confluent.decorator import ( app, @@ -31,6 +36,7 @@ async def test_decorator_confluent(): handle, handle_next, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker), TestApp(app): handle.mock.assert_called_once_with("") @@ -39,6 +45,7 @@ async def test_decorator_confluent(): @pytest.mark.asyncio() +@require_aiopika async def test_decorator_rabbit(): from docs.docs_src.getting_started.publishing.rabbit.decorator import ( app, @@ -46,6 +53,7 @@ async def test_decorator_rabbit(): handle, handle_next, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker), TestApp(app): handle.mock.assert_called_once_with("") @@ -54,6 +62,7 @@ async def test_decorator_rabbit(): @pytest.mark.asyncio() +@require_nats async def test_decorator_nats(): from docs.docs_src.getting_started.publishing.nats.decorator import ( app, @@ -61,6 +70,7 @@ async def test_decorator_nats(): handle, handle_next, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker), TestApp(app): handle.mock.assert_called_once_with("") @@ -69,6 +79,7 @@ async def test_decorator_nats(): @pytest.mark.asyncio() +@require_redis async def test_decorator_redis(): from docs.docs_src.getting_started.publishing.redis.decorator import ( app, @@ -76,6 +87,7 @@ async def test_decorator_redis(): handle, handle_next, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker), TestApp(app): handle.mock.assert_called_once_with("") diff --git a/tests/docs/getting_started/publishing/test_direct.py b/tests/docs/getting_started/publishing/test_direct.py index 535a52c1c8..1bbb8e142a 100644 --- a/tests/docs/getting_started/publishing/test_direct.py +++ b/tests/docs/getting_started/publishing/test_direct.py @@ -1,23 +1,59 @@ -from docs.docs_src.getting_started.publishing.confluent.direct_testing import ( - test_handle as test_handle_confluent, -) -from docs.docs_src.getting_started.publishing.kafka.direct_testing import ( - test_handle as test_handle_k, -) -from docs.docs_src.getting_started.publishing.nats.direct_testing import ( - test_handle as test_handle_n, -) -from docs.docs_src.getting_started.publishing.rabbit.direct_testing import ( - test_handle as test_handle_r, -) -from docs.docs_src.getting_started.publishing.redis.direct_testing import ( - test_handle as test_handle_red, -) +import pytest -__all__ = ( - "test_handle_r", - "test_handle_k", - "test_handle_n", - "test_handle_red", - "test_handle_confluent", +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, ) + + +@pytest.mark.asyncio() +@require_aiokafka +async def test_handle_kafka(): + from docs.docs_src.getting_started.publishing.kafka.direct_testing import ( + test_handle as test_handle_k, + ) + + await test_handle_k() + + +@pytest.mark.asyncio() +@require_confluent +async def test_handle_confluent(): + from docs.docs_src.getting_started.publishing.confluent.direct_testing import ( + test_handle as test_handle_confluent, + ) + + await test_handle_confluent() + + +@pytest.mark.asyncio() +@require_aiopika +async def test_handle_rabbit(): + from docs.docs_src.getting_started.publishing.rabbit.direct_testing import ( + test_handle as test_handle_r, + ) + + await test_handle_r() + + +@pytest.mark.asyncio() +@require_nats +async def test_handle_nats(): + from docs.docs_src.getting_started.publishing.nats.direct_testing import ( + test_handle as test_handle_n, + ) + + await test_handle_n() + + +@pytest.mark.asyncio() +@require_redis +async def test_handle_redis(): + from docs.docs_src.getting_started.publishing.redis.direct_testing import ( + test_handle as test_handle_red, + ) + + await test_handle_red() diff --git a/tests/docs/getting_started/publishing/test_object.py b/tests/docs/getting_started/publishing/test_object.py index d2bdc6b07c..d270cac965 100644 --- a/tests/docs/getting_started/publishing/test_object.py +++ b/tests/docs/getting_started/publishing/test_object.py @@ -1,23 +1,59 @@ -from docs.docs_src.getting_started.publishing.confluent.object_testing import ( - test_handle as test_handle_confluent, -) -from docs.docs_src.getting_started.publishing.kafka.object_testing import ( - test_handle as test_handle_k, -) -from docs.docs_src.getting_started.publishing.nats.object_testing import ( - test_handle as test_handle_n, -) -from docs.docs_src.getting_started.publishing.rabbit.object_testing import ( - test_handle as test_handle_r, -) -from docs.docs_src.getting_started.publishing.redis.object_testing import ( - test_handle as test_handle_red, -) +import pytest -__all__ = ( - "test_handle_k", - "test_handle_r", - "test_handle_n", - "test_handle_red", - "test_handle_confluent", +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, ) + + +@pytest.mark.asyncio() +@require_aiokafka +async def test_handle_kafka(): + from docs.docs_src.getting_started.publishing.kafka.object_testing import ( + test_handle as test_handle_k, + ) + + await test_handle_k() + + +@pytest.mark.asyncio() +@require_confluent +async def test_handle_confluent(): + from docs.docs_src.getting_started.publishing.confluent.object_testing import ( + test_handle as test_handle_confluent, + ) + + await test_handle_confluent() + + +@pytest.mark.asyncio() +@require_aiopika +async def test_handle_rabbit(): + from docs.docs_src.getting_started.publishing.rabbit.object_testing import ( + test_handle as test_handle_r, + ) + + await test_handle_r() + + +@pytest.mark.asyncio() +@require_nats +async def test_handle_nats(): + from docs.docs_src.getting_started.publishing.nats.object_testing import ( + test_handle as test_handle_n, + ) + + await test_handle_n() + + +@pytest.mark.asyncio() +@require_redis +async def test_handle_redis(): + from docs.docs_src.getting_started.publishing.redis.object_testing import ( + test_handle as test_handle_red, + ) + + await test_handle_red() diff --git a/tests/docs/getting_started/routers/test_base.py b/tests/docs/getting_started/routers/test_base.py index e23e788d0e..24004e71e0 100644 --- a/tests/docs/getting_started/routers/test_base.py +++ b/tests/docs/getting_started/routers/test_base.py @@ -1,14 +1,17 @@ import pytest from faststream import TestApp -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_base_router_kafka(): from docs.docs_src.getting_started.routers.kafka.router import ( app, @@ -16,6 +19,7 @@ async def test_base_router_kafka(): handle, handle_response, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker), TestApp(app): handle.mock.assert_called_once_with({"name": "John", "user_id": 1}) @@ -23,6 +27,7 @@ async def test_base_router_kafka(): @pytest.mark.asyncio() +@require_confluent async def test_base_router_confluent(): from docs.docs_src.getting_started.routers.confluent.router import ( app, @@ -30,6 +35,7 @@ async def test_base_router_confluent(): handle, handle_response, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker), TestApp(app): handle.mock.assert_called_once_with({"name": "John", "user_id": 1}) @@ -37,6 +43,7 @@ async def test_base_router_confluent(): @pytest.mark.asyncio() +@require_aiopika async def test_base_router_rabbit(): from docs.docs_src.getting_started.routers.rabbit.router import ( app, @@ -44,6 +51,7 @@ async def test_base_router_rabbit(): handle, handle_response, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker), TestApp(app): handle.mock.assert_called_once_with({"name": "John", "user_id": 1}) @@ -51,6 +59,7 @@ async def test_base_router_rabbit(): @pytest.mark.asyncio() +@require_nats async def test_base_router_nats(): from docs.docs_src.getting_started.routers.nats.router import ( app, @@ -58,6 +67,7 @@ async def test_base_router_nats(): handle, handle_response, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker), TestApp(app): handle.mock.assert_called_once_with({"name": "John", "user_id": 1}) @@ -65,6 +75,7 @@ async def test_base_router_nats(): @pytest.mark.asyncio() +@require_redis async def test_base_router_redis(): from docs.docs_src.getting_started.routers.redis.router import ( app, @@ -72,6 +83,7 @@ async def test_base_router_redis(): handle, handle_response, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker), TestApp(app): handle.mock.assert_called_once_with({"name": "John", "user_id": 1}) diff --git a/tests/docs/getting_started/routers/test_delay.py b/tests/docs/getting_started/routers/test_delay.py index 171aaaef4c..a951584837 100644 --- a/tests/docs/getting_started/routers/test_delay.py +++ b/tests/docs/getting_started/routers/test_delay.py @@ -1,19 +1,23 @@ import pytest from faststream import TestApp -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_delay_router_kafka(): from docs.docs_src.getting_started.routers.kafka.router_delay import ( app, broker, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker) as br, TestApp(app): next(iter(br._subscribers.values())).calls[ @@ -24,11 +28,13 @@ async def test_delay_router_kafka(): @pytest.mark.asyncio() +@require_confluent async def test_delay_router_confluent(): from docs.docs_src.getting_started.routers.confluent.router_delay import ( app, broker, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker) as br, TestApp(app): next(iter(br._subscribers.values())).calls[ @@ -39,11 +45,13 @@ async def test_delay_router_confluent(): @pytest.mark.asyncio() +@require_aiopika async def test_delay_router_rabbit(): from docs.docs_src.getting_started.routers.rabbit.router_delay import ( app, broker, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker) as br, TestApp(app): next(iter(br._subscribers.values())).calls[ @@ -54,11 +62,13 @@ async def test_delay_router_rabbit(): @pytest.mark.asyncio() +@require_nats async def test_delay_router_nats(): from docs.docs_src.getting_started.routers.nats.router_delay import ( app, broker, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker) as br, TestApp(app): next(iter(br._subscribers.values())).calls[ @@ -69,11 +79,13 @@ async def test_delay_router_nats(): @pytest.mark.asyncio() +@require_redis async def test_delay_router_redis(): from docs.docs_src.getting_started.routers.redis.router_delay import ( app, broker, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker) as br, TestApp(app): next(iter(br._subscribers.values())).calls[ diff --git a/tests/docs/getting_started/routers/test_delay_equal.py b/tests/docs/getting_started/routers/test_delay_equal.py index b0337d5cce..9f68b9edc5 100644 --- a/tests/docs/getting_started/routers/test_delay_equal.py +++ b/tests/docs/getting_started/routers/test_delay_equal.py @@ -1,14 +1,17 @@ import pytest from faststream import TestApp -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_delay_router_kafka(): from docs.docs_src.getting_started.routers.kafka.delay_equal import ( app, @@ -17,6 +20,7 @@ async def test_delay_router_kafka(): from docs.docs_src.getting_started.routers.kafka.router_delay import ( broker as control_broker, ) + from faststream.kafka import TestKafkaBroker assert broker._subscribers.keys() == control_broker._subscribers.keys() assert broker._publishers.keys() == control_broker._publishers.keys() @@ -30,6 +34,7 @@ async def test_delay_router_kafka(): @pytest.mark.asyncio() +@require_confluent async def test_delay_router_confluent(): from docs.docs_src.getting_started.routers.confluent.delay_equal import ( app, @@ -38,6 +43,7 @@ async def test_delay_router_confluent(): from docs.docs_src.getting_started.routers.confluent.router_delay import ( broker as control_broker, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker assert broker._subscribers.keys() == control_broker._subscribers.keys() assert broker._publishers.keys() == control_broker._publishers.keys() @@ -51,6 +57,7 @@ async def test_delay_router_confluent(): @pytest.mark.asyncio() +@require_aiopika async def test_delay_router_rabbit(): from docs.docs_src.getting_started.routers.rabbit.delay_equal import ( app, @@ -59,6 +66,7 @@ async def test_delay_router_rabbit(): from docs.docs_src.getting_started.routers.rabbit.router_delay import ( broker as control_broker, ) + from faststream.rabbit import TestRabbitBroker assert broker._subscribers.keys() == control_broker._subscribers.keys() assert broker._publishers.keys() == control_broker._publishers.keys() @@ -72,6 +80,7 @@ async def test_delay_router_rabbit(): @pytest.mark.asyncio() +@require_nats async def test_delay_router_nats(): from docs.docs_src.getting_started.routers.nats.delay_equal import ( app, @@ -80,6 +89,7 @@ async def test_delay_router_nats(): from docs.docs_src.getting_started.routers.nats.router_delay import ( broker as control_broker, ) + from faststream.nats import TestNatsBroker assert broker._subscribers.keys() == control_broker._subscribers.keys() assert broker._publishers.keys() == control_broker._publishers.keys() @@ -93,6 +103,7 @@ async def test_delay_router_nats(): @pytest.mark.asyncio() +@require_redis async def test_delay_router_redis(): from docs.docs_src.getting_started.routers.redis.delay_equal import ( app, @@ -101,6 +112,7 @@ async def test_delay_router_redis(): from docs.docs_src.getting_started.routers.redis.router_delay import ( broker as control_broker, ) + from faststream.redis import TestRedisBroker assert broker._subscribers.keys() == control_broker._subscribers.keys() assert broker._publishers.keys() == control_broker._publishers.keys() diff --git a/tests/docs/getting_started/serialization/test_parser.py b/tests/docs/getting_started/serialization/test_parser.py index 371558cc41..5285d7ee9b 100644 --- a/tests/docs/getting_started/serialization/test_parser.py +++ b/tests/docs/getting_started/serialization/test_parser.py @@ -1,68 +1,80 @@ import pytest from faststream import TestApp -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_nats async def test_parser_nats(): from docs.docs_src.getting_started.serialization.parser_nats import ( app, broker, handle, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker), TestApp(app): handle.mock.assert_called_once_with("") @pytest.mark.asyncio() +@require_aiokafka async def test_parser_kafka(): from docs.docs_src.getting_started.serialization.parser_kafka import ( app, broker, handle, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker), TestApp(app): handle.mock.assert_called_once_with("") @pytest.mark.asyncio() +@require_confluent async def test_parser_confluent(): from docs.docs_src.getting_started.serialization.parser_confluent import ( app, broker, handle, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker), TestApp(app): handle.mock.assert_called_once_with("") @pytest.mark.asyncio() +@require_aiopika async def test_parser_rabbit(): from docs.docs_src.getting_started.serialization.parser_rabbit import ( app, broker, handle, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker), TestApp(app): handle.mock.assert_called_once_with("") @pytest.mark.asyncio() +@require_redis async def test_parser_redis(): from docs.docs_src.getting_started.serialization.parser_redis import ( app, broker, handle, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker), TestApp(app): handle.mock.assert_called_once_with("") diff --git a/tests/docs/getting_started/subscription/test_annotated.py b/tests/docs/getting_started/subscription/test_annotated.py index 0511ccf3b3..21c1ed758d 100644 --- a/tests/docs/getting_started/subscription/test_annotated.py +++ b/tests/docs/getting_started/subscription/test_annotated.py @@ -1,96 +1,98 @@ import pytest from pydantic import ValidationError -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker -from tests.marks import python39 +from tests.marks import ( + python39, + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() @python39 class BaseCase: async def test_handle(self, setup): - broker, handle = setup + broker, handle, test_class = setup - async with self.test_class(broker) as br: + async with test_class(broker) as br: await br.publish({"name": "John", "user_id": 1}, "test") handle.mock.assert_called_once_with({"name": "John", "user_id": 1}) assert handle.mock is None async def test_validation_error(self, setup): - broker, handle = setup + broker, handle, test_class = setup - async with self.test_class(broker) as br: + async with test_class(broker) as br: with pytest.raises(ValidationError): await br.publish("wrong message", "test") handle.mock.assert_called_once_with("wrong message") +@require_aiokafka class TestKafka(BaseCase): - test_class = TestKafkaBroker - @pytest.fixture(scope="class") def setup(self): from docs.docs_src.getting_started.subscription.kafka.pydantic_annotated_fields import ( broker, handle, ) + from faststream.kafka import TestKafkaBroker - return (broker, handle) + return (broker, handle, TestKafkaBroker) +@require_confluent class TestConfluent(BaseCase): - test_class = TestConfluentKafkaBroker - @pytest.fixture(scope="class") def setup(self): from docs.docs_src.getting_started.subscription.confluent.pydantic_annotated_fields import ( broker, handle, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker - return (broker, handle) + return (broker, handle, TestConfluentKafkaBroker) +@require_aiopika class TestRabbit(BaseCase): - test_class = TestRabbitBroker - @pytest.fixture(scope="class") def setup(self): from docs.docs_src.getting_started.subscription.rabbit.pydantic_annotated_fields import ( broker, handle, ) + from faststream.rabbit import TestRabbitBroker - return (broker, handle) + return (broker, handle, TestRabbitBroker) +@require_nats class TestNats(BaseCase): - test_class = TestNatsBroker - @pytest.fixture(scope="class") def setup(self): from docs.docs_src.getting_started.subscription.nats.pydantic_annotated_fields import ( broker, handle, ) + from faststream.nats import TestNatsBroker - return (broker, handle) + return (broker, handle, TestNatsBroker) +@require_redis class TestRedis(BaseCase): - test_class = TestRedisBroker - @pytest.fixture(scope="class") def setup(self): from docs.docs_src.getting_started.subscription.redis.pydantic_annotated_fields import ( broker, handle, ) + from faststream.redis import TestRedisBroker - return (broker, handle) + return (broker, handle, TestRedisBroker) diff --git a/tests/docs/getting_started/subscription/test_filter.py b/tests/docs/getting_started/subscription/test_filter.py index d7c5475af4..1cd9588f55 100644 --- a/tests/docs/getting_started/subscription/test_filter.py +++ b/tests/docs/getting_started/subscription/test_filter.py @@ -1,14 +1,17 @@ import pytest from faststream import TestApp -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_kafka_filtering(): from docs.docs_src.getting_started.subscription.kafka.filter import ( app, @@ -16,6 +19,7 @@ async def test_kafka_filtering(): default_handler, handle, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker), TestApp(app): handle.mock.assert_called_once_with({"name": "John", "user_id": 1}) @@ -23,6 +27,7 @@ async def test_kafka_filtering(): @pytest.mark.asyncio() +@require_confluent async def test_confluent_filtering(): from docs.docs_src.getting_started.subscription.confluent.filter import ( app, @@ -30,6 +35,7 @@ async def test_confluent_filtering(): default_handler, handle, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker), TestApp(app): handle.mock.assert_called_once_with({"name": "John", "user_id": 1}) @@ -37,6 +43,7 @@ async def test_confluent_filtering(): @pytest.mark.asyncio() +@require_aiopika async def test_rabbit_filtering(): from docs.docs_src.getting_started.subscription.rabbit.filter import ( app, @@ -44,6 +51,7 @@ async def test_rabbit_filtering(): default_handler, handle, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker), TestApp(app): handle.mock.assert_called_once_with({"name": "John", "user_id": 1}) @@ -51,6 +59,7 @@ async def test_rabbit_filtering(): @pytest.mark.asyncio() +@require_nats async def test_nats_filtering(): from docs.docs_src.getting_started.subscription.nats.filter import ( app, @@ -58,6 +67,7 @@ async def test_nats_filtering(): default_handler, handle, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker), TestApp(app): handle.mock.assert_called_once_with({"name": "John", "user_id": 1}) @@ -65,6 +75,7 @@ async def test_nats_filtering(): @pytest.mark.asyncio() +@require_redis async def test_redis_filtering(): from docs.docs_src.getting_started.subscription.redis.filter import ( app, @@ -72,6 +83,7 @@ async def test_redis_filtering(): default_handler, handle, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker), TestApp(app): handle.mock.assert_called_once_with({"name": "John", "user_id": 1}) diff --git a/tests/docs/getting_started/subscription/test_pydantic.py b/tests/docs/getting_started/subscription/test_pydantic.py index c17a4bc4c4..51344f3ffe 100644 --- a/tests/docs/getting_started/subscription/test_pydantic.py +++ b/tests/docs/getting_started/subscription/test_pydantic.py @@ -1,18 +1,22 @@ import pytest -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiopika async def test_pydantic_model_rabbit(): from docs.docs_src.getting_started.subscription.rabbit.pydantic_model import ( broker, handle, ) + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker) as br: await br.publish({"name": "John", "user_id": 1}, "test-queue") @@ -20,11 +24,13 @@ async def test_pydantic_model_rabbit(): @pytest.mark.asyncio() +@require_aiokafka async def test_pydantic_model_kafka(): from docs.docs_src.getting_started.subscription.kafka.pydantic_model import ( broker, handle, ) + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker) as br: await br.publish({"name": "John", "user_id": 1}, "test-topic") @@ -32,11 +38,13 @@ async def test_pydantic_model_kafka(): @pytest.mark.asyncio() +@require_confluent async def test_pydantic_model_confluent(): from docs.docs_src.getting_started.subscription.confluent.pydantic_model import ( broker, handle, ) + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker) as br: await br.publish({"name": "John", "user_id": 1}, "test-topic") @@ -44,11 +52,13 @@ async def test_pydantic_model_confluent(): @pytest.mark.asyncio() +@require_nats async def test_pydantic_model_nats(): from docs.docs_src.getting_started.subscription.nats.pydantic_model import ( broker, handle, ) + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker) as br: await br.publish({"name": "John", "user_id": 1}, "test-subject") @@ -56,11 +66,13 @@ async def test_pydantic_model_nats(): @pytest.mark.asyncio() +@require_redis async def test_pydantic_model_redis(): from docs.docs_src.getting_started.subscription.redis.pydantic_model import ( broker, handle, ) + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker) as br: await br.publish({"name": "John", "user_id": 1}, "test-channel") diff --git a/tests/docs/getting_started/subscription/test_real.py b/tests/docs/getting_started/subscription/test_real.py index 415bde53c2..74a93869f7 100644 --- a/tests/docs/getting_started/subscription/test_real.py +++ b/tests/docs/getting_started/subscription/test_real.py @@ -1,47 +1,119 @@ import pytest -from docs.docs_src.getting_started.subscription.confluent.real_testing import ( - test_handle as test_handle_confluent, -) -from docs.docs_src.getting_started.subscription.confluent.real_testing import ( - test_validation_error as test_validation_error_confluent, -) -from docs.docs_src.getting_started.subscription.kafka.real_testing import ( - test_handle as test_handle_k, -) -from docs.docs_src.getting_started.subscription.kafka.real_testing import ( - test_validation_error as test_validation_error_k, -) -from docs.docs_src.getting_started.subscription.nats.real_testing import ( - test_handle as test_handle_n, -) -from docs.docs_src.getting_started.subscription.nats.real_testing import ( - test_validation_error as test_validation_error_n, -) -from docs.docs_src.getting_started.subscription.rabbit.real_testing import ( - test_handle as test_handle_r, -) -from docs.docs_src.getting_started.subscription.rabbit.real_testing import ( - test_validation_error as test_validation_error_r, -) -from docs.docs_src.getting_started.subscription.redis.real_testing import ( - test_handle as test_handle_red, -) -from docs.docs_src.getting_started.subscription.redis.real_testing import ( - test_validation_error as test_validation_error_red, +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, ) -pytest.mark.kafka(test_handle_k) -pytest.mark.kafka(test_validation_error_k) -pytest.mark.confluent(test_handle_confluent) -pytest.mark.confluent(test_validation_error_confluent) +@pytest.mark.kafka() +@pytest.mark.asyncio() +@require_aiokafka +async def test_handle_kafka(): + from docs.docs_src.getting_started.subscription.kafka.real_testing import ( + test_handle as test_handle_k, + ) + + await test_handle_k() + + +@pytest.mark.kafka() +@pytest.mark.asyncio() +@require_aiokafka +async def test_validate_kafka(): + from docs.docs_src.getting_started.subscription.kafka.real_testing import ( + test_validation_error as test_validation_error_k, + ) + + await test_validation_error_k() + + +@pytest.mark.confluent() +@pytest.mark.asyncio() +@require_confluent +async def test_handle_confluent(): + from docs.docs_src.getting_started.subscription.confluent.real_testing import ( + test_handle as test_handle_confluent, + ) + + await test_handle_confluent() + + +@pytest.mark.asyncio() +@pytest.mark.confluent() +@require_confluent +async def test_validate_confluent(): + from docs.docs_src.getting_started.subscription.confluent.real_testing import ( + test_validation_error as test_validation_error_confluent, + ) + + await test_validation_error_confluent() + + +@pytest.mark.asyncio() +@pytest.mark.rabbit() +@require_aiopika +async def test_handle_rabbit(): + from docs.docs_src.getting_started.subscription.rabbit.real_testing import ( + test_handle as test_handle_r, + ) + + await test_handle_r() + + +@pytest.mark.asyncio() +@pytest.mark.rabbit() +@require_aiopika +async def test_validate_rabbit(): + from docs.docs_src.getting_started.subscription.rabbit.real_testing import ( + test_validation_error as test_validation_error_r, + ) + + await test_validation_error_r() + + +@pytest.mark.asyncio() +@pytest.mark.nats() +@require_nats +async def test_handle_nats(): + from docs.docs_src.getting_started.subscription.nats.real_testing import ( + test_handle as test_handle_n, + ) + + await test_handle_n() + + +@pytest.mark.asyncio() +@pytest.mark.nats() +@require_nats +async def test_validate_nats(): + from docs.docs_src.getting_started.subscription.nats.real_testing import ( + test_validation_error as test_validation_error_n, + ) + + await test_validation_error_n() + + +@pytest.mark.asyncio() +@pytest.mark.redis() +@require_redis +async def test_handle_redis(): + from docs.docs_src.getting_started.subscription.redis.real_testing import ( + test_handle as test_handle_red, + ) + + await test_handle_red() -pytest.mark.rabbit(test_handle_r) -pytest.mark.rabbit(test_validation_error_r) -pytest.mark.nats(test_handle_n) -pytest.mark.nats(test_validation_error_n) +@pytest.mark.asyncio() +@pytest.mark.redis() +@require_redis +async def test_validate_redis(): + from docs.docs_src.getting_started.subscription.redis.real_testing import ( + test_validation_error as test_validation_error_red, + ) -pytest.mark.redis(test_handle_red) -pytest.mark.redis(test_validation_error_red) + await test_validation_error_red() diff --git a/tests/docs/getting_started/subscription/test_testing.py b/tests/docs/getting_started/subscription/test_testing.py index a1f87099af..5f46d5561e 100644 --- a/tests/docs/getting_started/subscription/test_testing.py +++ b/tests/docs/getting_started/subscription/test_testing.py @@ -1,43 +1,119 @@ -from docs.docs_src.getting_started.subscription.confluent.testing import ( - test_handle as test_handle_confluent, -) -from docs.docs_src.getting_started.subscription.confluent.testing import ( - test_validation_error as test_validation_error_confluent, -) -from docs.docs_src.getting_started.subscription.kafka.testing import ( - test_handle as test_handle_k, -) -from docs.docs_src.getting_started.subscription.kafka.testing import ( - test_validation_error as test_validation_error_k, -) -from docs.docs_src.getting_started.subscription.nats.testing import ( - test_handle as test_handle_n, -) -from docs.docs_src.getting_started.subscription.nats.testing import ( - test_validation_error as test_validation_error_n, -) -from docs.docs_src.getting_started.subscription.rabbit.testing import ( - test_handle as test_handle_r, -) -from docs.docs_src.getting_started.subscription.rabbit.testing import ( - test_validation_error as test_validation_error_r, -) -from docs.docs_src.getting_started.subscription.redis.testing import ( - test_handle as test_handle_rd, -) -from docs.docs_src.getting_started.subscription.redis.testing import ( - test_validation_error as test_validation_error_rd, -) +import pytest -__all__ = ( - "test_handle_r", - "test_validation_error_r", - "test_handle_rd", - "test_validation_error_rd", - "test_handle_k", - "test_validation_error_k", - "test_handle_confluent", - "test_validation_error_confluent", - "test_handle_n", - "test_validation_error_n", +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, ) + + +@pytest.mark.kafka() +@pytest.mark.asyncio() +@require_aiokafka +async def test_handle_kafka(): + from docs.docs_src.getting_started.subscription.kafka.testing import ( + test_handle as test_handle_k, + ) + + await test_handle_k() + + +@pytest.mark.kafka() +@pytest.mark.asyncio() +@require_aiokafka +async def test_validate_kafka(): + from docs.docs_src.getting_started.subscription.kafka.testing import ( + test_validation_error as test_validation_error_k, + ) + + await test_validation_error_k() + + +@pytest.mark.confluent() +@pytest.mark.asyncio() +@require_confluent +async def test_handle_confluent(): + from docs.docs_src.getting_started.subscription.confluent.testing import ( + test_handle as test_handle_confluent, + ) + + await test_handle_confluent() + + +@pytest.mark.asyncio() +@pytest.mark.confluent() +@require_confluent +async def test_validate_confluent(): + from docs.docs_src.getting_started.subscription.confluent.testing import ( + test_validation_error as test_validation_error_confluent, + ) + + await test_validation_error_confluent() + + +@pytest.mark.asyncio() +@pytest.mark.rabbit() +@require_aiopika +async def test_handle_rabbit(): + from docs.docs_src.getting_started.subscription.rabbit.testing import ( + test_handle as test_handle_r, + ) + + await test_handle_r() + + +@pytest.mark.asyncio() +@pytest.mark.rabbit() +@require_aiopika +async def test_validate_rabbit(): + from docs.docs_src.getting_started.subscription.rabbit.testing import ( + test_validation_error as test_validation_error_r, + ) + + await test_validation_error_r() + + +@pytest.mark.asyncio() +@pytest.mark.nats() +@require_nats +async def test_handle_nats(): + from docs.docs_src.getting_started.subscription.nats.testing import ( + test_handle as test_handle_n, + ) + + await test_handle_n() + + +@pytest.mark.asyncio() +@pytest.mark.nats() +@require_nats +async def test_validate_nats(): + from docs.docs_src.getting_started.subscription.nats.testing import ( + test_validation_error as test_validation_error_n, + ) + + await test_validation_error_n() + + +@pytest.mark.asyncio() +@pytest.mark.redis() +@require_redis +async def test_handle_redis(): + from docs.docs_src.getting_started.subscription.redis.testing import ( + test_handle as test_handle_rd, + ) + + await test_handle_rd() + + +@pytest.mark.asyncio() +@pytest.mark.redis() +@require_redis +async def test_validate_redis(): + from docs.docs_src.getting_started.subscription.redis.testing import ( + test_validation_error as test_validation_error_rd, + ) + + await test_validation_error_rd() diff --git a/tests/docs/index/test_basic.py b/tests/docs/index/test_basic.py index da4d6d246e..b495a0384f 100644 --- a/tests/docs/index/test_basic.py +++ b/tests/docs/index/test_basic.py @@ -1,15 +1,19 @@ import pytest -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_index_kafka_base(): from docs.docs_src.index.kafka.basic import broker, handle_msg + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(broker) as br: await br.publish({"user": "John", "user_id": 1}, "in-topic") @@ -22,8 +26,10 @@ async def test_index_kafka_base(): @pytest.mark.asyncio() +@require_confluent async def test_index_confluent_base(): from docs.docs_src.index.confluent.basic import broker, handle_msg + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(broker) as br: await br.publish({"user": "John", "user_id": 1}, "in-topic") @@ -36,8 +42,10 @@ async def test_index_confluent_base(): @pytest.mark.asyncio() +@require_aiopika async def test_index_rabbit_base(): from docs.docs_src.index.rabbit.basic import broker, handle_msg + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(broker) as br: await br.publish({"user": "John", "user_id": 1}, "in-queue") @@ -50,8 +58,10 @@ async def test_index_rabbit_base(): @pytest.mark.asyncio() +@require_nats async def test_index_nats_base(): from docs.docs_src.index.nats.basic import broker, handle_msg + from faststream.nats import TestNatsBroker async with TestNatsBroker(broker) as br: await br.publish({"user": "John", "user_id": 1}, "in-subject") @@ -64,8 +74,10 @@ async def test_index_nats_base(): @pytest.mark.asyncio() +@require_redis async def test_index_redis_base(): from docs.docs_src.index.redis.basic import broker, handle_msg + from faststream.redis import TestRedisBroker async with TestRedisBroker(broker) as br: await br.publish({"user": "John", "user_id": 1}, "in-channel") diff --git a/tests/docs/index/test_pydantic.py b/tests/docs/index/test_pydantic.py index 7a8e2a6251..426a104d5d 100644 --- a/tests/docs/index/test_pydantic.py +++ b/tests/docs/index/test_pydantic.py @@ -1,23 +1,93 @@ -from docs.docs_src.index.confluent.test import test_correct as test_confluent_correct -from docs.docs_src.index.confluent.test import test_invalid as test_confluent_invalid -from docs.docs_src.index.kafka.test import test_correct as test_k_correct -from docs.docs_src.index.kafka.test import test_invalid as test_k_invalid -from docs.docs_src.index.nats.test import test_correct as test_n_correct -from docs.docs_src.index.nats.test import test_invalid as test_n_invalid -from docs.docs_src.index.rabbit.test import test_correct as test_r_correct -from docs.docs_src.index.rabbit.test import test_invalid as test_r_invalid -from docs.docs_src.index.redis.test import test_correct as test_red_correct -from docs.docs_src.index.redis.test import test_invalid as test_red_invalid - -__all__ = ( - "test_k_correct", - "test_k_invalid", - "test_confluent_correct", - "test_confluent_invalid", - "test_r_correct", - "test_r_invalid", - "test_n_correct", - "test_n_invalid", - "test_red_correct", - "test_red_invalid", +import pytest + +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, ) + + +@pytest.mark.asyncio() +@require_aiokafka +async def test_kafka_correct(): + from docs.docs_src.index.kafka.test import test_correct as test_k_correct + + await test_k_correct() + + +@pytest.mark.asyncio() +@require_aiokafka +async def test_kafka_invalid(): + from docs.docs_src.index.kafka.test import test_invalid as test_k_invalid + + await test_k_invalid() + + +@pytest.mark.asyncio() +@require_confluent +async def test_confluent_correct(): + from docs.docs_src.index.confluent.test import ( + test_correct as test_confluent_correct, + ) + + await test_confluent_correct() + + +@pytest.mark.asyncio() +@require_confluent +async def test_confluent_invalid(): + from docs.docs_src.index.confluent.test import ( + test_invalid as test_confluent_invalid, + ) + + await test_confluent_invalid() + + +@pytest.mark.asyncio() +@require_aiopika +async def test_rabbit_correct(): + from docs.docs_src.index.rabbit.test import test_correct as test_r_correct + + await test_r_correct() + + +@pytest.mark.asyncio() +@require_aiopika +async def test_rabbit_invalid(): + from docs.docs_src.index.rabbit.test import test_invalid as test_r_invalid + + await test_r_invalid() + + +@pytest.mark.asyncio() +@require_nats +async def test_nats_correct(): + from docs.docs_src.index.nats.test import test_correct as test_n_correct + + await test_n_correct() + + +@pytest.mark.asyncio() +@require_nats +async def test_nats_invalid(): + from docs.docs_src.index.nats.test import test_invalid as test_n_invalid + + await test_n_invalid() + + +@pytest.mark.asyncio() +@require_redis +async def test_redis_correct(): + from docs.docs_src.index.redis.test import test_correct as test_red_correct + + await test_red_correct() + + +@pytest.mark.asyncio() +@require_redis +async def test_redis_invalid(): + from docs.docs_src.index.redis.test import test_invalid as test_red_invalid + + await test_red_invalid() diff --git a/tests/docs/integration/fastapi/test_base.py b/tests/docs/integration/fastapi/test_base.py index 982cdc378e..d6871fe04a 100644 --- a/tests/docs/integration/fastapi/test_base.py +++ b/tests/docs/integration/fastapi/test_base.py @@ -1,16 +1,20 @@ import pytest from fastapi.testclient import TestClient -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_fastapi_kafka_base(): from docs.docs_src.integrations.fastapi.kafka.base import app, hello, router + from faststream.kafka import TestKafkaBroker async with TestKafkaBroker(router.broker) as br: with TestClient(app) as client: @@ -26,8 +30,10 @@ async def test_fastapi_kafka_base(): @pytest.mark.asyncio() +@require_confluent async def test_fastapi_confluent_base(): from docs.docs_src.integrations.fastapi.confluent.base import app, hello, router + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker async with TestConfluentKafkaBroker(router.broker) as br: with TestClient(app) as client: @@ -43,8 +49,10 @@ async def test_fastapi_confluent_base(): @pytest.mark.asyncio() +@require_aiopika async def test_fastapi_rabbit_base(): from docs.docs_src.integrations.fastapi.rabbit.base import app, hello, router + from faststream.rabbit import TestRabbitBroker async with TestRabbitBroker(router.broker) as br: with TestClient(app) as client: @@ -60,8 +68,10 @@ async def test_fastapi_rabbit_base(): @pytest.mark.asyncio() +@require_nats async def test_fastapi_nats_base(): from docs.docs_src.integrations.fastapi.nats.base import app, hello, router + from faststream.nats import TestNatsBroker async with TestNatsBroker(router.broker) as br: with TestClient(app) as client: @@ -77,8 +87,10 @@ async def test_fastapi_nats_base(): @pytest.mark.asyncio() +@require_redis async def test_fastapi_redis_base(): from docs.docs_src.integrations.fastapi.redis.base import app, hello, router + from faststream.redis import TestRedisBroker async with TestRedisBroker(router.broker) as br: with TestClient(app) as client: diff --git a/tests/docs/integration/fastapi/test_depends.py b/tests/docs/integration/fastapi/test_depends.py index ae160bb622..2a7b917e8a 100644 --- a/tests/docs/integration/fastapi/test_depends.py +++ b/tests/docs/integration/fastapi/test_depends.py @@ -1,16 +1,20 @@ import pytest from fastapi.testclient import TestClient -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_fastapi_kafka_depends(): from docs.docs_src.integrations.fastapi.kafka.depends import app, router + from faststream.kafka import TestKafkaBroker @router.subscriber("test") async def handler(): ... @@ -23,8 +27,10 @@ async def handler(): ... @pytest.mark.asyncio() +@require_confluent async def test_fastapi_confluent_depends(): from docs.docs_src.integrations.fastapi.confluent.depends import app, router + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker @router.subscriber("test") async def handler(): ... @@ -37,8 +43,10 @@ async def handler(): ... @pytest.mark.asyncio() +@require_aiopika async def test_fastapi_rabbit_depends(): from docs.docs_src.integrations.fastapi.rabbit.depends import app, router + from faststream.rabbit import TestRabbitBroker @router.subscriber("test") async def handler(): ... @@ -51,8 +59,10 @@ async def handler(): ... @pytest.mark.asyncio() +@require_nats async def test_fastapi_nats_depends(): from docs.docs_src.integrations.fastapi.nats.depends import app, router + from faststream.nats import TestNatsBroker @router.subscriber("test") async def handler(): ... @@ -65,8 +75,10 @@ async def handler(): ... @pytest.mark.asyncio() +@require_redis async def test_fastapi_redis_depends(): from docs.docs_src.integrations.fastapi.redis.depends import app, router + from faststream.redis import TestRedisBroker @router.subscriber("test") async def handler(): ... diff --git a/tests/docs/integration/fastapi/test_multiple.py b/tests/docs/integration/fastapi/test_multiple.py index c3252682fe..8077d7b9fa 100644 --- a/tests/docs/integration/fastapi/test_multiple.py +++ b/tests/docs/integration/fastapi/test_multiple.py @@ -1,6 +1,14 @@ 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): @@ -18,6 +26,7 @@ def test_running(self, data): @pytest.mark.kafka() +@require_aiokafka class TestKafka(BaseCase): @pytest.fixture(scope="class") def data(self): @@ -27,6 +36,7 @@ def data(self): @pytest.mark.confluent() +@require_confluent class TestConfluent(BaseCase): @pytest.fixture(scope="class") def data(self): @@ -39,6 +49,7 @@ def data(self): @pytest.mark.nats() +@require_nats class TestNats(BaseCase): @pytest.fixture(scope="class") def data(self): @@ -48,6 +59,7 @@ def data(self): @pytest.mark.rabbit() +@require_aiopika class TestRabbit(BaseCase): @pytest.fixture(scope="class") def data(self): @@ -57,6 +69,7 @@ def data(self): @pytest.mark.redis() +@require_redis class TestRedis(BaseCase): @pytest.fixture(scope="class") def data(self): diff --git a/tests/docs/integration/fastapi/test_multiple_lifespan.py b/tests/docs/integration/fastapi/test_multiple_lifespan.py index 48099a376f..72dc782c51 100644 --- a/tests/docs/integration/fastapi/test_multiple_lifespan.py +++ b/tests/docs/integration/fastapi/test_multiple_lifespan.py @@ -1,6 +1,14 @@ 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): @@ -27,6 +35,7 @@ async def handler2(): ... @pytest.mark.kafka() +@require_aiokafka class TestKafka(BaseCase): @pytest.fixture(scope="class") def data(self): @@ -40,6 +49,7 @@ def data(self): @pytest.mark.confluent() +@require_confluent class TestConfluent(BaseCase): @pytest.fixture(scope="class") def data(self): @@ -53,6 +63,7 @@ def data(self): @pytest.mark.nats() +@require_nats class TestNats(BaseCase): @pytest.fixture(scope="class") def data(self): @@ -66,6 +77,7 @@ def data(self): @pytest.mark.rabbit() +@require_aiopika class TestRabbit(BaseCase): @pytest.fixture(scope="class") def data(self): @@ -79,6 +91,7 @@ def data(self): @pytest.mark.redis() +@require_redis class TestRedis(BaseCase): @pytest.fixture(scope="class") def data(self): diff --git a/tests/docs/integration/fastapi/test_send.py b/tests/docs/integration/fastapi/test_send.py index b8f1fca7d8..f1b3dde966 100644 --- a/tests/docs/integration/fastapi/test_send.py +++ b/tests/docs/integration/fastapi/test_send.py @@ -1,16 +1,20 @@ import pytest from fastapi.testclient import TestClient -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_fastapi_kafka_send(): from docs.docs_src.integrations.fastapi.kafka.send import app, router + from faststream.kafka import TestKafkaBroker @router.subscriber("test") async def handler(): ... @@ -23,8 +27,10 @@ async def handler(): ... @pytest.mark.asyncio() +@require_confluent async def test_fastapi_confluent_send(): from docs.docs_src.integrations.fastapi.confluent.send import app, router + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker @router.subscriber("test") async def handler(): ... @@ -37,8 +43,10 @@ async def handler(): ... @pytest.mark.asyncio() +@require_aiopika async def test_fastapi_rabbit_send(): from docs.docs_src.integrations.fastapi.rabbit.send import app, router + from faststream.rabbit import TestRabbitBroker @router.subscriber("test") async def handler(): ... @@ -51,8 +59,10 @@ async def handler(): ... @pytest.mark.asyncio() +@require_nats async def test_fastapi_nats_send(): from docs.docs_src.integrations.fastapi.nats.send import app, router + from faststream.nats import TestNatsBroker @router.subscriber("test") async def handler(): ... @@ -65,8 +75,10 @@ async def handler(): ... @pytest.mark.asyncio() +@require_redis async def test_fastapi_redis_send(): from docs.docs_src.integrations.fastapi.redis.send import app, router + from faststream.redis import TestRedisBroker @router.subscriber("test") async def handler(): ... diff --git a/tests/docs/integration/fastapi/test_startup.py b/tests/docs/integration/fastapi/test_startup.py index d4e80b8851..3c68863171 100644 --- a/tests/docs/integration/fastapi/test_startup.py +++ b/tests/docs/integration/fastapi/test_startup.py @@ -1,16 +1,20 @@ import pytest from fastapi.testclient import TestClient -from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker -from faststream.kafka import TestKafkaBroker -from faststream.nats import TestNatsBroker -from faststream.rabbit import TestRabbitBroker -from faststream.redis import TestRedisBroker +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, +) @pytest.mark.asyncio() +@require_aiokafka async def test_fastapi_kafka_startup(): from docs.docs_src.integrations.fastapi.kafka.startup import app, hello, router + from faststream.kafka import TestKafkaBroker @router.subscriber("test") async def handler(): ... @@ -21,8 +25,10 @@ async def handler(): ... @pytest.mark.asyncio() +@require_confluent async def test_fastapi_confluent_startup(): from docs.docs_src.integrations.fastapi.confluent.startup import app, hello, router + from faststream.confluent import TestKafkaBroker as TestConfluentKafkaBroker @router.subscriber("test") async def handler(): ... @@ -33,8 +39,10 @@ async def handler(): ... @pytest.mark.asyncio() +@require_aiopika async def test_fastapi_rabbit_startup(): from docs.docs_src.integrations.fastapi.rabbit.startup import app, hello, router + from faststream.rabbit import TestRabbitBroker @router.subscriber("test") async def handler(): ... @@ -45,8 +53,10 @@ async def handler(): ... @pytest.mark.asyncio() +@require_nats async def test_fastapi_nats_startup(): from docs.docs_src.integrations.fastapi.nats.startup import app, hello, router + from faststream.nats import TestNatsBroker @router.subscriber("test") async def handler(): ... @@ -57,8 +67,10 @@ async def handler(): ... @pytest.mark.asyncio() +@require_redis async def test_fastapi_redis_startup(): from docs.docs_src.integrations.fastapi.redis.startup import app, hello, router + from faststream.redis import TestRedisBroker @router.subscriber("test") async def handler(): ... diff --git a/tests/docs/integration/fastapi/test_test.py b/tests/docs/integration/fastapi/test_test.py index 0d5544e8d4..992335ff77 100644 --- a/tests/docs/integration/fastapi/test_test.py +++ b/tests/docs/integration/fastapi/test_test.py @@ -1,15 +1,49 @@ -from docs.docs_src.integrations.fastapi.confluent.test import ( - test_router as test_confluent, -) -from docs.docs_src.integrations.fastapi.kafka.test import test_router as test_k -from docs.docs_src.integrations.fastapi.nats.test import test_router as test_n -from docs.docs_src.integrations.fastapi.rabbit.test import test_router as test_r -from docs.docs_src.integrations.fastapi.redis.test import test_router as test_red - -__all__ = ( - "test_k", - "test_r", - "test_n", - "test_red", - "test_confluent", +import pytest + +from tests.marks import ( + require_aiokafka, + require_aiopika, + require_confluent, + require_nats, + require_redis, ) + + +@pytest.mark.asyncio() +@require_aiokafka +async def test_kafka(): + from docs.docs_src.integrations.fastapi.kafka.test import test_router + + await test_router() + + +@pytest.mark.asyncio() +@require_confluent +async def test_confluent(): + from docs.docs_src.integrations.fastapi.confluent.test import test_router + + await test_router() + + +@pytest.mark.asyncio() +@require_aiopika +async def test_rabbit(): + from docs.docs_src.integrations.fastapi.rabbit.test import test_router + + await test_router() + + +@pytest.mark.asyncio() +@require_nats +async def test_nats(): + from docs.docs_src.integrations.fastapi.nats.test import test_router + + await test_router() + + +@pytest.mark.asyncio() +@require_redis +async def test_redis(): + from docs.docs_src.integrations.fastapi.redis.test import test_router + + await test_router() diff --git a/tests/marks.py b/tests/marks.py index 80bb1cde5c..07bde035b0 100644 --- a/tests/marks.py +++ b/tests/marks.py @@ -23,3 +23,68 @@ not PYDANTIC_V2, reason="requires PydanticV1", ) + + +try: + from faststream.confluent import KafkaBroker +except ImportError: + HAS_CONFLUENT = False +else: + HAS_CONFLUENT = True + +require_confluent = pytest.mark.skipif( + not HAS_CONFLUENT, + reason="requires confluent-kafka", +) + + +try: + from faststream.kafka import KafkaBroker # noqa: F401 +except ImportError: + HAS_AIOKAFKA = False +else: + HAS_AIOKAFKA = True + +require_aiokafka = pytest.mark.skipif( + not HAS_AIOKAFKA, + reason="requires aiokafka", +) + + +try: + from faststream.rabbit import RabbitBroker # noqa: F401 +except ImportError: + HAS_AIOPIKA = False +else: + HAS_AIOPIKA = True + +require_aiopika = pytest.mark.skipif( + not HAS_AIOPIKA, + reason="requires aio-pika", +) + + +try: + from faststream.redis import RedisBroker # noqa: F401 +except ImportError: + HAS_REDIS = False +else: + HAS_REDIS = True + +require_redis = pytest.mark.skipif( + not HAS_REDIS, + reason="requires redis", +) + + +try: + from faststream.nats import NatsBroker # noqa: F401 +except ImportError: + HAS_NATS = False +else: + HAS_NATS = True + +require_nats = pytest.mark.skipif( + not HAS_NATS, + reason="requires nats-py", +) diff --git a/tests/opentelemetry/__init__.py b/tests/opentelemetry/__init__.py index 75763c2fee..20d03f3611 100644 --- a/tests/opentelemetry/__init__.py +++ b/tests/opentelemetry/__init__.py @@ -1,3 +1,3 @@ import pytest -pytest.importorskip("opentelemetry") +pytest.importorskip("opentelemetry.sdk")