"""Tests for the added pages: regions, routes, sources, logos, about, data, SEO files, the bulk archives and the response headers.""" import html 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), 51) page = read_out("routes/index.html") import html as _html text = _html.unescape(page) for r in routes: self.assertIn(f'href="/trails/{r["id"]}/"', page, r["id"]) self.assertIn(r["route"]["creator"], text, 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('