from datetime import date, datetime, timedelta, timezone from sqlalchemy import create_engine from app.collect import gongcan_sync from app.collect.gongcan import GongcanError, GongcanStore, claim_snapshot, save_snapshot from app.models import Base, CellData4G, CellData5G, GongcanSnapshot CITY = "江门" TODAY = date(2024, 1, 25) # 可取的周一:01-01、01-08、01-15、01-22 CELLS = { "4G": [{"cgi": "c1", "cell_name": "A", "station_name": "S", "latitude": 22.0, "longitude": 113.0}], "5G": [{"ncgi": "n1", "nrcell_name": "N", "station_name": "S", "latitude": 22.0, "longitude": 113.0}], } class Clock: def __init__(self): self.now = datetime(2024, 1, 25, 2, 0, tzinfo=timezone.utc) def __call__(self): return self.now class Platform: def __init__(self, broken=()): self.broken = set(broken) self.fetched = [] async def __call__(self, city, day): self.fetched.append(day) if day in self.broken: raise GongcanError("平台出错") return CELLS def make_store(platform, stored=(), clock=None): db = create_engine("sqlite://") Base.metadata.create_all(db, tables=[GongcanSnapshot.__table__, CellData4G.__table__, CellData5G.__table__]) clock = clock or Clock() for day in stored: save_snapshot(db, claim_snapshot(db, CITY, day, clock()), CELLS, clock()) return GongcanStore(db, platform, poll_seconds=0.01, clock=clock) def test_sync_fills_missing_mondays_newest_first_one_per_run(): platform = Platform() store = make_store(platform, stored=[date(2024, 1, 15)]) runs = [gongcan_sync.sync_once(CITY, today=TODAY, store=store) for _ in range(4)] assert runs == [(date(2024, 1, 22), 2), (date(2024, 1, 8), 1), (date(2024, 1, 1), 0), (None, 0)] assert platform.fetched == [date(2024, 1, 22), date(2024, 1, 8), date(2024, 1, 1)] def test_sync_stops_while_another_snapshot_is_loading_or_just_failed(): clock = Clock() platform = Platform(broken=[date(2024, 1, 22)]) store = make_store(platform, clock=clock) # 评估任务正在取 01-08:补取任务不再开第二个。 claim_snapshot(store.engine, CITY, date(2024, 1, 8), clock()) assert gongcan_sync.sync_once(CITY, today=TODAY, store=store) == (None, 4) save_snapshot(store.engine, 1, CELLS, clock()) try: gongcan_sync.sync_once(CITY, today=TODAY, store=store) except GongcanError as exc: assert "平台出错" in str(exc) else: raise AssertionError("失败应当抛出") assert gongcan_sync.sync_once(CITY, today=TODAY, store=store) == (None, 3) clock.now += timedelta(minutes=11) # 冷却后先补别的周,失败过的 01-22 留到最后再试。 assert gongcan_sync.sync_once(CITY, today=TODAY, store=store) == (date(2024, 1, 15), 2) assert gongcan_sync.sync_once(CITY, today=TODAY, store=store) == (date(2024, 1, 1), 1) assert platform.fetched == [date(2024, 1, 22), date(2024, 1, 15), date(2024, 1, 1)] def test_cache_status_counts_mondays_only(monkeypatch): clock = Clock() store = make_store(Platform(broken=[date(2024, 1, 22)]), stored=[date(2024, 1, 8), date(2024, 1, 15), date(2024, 1, 10)], clock=clock) try: gongcan_sync.sync_once(CITY, today=TODAY, store=store) except GongcanError: pass monkeypatch.setattr(gongcan_sync, "engine", lambda: store.engine) assert gongcan_sync.sync_cities() == [CITY] status = gongcan_sync.cache_status(TODAY) assert len(status) == 1 item = status[0] assert (item["city"], item["expected"], item["ready"], item["earliest"], item["latest"], item["loading"]) == (CITY, 4, 2, "2024-01-08", "2024-01-15", None) assert item["error"].startswith("2024-01-22 GongcanError: 平台出错") and item["errorAt"] def test_request_sync_does_nothing_without_a_queue(monkeypatch): monkeypatch.setattr(gongcan_sync, "postgres_conninfo", lambda: None) assert gongcan_sync.request_sync("江门市") is False monkeypatch.setattr(gongcan_sync, "postgres_conninfo", lambda: "postgresql://x") monkeypatch.setenv("DECOM_EVAL_SOURCE", "mock") assert gongcan_sync.request_sync("江门市") is False assert gongcan_sync.request_sync("") is False