"""Tests for the views built from the records: the extra lists, the place-by-list pages, tables, rankings, authors, recent changes, linked trails, the glossary and the method page. Each view's selection rule is tested on small fixtures; the data files are checked against the real records. These do not need the built site. python3 -m unittest discover -s web/tests """ import os 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 (authors, browse, common, datafiles, facets, placefacets, records, reference, regions, site, standing, # noqa: E402 summary, views, words) from test_hike import fake # noqa: E402 SITE = site.Site("Long Hikes", "T", repo_url="https://github.com/o/r") def mi(n): """The kilometres of a length in miles, unrounded, so a band's edge falls where the rule puts it.""" return n * words.KM_PER_MI def keys(recs, key): return [r["id"] for r in browse.members(browse.by_key(key), recs)] class LengthBandTest(unittest.TestCase): def test_bands_split_at_the_mile_figures(self): recs = [fake(1, km=mi(99)), fake(2, km=mi(100)), fake(3, km=mi(199)), fake(4, km=mi(200)), fake(5, km=mi(499)), fake(6, km=mi(500)), fake(7, km=mi(999)), fake(8, km=mi(1000))] self.assertEqual(keys(recs, "under-100-miles"), ["t1"]) self.assertEqual(sorted(keys(recs, "100-to-200-miles")), ["t2", "t3"]) self.assertEqual(sorted(keys(recs, "200-to-500-miles")), ["t4", "t5"]) self.assertEqual(sorted(keys(recs, "500-to-1000-miles")), ["t6", "t7"]) self.assertEqual(keys(recs, "longest"), ["t8"]) def test_each_trail_is_in_exactly_one_band(self): bands = ("under-100-miles", "100-to-200-miles", "200-to-500-miles", "500-to-1000-miles", "longest") recs = [fake(i, km=km) for i, km in enumerate(range(50, 3000, 37))] for r in recs: self.assertEqual(sum(1 for b in bands if browse.by_key(b).test(r)), 1, r["length_km"]) def test_under_200_is_the_two_short_bands(self): recs = [fake(1, km=mi(50)), fake(2, km=mi(150)), fake(3, km=mi(250))] self.assertEqual(sorted(keys(recs, "under-200-miles")), ["t1", "t2"]) self.assertTrue(browse.by_key("under-200-miles").search_title(9).startswith("Thru-Hikes Under 200 Miles")) class TimeListTest(unittest.TestCase): def test_bands_by_days(self): stated = lambda i, days: fake(i, hike={"days": days}) # noqa: E731 recs = [stated(1, [2, 4]), stated(2, [4, 4]), stated(3, [5, 7]), stated(4, [20, 28]), stated(5, [30, 60])] self.assertEqual(sorted(keys(recs, "weekend")), ["t1", "t2"]) self.assertEqual(keys(recs, "two-to-four-weeks"), ["t4"]) self.assertEqual(keys(recs, "a-month-or-more"), ["t5"]) self.assertEqual(sorted(keys(recs, "a-week-or-less")), ["t1", "t2", "t3"]) def test_note_says_how_many_are_stated_and_how_the_rest_were_estimated(self): recs = [fake(1, km=100, hike={"days": [3, 4]}), fake(2, km=100), fake(3, km=100)] note = browse.time_note(recs, recs) self.assertIn("A source states the time for 1 of these", note) self.assertIn("For the other 2 it is estimated from the length", note) self.assertIn("12 to 19 miles a day", note) self.assertIn("7 to 12", note) self.assertIn("all 1", browse.time_note(recs[:1], recs)) self.assertIn("No source states the time", browse.time_note(recs[1:], recs)) class KindListTest(unittest.TestCase): def test_high_routes_by_name_or_designation(self): a, b, c, d = fake(1), fake(2), fake(3), fake(4) a["name"] = "Alpine High Route" b["name"] = "Uinta Highline Trail" # a trail named Highline is not a high route c["common_name"] = "Big High Route" d["designation"] = "Person-defined high route" self.assertEqual(keys([a, b, c, d], "high-routes"), ["t1", "t3", "t4"]) def test_mostly_cross_country_and_unfinished(self): recs = [fake(1, hike={"travel": "mostly-cross-country"}), fake(2, hike={"travel": "some-cross-country"}), fake(3, hike={"unfinished": "Half is built."}), fake(4)] self.assertEqual(keys(recs, "mostly-cross-country"), ["t1"]) self.assertEqual(keys(recs, "off-trail"), ["t1", "t2"]) self.assertEqual(keys(recs, "unfinished"), ["t3"]) def test_holders_come_from_the_designation_alone(self): nrt, pc, fs, sp, club, route = (fake(i) for i in range(1, 7)) nrt["designation"] = "National Recreation Trail (certified 2008), managed by the Bureau of Land Management" pc["designation"] = "Parks Canada, Jasper National Park" fs["designation"] = "National Recreation Trail #31 (Forest Service, Tonto National Forest)" sp["designation"] = "BC Parks (Naikoon Provincial Park)" club["designation"] = "Keystone Trails Association" route["designation"] = "Person-defined route (Jo); no agency or association" route["kind"] = "route" route["derived"]["kind"] = "route" recs = [nrt, pc, fs, sp, club, route] self.assertEqual(keys(recs, "national-recreation-trails"), ["t1", "t3"]) self.assertEqual(keys(recs, "parks-canada"), ["t2"]) self.assertEqual(keys(recs, "forest-service"), ["t3"]) self.assertEqual(keys(recs, "state-and-provincial-parks"), ["t4"]) # a route that says it has no association is not kept by one self.assertEqual(keys(recs, "clubs-and-associations"), ["t5"]) class MonthListTest(unittest.TestCase): def test_a_month_holds_the_trails_whose_months_include_it(self): recs = [fake(1, hike={"months": [6, 7]}), fake(2, hike={"months": [8]}), fake(3)] self.assertEqual(keys(recs, "hike-in-july"), ["t1"]) self.assertEqual(keys(recs, "hike-in-august"), ["t2"]) def test_a_month_needs_eight_trails_and_says_how_many_have_a_season(self): july = browse.by_key("hike-in-july") self.assertEqual(july.min_size, 8) self.assertFalse(browse.is_shown(7, 100, july.min_size)) self.assertTrue(browse.is_shown(8, 100, july.min_size)) recs = [fake(i, hike={"months": [7]}) for i in range(10)] + [fake(20 + i) for i in range(10)] shown = {lst.key for lst, _ in browse.shown(recs)} self.assertIn("hike-in-july", shown) self.assertNotIn("hike-in-august", shown) note = july.note(browse.members(july, recs), recs) self.assertIn("Only 10 of the 20 trails have a season on file", note) def test_every_list_has_a_group_and_every_group_a_heading(self): headings = {k for k, _, _ in browse.GROUPS} for lst in browse.LISTS: self.assertIn(lst.group, headings, lst.key) def test_titles_are_unique_and_short_enough(self): titles = [lst.search_title(99) for lst in browse.LISTS] self.assertEqual(len(titles), len(set(titles))) for t in titles: self.assertLessEqual(len(t), common.TITLE_LIMIT, t) class PlaceFacetTest(unittest.TestCase): def setUp(self): # Eight trails in Arizona: six under 100 miles, three of them loops; two in Utah. self.recs = [fake(i, km=mi(50), regions=["AZ"], hike={"shape": "loop" if i < 3 else "point-to-point"}) for i in range(6)] self.recs += [fake(6, km=mi(150), regions=["AZ"]), fake(7, km=mi(150), regions=["AZ"])] self.recs += [fake(8, km=mi(50), regions=["UT"]), fake(9, km=mi(50), regions=["UT"])] # enough trails elsewhere that the lists are shown (no more than 75% of all trails) self.recs += [fake(20 + i, km=mi(400), regions=["NY"]) for i in range(12)] def pages(self): return placefacets.plan(self.recs) def test_a_page_needs_five_trails_in_the_place(self): pages = self.pages() self.assertTrue(pages) for p in pages: self.assertGreaterEqual(len(p["recs"]), browse.MIN_SIZE) self.assertNotIn("ut", {p["slug"].split("-")[1] for p in pages}) # two trails: no page def test_a_page_that_repeats_the_whole_place_is_left_out(self): for p in self.pages(): self.assertLessEqual(len(p["recs"]), browse.MAX_SHARE * len(p["info"]["records"])) def test_three_loops_are_too_few(self): self.assertNotIn("loops", {p["lst"].key for p in self.pages()}) def test_a_set_that_repeats_another_page_of_the_place_is_kept_once(self): # the six short trails are the same set under two lists: only one page for them sets = [frozenset(r["id"] for r in p["recs"]) for p in self.pages() if p["slug"] == "us-az"] self.assertEqual(len(sets), len(set(sets))) def test_title_and_path(self): p = self.pages()[0] self.assertEqual(p["path"], f"/regions/{p['slug']}/{p['lst'].key}/") title, desc, body, _ = placefacets.page(p, self.recs, SITE, self.pages()) self.assertIn("Arizona", title) self.assertIn(f"{len(p['recs'])}", title) self.assertEqual(str(body).count("