修复图表实时观测叠加缓存降级
This commit is contained in:
@@ -385,3 +385,171 @@ def test_overlay_latest_jma_does_not_downgrade_newer_payload_current():
|
||||
assert result is payload
|
||||
assert result["current"]["temp"] == 25.0
|
||||
assert result["local_time"] == "07:00"
|
||||
|
||||
|
||||
def test_non_amsc_raw_overlays_keep_their_source_codes():
|
||||
cases = [
|
||||
(
|
||||
"amos",
|
||||
observation_overlay.overlay_latest_amos_observation,
|
||||
"seoul",
|
||||
{
|
||||
"observed_at": "2026-06-16T06:03:00+09:00",
|
||||
"fetched_at": "2026-06-15T21:03:30+00:00",
|
||||
"station_code": "RKSS",
|
||||
"station_name": "Gimpo",
|
||||
"payload": {
|
||||
"source": "amos",
|
||||
"source_label": "AMOS",
|
||||
"icao": "RKSS",
|
||||
"temp": 22.4,
|
||||
"observation_time": "2026-06-16T06:03:00+09:00",
|
||||
},
|
||||
},
|
||||
),
|
||||
(
|
||||
"hko_obs",
|
||||
observation_overlay.overlay_latest_hko_observation,
|
||||
"hong kong",
|
||||
{
|
||||
"observed_at": "2026-06-16T05:55:00+08:00",
|
||||
"fetched_at": "2026-06-15T21:55:30+00:00",
|
||||
"station_code": "HKO",
|
||||
"station_name": "Hong Kong Observatory",
|
||||
"payload": {
|
||||
"source": "hko_obs",
|
||||
"source_label": "HKO",
|
||||
"station_code": "HKO",
|
||||
"station_label": "Hong Kong Observatory",
|
||||
"temp": 28.6,
|
||||
"obs_time": "2026-06-16T05:55:00+08:00",
|
||||
},
|
||||
},
|
||||
),
|
||||
(
|
||||
"mgm",
|
||||
observation_overlay.overlay_latest_mgm_observation,
|
||||
"ankara",
|
||||
{
|
||||
"observed_at": "2026-06-16T00:10:00+03:00",
|
||||
"fetched_at": "2026-06-15T21:10:30+00:00",
|
||||
"station_code": "17130",
|
||||
"station_name": "Ankara",
|
||||
"payload": {
|
||||
"source": "mgm",
|
||||
"source_label": "MGM",
|
||||
"icao": "17130",
|
||||
"station_label": "Ankara (Bölge/Center)",
|
||||
"temp": 25.3,
|
||||
"obs_time": "2026-06-16T00:10:00+03:00",
|
||||
},
|
||||
},
|
||||
),
|
||||
]
|
||||
|
||||
for source_code, overlay_fn, city, row in cases:
|
||||
class FakeDB:
|
||||
def get_latest_raw_observation(self, source, requested_city):
|
||||
assert (source, requested_city) == (source_code, city)
|
||||
return row
|
||||
|
||||
result = overlay_fn(
|
||||
FakeDB(),
|
||||
city,
|
||||
{
|
||||
"name": city,
|
||||
"temp_symbol": "°C",
|
||||
"current": {
|
||||
"temp": 19.0,
|
||||
"source_code": "metar",
|
||||
"obs_time": "2026-06-15T00:00:00+00:00",
|
||||
},
|
||||
"airport_current": {
|
||||
"temp": 19.0,
|
||||
"source_code": "metar",
|
||||
"obs_time": "2026-06-15T00:00:00+00:00",
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
assert result["current"]["source_code"] == source_code
|
||||
assert result["airport_current"]["source_code"] == source_code
|
||||
assert result["current"]["source_label"] == row["payload"]["source_label"]
|
||||
assert result["current"].get("settlement_source") != "amsc_awos"
|
||||
assert result["canonical_temperature"]["source"] == source_code
|
||||
|
||||
|
||||
def test_overlay_latest_cwa_resets_stale_taipei_detail_to_latest_local_day():
|
||||
class FakeWeather:
|
||||
def fetch_cwa_taipei_settlement_current(self):
|
||||
return {
|
||||
"source": "cwa",
|
||||
"source_label": "CWA",
|
||||
"station_code": "466920",
|
||||
"station_name": "臺北",
|
||||
"observation_time": "2026-06-16T15:30:00+08:00",
|
||||
"current": {
|
||||
"temp": 29.4,
|
||||
"max_temp_so_far": 31.0,
|
||||
},
|
||||
}
|
||||
|
||||
stale_payload = {
|
||||
"name": "taipei",
|
||||
"display_name": "Taipei",
|
||||
"temp_symbol": "°C",
|
||||
"local_date": "2026-06-14",
|
||||
"local_time": "18:00",
|
||||
"current": {
|
||||
"temp": 24.0,
|
||||
"source_code": "metar",
|
||||
"obs_time": "2026-06-14T10:00:00+00:00",
|
||||
},
|
||||
"airport_current": {
|
||||
"temp": 24.0,
|
||||
"source_code": "metar",
|
||||
"obs_time": "2026-06-14T10:00:00+00:00",
|
||||
},
|
||||
"airport_primary": {
|
||||
"temp": 24.0,
|
||||
"source_code": "metar",
|
||||
"obs_time": "2026-06-14T10:00:00+00:00",
|
||||
},
|
||||
"overview": {
|
||||
"local_date": "2026-06-14",
|
||||
"local_time": "18:00",
|
||||
"current_temp": 24.0,
|
||||
},
|
||||
"metar_today_obs": [
|
||||
{"time": "18:00", "temp": 24.0},
|
||||
],
|
||||
"timeseries": {
|
||||
"metar_today_obs": [
|
||||
{"time": "18:00", "temp": 24.0},
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
result = observation_overlay.overlay_latest_cwa_observation(
|
||||
FakeWeather(),
|
||||
"taipei",
|
||||
stale_payload,
|
||||
)
|
||||
|
||||
assert result["local_date"] == "2026-06-16"
|
||||
assert result["local_time"] == "15:30"
|
||||
assert result["current"]["temp"] == 29.4
|
||||
assert result["current"]["source_code"] == "cwa"
|
||||
assert result["airport_current"]["obs_time"] == "2026-06-16T15:30:00+08:00"
|
||||
assert result["overview"]["local_date"] == "2026-06-16"
|
||||
assert result["overview"]["current_temp"] == 29.4
|
||||
assert result["metar_today_obs"] == [
|
||||
{
|
||||
"time": "15:30",
|
||||
"temp": 29.4,
|
||||
"obs_time": "2026-06-16T15:30:00+08:00",
|
||||
"source_code": "cwa",
|
||||
"source_label": "CWA",
|
||||
}
|
||||
]
|
||||
assert result["timeseries"]["metar_today_obs"] == result["metar_today_obs"]
|
||||
|
||||
@@ -1431,6 +1431,76 @@ def test_chart_data_returns_cached_payload_when_optional_overlay_times_out(monke
|
||||
]
|
||||
|
||||
|
||||
def test_chart_data_waits_for_latest_observation_overlay_when_optional_timeout_is_short(monkeypatch):
|
||||
import asyncio
|
||||
|
||||
class FakeCache:
|
||||
def get_city_cache(self, kind, city):
|
||||
assert kind == "full"
|
||||
return {
|
||||
"payload": {
|
||||
"name": city,
|
||||
"display_name": city.title(),
|
||||
"temp_symbol": "°C",
|
||||
"risk": {"icao": "ZUUU"},
|
||||
"current": {
|
||||
"temp": 21.0,
|
||||
"source_code": "metar",
|
||||
"obs_time": "2026-06-14T16:00:00+00:00",
|
||||
},
|
||||
"airport_current": {
|
||||
"temp": 21.0,
|
||||
"source_code": "metar",
|
||||
"obs_time": "2026-06-14T16:00:00+00:00",
|
||||
},
|
||||
"hourly": {"times": ["13:00"], "temps": [21.0]},
|
||||
},
|
||||
}
|
||||
|
||||
def get_runway_obs_recent(self, icao, minutes=60):
|
||||
return []
|
||||
|
||||
def get_latest_raw_observation(self, source, city):
|
||||
if (source, city) != ("amsc_awos", "chengdu"):
|
||||
return None
|
||||
return {
|
||||
"source": "amsc_awos",
|
||||
"city": "chengdu",
|
||||
"station_code": "ZUUU",
|
||||
"station_name": "Chengdu Shuangliu",
|
||||
"status": "ok",
|
||||
"observed_at": "2026-06-14T17:00:00+00:00",
|
||||
"fetched_at": "2026-06-14T17:00:30+00:00",
|
||||
"payload": {
|
||||
"source": "amsc_awos",
|
||||
"source_label": "AMSC AWOS Chengdu Shuangliu (ZUUU)",
|
||||
"icao": "ZUUU",
|
||||
"temp_c": 25.8,
|
||||
"observation_time": "2026-06-14T17:00:00+00:00",
|
||||
"observation_time_local": "2026-06-15 01:00:00",
|
||||
},
|
||||
}
|
||||
|
||||
async def fake_run_in_threadpool(fn, *args, **kwargs):
|
||||
if fn is city_api.overlay_latest_amsc_observation:
|
||||
await asyncio.sleep(0.05)
|
||||
return fn(*args, **kwargs)
|
||||
|
||||
monkeypatch.setenv("POLYWEATHER_CITY_CHART_OPTIONAL_OVERLAY_TIMEOUT_MS", "1")
|
||||
monkeypatch.setattr(city_api, "run_in_threadpool", fake_run_in_threadpool)
|
||||
monkeypatch.setattr(city_api.legacy_routes, "_CACHE_DB", FakeCache())
|
||||
monkeypatch.setattr(
|
||||
city_api.legacy_routes,
|
||||
"_overlay_latest_wunderground_current",
|
||||
lambda city, payload: payload,
|
||||
)
|
||||
|
||||
payload = asyncio.run(city_api._get_city_chart_data("chengdu", force_refresh=False))
|
||||
|
||||
assert payload["current"]["temp"] == 25.8
|
||||
assert payload["airport_current"]["source_code"] == "amsc_awos"
|
||||
|
||||
|
||||
def test_chart_detail_payload_uses_threadpool_and_reuses_short_cache(monkeypatch):
|
||||
import asyncio
|
||||
|
||||
|
||||
Reference in New Issue
Block a user