Merge main into places-ship-2
This commit is contained in:
commit
1fbcfd5021
126 files changed
+2076
-9
No files matched your search
@@ -0,0 +1,135 @@
|
||||
"""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
|
||||
import validate as V # noqa: E402
|
||||
import refresh # 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_source_basis_needs_exactly_one_publisher(self):
|
||||
two = [src("Park Service"), src("Blog", url="https://example.org/b")]
|
||||
errors = check(season_basis="source", source=two)
|
||||
self.assertTrue(any("exactly one publisher" in e for e in errors), errors)
|
||||
same = [src("Park Service"), src("Park Service", url="https://example.org/b")]
|
||||
self.assertEqual(check(season_basis="source", source=same), [])
|
||||
|
||||
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)
|
||||
|
||||
def test_every_record_with_a_season_passes_the_validator(self):
|
||||
errors = []
|
||||
for tid in refresh.trail_ids(V.ROOT):
|
||||
rec = refresh.load_toml(os.path.join(V.ROOT, tid, "trail.toml"))
|
||||
if (rec.get("hike") or {}).get("season"):
|
||||
V.check_hike(tid, rec, errors)
|
||||
self.assertEqual(errors, [])
|
||||
|
||||
def test_provenance_keys_need_no_source_fact(self):
|
||||
hike = {"season": "June to September", "months": [6, 7, 8, 9], "season_basis": "primary",
|
||||
"source": [src("Park Service", facts=("season", "months"))]}
|
||||
errors = []
|
||||
V.check_hike("t", {"hike": hike}, errors)
|
||||
self.assertFalse([e for e in errors if "not named in the facts" in e], errors)
|
||||
self.assertFalse([e for e in errors if "not a known field" in e], errors)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,93 @@
|
||||
"""Tests for tools/season_batches.py and the researchers' brief.
|
||||
|
||||
Run from the repository root:
|
||||
python3 -m unittest discover tools/tests -p "test_season_batches.py"
|
||||
"""
|
||||
import math
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
sys.dont_write_bytecode = True
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import refresh # noqa: E402
|
||||
import season_batches as B # noqa: E402
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
|
||||
def write(root, tid, regions, season=None):
|
||||
os.makedirs(os.path.join(root, tid))
|
||||
hike = '[hike]\nshape = "loop"\n' + (f'season = "{season}"\nmonths = [6, 7]\n' if season else "")
|
||||
with open(os.path.join(root, tid, "trail.toml"), "w", encoding="utf-8") as f:
|
||||
f.write(f'name = "{tid}"\ncountry = "US"\nregions = {regions!r}\n\n'.replace("'", '"') + hike)
|
||||
|
||||
|
||||
class BatchesTest(unittest.TestCase):
|
||||
def test_missing_ids_are_the_records_without_a_season(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
write(root, "b-trail", ["CA"], season="June to July")
|
||||
write(root, "c-trail", ["CA"])
|
||||
write(root, "a-trail", ["WY"])
|
||||
self.assertEqual(B.missing_ids(root), ["a-trail", "c-trail"])
|
||||
|
||||
def test_batches_are_at_most_the_size_and_cover_every_id_once(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
for i in range(23):
|
||||
write(root, f"ca-{i:02d}", ["CA"])
|
||||
for i in range(7):
|
||||
write(root, f"pa-{i:02d}", ["PA"])
|
||||
got = B.batches(root, 10)
|
||||
flat = [x for b in got for x in b]
|
||||
self.assertEqual(sorted(flat), B.missing_ids(root))
|
||||
self.assertEqual(len(flat), len(set(flat)))
|
||||
self.assertTrue(all(0 < len(b) <= 10 for b in got))
|
||||
|
||||
def test_a_batch_stays_in_one_region_group_unless_joined(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
for i in range(23):
|
||||
write(root, f"ca-{i:02d}", ["CA"])
|
||||
for i in range(3):
|
||||
write(root, f"pa-{i:02d}", ["PA"])
|
||||
got = B.batches(root, 10)
|
||||
west = [b for b in got if all(x.startswith("ca-") for x in b)]
|
||||
self.assertEqual([len(b) for b in west], [10, 10])
|
||||
# the leftover 3 and the 3 Pennsylvania trails are each fewer than 5, so they share a batch
|
||||
mixed = [b for b in got if b not in west]
|
||||
self.assertEqual([len(b) for b in mixed], [6])
|
||||
|
||||
def test_a_small_leftover_joins_a_neighbour_with_room(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
for i in range(12):
|
||||
write(root, f"ca-{i:02d}", ["CA"]) # 10 and 2
|
||||
for i in range(4):
|
||||
write(root, f"or-{i:02d}", ["OR"]) # same group as CA
|
||||
got = B.batches(root, 10)
|
||||
self.assertEqual(sorted(len(b) for b in got), [6, 10])
|
||||
|
||||
def test_real_count(self):
|
||||
n = len(B.missing_ids(ROOT))
|
||||
recs = [t for t in refresh.trail_ids(ROOT)
|
||||
if not (refresh.load_toml(os.path.join(ROOT, t, "trail.toml")).get("hike") or {}).get("season")]
|
||||
self.assertEqual(n, len(recs))
|
||||
got = B.batches(ROOT)
|
||||
# each region group can leave one part-full batch
|
||||
self.assertGreaterEqual(len(got), math.ceil(n / 10))
|
||||
self.assertLessEqual(len(got), math.ceil(n / 10) + len(B.REGION_GROUPS))
|
||||
self.assertEqual(sorted(x for b in got for x in b), B.missing_ids(ROOT))
|
||||
|
||||
def test_every_region_code_in_the_records_has_a_group(self):
|
||||
for tid in refresh.trail_ids(ROOT):
|
||||
rec = refresh.load_toml(os.path.join(ROOT, tid, "trail.toml"))
|
||||
self.assertNotEqual(B.group_of(rec), B.OTHER, tid)
|
||||
|
||||
def test_the_brief_names_what_a_researcher_records(self):
|
||||
text = open(os.path.join(ROOT, "docs", "season-research.md"), encoding="utf-8").read()
|
||||
for want in ("hike.season_basis", "hike.months", "quote", "not-found", "publisher"):
|
||||
self.assertIn(want, text)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,162 @@
|
||||
"""Tests for tools/season_classify.py.
|
||||
|
||||
Run from the repository root:
|
||||
python3 -m unittest discover tools/tests
|
||||
"""
|
||||
import csv
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
sys.dont_write_bytecode = True
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import refresh # noqa: E402
|
||||
import season # noqa: E402
|
||||
import season_classify as C # noqa: E402
|
||||
import validate as V # noqa: E402
|
||||
import test_refresh as TR # noqa: E402
|
||||
|
||||
|
||||
def rec(**kw):
|
||||
base = {
|
||||
"hike": {
|
||||
"season": "June to September",
|
||||
"months": [6, 7, 8, 9],
|
||||
"source": [{"url": "https://www.example.org/trail",
|
||||
"publisher": "Example Publisher",
|
||||
"facts": ["season", "months"]}],
|
||||
}
|
||||
}
|
||||
base.update(kw)
|
||||
return base
|
||||
|
||||
|
||||
class SuggestBasisTest(unittest.TestCase):
|
||||
def test_official_site_host_is_primary(self):
|
||||
r = rec()
|
||||
r["hike"]["official_url"] = "https://example.org/trail-page"
|
||||
basis, reason = C.suggest_basis(r)
|
||||
self.assertEqual(basis, "primary")
|
||||
self.assertIn("official_url", reason)
|
||||
|
||||
def test_source_publisher_host_is_primary(self):
|
||||
r = rec(source={"publisher": "X", "publisher_url": "https://www.example.org/"})
|
||||
basis, reason = C.suggest_basis(r)
|
||||
self.assertEqual(basis, "primary")
|
||||
self.assertIn("publisher_url", reason)
|
||||
|
||||
def test_route_author_page_is_primary(self):
|
||||
r = rec(kind="route", route={"creator": "A Hiker"},
|
||||
guide=[{"title": "G", "url": "https://example.org/guide", "link_is": "author"}])
|
||||
basis, reason = C.suggest_basis(r)
|
||||
self.assertEqual(basis, "primary")
|
||||
self.assertIn("route author", reason)
|
||||
|
||||
def test_route_creator_words_host_is_primary(self):
|
||||
r = rec(kind="route", route={"creator": "A Hiker",
|
||||
"creator_words_url": "https://example.org/words"})
|
||||
basis, _ = C.suggest_basis(r)
|
||||
self.assertEqual(basis, "primary")
|
||||
|
||||
def test_two_unrelated_publishers_are_sources(self):
|
||||
r = rec()
|
||||
r["hike"]["source"] = [
|
||||
{"url": "https://a.example/t", "publisher": "Guide A", "facts": ["season"]},
|
||||
{"url": "https://b.example/t", "publisher": "Guide B", "facts": ["season", "months"]},
|
||||
]
|
||||
basis, reason = C.suggest_basis(r)
|
||||
self.assertEqual(basis, "sources")
|
||||
self.assertTrue(reason)
|
||||
|
||||
def test_one_unrelated_page_is_source(self):
|
||||
basis, reason = C.suggest_basis(rec())
|
||||
self.assertEqual(basis, "source")
|
||||
self.assertTrue(reason)
|
||||
|
||||
def test_www_and_bare_host_are_the_same_host(self):
|
||||
r = rec()
|
||||
r["hike"]["official_url"] = "https://example.org/x"
|
||||
r["hike"]["source"][0]["url"] = "https://www.example.org/y"
|
||||
self.assertEqual(C.suggest_basis(r)[0], "primary")
|
||||
|
||||
def test_non_route_guide_host_is_not_primary(self):
|
||||
r = rec(guide=[{"title": "G", "url": "https://example.org/guide", "link_is": "author"}])
|
||||
self.assertEqual(C.suggest_basis(r)[0], "source")
|
||||
|
||||
|
||||
class TableTest(unittest.TestCase):
|
||||
def test_table_covers_every_record_with_a_season(self):
|
||||
rows = C.table(V.ROOT)
|
||||
expected = 0
|
||||
for tid in refresh.trail_ids(V.ROOT):
|
||||
r = refresh.load_toml(os.path.join(V.ROOT, tid, "trail.toml"))
|
||||
if (r.get("hike") or {}).get("season"):
|
||||
expected += 1
|
||||
self.assertEqual(expected, 109)
|
||||
self.assertEqual(len(rows), expected)
|
||||
for row in rows:
|
||||
self.assertIn(row["suggested"], season.BASES)
|
||||
|
||||
def test_table_has_a_reason_for_every_row(self):
|
||||
for row in C.table(V.ROOT):
|
||||
self.assertTrue(row["reason"], row["id"])
|
||||
self.assertTrue(row["season"], row["id"])
|
||||
|
||||
|
||||
class ProposalsTest(unittest.TestCase):
|
||||
def test_a_proposal_collects_cleanly_and_applies(self):
|
||||
tmp = tempfile.mkdtemp()
|
||||
self.addCleanup(shutil.rmtree, tmp, True)
|
||||
root = TR.make_repo(tmp)
|
||||
path = os.path.join(root, "aaa", "trail.toml")
|
||||
text = TR.read(root, "aaa/trail.toml")
|
||||
text = text.replace('official_url = "https://example.org/aaa/"\n',
|
||||
'official_url = "https://example.org/aaa/"\n'
|
||||
'season = "June to September"\nmonths = [6, 7, 8, 9]\n', 1)
|
||||
text += ('\n[[hike.source]]\nurl = "https://example.org/aaa/season"\n'
|
||||
'publisher = "Example Club"\nfacts = ["season", "months"]\n')
|
||||
TR.write(path, text)
|
||||
d = os.path.join(tmp, "stage")
|
||||
os.makedirs(os.path.join(d, "season"))
|
||||
reviewed = os.path.join(d, "season", "classification.reviewed.csv")
|
||||
with open(reviewed, "w", newline="", encoding="utf-8") as f:
|
||||
w = csv.writer(f)
|
||||
w.writerow(["id", "basis", "note"])
|
||||
w.writerow(["aaa", "primary", ""])
|
||||
self.assertEqual(C.proposals(root, reviewed, d), 1)
|
||||
code, out = TR.main(root, "--dir", d, "collect")
|
||||
self.assertEqual(code, 0, out)
|
||||
coll = refresh.load_toml(os.path.join(d, "collected.toml"))
|
||||
props = coll["proposal"]
|
||||
self.assertEqual(len(props), 1)
|
||||
self.assertEqual(props[0]["status"], "proposed", out)
|
||||
with open(os.path.join(d, "owner.csv"), "w", newline="", encoding="utf-8") as f:
|
||||
w = csv.writer(f)
|
||||
w.writerow(["pid", "digest", "decision", "note"])
|
||||
w.writerow([props[0]["pid"], props[0]["digest"], "accept", ""])
|
||||
code, out = TR.main(root, "--dir", d, "collect")
|
||||
self.assertEqual(code, 0, out)
|
||||
code, out = TR.main(root, "--dir", d, "apply")
|
||||
self.assertEqual(code, 0, out)
|
||||
rec_after = refresh.load_toml(path)
|
||||
self.assertEqual(rec_after["hike"]["season_basis"], "primary")
|
||||
last = rec_after["history"][-1]
|
||||
self.assertEqual(last["fields"], ["hike.season_basis"])
|
||||
|
||||
def test_reviewed_rows_must_name_a_known_basis(self):
|
||||
tmp = tempfile.mkdtemp()
|
||||
self.addCleanup(shutil.rmtree, tmp, True)
|
||||
reviewed = os.path.join(tmp, "r.csv")
|
||||
with open(reviewed, "w", newline="", encoding="utf-8") as f:
|
||||
w = csv.writer(f)
|
||||
w.writerow(["id", "basis"])
|
||||
w.writerow(["aaa", "estimate"])
|
||||
with self.assertRaises(ValueError):
|
||||
C.read_reviewed(reviewed)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,33 @@
|
||||
"""Tests that season_basis and season_method are known fields and documented.
|
||||
|
||||
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 trail_history as H # noqa: E402
|
||||
import validate as V # noqa: E402
|
||||
|
||||
|
||||
class SeasonFieldsTest(unittest.TestCase):
|
||||
def test_the_new_fields_are_known_to_the_history_tool(self):
|
||||
for f in ("hike.season_basis", "hike.season_method"):
|
||||
self.assertIn(f, H.SCHEMA_FIELDS)
|
||||
self.assertTrue(H.is_fact(f))
|
||||
|
||||
def test_the_facts_text_names_them(self):
|
||||
self.assertIn("season_basis", H.FACTS_TEXT)
|
||||
|
||||
def test_the_schema_documents_them_and_the_estimate_exception(self):
|
||||
text = open(os.path.join(V.ROOT, "docs", "schema.md"), encoding="utf-8").read()
|
||||
for needle in ("`season_basis`", "`season_method`", "except season, which may be estimated"):
|
||||
self.assertIn(needle, text)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user