Skip to content

Commit 51d410e

Browse files
authored
Use faker pytest fixture (#65)
1 parent d06cd07 commit 51d410e

File tree

4 files changed

+49
-45
lines changed

4 files changed

+49
-45
lines changed

tests/test_config.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55

66
import stompman
77

8-
FAKER = faker.Faker()
98

10-
11-
def test_connection_parameters_from_pydantic_multihost_hosts() -> None:
9+
def test_connection_parameters_from_pydantic_multihost_hosts(faker: faker.Faker) -> None:
1210
full_host: dict[str, Any] = {
13-
"username": FAKER.pystr(),
14-
"password": FAKER.pystr(),
15-
"host": FAKER.pystr(),
16-
"port": FAKER.pyint(),
11+
"username": faker.pystr(),
12+
"password": faker.pystr(),
13+
"host": faker.pystr(),
14+
"port": faker.pyint(),
1715
}
1816
assert stompman.ConnectionParameters.from_pydantic_multihost_hosts(
1917
[{**full_host, "port": index} for index in range(5)] # type: ignore[typeddict-item]

tests/test_connection_lifespan.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@
2929
)
3030

3131
pytestmark = pytest.mark.anyio
32-
FAKER = faker.Faker()
3332

3433

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

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

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

6059

6160
@pytest.mark.usefixtures("mock_sleep")
62-
async def test_client_connection_lifespan_connection_not_confirmed(monkeypatch: pytest.MonkeyPatch) -> None:
61+
async def test_client_connection_lifespan_connection_not_confirmed(
62+
monkeypatch: pytest.MonkeyPatch, faker: faker.Faker
63+
) -> None:
6364
async def mock_wait_for(future: Coroutine[Any, Any, Any], timeout: float) -> object:
6465
assert timeout == connection_confirmation_timeout
6566
task = asyncio.create_task(future)
@@ -69,7 +70,7 @@ async def mock_wait_for(future: Coroutine[Any, Any, Any], timeout: float) -> obj
6970
original_wait_for = asyncio.wait_for
7071
monkeypatch.setattr("asyncio.wait_for", mock_wait_for)
7172
error_frame = build_dataclass(ErrorFrame)
72-
connection_confirmation_timeout = FAKER.pyint()
73+
connection_confirmation_timeout = faker.pyint()
7374

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

9091

9192
@pytest.mark.usefixtures("mock_sleep")
92-
async def test_client_connection_lifespan_unsupported_protocol_version() -> None:
93-
given_version = FAKER.pystr()
93+
async def test_client_connection_lifespan_unsupported_protocol_version(faker: faker.Faker) -> None:
94+
given_version = faker.pystr()
9495

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

108109

109-
async def test_client_connection_lifespan_disconnect_not_confirmed(monkeypatch: pytest.MonkeyPatch) -> None:
110+
async def test_client_connection_lifespan_disconnect_not_confirmed(
111+
monkeypatch: pytest.MonkeyPatch, faker: faker.Faker
112+
) -> None:
110113
wait_for_calls = []
111114

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

118121
original_wait_for = asyncio.wait_for
119122
monkeypatch.setattr("asyncio.wait_for", mock_wait_for)
120-
disconnect_confirmation_timeout = FAKER.pyint()
123+
disconnect_confirmation_timeout = faker.pyint()
121124
read_frames_yields = get_read_frames_with_lifespan([])
122125
read_frames_yields[-1].clear()
123126
connection_class, _ = create_spying_connection(*read_frames_yields)

tests/test_subscription.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@
3535
)
3636

3737
pytestmark = pytest.mark.anyio
38-
FAKER = faker.Faker()
3938

4039

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

4847
async with client:
4948
subscription = await client.subscribe(
@@ -79,13 +78,15 @@ async def test_client_subscribtions_lifespan_resubscribe(ack: AckMode) -> None:
7978
)
8079

8180

82-
async def test_client_subscribtions_lifespan_no_active_subs_in_aexit(monkeypatch: pytest.MonkeyPatch) -> None:
81+
async def test_client_subscribtions_lifespan_no_active_subs_in_aexit(
82+
monkeypatch: pytest.MonkeyPatch, faker: faker.Faker
83+
) -> None:
8384
monkeypatch.setattr(
8485
stompman.subscription,
8586
"_make_subscription_id",
86-
mock.Mock(side_effect=[(first_id := FAKER.pystr()), (second_id := FAKER.pystr())]),
87+
mock.Mock(side_effect=[(first_id := faker.pystr()), (second_id := faker.pystr())]),
8788
)
88-
first_destination, second_destination = FAKER.pystr(), FAKER.pystr()
89+
first_destination, second_destination = faker.pystr(), faker.pystr()
8990
connection_class, collected_frames = create_spying_connection(*get_read_frames_with_lifespan([]))
9091

