54 lines
2.3 KiB
Python
54 lines
2.3 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from scripts.combine_belgium_building_portfolio_specs import combine_specs
|
|
|
|
|
|
def write_spec(path: Path, slug: str, *, resolution: float = 0.25, status: str = "complete") -> Path:
|
|
path.write_text(json.dumps({
|
|
"status": status,
|
|
"side_m": 256.0,
|
|
"resolution_m": resolution,
|
|
"samples": [{"sample_slug": slug}],
|
|
}), encoding="utf-8")
|
|
return path
|
|
|
|
|
|
def test_combine_specs_records_provenance(tmp_path: Path) -> None:
|
|
payload = combine_specs([write_spec(tmp_path / "one.json", "one"), write_spec(tmp_path / "two.json", "two")])
|
|
assert payload["sample_count"] == 2
|
|
assert [sample["sample_slug"] for sample in payload["samples"]] == ["one", "two"]
|
|
assert all(len(source["sha256"]) == 64 for source in payload["source_specs"])
|
|
|
|
|
|
def test_combine_specs_rejects_duplicate_slugs(tmp_path: Path) -> None:
|
|
with pytest.raises(ValueError, match="Duplicate"):
|
|
combine_specs([write_spec(tmp_path / "one.json", "same"), write_spec(tmp_path / "two.json", "same")])
|
|
|
|
|
|
def test_combine_specs_rejects_mismatched_resolution(tmp_path: Path) -> None:
|
|
with pytest.raises(ValueError, match="resolution"):
|
|
combine_specs([write_spec(tmp_path / "one.json", "one"), write_spec(tmp_path / "two.json", "two", resolution=0.5)])
|
|
|
|
|
|
def test_combine_specs_rejects_incomplete_source(tmp_path: Path) -> None:
|
|
with pytest.raises(ValueError, match="not complete"):
|
|
combine_specs([write_spec(tmp_path / "one.json", "one", status="in_progress")])
|
|
|
|
|
|
def test_combine_specs_accepts_populated_legacy_v1_when_dimensions_come_from_complete_spec(tmp_path: Path) -> None:
|
|
legacy = tmp_path / "legacy.json"
|
|
legacy.write_text(json.dumps({"schema_version": 1, "samples": [{"sample_slug": "legacy"}]}), encoding="utf-8")
|
|
payload = combine_specs([legacy, write_spec(tmp_path / "current.json", "current")])
|
|
assert payload["sample_count"] == 2
|
|
assert payload["source_specs"][0]["source_status"] == "legacy_unstated"
|
|
|
|
|
|
def test_combine_specs_rejects_empty_legacy_v1(tmp_path: Path) -> None:
|
|
legacy = tmp_path / "legacy.json"
|
|
legacy.write_text(json.dumps({"schema_version": 1, "samples": []}), encoding="utf-8")
|
|
with pytest.raises(ValueError, match="not complete"):
|
|
combine_specs([legacy, write_spec(tmp_path / "current.json", "current")])
|