Add the season provenance checks (not yet wired)
This commit is contained in:
1 parent
9528c717f7
commit
e08aaa192b
2 files changed
+180
No files matched your search
@@ -0,0 +1,70 @@
|
||||
"""Season provenance checks for a trail record's [hike] table.
|
||||
|
||||
Python 3 standard library only. tools/validate.py imports this module; it
|
||||
must not import validate.py. The fields are documented in docs/schema.md.
|
||||
"""
|
||||
|
||||
BASES = ("primary", "sources", "source", "estimate")
|
||||
PROVENANCE_KEYS = frozenset({"season_basis", "season_method"})
|
||||
SEASON_REQUIRED = False
|
||||
|
||||
|
||||
def _naming(sources, fact):
|
||||
return [s for s in sources if isinstance(s, dict) and fact in (s.get("facts") or [])]
|
||||
|
||||
|
||||
def check_season(tid, hike, errors):
|
||||
"""Check season, months, season_basis and season_method agree."""
|
||||
has_season = "season" in hike
|
||||
has_months = "months" in hike
|
||||
has_basis = "season_basis" in hike
|
||||
has_method = "season_method" in hike
|
||||
basis = hike.get("season_basis")
|
||||
sources = hike.get("source") or []
|
||||
if not isinstance(sources, list):
|
||||
sources = []
|
||||
|
||||
if has_season != has_months:
|
||||
errors.append(f"{tid}: hike.season and hike.months must appear together")
|
||||
if not has_season:
|
||||
if SEASON_REQUIRED:
|
||||
errors.append(f"{tid}: hike.season is required")
|
||||
if has_basis:
|
||||
errors.append(f"{tid}: hike.season_basis must not be given without hike.season")
|
||||
if has_method:
|
||||
errors.append(f"{tid}: hike.season_method must not be given without hike.season")
|
||||
return
|
||||
|
||||
if not has_basis:
|
||||
errors.append(f"{tid}: hike.season_basis is required when hike.season is present")
|
||||
elif basis not in BASES:
|
||||
errors.append(
|
||||
f"{tid}: hike.season_basis must be one of {', '.join(BASES)}, not {basis!r}")
|
||||
|
||||
if basis == "estimate":
|
||||
method = hike.get("season_method")
|
||||
if not isinstance(method, str) or not method.strip():
|
||||
errors.append(f"{tid}: hike.season_method is required with season_basis \"estimate\"")
|
||||
elif len(method) >= 400:
|
||||
errors.append(f"{tid}: hike.season_method must be under 400 characters")
|
||||
elif has_method:
|
||||
errors.append(f"{tid}: hike.season_method is only given with season_basis \"estimate\"")
|
||||
|
||||
naming_season = _naming(sources, "season")
|
||||
if basis in ("primary", "source"):
|
||||
if not naming_season:
|
||||
errors.append(
|
||||
f"{tid}: hike.season_basis \"{basis}\" needs a [[hike.source]] naming season")
|
||||
elif basis == "sources":
|
||||
publishers = {str(s.get("publisher", "")).strip().casefold()
|
||||
for s in naming_season if str(s.get("publisher", "")).strip()}
|
||||
if len(publishers) < 2:
|
||||
errors.append(
|
||||
f"{tid}: hike.season_basis \"sources\" needs two sources from different "
|
||||
"publishers naming season")
|
||||
elif basis == "estimate":
|
||||
naming = {id(s) for s in _naming(sources, "season") + _naming(sources, "months")}
|
||||
if len(naming) != 1:
|
||||
errors.append(
|
||||
f"{tid}: hike.season_basis \"estimate\" needs exactly one source naming "
|
||||
"season or months")
|
||||
@@ -0,0 +1,110 @@
|
||||
"""Tests for tools/season.py check_season.
|
||||
|
||||
Run from the repository root:
|
||||
python3 -m unittest discover tools/tests
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
sys.dont_write_bytecode = True
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import season # noqa: E402
|
||||
|
||||
|
||||
def src(publisher, facts=("season", "months"), url="https://example.org/trail"):
|
||||
return {"url": url, "publisher": publisher, "facts": list(facts)}
|
||||
|
||||
|
||||
def check(**fields):
|
||||
hike = {
|
||||
"season": "June to September",
|
||||
"months": [6, 7, 8, 9],
|
||||
"season_basis": "primary",
|
||||
"source": [src("Park Service")],
|
||||
}
|
||||
for k, v in fields.items():
|
||||
if v is None:
|
||||
hike.pop(k, None)
|
||||
else:
|
||||
hike[k] = v
|
||||
errors = []
|
||||
season.check_season("t", hike, errors)
|
||||
return errors
|
||||
|
||||
|
||||
class SeasonBasisTest(unittest.TestCase):
|
||||
def test_primary_with_one_naming_source_passes(self):
|
||||
self.assertEqual(check(), [])
|
||||
|
||||
def test_season_without_basis_is_an_error(self):
|
||||
errors = check(season_basis=None)
|
||||
self.assertTrue(any("season_basis" in e for e in errors), errors)
|
||||
|
||||
def test_basis_without_season_is_an_error(self):
|
||||
errors = check(season=None, months=None)
|
||||
self.assertTrue(any("season_basis" in e for e in errors), errors)
|
||||
|
||||
def test_unknown_basis_is_an_error(self):
|
||||
errors = check(season_basis="guessed")
|
||||
self.assertTrue(any("season_basis" in e for e in errors), errors)
|
||||
|
||||
def test_months_without_season_is_an_error(self):
|
||||
errors = check(season=None, season_basis=None)
|
||||
self.assertTrue(errors)
|
||||
|
||||
def test_season_without_months_is_an_error(self):
|
||||
errors = check(months=None)
|
||||
self.assertTrue(errors)
|
||||
|
||||
def test_sources_needs_two_publishers(self):
|
||||
same = [src("Guide A", url="https://a.example/1"), src("guide a", url="https://a.example/2")]
|
||||
errors = check(season_basis="sources", source=same)
|
||||
self.assertEqual(len(errors), 1, errors)
|
||||
diff = [src("Guide A", url="https://a.example/1"), src("Guide B", url="https://b.example/1")]
|
||||
self.assertEqual(check(season_basis="sources", source=diff), [])
|
||||
|
||||
def test_source_basis_needs_a_naming_source(self):
|
||||
self.assertEqual(check(season_basis="source"), [])
|
||||
errors = check(season_basis="source", source=[src("Guide A", facts=("days",))])
|
||||
self.assertTrue(errors)
|
||||
|
||||
def test_primary_needs_a_naming_source(self):
|
||||
errors = check(source=[src("Park Service", facts=("days",))])
|
||||
self.assertTrue(errors)
|
||||
|
||||
def test_estimate_needs_a_method_and_refuses_one_otherwise(self):
|
||||
self.assertTrue(check(season_basis="estimate"))
|
||||
self.assertEqual(
|
||||
check(season_basis="estimate", season_method="Estimated from climate normals."), [])
|
||||
self.assertTrue(check(season_basis="estimate", season_method=""))
|
||||
self.assertTrue(check(season_basis="estimate", season_method="x" * 401))
|
||||
self.assertTrue(check(season_method="Estimated from climate normals."))
|
||||
|
||||
def test_estimate_with_two_naming_sources_is_an_error(self):
|
||||
two = [src("Climate", url="https://c.example/1"), src("Other", url="https://o.example/1")]
|
||||
errors = check(season_basis="estimate", season_method="Estimated from climate.", source=two)
|
||||
self.assertTrue(errors)
|
||||
|
||||
def test_estimate_with_no_naming_source_is_an_error(self):
|
||||
errors = check(season_basis="estimate", season_method="Estimated from climate.",
|
||||
source=[src("Climate", facts=("days",))])
|
||||
self.assertTrue(errors)
|
||||
|
||||
def test_a_hike_without_season_passes_until_required(self):
|
||||
self.assertEqual(check(season=None, months=None, season_basis=None, source=None), [])
|
||||
|
||||
def test_required_flag_makes_it_an_error(self):
|
||||
with mock.patch.object(season, "SEASON_REQUIRED", True):
|
||||
errors = check(season=None, months=None, season_basis=None, source=None)
|
||||
self.assertTrue(any("season" in e for e in errors), errors)
|
||||
|
||||
def test_method_without_season_is_an_error(self):
|
||||
errors = check(season=None, months=None, season_basis=None, season_method="Some method.")
|
||||
self.assertTrue(errors)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user