from pathlib import Path import sys import pytest SCRIPTS = Path(__file__).resolve().parents[1] / "scripts" sys.path.insert(0, str(SCRIPTS)) from fetch_utils import fetch_with_cache # noqa: E402 from source_registry import ( # noqa: E402 SourceSpec, _REGISTRY, get_source, register, register_defaults, ) def setup_function(): _REGISTRY.clear() def test_source_spec_requires_url_and_license_class(): spec = SourceSpec( source_id="nhdplus_flow", url="https://example.invalid/nhd", organization="US EPA", license_class="public", expected_format="geojson", ) assert spec.source_id == "nhdplus_flow" def test_source_spec_rejects_missing_url(): with pytest.raises(ValueError, match="url"): SourceSpec( source_id="missing_url", url="", organization="US EPA", license_class="public", expected_format="geojson", ) def test_source_spec_rejects_unknown_license_class(): with pytest.raises(ValueError, match="license_class"): SourceSpec( source_id="bad_license", url="https://example.invalid/data", organization="US EPA", license_class="unclear", expected_format="geojson", ) def test_unknown_source_raises(): register_defaults() with pytest.raises(KeyError): get_source("not_a_real_source") def test_register_defaults_includes_nhdplus_or_wbd(): register_defaults() spec = get_source("wbd_huc8") assert "http" in spec.url def test_register_adds_custom_source(): spec = SourceSpec( source_id="custom_source", url="https://example.invalid/custom.csv", organization="Example", license_class="open", expected_format="csv", ) register(spec) assert get_source("custom_source") == spec def test_fetch_with_cache_writes_data_digest_and_retrieval_time(tmp_path): spec = SourceSpec( source_id="cached_source", url="https://example.invalid/cached.csv", organization="Example", license_class="open", expected_format="csv", ) destination = tmp_path / "cached.csv" fetch_with_cache(spec, destination, content=b"sample,data\n1,2\n") assert destination.read_bytes() == b"sample,data\n1,2\n" assert (tmp_path / "cached.csv.sha256").exists() assert (tmp_path / "cached.csv.retrieved_at.txt").exists()