Skip to content

Commit

Permalink
add Client unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zerlok committed Nov 30, 2024
1 parent e6de10d commit 5547033
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
55 changes: 55 additions & 0 deletions tests/unit/rpc/test_client.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 4 additions & 4 deletions tests/unit/rpc/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand All @@ -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,
)


Expand Down

0 comments on commit 5547033

Please sign in to comment.