Fix tests to match decision service API and add filtering to non-streaming endpoint
- Update test mocks from _get_disliked_ids to _get_user_id_safe - Fix decision service test method names (clear_decision -> remove_decision, etc.) - Fix positional vs keyword arg assertion in set_decision test - Add decision_filter param to non-streaming listing_geojson endpoint
This commit is contained in:
parent
a2745c1478
commit
43f9d210fb
3 changed files with 26 additions and 7 deletions
19
api/app.py
19
api/app.py
|
|
@ -171,6 +171,7 @@ async def get_listing_geojson(
|
|||
user: Annotated[User, Depends(get_current_user)],
|
||||
query_parameters: Annotated[QueryParameters, Depends(get_query_parameters)],
|
||||
limit: int | None = None,
|
||||
decision_filter: str = "all",
|
||||
) -> dict:
|
||||
"""Get listings as GeoJSON for map display."""
|
||||
if limit is not None:
|
||||
|
|
@ -183,6 +184,24 @@ async def get_listing_geojson(
|
|||
query_parameters=query_parameters,
|
||||
limit=limit,
|
||||
)
|
||||
|
||||
# Apply decision filtering
|
||||
if decision_filter != "everything":
|
||||
user_id = _get_user_id_safe(user.email)
|
||||
if user_id is not None:
|
||||
decision_repo = DecisionRepository(engine)
|
||||
disliked_ids = decision_service.get_disliked_ids(decision_repo, user_id, query_parameters.listing_type.value) if decision_filter == "all" else None
|
||||
liked_ids = decision_service.get_liked_ids(decision_repo, user_id, query_parameters.listing_type.value) if decision_filter == "liked" else None
|
||||
features = result.data.get("features", [])
|
||||
features = [
|
||||
f for f in features
|
||||
if _should_include(
|
||||
f.get("properties", {}).get("id", 0),
|
||||
decision_filter, disliked_ids, liked_ids,
|
||||
)
|
||||
]
|
||||
result.data["features"] = features
|
||||
|
||||
return result.data
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -108,7 +108,7 @@ class TestListingGeoJsonEndpoint:
|
|||
def mock_export(self):
|
||||
"""Mock the export service."""
|
||||
with patch("api.app.export_service.export_to_geojson") as mock, \
|
||||
patch("api.app._get_disliked_ids", return_value=set()):
|
||||
patch("api.app._get_user_id_safe", return_value=None):
|
||||
mock.return_value = MagicMock(
|
||||
data={"type": "FeatureCollection", "features": [{"type": "Feature"}]}
|
||||
)
|
||||
|
|
@ -175,7 +175,7 @@ class TestStreamingEndpoint:
|
|||
def mock_repository(self):
|
||||
"""Mock the repository methods and bypass cache."""
|
||||
with patch("api.app.get_cached_count", return_value=None), \
|
||||
patch("api.app._get_disliked_ids", return_value=set()), \
|
||||
patch("api.app._get_user_id_safe", return_value=None), \
|
||||
patch("api.app.ListingRepository") as MockRepo:
|
||||
mock_instance = MagicMock()
|
||||
mock_instance.count_listings.return_value = 3
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ class TestSetDecision:
|
|||
)
|
||||
assert result.decision == "liked"
|
||||
repo.upsert_decision.assert_called_once_with(
|
||||
user_id=1, listing_id=100, listing_type="RENT", decision="liked"
|
||||
1, 100, "RENT", "liked"
|
||||
)
|
||||
|
||||
def test_set_disliked(self) -> None:
|
||||
|
|
@ -58,7 +58,7 @@ class TestGetDecisions:
|
|||
listing_type="RENT", decision="liked",
|
||||
),
|
||||
]
|
||||
result = decision_service.get_decisions(repo, user_id=1)
|
||||
result = decision_service.get_user_decisions(repo, user_id=1)
|
||||
assert len(result) == 1
|
||||
|
||||
|
||||
|
|
@ -66,7 +66,7 @@ class TestClearDecision:
|
|||
def test_clear_existing(self) -> None:
|
||||
repo = MagicMock()
|
||||
repo.delete_decision.return_value = True
|
||||
result = decision_service.clear_decision(
|
||||
result = decision_service.remove_decision(
|
||||
repo, user_id=1, listing_id=100, listing_type="RENT"
|
||||
)
|
||||
assert result is True
|
||||
|
|
@ -74,7 +74,7 @@ class TestClearDecision:
|
|||
def test_clear_nonexistent(self) -> None:
|
||||
repo = MagicMock()
|
||||
repo.delete_decision.return_value = False
|
||||
result = decision_service.clear_decision(
|
||||
result = decision_service.remove_decision(
|
||||
repo, user_id=1, listing_id=100, listing_type="RENT"
|
||||
)
|
||||
assert result is False
|
||||
|
|
@ -84,7 +84,7 @@ class TestGetDislikedListingIds:
|
|||
def test_returns_disliked_set(self) -> None:
|
||||
repo = MagicMock()
|
||||
repo.get_disliked_listing_ids.return_value = {200, 300}
|
||||
result = decision_service.get_disliked_listing_ids(
|
||||
result = decision_service.get_disliked_ids(
|
||||
repo, user_id=1, listing_type="RENT"
|
||||
)
|
||||
assert result == {200, 300}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue