Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.

Commit 80ca7b0

Browse files
committed
review: enable overriding of orders_db and opportunities in stapi_client fixture
1 parent 528a513 commit 80ca7b0

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

tests/conftest.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,25 @@ def mock_product_test_spotlight(
6262
)
6363

6464

65-
@asynccontextmanager
66-
async def lifespan(app: FastAPI) -> AsyncIterator[dict[str, Any]]:
67-
try:
68-
yield {
69-
"_orders_db": InMemoryOrderDB(),
70-
"_opportunities": [],
71-
}
72-
finally:
73-
pass
74-
75-
7665
@pytest.fixture
77-
def stapi_client(mock_product_test_spotlight, base_url: str) -> Iterator[TestClient]:
66+
def stapi_client(
67+
mock_product_test_spotlight,
68+
base_url: str,
69+
orders_db: InMemoryOrderDB | None = None,
70+
opportunities: list[Opportunity] | None = None,
71+
) -> Iterator[TestClient]:
72+
@asynccontextmanager
73+
async def lifespan(app: FastAPI) -> AsyncIterator[dict[str, Any]]:
74+
try:
75+
yield {
76+
"_orders_db": orders_db if orders_db is not None else InMemoryOrderDB(),
77+
"_opportunities": opportunities
78+
if opportunities is not None
79+
else mock_test_spotlight_opportunities(),
80+
}
81+
finally:
82+
pass
83+
7884
root_router = RootRouter(
7985
get_orders=mock_get_orders,
8086
get_order=mock_get_order,
@@ -131,7 +137,6 @@ def mock_provider() -> Provider:
131137
)
132138

133139

134-
@pytest.fixture
135140
def mock_test_spotlight_opportunities() -> list[Opportunity]:
136141
"""Fixture to create mock data for Opportunities for `test-spotlight-1`."""
137142
now = datetime.now(timezone.utc) # Use timezone-aware datetime

tests/test_opportunity.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@
33
import pytest
44
from fastapi.testclient import TestClient
55

6-
from stapi_fastapi.models.opportunity import Opportunity, OpportunityCollection
6+
from stapi_fastapi.models.opportunity import OpportunityCollection
77

88
from .test_datetime_interval import rfc3339_strftime
99

1010

1111
@pytest.mark.parametrize("product_id", ["test-spotlight"])
1212
def test_search_opportunities_response(
1313
product_id: str,
14-
mock_test_spotlight_opportunities: list[Opportunity],
1514
stapi_client: TestClient,
1615
assert_link,
1716
) -> None:
18-
stapi_client.app_state["_opportunities"] = mock_test_spotlight_opportunities
19-
2017
now = datetime.now(UTC)
2118
start = now
2219
end = start + timedelta(days=5)

0 commit comments

Comments
 (0)