coastwise / tests /test_source_refresh.py
Stephen Lee
feat: add official source cache foundation
a3c85ad
Raw
History Blame Contribute Delete
1.34 kB
"""Source refresh tests."""
from datetime import datetime, timezone
from coastwise.schemas import RefreshStatus
from coastwise.source_cache import DEFAULT_SEED_CACHE_DIR, load_seed_cache
from coastwise.source_refresh import refresh_official_sources
from coastwise.source_registry import load_official_sources
NOW = datetime(2026, 6, 14, 13, tzinfo=timezone.utc)
def test_refresh_official_sources_reports_updated_unchanged_failed_and_partial():
sources = load_official_sources()[:3]
cache = load_seed_cache(DEFAULT_SEED_CACHE_DIR)
def fetcher(source):
if source.id == sources[0].id:
return cache.snapshots_by_source_id[source.id].raw_text + " updated"
if source.id == sources[1].id:
return cache.snapshots_by_source_id[source.id].raw_text
raise OSError("network unavailable")
result, refreshed_cache = refresh_official_sources(sources, cache, NOW, fetcher)
assert result.source_results[sources[0].id] is RefreshStatus.UPDATED
assert result.source_results[sources[1].id] is RefreshStatus.UNCHANGED
assert result.source_results[sources[2].id] is RefreshStatus.FAILED
assert sources[2].id in result.cache_preserved_source_ids
assert refreshed_cache.snapshots_by_source_id[sources[2].id].content_hash
assert "failed" in result.user_message.lower()