Files
longhikes/tools/tests/test_links.py
T
swaitsandClaude Fable 5.2 5c716c3c62 Check LongTrailsWiki's lists, link to its pages, drop five non-trails
LongTrailsWiki. Its maintainer pointed out its List of Long Trails and
its backlog, the List of Trails to Review. Every entry in the United
States and Canada on both was checked against the inclusion rule: 188
entries on the first, 573 on the second. Seven trails that meet the
rule and were missing are added: Prescott Circle, High Country Pathway,
Guadalupe Ridge, Toiyabe Crest and General George Crook trails, and the
Condor Trail and Mojave-Sonoran Trail as routes. Nothing is copied from
the wiki, whose content is share-alike: it tells us a trail exists, and
each record's facts come from the pages it names.

Links. A record can now carry any number of [[link]] tables for other
pages about the trail, each with a title, a publisher and an address.
The official site stays in hike.official_url. 97 trails link to their
page on LongTrailsWiki. The validator checks the new table.

Credit. The README gains "Other lists of long trails", naming the lists
this dataset has been checked against, LongTrailsWiki and OpenLongTrails
first.

Removed, as not walking trails: The Great Trail (Trans Canada Trail, a
30,000 km network of greenways, waterways and roads), the Great Western
Trail (a motor route), and the Iditarod, Nez Perce and Overmountain
Victory National Historic Trails. Each was marked undecided, and its
own note said why it did not belong.

From saved copies of sites that refuse automated reads: the Black
Canyon National Recreation Trail and the Montana Trail are added;
missing facts are filled on six trails; and two facts are corrected
from the holders' own pages (the Colorado Trail's high point is
13,271 ft, and the East Coast Trail is one linear trail, not a network).

The dataset now holds 182 trails, 102 with a track.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-14 23:52:47 -07:00

64 lines
2.4 KiB
Python

"""Tests for the [[link]] checks in tools/validate.py: no network, no numpy.
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
WIKI = {"title": "Appalachian Trail", "publisher": "LongTrailsWiki",
"url": "https://www.longtrailswiki.net/wiki/Appalachian_Trail"}
def check(rec):
errors = []
V.check_links("t", rec, errors)
return errors
class LinkTest(unittest.TestCase):
def test_no_link_is_valid(self):
self.assertEqual(check({}), [])
def test_good_links_are_valid(self):
other = {"title": "Appalachian Trail", "publisher": "Wikipedia",
"url": "https://en.wikipedia.org/wiki/Appalachian_Trail", "note": "A short history."}
self.assertEqual(check({"link": [WIKI, other]}), [])
def test_each_required_field(self):
for field in ("title", "publisher", "url"):
link = {k: v for k, v in WIKI.items() if k != field}
self.assertTrue(any(f"missing {field}" in e for e in check({"link": [link]})), field)
self.assertTrue(check({"link": [dict(WIKI, **{field: " "})]}), field)
def test_url_must_be_http(self):
errors = check({"link": [dict(WIKI, url="ftp://example.org/a")]})
self.assertTrue(any("http(s)" in e for e in errors))
def test_unknown_key(self):
self.assertTrue(any("unknown field rank" in e for e in check({"link": [dict(WIKI, rank=1)]})))
def test_duplicate_url(self):
twin = dict(WIKI, title="Again", url="http://www.longtrailswiki.net/wiki/Appalachian_Trail/")
self.assertTrue(any("repeats" in e for e in check({"link": [WIKI, twin]})))
def test_official_url_and_publisher_url_are_not_links(self):
rec = {"hike": {"official_url": "https://www.nps.gov/appa/index.htm"},
"source": {"publisher_url": "https://example.org/trail"}}
for url in ("https://www.nps.gov/appa/index.htm/", "http://example.org/trail"):
errors = check(dict(rec, link=[dict(WIKI, url=url)]))
self.assertTrue(any("official site" in e for e in errors), url)
def test_single_table_is_not_an_array(self):
self.assertTrue(any("array of tables" in e for e in check({"link": dict(WIKI)})))
if __name__ == "__main__":
unittest.main()