Stop legacy city refreshes from fetching sources

This commit is contained in:
2569718930@qq.com
2026-06-14 22:51:15 +08:00
parent a9e9eed2f1
commit 7fb82c5eba
4 changed files with 247 additions and 47 deletions
+41 -1
View File
@@ -99,7 +99,7 @@ def test_refresh_city_panel_cache_persists_canonical_temperature(monkeypatch):
lambda city, force_refresh=False, detail_mode="panel": _sample_panel_payload(),
)
payload = city_runtime._refresh_city_panel_cache("shanghai")
payload = city_runtime._refresh_city_panel_cache("shanghai", allow_external_fetch=True)
assert payload["canonical_temperature"]["value"] == 31.2
assert payload["canonical_temperature"]["source"] == "amsc_awos"
@@ -109,6 +109,46 @@ def test_refresh_city_panel_cache_persists_canonical_temperature(monkeypatch):
assert writes["canonical"][1]["observed_at"] == "2026-06-14T01:01:00+00:00"
def test_refresh_city_panel_cache_defaults_to_queue_without_sync_analyze(monkeypatch):
import web.services.city_runtime as city_runtime
enqueued = []
class FakeDB:
def get_city_cache(self, kind, city):
assert kind == "panel"
assert city == "shanghai"
return None
def get_canonical_temperature(self, city):
assert city == "shanghai"
return None
def enqueue_observation_refresh_request(self, **kwargs):
enqueued.append(kwargs)
return True
def fail_analyze(*_args, **_kwargs):
raise AssertionError("business cache refresh must not call _analyze by default")
monkeypatch.setattr(city_runtime, "_CACHE_DB", FakeDB())
monkeypatch.setattr(city_runtime, "_analyze", fail_analyze)
payload = city_runtime._refresh_city_panel_cache("shanghai")
assert payload["status"] == "initializing"
assert payload["stale_reason"] == "collector_refresh_queued"
assert payload["current"]["observation_status"] == "initializing"
assert enqueued == [
{
"city": "shanghai",
"kind": "panel",
"priority": "high",
"reason": "cold_start",
}
]
def test_city_panel_cold_cache_returns_canonical_latest_without_sync_refresh(monkeypatch):
import web.services.city_api as city_api
+13 -18
View File
@@ -53,7 +53,7 @@ def test_city_payloads_expose_wunderground_current():
assert detail["timeseries"]["wunderground_today_obs"] == [{"time": "13:30", "temp": 26}]
def test_api_payload_overlays_latest_wunderground_state(monkeypatch):
def test_api_payload_overlay_uses_cached_wunderground_state_without_fetch(monkeypatch):
stale_payload = {
"name": "guangzhou",
"temp_symbol": "°C",
@@ -78,26 +78,21 @@ def test_api_payload_overlays_latest_wunderground_state(monkeypatch):
},
}
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}],
}
fetch_calls = []
monkeypatch.setattr(city_runtime._weather, "fetch_wunderground_historical", fake_fetch)
def fail_fetch(*args, **kwargs):
fetch_calls.append((args, kwargs))
raise AssertionError("city API overlay must not fetch Wunderground directly")
monkeypatch.setattr(city_runtime._weather, "fetch_wunderground_historical", fail_fetch)
overlay = getattr(city_runtime, "_overlay_latest_wunderground_current", None)
assert callable(overlay), "city API must overlay cached payloads with latest WU state"
assert callable(overlay), "city API must preserve cached WU state without direct fetch"
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 payload["wunderground_current"]["temp"] == 36
assert payload["wunderground_current"]["max_so_far"] == 36
assert payload["official"]["wunderground_current"]["max_so_far"] == 36
assert payload["timeseries"]["wunderground_today_obs"] == [{"time": "14:00", "temp": 36}]
assert stale_payload["wunderground_current"]["max_so_far"] == 36
assert fetch_calls == []