Add sourced day counts, seasons and permit notes, and record why short trails are listed
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
d00df3b8ee
commit
007ff5fb77
58 files changed
+825
-84
No files matched your search
@@ -0,0 +1,45 @@
|
||||
"""Tests for the short_reason rule in tools/validate.py.
|
||||
|
||||
Run from the repository root:
|
||||
python3 -m unittest discover tools/tests
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
sys.dont_write_bytecode = True
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import validate as V # noqa: E402
|
||||
|
||||
|
||||
def check(rec):
|
||||
errors, warnings = [], []
|
||||
V.check_short_reason("t", rec, errors, warnings)
|
||||
return errors, warnings
|
||||
|
||||
|
||||
class ShortReasonTests(unittest.TestCase):
|
||||
def test_under_the_floor_needs_a_reason(self):
|
||||
self.assertEqual(len(check({"length_km": 67})[0]), 1)
|
||||
self.assertEqual(len(check({"length_km": 67, "short_reason": " "})[0]), 1)
|
||||
|
||||
def test_under_the_floor_with_a_reason_passes(self):
|
||||
self.assertEqual(check({"length_km": 67, "short_reason": "It takes five days."}), ([], []))
|
||||
|
||||
def test_at_or_over_the_floor_refuses_a_reason(self):
|
||||
self.assertEqual(len(check({"length_km": 80, "short_reason": "It takes five days."})[0]), 1)
|
||||
self.assertEqual(len(check({"length_km": 300, "short_reason": "x"})[0]), 1)
|
||||
|
||||
def test_at_or_over_the_floor_needs_nothing(self):
|
||||
self.assertEqual(check({"length_km": 80}), ([], []))
|
||||
self.assertEqual(check({"length_km": 1931}), ([], []))
|
||||
|
||||
def test_the_under_review_sentence_is_a_warning_not_an_error(self):
|
||||
errors, warnings = check({"length_km": 64, "short_reason": V.UNDER_REVIEW})
|
||||
self.assertEqual(errors, [])
|
||||
self.assertEqual(len(warnings), 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -19,7 +19,7 @@ URL_RE = re.compile(r"^https?://[^\s/]+\S*$")
|
||||
# names these. A table name on its own ("source", "hike.source") names every field in it.
|
||||
_TABLES = {
|
||||
"": ("id", "name", "common_name", "short_name", "short_name_url", "country", "regions", "length_km",
|
||||
"length_note", "system_km", "system_note", "built_km", "kind", "tier", "designation", "wave", "wave_note", "inclusion"),
|
||||
"length_note", "system_km", "system_note", "built_km", "kind", "tier", "designation", "wave", "wave_note", "inclusion", "short_reason"),
|
||||
"source": ("status", "publisher", "publisher_url", "url", "discovery", "format", "crs",
|
||||
"licence", "licence_text", "permission_quote", "permission_by",
|
||||
"permission_date", "permission_url", "attribution", "osm_only", "mainline",
|
||||
|
||||
@@ -466,6 +466,30 @@ def _whole_km(value):
|
||||
return isinstance(value, int) and not isinstance(value, bool) and value > 0
|
||||
|
||||
|
||||
SHORT_FLOOR_KM = 80 # 50 miles: the inclusion floor in README.md
|
||||
UNDER_REVIEW = "Listed before the test was written; under review."
|
||||
|
||||
|
||||
def check_short_reason(tid, rec, errors, warnings=None):
|
||||
"""short_reason (docs/schema.md): the one sentence saying why a trail under the floor is listed.
|
||||
|
||||
Required on every record whose length_km is under SHORT_FLOOR_KM and refused on any other. The
|
||||
sentence UNDER_REVIEW is accepted, as a warning, for a record whose own text supports none of
|
||||
the owner's three tests; the owner decides whether it stays."""
|
||||
length = rec.get("length_km")
|
||||
reason = rec.get("short_reason")
|
||||
short = isinstance(length, (int, float)) and not isinstance(length, bool) and length < SHORT_FLOOR_KM
|
||||
if reason is not None and not (isinstance(reason, str) and reason.strip()):
|
||||
errors.append(f"{tid}: short_reason must be a non-empty sentence")
|
||||
elif short and reason is None:
|
||||
errors.append(f"{tid}: length_km {length} is under the {SHORT_FLOOR_KM} km floor, so "
|
||||
"short_reason must say why it is listed")
|
||||
elif not short and reason is not None:
|
||||
errors.append(f"{tid}: short_reason is only for a trail under {SHORT_FLOOR_KM} km")
|
||||
elif short and reason.strip() == UNDER_REVIEW and warnings is not None:
|
||||
warnings.append(f"{tid}: under {SHORT_FLOOR_KM} km and no reason for listing it is supported yet")
|
||||
|
||||
|
||||
def check_held_note(tid, rec, errors):
|
||||
"""A held record says why in wave_note: the page quotes it, and says nothing when there is none."""
|
||||
if str(rec.get("wave", "")) == "held" and not str(rec.get("wave_note", "")).strip():
|
||||
@@ -738,6 +762,7 @@ def check_left_out(root, trails, errors):
|
||||
|
||||
def main():
|
||||
errors = []
|
||||
warnings = []
|
||||
trail_ids = build_index.trail_dirs(ROOT)
|
||||
if not trail_ids:
|
||||
print("no trail directories found")
|
||||
@@ -761,6 +786,7 @@ def main():
|
||||
check_common_name(tid, rec, errors)
|
||||
check_length_facts(tid, rec, errors)
|
||||
check_held_note(tid, rec, errors)
|
||||
check_short_reason(tid, rec, errors, warnings)
|
||||
if rec.get("id") != tid:
|
||||
errors.append(f"{tid}: id {rec.get('id')!r} does not match directory name")
|
||||
if rec.get("country") not in ("US", "CA"):
|
||||
@@ -917,6 +943,8 @@ def main():
|
||||
except FileNotFoundError as e:
|
||||
errors.append(f"generated file missing ({e.filename}): run python3 tools/build_index.py")
|
||||
|
||||
for w in warnings:
|
||||
print(f"WARN {w}")
|
||||
if errors:
|
||||
for e in errors:
|
||||
print(f"ERROR {e}")
|
||||
|
||||
Reference in new issue
Block a user