diff --git a/web/tests/golden/state-pages.json b/web/tests/golden/state-pages.json new file mode 100644 index 0000000..1863e2c --- /dev/null +++ b/web/tests/golden/state-pages.json @@ -0,0 +1,14 @@ +{ + "/regions/": "e70a9608ef0ee6a97d0591ad582abff1b165371dee5111e3277df06a53a554e1", + "/regions/atlantic-canada/": "6af5c4a79cac82eb0a4342297fd4f6755890e98597a98f219087c56432f06243", + "/regions/ca-on/": "12b5726d800090233f082a572cf1472f152552cef8fa5019f49b83e32e337eef", + "/regions/central-canada/": "fa6f8bbe466b5978f1edda22371b6aab8546efa567eee0d0405c717e278b8a3e", + "/regions/mountain-west/": "c079eb93a55c2279f24083983230057940eb032cb034057c822c52230ce22e05", + "/regions/north/": "fdfd201ae1e42b8fd17dd055815d4b06392a8ddb6ac3e72a06f15ec2b333fb56", + "/regions/us-co/": "45cb1b92fd22ffc35f82a26fe2c71b66836b0624bf0e195f9c4926dfab6a8900", + "/regions/us-co/loops/": "8883c9aac63f100196f9e853d6738a42b8199f1250c10ec34d1d0cfe8c998e7d", + "/regions/us-co/with-gpx/": "abc3f7e44cbcbf8d6cce3b5d5065e6e7c085ec5a4033842af8909077f87e1e18", + "/regions/us-hi/": "1e5681ffbd50e382db7d69528fde602069006bf0cddad494d817369224bd96ad", + "/regions/us-ut/": "065bcc01e14eb588dc1b118c466f44474fca0172c30037fb193d83a044624100", + "/regions/western-canada/": "91b40d35dd3d1a093d38d01bbfcf1ef536221a69631fc58e46732da81d0582c3" +} diff --git a/web/tests/make_golden.py b/web/tests/make_golden.py new file mode 100644 index 0000000..135b3c8 --- /dev/null +++ b/web/tests/make_golden.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +"""Regenerate web/tests/golden/state-pages.json from the fixtures in +test_state_pages_unchanged.py. Run only deliberately, and read the diff: + python3 web/tests/make_golden.py +""" +import json +import os +import sys + +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, HERE) + +from test_state_pages_unchanged import GOLDEN, build_map # noqa: E402 + +if __name__ == "__main__": + data = build_map() + os.makedirs(os.path.dirname(GOLDEN), exist_ok=True) + with open(GOLDEN, "w", encoding="utf-8") as f: + json.dump(data, f, indent=1, sort_keys=True) + f.write("\n") + print(f"wrote {GOLDEN}: {len(data)} pages") + for path in sorted(data): + print(" ", path) diff --git a/web/tests/test_state_pages_unchanged.py b/web/tests/test_state_pages_unchanged.py new file mode 100644 index 0000000..e76d354 --- /dev/null +++ b/web/tests/test_state_pages_unchanged.py @@ -0,0 +1,109 @@ +"""Guard: the built state, group and state-list pages must not change while place pages are added. + +Later ships may add a place-links nav to a state page and place names to its search text; +normalise() strips exactly those two things, and nothing else. Regenerate the golden only +deliberately: python3 web/tests/make_golden.py. +""" +import hashlib +import json +import os +import re +import sys +import unittest + +WEB = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, WEB) +sys.path.insert(0, HERE) + +from olt import bounds, facets, placefacets, regions, site # noqa: E402 +from test_hike import fake # noqa: E402 + +GOLDEN = os.path.join(HERE, "golden", "state-pages.json") + + +def normalise(html): + """Remove the two things later ships are allowed to change on a state page: the + data-search attributes (they gain place names) and the place-links nav block.""" + html = re.sub(r'\s*data-search="[^"]*"', "", html) + html = re.sub(r'', + "", html, flags=re.S) + return html + + +def fixtures(): + """12 fake records over Colorado, Utah and Ontario, plus one across all of Canada: + mixed trails and routes, loops, and tracks with a bbox as test_place_view sets.""" + specs = [ + # (regions, country, km, hike, route, line) + (["CO"], "US", 120, {"shape": "loop", "terrain": ["alpine"]}, False, True), + (["CO"], "US", 180, {"shape": "loop", "terrain": ["forest"]}, False, True), + (["CO", "UT"], "US", 260, {"shape": "loop", "terrain": ["desert"]}, False, False), + (["CO"], "US", 340, {"shape": "loop", "terrain": ["alpine"]}, True, True), + (["CO"], "US", 90, {"shape": "loop", "terrain": ["forest"]}, False, False), + (["CO"], "US", 800, {"shape": "point-to-point", "terrain": ["alpine"]}, False, True), + (["CO", "UT"], "US", 1500, {"shape": "point-to-point", "terrain": ["desert"]}, False, True), + (["UT"], "US", 210, {"shape": "point-to-point", "terrain": ["canyon"]}, True, False), + (["UT"], "US", 130, {"shape": "point-to-point", "terrain": ["desert"]}, False, True), + (["ON"], "CA", 420, {"shape": "point-to-point", "terrain": ["forest", "lakes"]}, False, True), + (["ON"], "CA", 110, {"shape": "point-to-point", "terrain": ["forest"]}, False, False), + (["all provinces and territories"], "CA", 3000, {"shape": "point-to-point", "terrain": ["forest"]}, False, True), + ] + records = [] + for i, (regs, country, km, hike, route, line) in enumerate(specs, 1): + rec = fake(i, km=km, hike=dict(hike, summary=f"A walk number {i}.", days=[7, 14]), + regions=regs, country=country, line=line) + if route: + rec["derived"]["kind"] = "route" + rec["route"] = {"creator": "A Walker", "year": 2010} + key = facets.place_keys(rec)[0] if facets.place_keys(rec) else None + box = bounds.place_box(key) if key and key != facets.ACROSS_CANADA else None + cx, cy = ((box[0] + box[2]) / 2, (box[1] + box[3]) / 2) if box else (-100.0, 45.0) + rec["derived"]["bbox"] = [cx - 0.3, cy - 0.2, cx + 0.3, cy + 0.2] + if line: + rec["derived"]["files"] = dict(rec["derived"].get("files", {}), line="x.gpx") + records.append(rec) + return records + + +def build_map(): + """{path: sha256(normalise(page))} for every state, group, hub, empty-place and + state-list page the fixtures produce.""" + records = fixtures() + s = site.Site("Long Hikes", "T", repo_url="https://github.com/o/r") + out = {} + + def add(path, page): + text = "\n".join(str(p) for p in page if isinstance(p, str) or hasattr(p, "__html__")) + out[path] = hashlib.sha256(normalise(text).encode()).hexdigest() + + by_slug, _ = regions.collect_regions(records) + pages = placefacets.plan(records) + by_place = placefacets.pages_by_place(pages) + for slug in sorted(by_slug): + facet_pages = [(placefacets.label(p["lst"]), p["path"], len(p["recs"])) + for p in by_place.get(slug, [])] + add(f"/regions/{slug}/", regions.region_page(slug, records, s, facet_pages)) + for g, _, _ in facets.PLACE_GROUPS: + page = regions.group_page(g, records, s) + if page: + add(regions.group_path(g), page) + add("/regions/", regions.regions_index_page(records, s)) + add("/regions/us-hi/", regions.empty_place_page("US-HI", records, s, [])) + for p in pages: + add(p["path"], placefacets.page(p, records, s, pages)) + return out + + +class StatePagesUnchangedTest(unittest.TestCase): + def test_every_state_group_and_state_list_page_is_byte_identical_after_normalising(self): + with open(GOLDEN, encoding="utf-8") as f: + want = json.load(f) + got = build_map() + differ = sorted(p for p in set(want) | set(got) if want.get(p) != got.get(p)) + self.assertEqual(differ, [], "pages that differ from the golden: " + ", ".join(differ)) + self.assertEqual(got, want) + + +if __name__ == "__main__": + unittest.main()