Spaces:
Running
Running
| import pytest | |
| from app.ai.tools import listing_tool | |
| def test_google_components_are_normalized_to_listing_geo_fields(): | |
| components = [ | |
| {"long_name": "Fidjrosse", "short_name": "Fidjrosse", "types": ["sublocality", "political"]}, | |
| {"long_name": "Cotonou", "short_name": "Cotonou", "types": ["locality", "political"]}, | |
| {"long_name": "Littoral Department", "short_name": "Littoral", "types": ["administrative_area_level_1", "political"]}, | |
| {"long_name": "Benin", "short_name": "BJ", "types": ["country", "political"]}, | |
| ] | |
| assert listing_tool._city_from_google_components(components) == "Cotonou" | |
| assert listing_tool._first_google_component(components, ("country",)) == "Benin" | |
| assert listing_tool._first_google_component(components, ("country",), "short_name").lower() == "bj" | |
| def test_nominatim_address_is_normalized_to_listing_geo_fields(): | |
| address = { | |
| "suburb": "Fidjrosse", | |
| "city": "Cotonou", | |
| "country": "Benin", | |
| "country_code": "bj", | |
| } | |
| assert listing_tool._city_from_nominatim_address(address) == "Cotonou" | |
| async def test_google_zero_results_does_not_fallback_to_nominatim(monkeypatch): | |
| async def fake_google(session, query): | |
| return { | |
| "success": False, | |
| "fallback_allowed": False, | |
| "error": "Location not found by Google", | |
| "status": "ZERO_RESULTS", | |
| } | |
| async def fail_nominatim(*args, **kwargs): | |
| raise AssertionError("Nominatim should not be called when Google returns ZERO_RESULTS") | |
| monkeypatch.setattr(listing_tool, "_google_geocode_query", fake_google) | |
| monkeypatch.setattr(listing_tool, "_nominatim_query", fail_nominatim) | |
| result = await listing_tool.geocode_address("not a real place", "Cotonou") | |
| assert result["success"] is False | |
| assert result["provider"] == "google" | |
| assert result["status"] == "ZERO_RESULTS" | |
| async def test_google_service_failure_falls_back_to_nominatim(monkeypatch): | |
| async def fake_google(session, query): | |
| return { | |
| "success": False, | |
| "fallback_allowed": True, | |
| "error": "Google geocoding HTTP 503", | |
| "status": "HTTP_503", | |
| } | |
| async def fake_nominatim(session, query, limit=1, countrycodes=None): | |
| if query == "Cotonou": | |
| return [{ | |
| "lat": "6.3703", | |
| "lon": "2.3912", | |
| "address": {"country_code": "bj"}, | |
| }] | |
| return [{ | |
| "lat": "6.3630", | |
| "lon": "2.3900", | |
| "display_name": "Fidjrosse, Cotonou, Benin", | |
| "address": {"city": "Cotonou", "country_code": "bj"}, | |
| }] | |
| monkeypatch.setattr(listing_tool, "_google_geocode_query", fake_google) | |
| monkeypatch.setattr(listing_tool, "_nominatim_query", fake_nominatim) | |
| result = await listing_tool.geocode_address("Fidjrosse", "Cotonou") | |
| assert result["success"] is True | |
| assert result["provider"] == "nominatim" | |
| assert result["city"] == "Cotonou" | |