优先使用本地官方观测修复图表旧值

This commit is contained in:
2569718930@qq.com
2026-06-16 16:43:15 +08:00
parent 6943f637ad
commit ba59aebf87
3 changed files with 260 additions and 13 deletions
+135
View File
@@ -1383,6 +1383,141 @@ def test_chart_data_cache_hit_overlays_latest_jma_amedas(monkeypatch):
]
def test_chart_data_cache_hit_overlays_latest_jma_from_airport_obs_log(monkeypatch):
import asyncio
class FakeCache:
def get_city_cache(self, kind, city):
assert kind == "full"
return {
"payload": {
"name": city,
"display_name": "Tokyo",
"temp_symbol": "°C",
"local_date": "2026-06-14",
"local_time": "19:00",
"current": {
"temp": 23.0,
"source_code": "metar",
"obs_time": "2026-06-14T10:00:00+00:00",
},
"airport_current": {
"temp": 23.0,
"source_code": "metar",
"obs_time": "2026-06-14T10:00:00+00:00",
},
"metar_today_obs": [{"time": "19:00", "temp": 23.0}],
"timeseries": {"metar_today_obs": [{"time": "19:00", "temp": 23.0}]},
},
}
def get_runway_obs_recent(self, icao, minutes=60):
return []
def get_latest_raw_observation(self, source, city):
return None
def get_airport_obs_recent(self, icao, minutes=30):
assert icao == "44166"
return [
{
"icao": "44166",
"city": "tokyo",
"temp_c": 24.0,
"obs_time": "2026-06-16T06:00:00+09:00",
"created_at": "2026-06-15T21:00:15+00:00",
}
]
class FakeWeather:
def fetch_jma_amedas_official_nearby(self, city, use_fahrenheit=False):
return []
def fetch_jma_amedas_current(self, city, use_fahrenheit=False):
return None
monkeypatch.setattr(city_api.legacy_routes, "_CACHE_DB", FakeCache())
monkeypatch.setattr(city_api.legacy_routes, "_weather", FakeWeather())
monkeypatch.setattr(
city_api.legacy_routes,
"_overlay_latest_wunderground_current",
lambda city, payload: payload,
)
payload = asyncio.run(city_api._get_city_chart_data("tokyo", force_refresh=False))
assert payload["local_date"] == "2026-06-16"
assert payload["local_time"] == "06:00"
assert payload["airport_current"]["temp"] == 24.0
assert payload["airport_current"]["source_code"] == "jma_amedas"
def test_chart_data_cache_hit_overlays_latest_cwa_from_airport_obs_log(monkeypatch):
import asyncio
class FakeCache:
def get_city_cache(self, kind, city):
assert kind == "full"
return {
"payload": {
"name": city,
"display_name": "Taipei",
"temp_symbol": "°C",
"local_date": "2026-06-14",
"local_time": "18:00",
"current": {
"temp": 26.0,
"source_code": "cwa",
"obs_time": "2026-06-14T10:00:00+00:00",
},
"airport_current": {
"temp": 26.0,
"source_code": "cwa",
"obs_time": "2026-06-14T10:00:00+00:00",
},
"metar_today_obs": [{"time": "18:00", "temp": 26.0}],
"timeseries": {"metar_today_obs": [{"time": "18:00", "temp": 26.0}]},
},
}
def get_runway_obs_recent(self, icao, minutes=60):
return []
def get_latest_raw_observation(self, source, city):
return None
def get_airport_obs_recent(self, icao, minutes=30):
assert icao == "466920"
return [
{
"icao": "466920",
"city": "taipei",
"temp_c": 29.4,
"obs_time": "2026-06-16T15:30:00+08:00",
"created_at": "2026-06-16T07:30:15+00:00",
}
]
class FakeWeather:
def fetch_cwa_taipei_settlement_current(self):
return None
monkeypatch.setattr(city_api.legacy_routes, "_CACHE_DB", FakeCache())
monkeypatch.setattr(city_api.legacy_routes, "_weather", FakeWeather())
monkeypatch.setattr(
city_api.legacy_routes,
"_overlay_latest_wunderground_current",
lambda city, payload: payload,
)
payload = asyncio.run(city_api._get_city_chart_data("taipei", force_refresh=False))
assert payload["local_date"] == "2026-06-16"
assert payload["local_time"] == "15:30"
assert payload["airport_current"]["temp"] == 29.4
assert payload["airport_current"]["source_code"] == "cwa"
def test_chart_data_returns_cached_payload_when_optional_overlay_times_out(monkeypatch):
import asyncio