-
-
Notifications
You must be signed in to change notification settings - Fork 47
fix: add weather location fallbacks #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gzhang33
wants to merge
7
commits into
Anyesh:main
Choose a base branch
from
gzhang33:codex/weather-location-fallback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c5bf25f
fix: add weather location fallbacks
a9b5d93
Potential fix for pull request finding
gzhang33 135707d
fix: handle geocoding failures explicitly
fe3381c
refactor: centralize geocoding and location provider config
cd19b6b
fix: clean up PR validation issues
5e0d678
chore: remove env example overrides
8437fe5
chore: trim weather fallback diff
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| from datetime import datetime | ||
| from unittest.mock import AsyncMock, patch | ||
|
|
||
| import pytest | ||
| from httpx import AsyncClient | ||
|
|
||
| from app.api.weather import GEOCODING_FAILURE_DETAIL | ||
| from app.services.weather_service import GeocodingServiceError | ||
|
|
||
|
|
||
| class TestWeatherApi: | ||
| @pytest.mark.asyncio | ||
| async def test_current_weather_uses_location_name_without_persisting_coordinates( | ||
| self, client: AsyncClient, test_user, auth_headers, db_session | ||
| ): | ||
| test_user.location_name = "New York City" | ||
| test_user.location_lat = None | ||
| test_user.location_lon = None | ||
| await db_session.commit() | ||
|
|
||
| geocode_mock = AsyncMock(return_value=(40.7128, -74.0060, "New York City")) | ||
| weather_mock = AsyncMock( | ||
| return_value=type( | ||
| "Weather", | ||
| (), | ||
| { | ||
| "temperature": 12.5, | ||
| "feels_like": 7.3, | ||
| "humidity": 50, | ||
| "precipitation_chance": 10, | ||
| "precipitation_mm": 0.0, | ||
| "wind_speed": 23.4, | ||
| "condition": "partly cloudy", | ||
| "condition_code": 2, | ||
| "is_day": True, | ||
| "uv_index": 1.8, | ||
| "timestamp": datetime(2026, 5, 12, 15, 21, 35), | ||
| }, | ||
| )() | ||
| ) | ||
|
|
||
| with ( | ||
| patch("app.api.weather.WeatherService.geocode_location_name", geocode_mock), | ||
| patch("app.api.weather.WeatherService.get_current_weather", weather_mock), | ||
| ): | ||
| response = await client.get("/api/v1/weather/current", headers=auth_headers) | ||
|
|
||
| assert response.status_code == 200 | ||
| geocode_mock.assert_awaited_once_with("New York City") | ||
| weather_mock.assert_awaited_once_with(40.7128, -74.0060) | ||
|
|
||
| await db_session.refresh(test_user) | ||
| assert test_user.location_lat is None | ||
| assert test_user.location_lon is None | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_forecast_returns_400_when_location_missing( | ||
| self, client: AsyncClient, test_user, auth_headers, db_session | ||
| ): | ||
| test_user.location_name = None | ||
| test_user.location_lat = None | ||
| test_user.location_lon = None | ||
| await db_session.commit() | ||
|
|
||
| response = await client.get("/api/v1/weather/forecast", headers=auth_headers) | ||
|
|
||
| assert response.status_code == 400 | ||
| assert response.json()["detail"] == ( | ||
| "Location not set. Please provide coordinates or set your location in settings." | ||
| ) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_current_weather_returns_503_when_saved_location_geocoding_fails( | ||
| self, client: AsyncClient, test_user, auth_headers, db_session | ||
| ): | ||
| test_user.location_name = "New York City" | ||
| test_user.location_lat = None | ||
| test_user.location_lon = None | ||
| await db_session.commit() | ||
|
|
||
| geocode_mock = AsyncMock(side_effect=GeocodingServiceError("geocoder unavailable")) | ||
|
|
||
| with patch("app.api.weather.WeatherService.geocode_location_name", geocode_mock): | ||
| response = await client.get("/api/v1/weather/current", headers=auth_headers) | ||
|
|
||
| assert response.status_code == 503 | ||
| assert response.json()["detail"] == GEOCODING_FAILURE_DETAIL | ||
| geocode_mock.assert_awaited_once_with("New York City") | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_forecast_returns_503_when_saved_location_geocoding_fails( | ||
| self, client: AsyncClient, test_user, auth_headers, db_session | ||
| ): | ||
| test_user.location_name = "New York City" | ||
| test_user.location_lat = None | ||
| test_user.location_lon = None | ||
| await db_session.commit() | ||
|
|
||
| geocode_mock = AsyncMock(side_effect=GeocodingServiceError("geocoder unavailable")) | ||
|
|
||
| with patch("app.api.weather.WeatherService.geocode_location_name", geocode_mock): | ||
| response = await client.get("/api/v1/weather/forecast", headers=auth_headers) | ||
|
|
||
| assert response.status_code == 503 | ||
| assert response.json()["detail"] == GEOCODING_FAILURE_DETAIL | ||
| geocode_mock.assert_awaited_once_with("New York City") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.