9192
async with EnrichedClient(connection_class=connection_class) as client:
@@ -110,10 +111,11 @@ async def test_client_subscribtions_lifespan_no_active_subs_in_aexit(monkeypatch
110111
@pytest.mark.parametrize("direct_error", [True, False])
111112
async def test_client_subscribtions_lifespan_with_active_subs_in_aexit(
112113
monkeypatch: pytest.MonkeyPatch,
114+
faker: faker.Faker,
113115
*,
114116
direct_error: bool,
115117
) -> None:
116-
subscription_id, destination = FAKER.pystr(), FAKER.pystr()
118+
subscription_id, destination = faker.pystr(), faker.pystr()
117119
monkeypatch.setattr(stompman.subscription, "_make_subscription_id", mock.Mock(return_value=subscription_id))
118120
connection_class, collected_frames = create_spying_connection(*get_read_frames_with_lifespan([]))
119121

@@ -140,11 +142,11 @@ async def test_client_subscribtions_lifespan_with_active_subs_in_aexit(
140142
)
141143

142144

143-
async def test_client_listen_routing_ok(monkeypatch: pytest.MonkeyPatch) -> None:
145+
async def test_client_listen_routing_ok(monkeypatch: pytest.MonkeyPatch, faker: faker.Faker) -> None:
144146
monkeypatch.setattr(
145147
stompman.subscription,
146148
"_make_subscription_id",
147-
mock.Mock(side_effect=[(first_sub_id := FAKER.pystr()), (second_sub_id := FAKER.pystr())]),
149+
mock.Mock(side_effect=[(first_sub_id := faker.pystr()), (second_sub_id := faker.pystr())]),
148150
)
149151
connection_class, _ = create_spying_connection(
150152
*get_read_frames_with_lifespan(
@@ -168,10 +170,10 @@ async def test_client_listen_routing_ok(monkeypatch: pytest.MonkeyPatch) -> None
168170
on_heartbeat=(on_heartbeat := mock.Mock()),
169171
) as client:
170172
first_subscription = await client.subscribe(
171-
FAKER.pystr(), handler=first_message_handler, on_suppressed_exception=first_error_handler
173+
faker.pystr(), handler=first_message_handler, on_suppressed_exception=first_error_handler
172174
)
173175
second_subscription = await client.subscribe(
174-
FAKER.pystr(), handler=second_message_handler, on_suppressed_exception=second_error_handler
176+
faker.pystr(), handler=second_message_handler, on_suppressed_exception=second_error_handler
175177
)
176178
await asyncio.sleep(0)
177179
await asyncio.sleep(0)
@@ -191,9 +193,9 @@ async def test_client_listen_routing_ok(monkeypatch: pytest.MonkeyPatch) -> None
191193
@pytest.mark.parametrize("side_effect", [None, SomeError])
192194
@pytest.mark.parametrize("ack", ["client", "client-individual"])
193195
async def test_client_listen_unsubscribe_before_ack_or_nack(
194-
monkeypatch: pytest.MonkeyPatch, ack: AckMode, side_effect: object
196+
monkeypatch: pytest.MonkeyPatch, faker: faker.Faker, ack: AckMode, side_effect: object
195197
) -> None:
196-
subscription_id, destination = FAKER.pystr(), FAKER.pystr()
198+
subscription_id, destination = faker.pystr(), faker.pystr()
197199
monkeypatch.setattr(stompman.subscription, "_make_subscription_id", mock.Mock(return_value=subscription_id))
198200

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

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

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

249253

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

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

276280

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

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

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

292296
assert len(exc_info.value.exceptions) == 1

tests/test_transaction.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@
2727
from stompman.frames import SendHeaders
2828

2929
pytestmark = pytest.mark.anyio
30-
FAKER = faker.Faker()
3130

3231

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

36-
transaction_id = FAKER.pystr()
35+
transaction_id = faker.pystr()
3736
monkeypatch.setattr(stompman.transaction, "_make_transaction_id", mock.Mock(return_value=transaction_id))
3837

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

6160

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

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

7776

78-
async def test_commit_pending_transactions(monkeypatch: pytest.MonkeyPatch) -> None:
79-
body, destination = FAKER.binary(length=10), FAKER.pystr()
77+
async def test_commit_pending_transactions(monkeypatch: pytest.MonkeyPatch, faker: faker.Faker) -> None:
78+
body, destination = faker.binary(length=10), faker.pystr()
8079
monkeypatch.setattr(
8180
stompman.transaction,
8281
"_make_transaction_id",
83-
mock.Mock(side_effect=[(first_id := FAKER.pystr()), (second_id := FAKER.pystr())]),
82+
mock.Mock(side_effect=[(first_id := faker.pystr()), (second_id := faker.pystr())]),
8483
)
8584
connection_class, collected_frames = create_spying_connection(*get_read_frames_with_lifespan([CONNECTED_FRAME], []))
8685
async with EnrichedClient(connection_class=connection_class) as client:

0 commit comments

Comments
 (0)