From 55470336407161937167b2081e26ff2cd264c282 Mon Sep 17 00:00:00 2001 From: zerlok Date: Sat, 30 Nov 2024 19:33:22 +0100 Subject: [PATCH] add Client unit tests --- tests/conftest.py | 7 ++++- tests/unit/rpc/test_client.py | 55 +++++++++++++++++++++++++++++++++++ tests/unit/rpc/test_server.py | 8 ++--- 3 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 tests/unit/rpc/test_client.py diff --git a/tests/conftest.py b/tests/conftest.py index c3a3ac7..d2148d2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -17,7 +17,7 @@ from brokrpc.model import ConsumerResult from brokrpc.options import BindingOptions, BrokerOptions from brokrpc.retry import ConstantDelay, DelayRetryStrategy, ExponentialDelay, MultiplierDelay -from brokrpc.rpc.abc import UnaryUnaryHandler +from brokrpc.rpc.abc import RPCSerializer, UnaryUnaryHandler from tests.stub.driver import StubBrokerDriver, StubConsumer BROKER_IS_CONNECTED: t.Final = pytest.mark.parametrize( @@ -163,3 +163,8 @@ def stub_binding_options(stub_routing_key: str) -> BindingOptions: @pytest.fixture def mock_serializer() -> Serializer[object, BinaryMessage]: return create_autospec(Serializer) + + +@pytest.fixture +def mock_rpc_serializer() -> RPCSerializer[object, object]: + return create_autospec(RPCSerializer) diff --git a/tests/unit/rpc/test_client.py b/tests/unit/rpc/test_client.py new file mode 100644 index 0000000..6e3389a --- /dev/null +++ b/tests/unit/rpc/test_client.py @@ -0,0 +1,55 @@ +import pytest + +from brokrpc.abc import Serializer +from brokrpc.broker import Broker, BrokerIsNotConnectedError +from brokrpc.message import BinaryMessage, Message +from brokrpc.rpc.abc import CallerSerializer +from brokrpc.rpc.client import Client +from tests.conftest import BROKER_IS_CONNECTED, BROKER_IS_NOT_CONNECTED + + +@BROKER_IS_CONNECTED +async def test_publisher_ok[U]( + client: Client, + stub_routing_key: str, + mock_serializer: Serializer[Message[U], BinaryMessage], +) -> None: + async with client.publisher(routing_key=stub_routing_key, serializer=mock_serializer): + pass + + +@BROKER_IS_NOT_CONNECTED +async def test_publisher_error[U]( + client: Client, + stub_routing_key: str, + mock_serializer: Serializer[Message[U], BinaryMessage], +) -> None: + with pytest.raises(BrokerIsNotConnectedError): + async with client.publisher(routing_key=stub_routing_key, serializer=mock_serializer): + pass + + +@BROKER_IS_CONNECTED +async def test_unary_unary_caller_ok[U, V]( + client: Client, + stub_routing_key: str, + mock_rpc_serializer: CallerSerializer[U, V], +) -> None: + async with client.unary_unary_caller(routing_key=stub_routing_key, serializer=mock_rpc_serializer): + pass + + +@BROKER_IS_NOT_CONNECTED +async def test_unary_unary_caller_error[U, V]( + client: Client, + stub_routing_key: str, + mock_rpc_serializer: CallerSerializer[U, V], +) -> None: + with pytest.raises(BrokerIsNotConnectedError): + async with client.unary_unary_caller(routing_key=stub_routing_key, serializer=mock_rpc_serializer): + pass + + +@pytest.fixture +def client(stub_broker: Broker) -> Client: + return Client(stub_broker) diff --git a/tests/unit/rpc/test_server.py b/tests/unit/rpc/test_server.py index 4ea980a..ce91fe9 100644 --- a/tests/unit/rpc/test_server.py +++ b/tests/unit/rpc/test_server.py @@ -76,12 +76,12 @@ def test_register_unary_unary_ok[U, V]( server: Server, mock_unary_unary_handler: UnaryUnaryHandler[Request[U], V], stub_routing_key: str, - mock_serializer: HandlerSerializer[U, V], + mock_rpc_serializer: HandlerSerializer[U, V], ) -> None: server.register_unary_unary_handler( func=mock_unary_unary_handler, routing_key=stub_routing_key, - serializer=mock_serializer, + serializer=mock_rpc_serializer, ) @@ -91,13 +91,13 @@ def test_register_unary_unary_error[U, V]( server: Server, mock_unary_unary_handler: UnaryUnaryHandler[Request[U], V], stub_routing_key: str, - mock_serializer: HandlerSerializer[U, V], + mock_rpc_serializer: HandlerSerializer[U, V], ) -> None: with pytest.raises(ServerNotInConfigurableStateError): server.register_unary_unary_handler( func=mock_unary_unary_handler, routing_key=stub_routing_key, - serializer=mock_serializer, + serializer=mock_rpc_serializer, )