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

Commit 3db2a60

Browse files
committed
tests: add more async tests and fix a few bugs
1 parent f21a152 commit 3db2a60

File tree

6 files changed

+232
-57
lines changed

6 files changed

+232
-57
lines changed

src/stapi_fastapi/models/opportunity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class OpportunitySearchRecord(BaseModel):
6565

6666

6767
class OpportunitySearchRecords(BaseModel):
68-
records: list[OpportunitySearchRecord]
68+
search_records: list[OpportunitySearchRecord]
6969
links: list[Link] = Field(default_factory=list)
7070

7171

src/stapi_fastapi/routers/product_router.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,9 @@ async def get_opportunity_collection(
429429
type=TYPE_JSON,
430430
),
431431
)
432-
return GeoJSONResponse(content=opportunity_collection.model_dump_json())
432+
return GeoJSONResponse(
433+
content=opportunity_collection.model_dump(mode="json")
434+
)
433435
case Success(Maybe.empty):
434436
raise NotFoundException("Opportunity Collection not found")
435437
case Failure(e):

src/stapi_fastapi/routers/root_router.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ async def get_opportunity_search_record(
409409
Get the Opportunity Search Record with `search_record_id`.
410410
"""
411411
match await self._get_opportunity_search_record(search_record_id, request):
412-
case Success(Maybe(Some(search_record))):
412+
case Success(Some(search_record)):
413413
self.add_opportunity_search_record_self_link(search_record, request)
414414
return search_record
415415
case Success(Maybe.empty):

tests/backends.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,18 @@ async def mock_search_opportunities_async(
147147
request: Request,
148148
) -> ResultE[OpportunitySearchRecord]:
149149
try:
150-
status = OpportunitySearchStatus(
150+
received_status = OpportunitySearchStatus(
151151
timestamp=datetime.now(timezone.utc),
152152
status_code=OpportunitySearchStatusCode.received,
153153
)
154154
search_record = OpportunitySearchRecord(
155155
id=str(uuid4()),
156156
product_id=product_router.product.id,
157157
opportunity_request=search,
158-
status=status,
158+
status=received_status,
159159
links=[],
160160
)
161-
request.state._opportunities_db._search_records[search_record.id] = (
162-
search_record
163-
)
164-
request.state._opportunities_db._search_record_statuses[
165-
search_record.id
166-
].insert(0, status)
161+
request.state._opportunities_db.put_search_record(search_record)
167162
return Success(search_record)
168163
except Exception as e:
169164
return Failure(e)
@@ -175,7 +170,7 @@ async def mock_get_opportunity_collection(
175170
try:
176171
return Success(
177172
Maybe.from_optional(
178-
request.state._opportunities_db._collections.get(
173+
request.state._opportunities_db.get_opportunity_collection(
179174
opportunity_collection_id
180175
)
181176
)
@@ -192,7 +187,7 @@ async def mock_get_opportunity_search_records(
192187
try:
193188
start = 0
194189
limit = min(limit, 100)
195-
search_records = list(request.state._opportunities_db._search_records.values())
190+
search_records = request.state._opportunities_db.get_search_records()
196191

197192
if next:
198193
start = int(next)
@@ -212,7 +207,7 @@ async def mock_get_opportunity_search_record(
212207
try:
213208
return Success(
214209
Maybe.from_optional(
215-
request.state._opportunities_db._search_records.get(search_record_id)
210+
request.state._opportunities_db.get_search_record(search_record_id)
216211
)
217212
)
218213
except Exception as e:

tests/shared.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from collections import defaultdict
2+
from copy import deepcopy
23
from datetime import datetime, timedelta, timezone
34
from typing import Any, Literal, Self
45
from uuid import uuid4
@@ -51,9 +52,25 @@ def __init__(self) -> None:
5152
class InMemoryOpportunityDB:
5253
def __init__(self) -> None:
5354
self._search_records: dict[str, OpportunitySearchRecord] = {}
54-
self._search_record_statuses: dict[str, list[OrderStatus]] = defaultdict(list)
5555
self._collections: dict[str, OpportunityCollection] = {}
5656

57+
def get_search_record(self, search_id: str) -> OpportunitySearchRecord | None:
58+
return deepcopy(self._search_records.get(search_id))
59+
60+
def get_search_records(self) -> list[OpportunitySearchRecord]:
61+
return deepcopy(list(self._search_records.values()))
62+
63+
def put_search_record(self, search_record: OpportunitySearchRecord) -> None:
64+
self._search_records[search_record.id] = deepcopy(search_record)
65+
66+
def get_opportunity_collection(self, collection_id) -> OpportunityCollection | None:
67+
return deepcopy(self._collections.get(collection_id))
68+
69+
def put_opportunity_collection(self, collection: OpportunityCollection) -> None:
70+
if collection.id is None:
71+
raise ValueError("collection must have an id")
72+
self._collections[collection.id] = deepcopy(collection)
73+
5774

5875
class MyProductConstraints(BaseModel):
5976
off_nadir: int

0 commit comments

Comments
 (0)