GMBA draws the Adirondacks by relief and its outline stops about 21 km east of Cranberry Lake, so the 80 km Cranberry Lake 50 loop, inside the Adirondack Park, was in no range. The cause is the source's outline, not the trail: an allow-list entry can now push a range's outline out (grow_km, with grow_clip to keep it off ground it should not reach), and the Adirondack Mountains use it so the loop is placed without a rule for one trail. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
530 lines
27 KiB
Python
530 lines
27 KiB
Python
"""Tests for tools/place_trails.py. Needs the tool's venv (.venv-places); with the system
|
|
Python the whole file is skipped, not failed. No network: synthetic outlines and tracks.
|
|
|
|
.venv-places/bin/python -m unittest discover tools/tests -p "test_place_trails.py"
|
|
"""
|
|
import json
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
sys.dont_write_bytecode = True
|
|
TOOLS = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
ROOT = os.path.dirname(TOOLS)
|
|
sys.path.insert(0, TOOLS)
|
|
|
|
try:
|
|
import shapely # noqa: F401
|
|
HAVE = True
|
|
except ImportError:
|
|
HAVE = False
|
|
|
|
if HAVE:
|
|
from shapely.geometry import LineString, MultiLineString, Polygon
|
|
import check_places as C
|
|
import place_trails as P
|
|
|
|
# At latitude 43, 0.0063 degrees of latitude is about 0.70 km.
|
|
BOX = None # built in setUp when shapely is there
|
|
|
|
|
|
def write_track(d, name, lines):
|
|
feats = [{"type": "Feature", "properties": {},
|
|
"geometry": {"type": "LineString", "coordinates": line}} for line in lines]
|
|
with open(os.path.join(d, name), "w") as f:
|
|
json.dump({"type": "FeatureCollection", "features": feats}, f)
|
|
|
|
|
|
def write_record(d, line_file="line.geojson"):
|
|
with open(os.path.join(d, "trail.toml"), "w") as f:
|
|
f.write(f'id = "x"\n[source]\nline_file = "{line_file}"\n')
|
|
|
|
|
|
@unittest.skipUnless(HAVE, "shapely (the .venv-places venv) is not installed")
|
|
class GeometryTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.box = Polygon([(-110.0, 43.0), (-109.0, 43.0), (-109.0, 44.0), (-110.0, 44.0)])
|
|
|
|
def test_pieces_inside_are_summed(self):
|
|
parts = [LineString([(-109.5 + 0.1 * i, 43.5), (-109.5 + 0.1 * i, 43.5063)]) for i in range(3)]
|
|
km = P.km_inside(MultiLineString(parts), self.box)
|
|
self.assertAlmostEqual(km, 2.1, delta=0.15)
|
|
self.assertGreater(km, P.MIN_KM)
|
|
|
|
def test_a_skimming_track_is_under_the_threshold(self):
|
|
# about 1.5 km inside
|
|
track = LineString([(-109.5, 43.5), (-109.5, 43.5135)])
|
|
km = P.km_inside(track, self.box)
|
|
self.assertAlmostEqual(km, 1.5, delta=0.1)
|
|
self.assertLess(km, P.MIN_KM)
|
|
|
|
def test_a_hole_is_not_inside(self):
|
|
hole = [(-109.6, 43.4), (-109.4, 43.4), (-109.4, 43.6), (-109.6, 43.6)]
|
|
outline = Polygon(self.box.exterior.coords, [hole])
|
|
track = LineString([(-109.5, 43.45), (-109.5, 43.55)])
|
|
self.assertEqual(P.km_inside(track, outline), 0.0)
|
|
|
|
def test_alternates_are_ignored(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
write_record(d)
|
|
write_track(d, "line.geojson", [[[-108.0, 43.5], [-108.0, 43.51]]]) # outside
|
|
write_track(d, "alternates.geojson", [[[-109.5, 43.1], [-109.5, 43.9]]]) # long, inside
|
|
track = P.main_track(d)
|
|
self.assertIsNotNone(track)
|
|
self.assertEqual(P.km_inside(track, self.box), 0.0)
|
|
|
|
def test_a_record_with_no_line_file_has_no_track(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
write_record(d, line_file="")
|
|
self.assertIsNone(P.main_track(d))
|
|
|
|
def test_box_of_is_west_south_east_north_rounded(self):
|
|
outline = Polygon([(-110.004, 42.501), (-108.499, 42.501), (-108.499, 43.499), (-110.004, 43.499)])
|
|
self.assertEqual(P.box_of(outline), [-110.0, 42.5, -108.5, 43.5])
|
|
|
|
def test_track_hash_matches_the_validators(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
write_record(d)
|
|
write_track(d, "line.geojson", [[[-109.5, 43.5], [-109.4, 43.5]]])
|
|
self.assertEqual(P.track_hash(d), C.track_hash(os.path.join(d, "line.geojson")))
|
|
|
|
|
|
def outline(kind, source_id, name, geom, slug=None, hint=()):
|
|
return P.Outline(kind=kind, source="nps", source_id=source_id, name=name,
|
|
state_codes_hint=list(hint), geometry=geom, slug=slug or P.slugify(name))
|
|
|
|
|
|
def rec(rid, regions, track):
|
|
return {"id": rid, "name": rid, "regions": regions, "track": track}
|
|
|
|
|
|
@unittest.skipUnless(HAVE, "shapely (the .venv-places venv) is not installed")
|
|
class ProposalTest(unittest.TestCase):
|
|
def setUp(self):
|
|
# Two overlapping places; vertical tracks of about 10 km inside each.
|
|
self.park = outline("park", "P1", "Alpha Park",
|
|
Polygon([(-110, 43), (-109, 43), (-109, 44), (-110, 44)]), hint=["WY"])
|
|
self.range = outline("range", "R1", "Beta Range",
|
|
Polygon([(-109.6, 43), (-108.6, 43), (-108.6, 44), (-109.6, 44)]))
|
|
self.inside = LineString([(-109.5, 43.2), (-109.5, 43.29)]) # in both, ~10 km
|
|
self.park_only = LineString([(-109.9, 43.2), (-109.9, 43.29)])
|
|
self.outside = LineString([(-107.0, 43.2), (-107.0, 43.29)])
|
|
|
|
def test_propose_lists_per_trail_places_and_per_place_trails(self):
|
|
records = [rec("a", ["WY"], self.inside), rec("b", ["WY"], self.park_only),
|
|
rec("c", ["WY"], self.inside)]
|
|
p = P.propose(records, [self.park, self.range])
|
|
self.assertEqual({k for k, _ in p.per_trail["a"]}, {("park", "alpha-park"), ("range", "beta-range")})
|
|
self.assertEqual({t for t, _ in p.per_place[("park", "alpha-park")].trails}, {"a", "b", "c"})
|
|
self.assertEqual({t for t, _ in p.per_place[("range", "beta-range")].trails}, {"a", "c"})
|
|
|
|
def test_a_trail_under_the_threshold_is_not_in_the_place(self):
|
|
skim = LineString([(-109.5, 43.5), (-109.5, 43.509)]) # ~1.0 km
|
|
p = P.propose([rec("a", ["WY"], skim)], [self.park])
|
|
self.assertEqual(p.per_trail["a"], [])
|
|
self.assertNotIn(("park", "alpha-park"), p.per_place)
|
|
|
|
def test_places_with_fewer_than_three_trails_are_marked_no_page(self):
|
|
records = [rec("a", ["WY"], self.inside), rec("b", ["WY"], self.park_only),
|
|
rec("c", ["WY"], self.inside)]
|
|
p = P.propose(records, [self.park, self.range])
|
|
self.assertTrue(p.per_place[("park", "alpha-park")].page)
|
|
self.assertFalse(p.per_place[("range", "beta-range")].page)
|
|
|
|
def test_near_threshold_items_are_listed_first_and_flagged(self):
|
|
near = LineString([(-109.5, 43.5), (-109.5, 43.5153)]) # ~1.7 km: in, but near the line
|
|
p = P.propose([rec("a", ["WY"], near), rec("b", ["WY"], self.inside)], [self.park])
|
|
self.assertEqual(p.near[0].trail, "a")
|
|
self.assertTrue(any(abs(km - P.MIN_KM) <= 0.5 for _, km in p.per_trail["a"]))
|
|
|
|
def test_where_is_the_source_states_not_the_visiting_trails_states(self):
|
|
# trails from other states visit the place: where stays the place's own
|
|
records = [rec("a", ["WY"], self.park_only), rec("b", ["WY", "CO"], self.park_only),
|
|
rec("c", ["OR", "WA"], self.park_only), rec("d", ["UT"], self.park_only)]
|
|
p = P.propose(records, [self.park])
|
|
self.assertEqual(p.per_place[("park", "alpha-park")].where, ["WY"])
|
|
|
|
def test_where_of_a_source_with_states_uses_them_and_drops_unknown_codes(self):
|
|
o = outline("park", "P9", "Edge Park", self.park.geometry, hint=["MT", "ID", "PR"])
|
|
self.assertEqual(P.where_of(o), ["ID", "MT"])
|
|
|
|
def test_state_codes_reads_the_separators_the_sources_use(self):
|
|
self.assertEqual(P.state_codes("ID-MT-WY"), ["ID", "MT", "WY"])
|
|
self.assertEqual(P.state_codes("AR/OK"), ["AR", "OK"])
|
|
self.assertEqual(P.state_codes("FL 6/"), ["FL"])
|
|
self.assertEqual(P.state_codes("SAMOA"), [])
|
|
self.assertEqual(P.state_codes(None), [])
|
|
|
|
def test_where_of_a_range_is_found_from_the_state_boxes(self):
|
|
# wholly inside the Wyoming box, clear of every other state's box
|
|
o = outline("range", "R5", "Wyoming Range", self.park.geometry)
|
|
o.state_codes_hint = []
|
|
self.assertEqual(P.where_of(o), ["WY"])
|
|
|
|
def test_where_of_a_place_in_two_states_lists_both(self):
|
|
both = Polygon([(-112.0, 40.0), (-110.0, 40.0), (-110.0, 42.0), (-112.0, 42.0)])
|
|
o = outline("range", "R6", "Border Range", both)
|
|
self.assertEqual(P.where_of(o), ["UT", "WY"])
|
|
|
|
def test_a_hand_where_overrides_the_source_and_the_boxes(self):
|
|
o = outline("range", "R7", "Hand Range", self.park.geometry, hint=["WY"])
|
|
o.where_hand = ("WY", "MT")
|
|
self.assertEqual(P.where_of(o), ["MT", "WY"])
|
|
|
|
def test_yosemite_like_outline_is_california_only(self):
|
|
yose = Polygon([(-120.0, 37.5), (-119.2, 37.5), (-119.2, 38.2), (-120.0, 38.2)])
|
|
o = outline("park", "YOSE", "Yosemite National Park", yose, hint=["CA"])
|
|
self.assertEqual(P.where_of(o), ["CA"])
|
|
|
|
def test_one_name_in_a_kind_and_source_is_one_place_with_both_ids(self):
|
|
a = outline("range", "17222", "Palisades", self.park.geometry)
|
|
b = outline("range", "19588", "Palisades", self.range.geometry)
|
|
c = outline("range", "5", "Other Range", self.range.geometry)
|
|
out = P.merge_same_name([a, b, c])
|
|
self.assertEqual([o.name for o in out], ["Palisades", "Other Range"])
|
|
self.assertEqual(out[0].source_id, ["17222", "19588"])
|
|
self.assertAlmostEqual(out[0].geometry.area,
|
|
self.park.geometry.union(self.range.geometry).area)
|
|
out = P.assign_slugs(out)
|
|
self.assertEqual(out[0].slug, "palisades") # no numeric suffix, no state code
|
|
|
|
def test_the_same_name_from_two_sources_or_kinds_is_not_merged(self):
|
|
a = outline("park", "1", "Glacier", self.park.geometry, hint=["MT"])
|
|
b = outline("park", "2", "Glacier", self.range.geometry, hint=["BC"])
|
|
b.source = "cpcad"
|
|
w = outline("wilderness", "3", "Glacier", self.range.geometry)
|
|
self.assertEqual(len(P.merge_same_name([a, b, w])), 3)
|
|
|
|
def test_two_far_apart_wilderness_areas_of_one_name_stay_two_places(self):
|
|
far = Polygon([(-80, 35), (-79, 35), (-79, 36), (-80, 36)])
|
|
a = outline("wilderness", "1", "Bear Wilderness", self.park.geometry, hint=["WY"])
|
|
b = outline("wilderness", "2", "Bear Wilderness", far, hint=["NC"])
|
|
self.assertEqual(len(P.merge_same_name([a, b])), 2)
|
|
touching = outline("wilderness", "3", "Bear Wilderness", self.range.geometry, hint=["WY"])
|
|
self.assertEqual(len(P.merge_same_name([a, touching])), 1)
|
|
|
|
def test_a_wilderness_with_a_parks_exact_trails_and_a_matching_name_is_not_listed(self):
|
|
wild = outline("wilderness", "W1", "Alpha Wilderness", self.park.geometry)
|
|
wild.source = "wilderness-connect"
|
|
records = [rec("a", ["WY"], self.park_only), rec("b", ["WY"], self.park_only),
|
|
rec("c", ["WY"], self.park_only)]
|
|
p = P.propose(records, [self.park, wild])
|
|
self.assertIn(("park", "alpha-park"), p.per_place)
|
|
self.assertNotIn(("wilderness", "alpha-wilderness"), p.per_place)
|
|
self.assertTrue(all(k[0] != "wilderness" for k, _ in p.per_trail["a"]))
|
|
self.assertEqual([n for n in p.near if n.key[0] == "wilderness"], [])
|
|
|
|
def test_the_joined_park_and_its_hyphenated_wilderness_are_one_page(self):
|
|
sets = {("park", "sequoia-and-kings-canyon"): {"a", "b", "c"},
|
|
("wilderness", "sequoia-kings-canyon-wilderness"): {"a", "b", "c"},
|
|
("wilderness", "john-muir-wilderness"): {"a", "b", "c"}}
|
|
names = {("park", "sequoia-and-kings-canyon"): "Sequoia and Kings Canyon",
|
|
("wilderness", "sequoia-kings-canyon-wilderness"): "Sequoia-Kings Canyon Wilderness",
|
|
("wilderness", "john-muir-wilderness"): "John Muir Wilderness"}
|
|
self.assertEqual(P.doubled_places(sets, names), {("wilderness", "sequoia-kings-canyon-wilderness")})
|
|
|
|
def test_a_wilderness_with_more_or_fewer_trails_than_the_park_is_kept(self):
|
|
names = {("park", "alpha"): "Alpha National Park", ("wilderness", "alpha-wilderness"): "Alpha Wilderness"}
|
|
sets = {("park", "alpha"): {"a", "b"}, ("wilderness", "alpha-wilderness"): {"a"}}
|
|
self.assertEqual(P.doubled_places(sets, names), set())
|
|
sets[("wilderness", "alpha-wilderness")] = {"a", "b", "c"}
|
|
self.assertEqual(P.doubled_places(sets, names), set())
|
|
|
|
def test_a_check_counts_stated_trails_too_when_it_finds_a_doubled_page(self):
|
|
names = {("park", "alpha"): "Alpha National Park", ("wilderness", "alpha-wilderness"): "Alpha Wilderness"}
|
|
tracked = {("park", "alpha"): {"a", "b"}, ("wilderness", "alpha-wilderness"): {"a", "b"}}
|
|
self.assertEqual(len(P.doubled_places(tracked, names)), 1)
|
|
stated = {("park", "alpha"): {"a", "b", "z"}, ("wilderness", "alpha-wilderness"): {"a", "b"}}
|
|
self.assertEqual(P.doubled_places(stated, names), set())
|
|
|
|
def test_a_denied_place_is_not_in_the_catalogue(self):
|
|
arch = outline("park", "JEFF", "Gateway Arch National Park", self.park.geometry)
|
|
keep = outline("park", "YOSE", "Yosemite National Park", self.range.geometry)
|
|
self.assertEqual([o.source_id for o in P.apply_denylist([arch, keep])], ["YOSE"])
|
|
self.assertTrue(all(reason.strip() for reason in P.DENY.values()))
|
|
self.assertIn(("park", "nps", "JEFF"), P.DENY)
|
|
|
|
def test_the_cpcad_query_asks_for_ontario_and_quebec_parks_and_escapes_quotes(self):
|
|
where = P.cpcad_where()
|
|
self.assertNotIn("LOC IN", where) # no province is left out by a filter on location
|
|
self.assertIn("'Provincial Park'", where) # Ontario's type
|
|
self.assertIn("'Quebec''s national park'", where) # Quebec's type, quote doubled
|
|
self.assertIn("'A - Park'", where) # British Columbia's
|
|
|
|
def test_cpcad_names_are_cleaned_for_a_hiker(self):
|
|
self.assertEqual(P._clean_cpcad_name("Algonquin Provincial Park (Natural Environment Class)"),
|
|
"Algonquin Provincial Park")
|
|
self.assertEqual(P._clean_cpcad_name("Frontenac National Park (Québec)"),
|
|
"Frontenac National Park")
|
|
self.assertEqual(P._clean_cpcad_name("Killarney Lakelands And Headwaters Provincial Park"),
|
|
"Killarney Lakelands and Headwaters Provincial Park")
|
|
|
|
def test_a_grown_outline_reaches_a_track_just_outside_it_but_only_inside_its_clip(self):
|
|
# about 20 km east of the park's edge (-109.0): a track the source's outline missed
|
|
east = LineString([(-108.75, 43.2), (-108.75, 43.29)])
|
|
self.assertEqual(P.propose([rec("a", ["WY"], east)], [self.park]).per_trail["a"], [])
|
|
grown = P.grow_outline(self.park.geometry, 25)
|
|
self.assertGreater(grown.area, self.park.geometry.area)
|
|
self.assertTrue(grown.contains(east))
|
|
# a clip keeps the growth off ground the owner did not mean: nothing east of -108.9
|
|
clipped = P.grow_outline(self.park.geometry, 25, clip=[-111.0, 42.0, -108.9, 45.0])
|
|
self.assertFalse(clipped.intersects(east))
|
|
self.assertTrue(clipped.contains(self.park.geometry))
|
|
|
|
def test_a_slug_collision_inside_a_kind_gets_the_state_code(self):
|
|
a = outline("park", "P1", "Glacier", self.park.geometry, slug=None, hint=["MT"])
|
|
b = outline("park", "P2", "Glacier", self.range.geometry, slug=None, hint=["BC"])
|
|
out = P.assign_slugs([a, b])
|
|
self.assertEqual({o.slug for o in out}, {"glacier-mt", "glacier-bc"})
|
|
|
|
def test_the_proposal_names_every_trail_with_no_track_as_needing_a_person(self):
|
|
p = P.propose([rec("a", ["WY"], self.inside), rec("z", ["WY"], None)], [self.park])
|
|
self.assertEqual(p.no_track, ["z"])
|
|
with tempfile.TemporaryDirectory() as d:
|
|
path = os.path.join(d, "proposal.md")
|
|
P.render_proposal(p, path)
|
|
text = open(path).read()
|
|
self.assertIn("needs a person", text)
|
|
self.assertIn("z", text)
|
|
|
|
def test_a_parent_range_is_not_stored_when_a_child_is(self):
|
|
big = outline("range", "R0", "Big Range",
|
|
Polygon([(-111, 42), (-108, 42), (-108, 45), (-111, 45)]))
|
|
big.path = "Continent > Big Range"
|
|
self.range.path = "Continent > Big Range > Beta Range"
|
|
p = P.propose([rec("a", ["WY"], self.inside)], [big, self.range])
|
|
self.assertEqual({k for k, _ in p.per_trail["a"]}, {("range", "beta-range")})
|
|
# the parent's page count still holds the trail, as measured
|
|
self.assertEqual({t for t, _ in p.per_place[("range", "big-range")].trails}, {"a"})
|
|
|
|
def test_one_gmba_id_outside_the_allow_list_is_not_proposed_but_is_listed_by_candidates(self):
|
|
records = [rec(f"t{i}", ["WY"], self.inside) for i in range(3)]
|
|
all_ranges = [dict(gmba_id="R1", name="Beta Range", path="Beta Range",
|
|
geometry=self.range.geometry)]
|
|
p = P.propose(records, []) # nothing allow-listed
|
|
self.assertEqual(p.per_place, {})
|
|
found = P.range_candidates(records, all_ranges, allowlisted=set())
|
|
self.assertEqual(len(found), 1)
|
|
self.assertEqual(found[0]["gmba_id"], "R1")
|
|
self.assertEqual(len(found[0]["trails"]), 3)
|
|
|
|
|
|
import re as _re
|
|
import tomllib as _tomllib
|
|
|
|
|
|
@unittest.skipUnless(HAVE, "shapely (the .venv-places venv) is not installed")
|
|
class AllowlistTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.al = P.load_allowlist(P.default_allowlist_path())
|
|
self.by_slug = {}
|
|
for r in self.al["range"]:
|
|
self.by_slug.setdefault(r["slug"], []).append(r)
|
|
|
|
def test_the_reviewed_list_has_the_kept_and_the_named_ranges(self):
|
|
slugs = set(self.by_slug)
|
|
for must in ("rocky-mountains", "appalachian-mountains", "palisades", "great-western-divide",
|
|
"wind-river-range", "teton-range", "uinta-mountains", "wasatch-range",
|
|
"san-juan-mountains", "sawtooth-range", "beartooth-mountains", "absaroka-range",
|
|
"bitterroot-range", "great-smoky-mountains", "canadian-rockies",
|
|
"olympic-mountains", "white-mountains", "bighorn-mountains",
|
|
"sangre-de-cristo-mountains", "wallowa-mountains", "ruby-mountains", "trinity-alps",
|
|
"selkirk-mountains", "brooks-range", "alaska-range", "ouachita-mountains",
|
|
"catskill-mountains"):
|
|
self.assertIn(must, slugs)
|
|
self.assertNotIn("ozark-mountains", slugs) # the source has only the Ozark Plateau
|
|
self.assertEqual(self.by_slug["white-mountains"][0]["gmba_id"], "12331")
|
|
|
|
def test_at_most_two_kept_levels_apply_to_a_trail(self):
|
|
for r in self.al["range"]:
|
|
for parent in r.get("within") or []:
|
|
self.assertIn(parent, self.by_slug, f"{r['slug']} names an unknown parent")
|
|
for grand in self.by_slug[parent][0].get("within") or []:
|
|
self.fail(f"{r['slug']} > {parent} > {grand} is three levels")
|
|
|
|
def test_a_slug_is_on_one_name_and_display_names_carry_no_source_qualifier(self):
|
|
for slug, rows in self.by_slug.items():
|
|
self.assertEqual(len({r["name"] for r in rows}), 1, slug)
|
|
for r in self.al["range"]:
|
|
self.assertNotIn("(", r["name"], r["slug"])
|
|
|
|
def test_a_park_entry_carries_no_where_it_was_not_given(self):
|
|
glacier = [p for p in self.al["park"] if p["source_id"] == "GLAC"][0]
|
|
self.assertNotIn("where", glacier) # the source's own state field says MT
|
|
self.assertEqual(glacier["slug"], "glacier")
|
|
|
|
def test_every_range_has_its_states_where_a_hand_value_is_given_and_codes_are_known(self):
|
|
known = set(P.state_boxes())
|
|
for r in self.al["range"]:
|
|
for c in r.get("where") or []:
|
|
self.assertIn(c, known, r["slug"])
|
|
|
|
|
|
def make_repo(root):
|
|
"""Three tracked records and one trackless, minimal trail.toml files."""
|
|
for rid, line in (("aa", True), ("bb", True), ("cc", True), ("zz", False)):
|
|
d = os.path.join(root, rid)
|
|
os.makedirs(d)
|
|
with open(os.path.join(d, "trail.toml"), "w") as f:
|
|
f.write(f'id = "{rid}"\nname = "{rid} Trail"\nregions = ["WY"]\n\n'
|
|
f'[source]\nline_file = "{"line.geojson" if line else ""}"\n\n'
|
|
'[[history]]\ndate = "2026-01-01"\nchange = "The record was added."\n'
|
|
'fields = ["record"]\nbasis = "The record\'s own source entry."\n'
|
|
'asked_by = "the dataset\'s own check"\noutcome = "applied"\n')
|
|
if line:
|
|
write_track(d, "line.geojson", [[[-109.5, 43.2], [-109.5, 43.29]]])
|
|
with open(os.path.join(root, "places.toml"), "w") as f:
|
|
f.write("# The place registry (test).\n")
|
|
|
|
|
|
def read_toml(path):
|
|
with open(path, "rb") as f:
|
|
return _tomllib.load(f)
|
|
|
|
|
|
@unittest.skipUnless(HAVE, "shapely (the .venv-places venv) is not installed")
|
|
class ApplyTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.root = self.tmp.name
|
|
make_repo(self.root)
|
|
self.park = outline("park", "P1", "Alpha Park",
|
|
Polygon([(-110, 43), (-109, 43), (-109, 44), (-110, 44)]), hint=["WY"])
|
|
track = LineString([(-109.5, 43.2), (-109.5, 43.29)])
|
|
records = [rec("aa", ["WY"], track), rec("bb", ["WY"], track),
|
|
rec("cc", ["WY"], track), rec("zz", ["WY"], None)]
|
|
self.p = P.propose(records, [self.park])
|
|
self.p.versions = {"nps": "2026-11-20"}
|
|
self.pdir = os.path.join(self.root, "out")
|
|
P.render_proposal(self.p, os.path.join(self.pdir, "proposal.md"))
|
|
self.pjson = os.path.join(self.pdir, "proposal.json")
|
|
|
|
def tearDown(self):
|
|
self.tmp.cleanup()
|
|
|
|
def trail_toml(self, rid):
|
|
return os.path.join(self.root, rid, "trail.toml")
|
|
|
|
def test_apply_adds_place_tables_stamp_and_hash_by_line_edits(self):
|
|
before = open(self.trail_toml("aa")).read()
|
|
written = P.apply(self.pjson, self.root, "2026-11-20")
|
|
self.assertIn("aa", written)
|
|
after = open(self.trail_toml("aa")).read()
|
|
data = read_toml(self.trail_toml("aa"))
|
|
self.assertEqual(data["places_checked"], "2026-11-20")
|
|
self.assertEqual(data["places_track"],
|
|
C.track_hash(os.path.join(self.root, "aa", "line.geojson")))
|
|
self.assertEqual(data["place"][0]["slug"], "alpha-park")
|
|
# everything that was there before is still there, byte for byte, as lines
|
|
for line in before.splitlines():
|
|
if line.strip():
|
|
self.assertIn(line, after.splitlines(), line)
|
|
|
|
def test_apply_adds_one_history_entry_naming_place_with_no_distance_in_it(self):
|
|
P.apply(self.pjson, self.root, "2026-11-20")
|
|
data = read_toml(self.trail_toml("aa"))
|
|
self.assertEqual(len(data["history"]), 2)
|
|
e = data["history"][-1]
|
|
self.assertEqual(e["fields"], ["place"])
|
|
self.assertEqual(e["asked_by"], "the dataset's own check")
|
|
self.assertTrue(e["basis_url"].startswith("http"))
|
|
self.assertIsNone(_re.search(r"\d\s*(km|mi)", e["change"] + e["basis"]))
|
|
|
|
def test_a_rerun_that_only_changes_km_adds_no_history_and_keeps_the_stamp_date_rule(self):
|
|
P.apply(self.pjson, self.root, "2026-11-20")
|
|
data = json.load(open(self.pjson))
|
|
data["per_trail"]["aa"][0]["km"] += 1.25
|
|
data["per_place"]["park/alpha-park"]["trails"][0]["km"] += 1.25
|
|
p2 = os.path.join(self.pdir, "proposal2.json")
|
|
json.dump(data, open(p2, "w"))
|
|
P.apply(p2, self.root, "2026-12-01")
|
|
rec = read_toml(self.trail_toml("aa"))
|
|
self.assertEqual(len(rec["history"]), 2) # no new entry
|
|
self.assertEqual(rec["places_checked"], "2026-12-01")
|
|
self.assertAlmostEqual(rec["place"][0]["km"], data["per_trail"]["aa"][0]["km"], delta=0.06)
|
|
|
|
def test_a_rerun_that_adds_or_removes_a_place_adds_history(self):
|
|
P.apply(self.pjson, self.root, "2026-11-20")
|
|
data = json.load(open(self.pjson))
|
|
data["per_trail"]["aa"] = []
|
|
p2 = os.path.join(self.pdir, "proposal2.json")
|
|
json.dump(data, open(p2, "w"))
|
|
P.apply(p2, self.root, "2026-12-01")
|
|
rec = read_toml(self.trail_toml("aa"))
|
|
self.assertEqual(len(rec["history"]), 3)
|
|
self.assertNotIn("place", rec)
|
|
|
|
def test_registry_lists_only_named_places_and_is_sorted(self):
|
|
P.apply(self.pjson, self.root, "2026-11-20")
|
|
reg = read_toml(os.path.join(self.root, "places.toml"))["place"]
|
|
self.assertEqual([e["slug"] for e in reg], ["alpha-park"])
|
|
self.assertEqual(reg[0]["source_version"], "2026-11-20")
|
|
self.assertEqual(reg[0]["box"], [-110.0, 43.0, -109.0, 44.0])
|
|
|
|
def test_a_reviewed_where_in_the_registry_survives_a_rerun(self):
|
|
P.apply(self.pjson, self.root, "2026-11-20")
|
|
path = os.path.join(self.root, "places.toml")
|
|
text = open(path).read()
|
|
self.assertIn('where = ["WY"]', text)
|
|
open(path, "w").write(text.replace('where = ["WY"]', 'where = ["MT", "WY"]'))
|
|
P.apply(self.pjson, self.root, "2026-12-01")
|
|
self.assertEqual(read_toml(path)["place"][0]["where"], ["MT", "WY"])
|
|
|
|
def test_stated_requires_an_http_source_a_quote_and_a_trackless_record(self):
|
|
P.apply(self.pjson, self.root, "2026-11-20")
|
|
with self.assertRaises(ValueError):
|
|
P.add_stated(self.root, "zz", "park", "alpha-park", "a book", "It says so.", "2026-11-20")
|
|
with self.assertRaises(ValueError):
|
|
P.add_stated(self.root, "zz", "park", "alpha-park", "https://example.org/", "", "2026-11-20")
|
|
with self.assertRaises(ValueError):
|
|
P.add_stated(self.root, "aa", "park", "alpha-park", "https://example.org/", "Q.", "2026-11-20")
|
|
P.add_stated(self.root, "zz", "park", "alpha-park", "https://example.org/t",
|
|
"The trail crosses Alpha Park.", "2026-11-20")
|
|
rec = read_toml(self.trail_toml("zz"))
|
|
self.assertEqual(rec["place"][0]["basis"], "stated")
|
|
self.assertEqual(rec["places_checked"], "2026-11-20")
|
|
self.assertEqual(rec["history"][-1]["fields"], ["place"])
|
|
|
|
def test_stamp_none_sets_places_checked_with_no_place_tables_and_no_history(self):
|
|
P.stamp_none(self.root, "zz", "2026-11-20")
|
|
rec = read_toml(self.trail_toml("zz"))
|
|
self.assertEqual(rec["places_checked"], "2026-11-20")
|
|
self.assertNotIn("place", rec)
|
|
self.assertEqual(len(rec["history"]), 1)
|
|
|
|
def test_check_reports_a_place_gained_lost_a_moved_boundary_and_a_place_crossing_three_trails(self):
|
|
P.apply(self.pjson, self.root, "2026-11-20")
|
|
records = P.load_records(self.root)
|
|
moved = outline("park", "P1", "Alpha Park",
|
|
Polygon([(-110, 43), (-109, 43), (-109, 43.25), (-110, 43.25)]), hint=["WY"])
|
|
diffs = P.check(self.root, None, outlines=[moved], versions={"nps": "2026-11-20"},
|
|
records=records)
|
|
text = "\n".join(diffs)
|
|
self.assertIn("measurement changed", text) # the moved boundary
|
|
gone = outline("park", "P9", "Elsewhere Park",
|
|
Polygon([(-100, 43), (-99, 43), (-99, 44), (-100, 44)]), hint=["WY"])
|
|
diffs = P.check(self.root, None, outlines=[gone], versions={"nps": "2026-11-20"},
|
|
records=records)
|
|
text = "\n".join(diffs)
|
|
self.assertIn("lost park/alpha-park", text)
|
|
self.assertIn("crossed the 3-trail line", text)
|
|
|
|
def test_check_exits_nonzero_with_differences_and_zero_without(self):
|
|
P.apply(self.pjson, self.root, "2026-11-20")
|
|
records = P.load_records(self.root)
|
|
self.assertEqual(P.check(self.root, None, outlines=[self.park],
|
|
versions={"nps": "2026-11-20"}, records=records), [])
|
|
|
|
def test_the_written_records_pass_check_places(self):
|
|
P.apply(self.pjson, self.root, "2026-11-20")
|
|
P.stamp_none(self.root, "zz", "2026-11-20")
|
|
trails = [read_toml(self.trail_toml(r)) for r in ("aa", "bb", "cc", "zz")]
|
|
errors = []
|
|
C.check(self.root, trails, errors)
|
|
self.assertEqual(errors, [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|