Skip to content

Commit

Permalink
Use faker pytest fixture (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
vrslev authored Aug 23, 2024
1 parent d06cd07 commit 51d410e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 45 deletions.
12 changes: 5 additions & 7 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@

import stompman

FAKER = faker.Faker()


def test_connection_parameters_from_pydantic_multihost_hosts() -> None:
def test_connection_parameters_from_pydantic_multihost_hosts(faker: faker.Faker) -> None:
full_host: dict[str, Any] = {
"username": FAKER.pystr(),
"password": FAKER.pystr(),
"host": FAKER.pystr(),
"port": FAKER.pyint(),
"username": faker.pystr(),
"password": faker.pystr(),
"host": faker.pystr(),
"port": faker.pyint(),
}
assert stompman.ConnectionParameters.from_pydantic_multihost_hosts(
[{**full_host, "port": index} for index in range(5)] # type: ignore[typeddict-item]
Expand Down
21 changes: 12 additions & 9 deletions tests/test_connection_lifespan.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@
)

pytestmark = pytest.mark.anyio
FAKER = faker.Faker()


async def test_client_connection_lifespan_ok(monkeypatch: pytest.MonkeyPatch) -> None:
async def test_client_connection_lifespan_ok(monkeypatch: pytest.MonkeyPatch, faker: faker.Faker) -> None:
connected_frame = build_dataclass(ConnectedFrame, headers={"version": Client.PROTOCOL_VERSION, "heart-beat": "1,1"})
connection_class, collected_frames = create_spying_connection(
[connected_frame], [], [(receipt_frame := build_dataclass(ReceiptFrame))]
)

disconnect_frame = DisconnectFrame(headers={"receipt": (receipt_id := FAKER.pystr())})
disconnect_frame = DisconnectFrame(headers={"receipt": (receipt_id := faker.pystr())})
monkeypatch.setattr(stompman.connection_lifespan, "_make_receipt_id", mock.Mock(return_value=receipt_id))

async with EnrichedClient(
Expand All @@ -59,7 +58,9 @@ async def test_client_connection_lifespan_ok(monkeypatch: pytest.MonkeyPatch) ->


@pytest.mark.usefixtures("mock_sleep")
async def test_client_connection_lifespan_connection_not_confirmed(monkeypatch: pytest.MonkeyPatch) -> None:
async def test_client_connection_lifespan_connection_not_confirmed(
monkeypatch: pytest.MonkeyPatch, faker: faker.Faker
) -> None:
async def mock_wait_for(future: Coroutine[Any, Any, Any], timeout: float) -> object:
assert timeout == connection_confirmation_timeout
task = asyncio.create_task(future)
Expand All @@ -69,7 +70,7 @@ async def mock_wait_for(future: Coroutine[Any, Any, Any], timeout: float) -> obj
original_wait_for = asyncio.wait_for
monkeypatch.setattr("asyncio.wait_for", mock_wait_for)
error_frame = build_dataclass(ErrorFrame)
connection_confirmation_timeout = FAKER.pyint()
connection_confirmation_timeout = faker.pyint()

class MockConnection(BaseMockConnection):
@staticmethod
Expand All @@ -89,8 +90,8 @@ async def read_frames() -> AsyncGenerator[AnyServerFrame, None]:


@pytest.mark.usefixtures("mock_sleep")
async def test_client_connection_lifespan_unsupported_protocol_version() -> None:
given_version = FAKER.pystr()
async def test_client_connection_lifespan_unsupported_protocol_version(faker: faker.Faker) -> None:
given_version = faker.pystr()

with pytest.raises(FailedAllConnectAttemptsError) as exc_info:
await EnrichedClient(
Expand All @@ -106,7 +107,9 @@ async def test_client_connection_lifespan_unsupported_protocol_version() -> None
)


async def test_client_connection_lifespan_disconnect_not_confirmed(monkeypatch: pytest.MonkeyPatch) -> None:
async def test_client_connection_lifespan_disconnect_not_confirmed(
monkeypatch: pytest.MonkeyPatch, faker: faker.Faker
) -> None:
wait_for_calls = []

async def mock_wait_for(future: Coroutine[Any, Any, Any], timeout: float) -> object:
Expand All @@ -117,7 +120,7 @@ async def mock_wait_for(future: Coroutine[Any, Any, Any], timeout: float) -> obj

original_wait_for = asyncio.wait_for
monkeypatch.setattr("asyncio.wait_for", mock_wait_for)
disconnect_confirmation_timeout = FAKER.pyint()
disconnect_confirmation_timeout = faker.pyint()
read_frames_yields = get_read_frames_with_lifespan([])
read_frames_yields[-1].clear()
connection_class, _ = create_spying_connection(*read_frames_yields)
Expand Down
44 changes: 24 additions & 20 deletions tests/test_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@
)

pytestmark = pytest.mark.anyio
FAKER = faker.Faker()


@pytest.mark.parametrize("ack", get_args(AckMode))
async def test_client_subscribtions_lifespan_resubscribe(ack: AckMode) -> None:
async def test_client_subscribtions_lifespan_resubscribe(ack: AckMode, faker: faker.Faker) -> None:
connection_class, collected_frames = create_spying_connection(*get_read_frames_with_lifespan([CONNECTED_FRAME], []))
client = EnrichedClient(connection_class=connection_class)
sub_destination, message_destination, message_body = FAKER.pystr(), FAKER.pystr(), FAKER.binary(length=10)
sub_extra_headers = FAKER.pydict(value_types=[str])
sub_destination, message_destination, message_body = faker.pystr(), faker.pystr(), faker.binary(length=10)
sub_extra_headers = faker.pydict(value_types=[str])

async with client:
subscription = await client.subscribe(
Expand Down Expand Up @@ -79,13 +78,15 @@ async def test_client_subscribtions_lifespan_resubscribe(ack: AckMode) -> None:
)


async def test_client_subscribtions_lifespan_no_active_subs_in_aexit(monkeypatch: pytest.MonkeyPatch) -> None:
async def test_client_subscribtions_lifespan_no_active_subs_in_aexit(
monkeypatch: pytest.MonkeyPatch, faker: faker.Faker
) -> None:
monkeypatch.setattr(
stompman.subscription,
"_make_subscription_id",
mock.Mock(side_effect=[(first_id := FAKER.pystr()), (second_id := FAKER.pystr())]),
mock.Mock(side_effect=[(first_id := faker.pystr()), (second_id := faker.pystr())]),
)
first_destination, second_destination = FAKER.pystr(), FAKER.pystr()
first_destination, second_destination = faker.pystr(), faker.pystr()
connection_class, collected_frames = create_spying_connection(*get_read_frames_with_lifespan([]))

async with EnrichedClient(connection_class=connection_class) as client:
Expand All @@ -110,10 +111,11 @@ async def test_client_subscribtions_lifespan_no_active_subs_in_aexit(monkeypatch
@pytest.mark.parametrize("direct_error", [True, False])
async def test_client_subscribtions_lifespan_with_active_subs_in_aexit(
monkeypatch: pytest.MonkeyPatch,
faker: faker.Faker,
*,
direct_error: bool,
) -> None:
subscription_id, destination = FAKER.pystr(), FAKER.pystr()
subscription_id, destination = faker.pystr(), faker.pystr()
monkeypatch.setattr(stompman.subscription, "_make_subscription_id", mock.Mock(return_value=subscription_id))
connection_class, collected_frames = create_spying_connection(*get_read_frames_with_lifespan([]))

Expand All @@ -140,11 +142,11 @@ async def test_client_subscribtions_lifespan_with_active_subs_in_aexit(
)


async def test_client_listen_routing_ok(monkeypatch: pytest.MonkeyPatch) -> None:
async def test_client_listen_routing_ok(monkeypatch: pytest.MonkeyPatch, faker: faker.Faker) -> None:
monkeypatch.setattr(
stompman.subscription,
"_make_subscription_id",
mock.Mock(side_effect=[(first_sub_id := FAKER.pystr()), (second_sub_id := FAKER.pystr())]),
mock.Mock(side_effect=[(first_sub_id := faker.pystr()), (second_sub_id := faker.pystr())]),
)
connection_class, _ = create_spying_connection(
*get_read_frames_with_lifespan(
Expand All @@ -168,10 +170,10 @@ async def test_client_listen_routing_ok(monkeypatch: pytest.MonkeyPatch) -> None
on_heartbeat=(on_heartbeat := mock.Mock()),
) as client:
first_subscription = await client.subscribe(
FAKER.pystr(), handler=first_message_handler, on_suppressed_exception=first_error_handler
faker.pystr(), handler=first_message_handler, on_suppressed_exception=first_error_handler
)
second_subscription = await client.subscribe(
FAKER.pystr(), handler=second_message_handler, on_suppressed_exception=second_error_handler
faker.pystr(), handler=second_message_handler, on_suppressed_exception=second_error_handler
)
await asyncio.sleep(0)
await asyncio.sleep(0)
Expand All @@ -191,9 +193,9 @@ async def test_client_listen_routing_ok(monkeypatch: pytest.MonkeyPatch) -> None
@pytest.mark.parametrize("side_effect", [None, SomeError])
@pytest.mark.parametrize("ack", ["client", "client-individual"])
async def test_client_listen_unsubscribe_before_ack_or_nack(
monkeypatch: pytest.MonkeyPatch, ack: AckMode, side_effect: object
monkeypatch: pytest.MonkeyPatch, faker: faker.Faker, ack: AckMode, side_effect: object
) -> None:
subscription_id, destination = FAKER.pystr(), FAKER.pystr()
subscription_id, destination = faker.pystr(), faker.pystr()
monkeypatch.setattr(stompman.subscription, "_make_subscription_id", mock.Mock(return_value=subscription_id))

message_frame = build_dataclass(MessageFrame, headers={"subscription": subscription_id})
Expand All @@ -218,8 +220,10 @@ async def test_client_listen_unsubscribe_before_ack_or_nack(

@pytest.mark.parametrize("ok", [True, False])
@pytest.mark.parametrize("ack", ["client", "client-individual"])
async def test_client_listen_ack_nack_sent(monkeypatch: pytest.MonkeyPatch, ack: AckMode, *, ok: bool) -> None:
subscription_id, destination, message_id = FAKER.pystr(), FAKER.pystr(), FAKER.pystr()
async def test_client_listen_ack_nack_sent(
monkeypatch: pytest.MonkeyPatch, faker: faker.Faker, ack: AckMode, *, ok: bool
) -> None:
subscription_id, destination, message_id = faker.pystr(), faker.pystr(), faker.pystr()
monkeypatch.setattr(stompman.subscription, "_make_subscription_id", mock.Mock(return_value=subscription_id))

message_frame = build_dataclass(
Expand Down Expand Up @@ -248,8 +252,8 @@ async def test_client_listen_ack_nack_sent(monkeypatch: pytest.MonkeyPatch, ack:


@pytest.mark.parametrize("ok", [True, False])
async def test_client_listen_auto_ack_nack(monkeypatch: pytest.MonkeyPatch, *, ok: bool) -> None:
subscription_id, destination, message_id = FAKER.pystr(), FAKER.pystr(), FAKER.pystr()
async def test_client_listen_auto_ack_nack(monkeypatch: pytest.MonkeyPatch, faker: faker.Faker, *, ok: bool) -> None:
subscription_id, destination, message_id = faker.pystr(), faker.pystr(), faker.pystr()
monkeypatch.setattr(stompman.subscription, "_make_subscription_id", mock.Mock(return_value=subscription_id))

message_frame = build_dataclass(
Expand All @@ -274,7 +278,7 @@ async def test_client_listen_auto_ack_nack(monkeypatch: pytest.MonkeyPatch, *, o
)


async def test_client_listen_raises_on_aexit(monkeypatch: pytest.MonkeyPatch) -> None:
async def test_client_listen_raises_on_aexit(monkeypatch: pytest.MonkeyPatch, faker: faker.Faker) -> None:
monkeypatch.setattr("asyncio.sleep", partial(asyncio.sleep, 0))

connection_class, _ = create_spying_connection(*get_read_frames_with_lifespan([]))
Expand All @@ -286,7 +290,7 @@ async def close_connection_soon(client: stompman.Client) -> None:

with pytest.raises(ExceptionGroup) as exc_info: # noqa: PT012
async with asyncio.TaskGroup() as task_group, EnrichedClient(connection_class=connection_class) as client:
await client.subscribe(FAKER.pystr(), noop_message_handler, on_suppressed_exception=noop_error_handler)
await client.subscribe(faker.pystr(), noop_message_handler, on_suppressed_exception=noop_error_handler)
task_group.create_task(close_connection_soon(client))

assert len(exc_info.value.exceptions) == 1
Expand Down
17 changes: 8 additions & 9 deletions tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@
from stompman.frames import SendHeaders

pytestmark = pytest.mark.anyio
FAKER = faker.Faker()


async def test_send_message_and_enter_transaction_ok(monkeypatch: pytest.MonkeyPatch) -> None:
body, destination, expires, content_type = FAKER.binary(), FAKER.pystr(), FAKER.pystr(), FAKER.pystr()
async def test_send_message_and_enter_transaction_ok(monkeypatch: pytest.MonkeyPatch, faker: faker.Faker) -> None:
body, destination, expires, content_type = faker.binary(length=10), faker.pystr(), faker.pystr(), faker.pystr()

transaction_id = FAKER.pystr()
transaction_id = faker.pystr()
monkeypatch.setattr(stompman.transaction, "_make_transaction_id", mock.Mock(return_value=transaction_id))

connection_class, collected_frames = create_spying_connection(*get_read_frames_with_lifespan([]))
Expand All @@ -59,8 +58,8 @@ async def test_send_message_and_enter_transaction_ok(monkeypatch: pytest.MonkeyP
)


async def test_send_message_and_enter_transaction_abort(monkeypatch: pytest.MonkeyPatch) -> None:
transaction_id = FAKER.pystr()
async def test_send_message_and_enter_transaction_abort(monkeypatch: pytest.MonkeyPatch, faker: faker.Faker) -> None:
transaction_id = faker.pystr()
monkeypatch.setattr(stompman.transaction, "_make_transaction_id", mock.Mock(return_value=transaction_id))
connection_class, collected_frames = create_spying_connection(*get_read_frames_with_lifespan([]))

Expand All @@ -75,12 +74,12 @@ async def test_send_message_and_enter_transaction_abort(monkeypatch: pytest.Monk
)


async def test_commit_pending_transactions(monkeypatch: pytest.MonkeyPatch) -> None:
body, destination = FAKER.binary(length=10), FAKER.pystr()
async def test_commit_pending_transactions(monkeypatch: pytest.MonkeyPatch, faker: faker.Faker) -> None:
body, destination = faker.binary(length=10), faker.pystr()
monkeypatch.setattr(
stompman.transaction,
"_make_transaction_id",
mock.Mock(side_effect=[(first_id := FAKER.pystr()), (second_id := FAKER.pystr())]),
mock.Mock(side_effect=[(first_id := faker.pystr()), (second_id := faker.pystr())]),
)
connection_class, collected_frames = create_spying_connection(*get_read_frames_with_lifespan([CONNECTED_FRAME], []))
async with EnrichedClient(connection_class=connection_class) as client:
Expand Down

0 comments on commit 51d410e

Please sign in to comment.