Fix WU historical current observation merge

This commit is contained in:
2569718930@qq.com
2026-05-29 17:20:07 +08:00
parent 8f52b1d80c
commit 9e3a4e2f45
13 changed files with 859 additions and 9 deletions
+76 -1
View File
@@ -1,4 +1,5 @@
from web.services.city_payloads import build_city_summary_payload
from web.services.city_payloads import build_city_detail_payload, build_city_summary_payload
from web.services import city_runtime
def test_city_summary_payload_preserves_deb_metadata():
@@ -26,3 +27,77 @@ def test_city_summary_payload_preserves_deb_metadata():
"bias_adjustment": 1.3,
"bias_samples": 18,
}
def test_city_payloads_expose_wunderground_current():
data = {
"name": "shanghai",
"display_name": "Shanghai",
"temp_symbol": "°C",
"current": {},
"risk": {},
"wunderground_current": {
"source": "wunderground_historical",
"station_code": "ZSPD",
"max_so_far": 26,
"today_obs": [{"time": "13:30", "temp": 26}],
},
}
summary = build_city_summary_payload(data)
detail = build_city_detail_payload(data)
assert summary["wunderground_current"]["max_so_far"] == 26
assert detail["wunderground_current"]["max_so_far"] == 26
assert detail["official"]["wunderground_current"]["station_code"] == "ZSPD"
assert detail["timeseries"]["wunderground_today_obs"] == [{"time": "13:30", "temp": 26}]
def test_api_payload_overlays_latest_wunderground_state(monkeypatch):
stale_payload = {
"name": "guangzhou",
"temp_symbol": "°C",
"utc_offset_seconds": 28800,
"wunderground_current": {
"source": "wunderground_historical",
"station_code": "ZGGG",
"temp": 36,
"max_so_far": 36,
"today_obs": [{"time": "14:00", "temp": 36}],
},
"official": {
"wunderground_current": {
"source": "wunderground_historical",
"station_code": "ZGGG",
"temp": 36,
"max_so_far": 36,
},
},
"timeseries": {
"wunderground_today_obs": [{"time": "14:00", "temp": 36}],
},
}
def fake_fetch(city: str, *, use_fahrenheit: bool, utc_offset: int):
assert city == "guangzhou"
assert use_fahrenheit is False
assert utc_offset == 28800
return {
"source": "wunderground_historical",
"station_code": "ZGGG",
"temp": 38,
"max_so_far": 38,
"today_obs": [{"time": "15:17", "temp": 38}],
}
monkeypatch.setattr(city_runtime._weather, "fetch_wunderground_historical", fake_fetch)
overlay = getattr(city_runtime, "_overlay_latest_wunderground_current", None)
assert callable(overlay), "city API must overlay cached payloads with latest WU state"
payload = overlay("guangzhou", stale_payload)
assert payload["wunderground_current"]["temp"] == 38
assert payload["wunderground_current"]["max_so_far"] == 38
assert payload["official"]["wunderground_current"]["max_so_far"] == 38
assert payload["timeseries"]["wunderground_today_obs"] == [{"time": "15:17", "temp": 38}]
assert stale_payload["wunderground_current"]["max_so_far"] == 36