Repository navigation
Expand file tree
/
Copy pathtest_outbox_processor.py
More file actions
58 lines (48 loc) · 1.58 KB
/
Copy pathtest_outbox_processor.py
File metadata and controls
58 lines (48 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pytest
from sqlalchemy import insert, select
from app.models import OrderDB, OutboxEvent
from app.outbox_processor import process_outbox
@pytest.mark.anyio
async def test_process_outbox_sends_event_and_updates_status(db_session, mocker):
# Insert the order first
await db_session.execute(
insert(OrderDB).values(
id="123",
type="limit",
side="buy",
instrument="ABCDEFGHIJKL",
limit_price=123.45,
quantity=10,
)
)
# Insert a fake event into DB
await db_session.execute(
insert(OutboxEvent).values(
order_id="123",
status="PENDING",
retry_count=0,
processed=False,
event_type="ORDER_CREATED",
)
)
# Commit to ensure the order and event are in the DB
await db_session.commit()
# Patch publish_to_rabbitmq to not hit Rabbit
mock_publish = mocker.patch(
"app.outbox_processor.publish_to_rabbitmq", return_value=None
)
# Patch get_session to yield our test db_session
async def fake_get_session():
yield db_session
mocker.patch("app.outbox_processor.get_session", fake_get_session)
# Run process_outbox
await process_outbox()
# Check publish was called
mock_publish.assert_any_call({"order_id": "123"})
# Verify event status updated in DB
result = await db_session.execute(
select(OutboxEvent).where(OutboxEvent.order_id == "123")
)
event = result.scalar_one()
assert event.status == "SENT"
assert event.processed is True