"""Tests for the site's taxonomy and vocabulary (olt/facets.py and the pages that use it): each attribute's name and values, the time estimate, the place grouping, filtering with missing values and the coverage threshold, one word for one thing on every browsing page, and the URLs the live site has always had. The fixture tests need no built site; BuiltVocabularyTest builds it (as test_pages does). """ import html import os import re import sys import unittest WEB = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ROOT = os.path.dirname(WEB) sys.path.insert(0, WEB) sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from olt import facets, home, records, trail, words # noqa: E402 from test_hike import GH, fake, text, top_part # noqa: E402 import builtsite # noqa: E402 OUT = builtsite.OUT def route(i=1, policy="sold", **kw): rec = fake(i, **kw) rec["kind"] = "route" rec["derived"]["kind"] = "route" rec["route"] = {"creator": "Andrew Skurka", "year": 2018, "line_policy": policy} rec["guide"] = [{"title": "Guide", "author": "Andrew Skurka", "access": "purchase", "url": "https://guide.example/", "format": "book"}] return rec class AttributeTest(unittest.TestCase): def test_names(self): self.assertEqual((facets.WHERE, facets.LENGTH, facets.TIME, facets.SEASON, facets.TERRAIN, facets.KIND, facets.WALKING, facets.SHAPE, facets.GPX), ("Where", "Length", "Time", "Season", "Terrain", "Kind", "Walking", "Shape", "GPX")) def test_values(self): self.assertEqual(list(facets.TERRAIN_VALUES), ["alpine", "forest", "desert", "coast", "prairie", "canyon", "tundra", "lakes", "farmland"]) self.assertEqual(facets.WALKING_VALUES, {"trail": "On trail", "trail-and-road": "Trail and road", "some-cross-country": "Some cross-country", "mostly-cross-country": "Mostly cross-country"}) self.assertEqual(facets.SHAPES, {"point-to-point": "End to end", "loop": "Loop", "network": "Network"}) self.assertEqual(facets.KINDS, {"trail": "Official trail", "route": "Route"}) self.assertEqual([label for _, label, _ in facets.TIME_BANDS], ["A week or less", "One to two weeks", "Two to four weeks", "One to three months", "More than three months"]) self.assertEqual(facets.ROUTE_DEFINITION, "A route is a trail that a hiker worked out and published. No agency or association " "looks after it as one trail, and there may be no signs for it.") def test_kind_and_route_line(self): self.assertEqual(facets.kind(fake(1)), "trail") self.assertEqual(facets.route_by(fake(1)), "") r = route() self.assertEqual(facets.kind(r), "route") self.assertEqual(facets.route_by(r), "Route by Andrew Skurka, 2018") del r["route"]["year"] self.assertEqual(facets.route_by(r), "Route by Andrew Skurka") def test_walking_is_not_kind(self): # a route on maintained trail says so; an official trail can have road walks r = route(hike={"travel": "trail"}) self.assertEqual(facets.walking(r), "trail") t = fake(2, hike={"travel": "trail-and-road"}) self.assertEqual(facets.kind(t), "trail") self.assertEqual(facets.walking(t), "trail-and-road") page = text(top_part(trail.render(r, GH)[2])) self.assertIn("Walking On trail", page) self.assertIn("Route by Andrew Skurka, 2018", page) self.assertIn("What is a route?", page) self.assertIn(facets.ROUTE_DEFINITION, page) def test_gpx_status(self): cases = [(fake(1), "elsewhere"), (route(policy="open"), "free"), (route(policy="sold"), "sold"), (route(policy="withheld"), "unpublished"), (route(policy="unclear"), "elsewhere")] for rec, want in cases: self.assertEqual(facets.gpx(rec), want, rec.get("route")) from test_hike import with_line partial = with_line(fake(1)) self.assertEqual(facets.gpx(partial), "partial") whole = with_line(fake(1)) whole["verification"]["difference_percent"] = -2.0 self.assertEqual(facets.gpx(whole), "here") self.assertEqual(set(facets.GPX_STATUS.values()), {"GPX to download here", "Partial GPX to download here", "Free from its author", "Sold by its author", "Not published", "Not here, see the official site"}) def test_designation_badge_only_for_nst(self): self.assertIn("National Scenic Trail", home.entry(fake(1, tier="national"))) for tier in ("regional", "short", "deferred", "candidate"): self.assertNotIn("tag--", home.entry(fake(1, tier=tier)), tier) class TimeTest(unittest.TestCase): def test_stated(self): rec = fake(1, hike={"days": [150, 210]}) self.assertEqual(facets.time_days(rec), (150, 210, False)) self.assertEqual(facets.time_text(rec), "5 to 7 months") self.assertEqual(facets.estimate_note(rec), "") self.assertEqual(facets.time_band(rec), "long") def test_estimate_from_length(self): rec = fake(1, km=300) # 300 km at 20 to 30 km a day: 10 to 15 days self.assertEqual(facets.time_days(rec), (10, 15, True)) self.assertEqual(facets.time_text(rec), "About 1 to 2 weeks") note = facets.estimate_note(rec) # miles first: both in the HTML, CSS orders them self.assertTrue(note.startswith("Estimated from the length, at ")) self.assertLess(note.index('12 to 19 mi'), note.index('20 to 30 km')) self.assertTrue(note.endswith(" a day.")) self.assertEqual(facets.time_band(rec), "two-weeks") # cross-country is slower: 12 to 20 km a day rec = fake(2, km=300, hike={"travel": "mostly-cross-country"}) self.assertEqual(facets.time_days(rec), (15, 25, True)) self.assertIn("12 to 20 km", facets.estimate_note(rec)) self.assertIn("7 to 12 mi", facets.estimate_note(rec)) self.assertEqual(facets.time_band(rec), "month") def test_every_trail_has_a_time_and_its_label(self): for rec in records.load_records(ROOT): lo, hi, est = facets.time_days(rec) self.assertTrue(0 < lo <= hi, rec["id"]) self.assertIn(facets.time_band(rec), facets.TIME_LABEL, rec["id"]) self.assertEqual(bool(facets.estimate_note(rec)), est, rec["id"]) row = text(home.entry(fake(1, km=300))) self.assertIn("About 1 to 2 weeks estimate", row) self.assertNotIn("estimate", text(home.entry(fake(1, hike={"days": [4, 6]})))) def test_bands(self): self.assertEqual(facets.time_band(fake(1, hike={"days": [5, 8]})), "week") # middle 6.5 days self.assertEqual(facets.time_band(fake(1, hike={"days": [7, 21]})), "two-weeks") self.assertEqual(facets.time_band(fake(1, hike={"days": [20, 35]})), "month") class PlaceTest(unittest.TestCase): def test_every_code_in_exactly_one_group(self): keys = [k for _, _, ks in facets.PLACE_GROUPS for k in ks] self.assertEqual(len(keys), len(set(keys))) every = {f"US-{c}" for c in words.US_STATES} | {f"CA-{c}" for c in words.CA_PROVINCES} self.assertEqual(set(keys), every) def test_every_code_the_records_use(self): for rec in records.load_records(ROOT): keys = facets.place_keys(rec) self.assertTrue(keys, rec["id"]) for key in keys: self.assertTrue(key in facets.GROUP_OF or key == facets.ACROSS_CANADA, f"{rec['id']}: {key}") self.assertTrue(facets.place_groups(rec), rec["id"]) def test_tokens(self): rec = fake(1, regions=["AZ", "NM"]) self.assertEqual(facets.where_tokens(rec), ["US", "US-AZ", "US-NM", "g:southwest"]) across = fake(2, regions=[words.ALL_CANADA], country="CA") self.assertEqual(facets.place_keys(across), ["CA-*"]) self.assertEqual(set(facets.place_groups(across)), {"north", "western-canada", "central-canada", "atlantic-canada"}) # a US trail that names a province is in that province's group ect = fake(3, regions=["ME", "QC", "NB"]) self.assertEqual(facets.place_keys(ect), ["US-ME", "CA-QC", "CA-NB"]) self.assertIn("atlantic-canada", facets.place_groups(ect)) def test_breadcrumb_by_place(self): body = trail.render(fake(1, regions=["CO"]), GH)[2] crumbs = body[body.index('