"""Tests for the added pages: regions, routes, sources, logos, about, data, SEO files, the bulk archives and the response headers.""" import html import json import os import re import subprocess import sys import unittest import zipfile WEB = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ROOT = os.path.dirname(WEB) sys.path.insert(0, WEB) import serve # noqa: E402 from olt import common, docs, facets, home, records, regions as regions_mod, seo, site, words # noqa: E402 OUT = os.path.join(WEB, ".cache", "test-site") def setUpModule(): os.makedirs(os.path.dirname(OUT), exist_ok=True) subprocess.run([sys.executable, os.path.join(WEB, "build.py"), "--out", OUT], check=True, stdout=subprocess.DEVNULL) def read_out(relpath): with open(os.path.join(OUT, *relpath.split("/")), encoding="utf-8") as f: return f.read() def entry_ids(doc): return re.findall(r'
  • {slug}") self.assertTrue(os.path.isfile(os.path.join(OUT, "regions", "index.html"))) def test_each_record_on_each_region_page(self): for rec in self.records: for code in rec.get("regions", []): if code == words.ALL_CANADA: page = read_out("regions/index.html") else: slug = words.region_key(code, rec["country"]).lower() page = read_out(f"regions/{slug}/index.html") self.assertIn(f'href="/trails/{rec["id"]}/"', page, f'{rec["id"]} in {code}') def test_all_routes_on_routes_page(self): routes = [r for r in self.records if r["derived"]["kind"] == "route"] self.assertEqual(len(routes), 58) page = read_out("routes/index.html") import html as _html text = _html.unescape(re.sub(r"<[^>]+>", "", page)) # a credit naming two authors links each name for r in routes: self.assertIn(f'href="/trails/{r["id"]}/"', page, r["id"]) self.assertIn(r["route"]["creator"], text, r["id"]) def test_routes_page_author_count_is_the_one_on_the_authors_page(self): """The Routes page's visible line, its meta description and its intro say the same number of authors, and it is the number of authors listed on /authors/ (one page each).""" import html as _html from olt import authors n = len(authors.author_pages(self.records, ROOT)) page = read_out("routes/index.html") visible = re.search(r'

    \d+ routes? by (\d+) authors?', page) meta = re.search(r']+>", "", page))) self.assertTrue(visible and meta and intro) described = re.search(r"by (\d+) authors", _html.unescape(meta.group(1))) self.assertEqual(int(visible.group(1)), n) self.assertEqual(int(described.group(1)), n) self.assertEqual(int(intro.group(1)), n) index = read_out("authors/index.html") self.assertIn(f"worked out by {n} authors", index) listed = {m for m in re.findall(r'href="(/authors/[^/"]+/)"', index)} self.assertEqual(len(listed), n) def test_ways_into_the_authors_pages_are_routes(self): """A route's row names its author as a link to their page (each named person who has one); the Routes page head counts routes and authors and links /authors/; /lists/ lists Route authors beside Routes; the trail page links the author where it names them.""" import html as _html from olt import authors routes = [r for r in self.records if r["derived"]["kind"] == "route"] gs = authors.author_pages(self.records, ROOT) links = authors.link_map(self.records, ROOT) row = re.compile(r'

    (.*?)

    ') home = read_out("index.html") by_line = {m for m in row.findall(home)} for r in routes: for name, href in links.get(r["route"]["creator"], []): self.assertIn(f'{_html.escape(name)}', "".join(by_line), r["id"]) # a credit with two people links each; a credit with no page stays plain self.assertTrue(any(len(v) > 1 for v in links.values())) self.assertEqual(len(gs), len({n for v in links.values() for n, _ in v})) # Routes page head page = read_out("routes/index.html") head = page[:page.index('
    ')] self.assertIn(f'

    {len(routes)} routes by {len(gs)} authors

    ', head) # Lists index: in the group Routes is in lists = read_out("lists/index.html") group = lists[lists.index('id="group-standing"'):] group = group[:group.index("")] self.assertIn('Routes', group) self.assertIn('Route authors', group) # the trail page links the author's name in its head and its Author fact for r in routes: for name, href in links.get(r["route"]["creator"], []): trail_page = read_out(f"trails/{r['id']}/index.html") self.assertGreaterEqual(trail_page.count(f''), 2, r["id"]) def test_every_logo_on_logos_page(self): page = read_out("logos/index.html") for r in self.records: if "shield" in r["derived"]["files"]: self.assertIn(f'href="/trails/{r["id"]}/"', page, r["id"]) self.assertIn("no-logo-exists", page) self.assertIn("logo-not-found", page) def test_added_pages_exist(self): for rel in ("sources/index.html", "about/index.html", "data/index.html", "data/schema/index.html", "data/changelog/index.html", "licences/LICENSE", "licences/MIT.txt", "robots.txt"): self.assertTrue(os.path.isfile(os.path.join(OUT, *rel.split("/"))), rel) def test_no_dead_internal_links_in_added_pages(self): import re for rel in ("regions/index.html", "routes/index.html", "lists/index.html", "sources/index.html", "logos/index.html", "about/index.html", "data/index.html", "data/schema/index.html", "data/changelog/index.html"): html = read_out(rel) for url in re.findall(r'(?:href|src)="(/[^"]*)"', html): path = url.split("#", 1)[0].split("?", 1)[0] if not path: continue if path.endswith("/"): path += "index.html" target = os.path.join(OUT, *path.lstrip("/").split("/")) self.assertTrue(os.path.isfile(target), f"{rel} -> {url}") def test_region_map_limited_to_region_ids(self): page = read_out("regions/us-vt/index.html") self.assertIn('data-map="overview"', page) self.assertIn("data-ids=", page) self.assertIn("long-trail", page) def test_home_region_links_point_to_region_pages(self): page = read_out("index.html") self.assertIn('href="/regions/us-vt/"', page) def test_where_counts_match_the_place_pages(self): home_doc = read_out("index.html") by_slug, across = regions_mod.collect_regions(self.records) # each state or province option's count is the number of trails on its place page found = re.findall(r'', home_doc) self.assertEqual({v for v, _ in found}, {i["key"] for i in by_slug.values()}) for value, n in found: page = read_out(f"regions/{value.lower()}/index.html") self.assertEqual(int(n), len(entry_ids(page)), value) # and the rows that say they are there self.assertEqual(int(n), len(re.findall(rf'data-where="[^"]*\b{value}\b', home_doc)), value) for country, label in (("US", "United States"), ("CA", "Canada")): n = int(re.search(rf'', home_doc).group(1)) self.assertEqual(n, sum(1 for r in self.records if country in common.countries(r)), country) for g, name, _ in facets.PLACE_GROUPS: m = re.search(rf'', home_doc) n = sum(1 for r in self.records if g in facets.place_groups(r)) self.assertEqual(int(m.group(1)) if m else 0, n, g) # No trail spans a whole country now that the Trans Canada Trail is gone; # the "across" section appears only when one does. self.assertEqual('id="across-canada"' in read_out("regions/index.html"), bool(across)) self.assertEqual('id="canada-wide"' in read_out("regions/index.html"), bool(across)) # the old anchor def test_list_pages_share_the_home_list(self): for rel in ("index.html", "regions/us-co/index.html", "routes/index.html", "lists/national-scenic-trails/index.html"): doc = read_out(rel) results = doc[doc.index('