"""The "History of this record" section of a trail page, and the permission block in the credit box.""" import html import os import re import sys import unittest WEB = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, WEB) sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from olt import records, site, trail, words # noqa: E402 from test_hike import with_line # noqa: E402 from test_pages import sample_record # noqa: E402 S = site.Site("N", "T", repo_url="https://github.com/o/r") QUOTE = "It's completely public and free to share." ADDED = {"date": "2026-11-01", "change": "The record was added.", "fields": ["record"], "basis": "The record's own source entry: Example Trail Association.", "asked_by": "the dataset's own check", "outcome": "applied"} CHANGED = {"date": "2026-11-12", "change": "The length was changed to 160 miles (257 km).", "fields": ["length_km", "length_note"], "basis": "The association's page for the trail gives 160 miles.", "basis_url": "https://example.org/trail?a=1&b=2", "asked_by": "the trail association's president", "outcome": "applied"} DECLINED = {"date": "2026-11-14", "change": "A request to shorten it to 150 miles (241 km) was not applied.", "fields": ["length_km"], "basis": "The association's own page still says 160 miles.", "basis_url": "https://github.com/o/r/issues/9", "asked_by": "a hiker", "outcome": "declined"} def with_history(*entries): rec = sample_record("Example Trail") rec["history"] = [dict(e) for e in entries] return rec def flat(text): return re.sub(r"\s+", " ", html.unescape(re.sub(r"<[^>]+>", " ", text))) def permission_record(): rec = with_line(sample_record("Example Trail")) rec["source"].update({"licence": "stated-permission", "attribution": "Example Trail Association", "licence_text": "The association's president answered a form on the issue tracker.", "permission_quote": QUOTE, "permission_by": "the association's president", "permission_date": "2026-11-16", "permission_url": "https://github.com/o/r/issues/2"}) return rec class HistorySectionTest(unittest.TestCase): def section(self, *entries): return trail.history_section(with_history(*entries)) def test_collapsed_section_beside_about_this_data(self): body = trail.render(with_history(ADDED, CHANGED), S)[2] self.assertIn('
', body) self.assertNotIn("
History of this record", body) self.assertLess(body.index('
'), body.index("History of this record")) self.assertEqual(body.count("History of this record"), 1) def test_one_entry(self): part = self.section(ADDED, CHANGED) self.assertIn('
  • ', part) self.assertIn('', part) self.assertIn('

    The length was changed to 160 miles (257 km).

    ', part) self.assertIn('

    Basis: ' "The association's page for the trail gives 160 miles. " 'https://example.org/trail?a=1&b=2

    ', part) self.assertIn('

    Asked by: ' "the trail association's president

    ", part) self.assertIn("
  • length_km
  • length_note
  • ", part) def test_entries_run_oldest_first_as_the_record_has_them(self): part = self.section(ADDED, CHANGED, DECLINED) self.assertLess(part.index("2026-11-01"), part.index("2026-11-12")) self.assertLess(part.index("2026-11-12"), part.index("2026-11-14")) self.assertIn("3 entries", part) self.assertIn("1 entry", self.section(ADDED)) def test_declined_request_has_a_plain_label_and_applied_has_none(self): part = self.section(ADDED, DECLINED) self.assertIn('
  • ', part) self.assertEqual(part.count("Request declined"), 1) declined = part[part.index("history__entry--declined"):] self.assertIn('Request declined', declined) self.assertNotIn("Request declined", part[:part.index("history__entry--declined")]) def test_no_address_means_no_link(self): part = self.section(ADDED) self.assertNotIn("", part) self.assertIn("<script>", part) self.assertIn("& <b>", part) def test_a_record_with_no_history_has_no_section(self): rec = sample_record("Example Trail") self.assertEqual(trail.history_section(rec), "") self.assertNotIn("History of this record", trail.render(rec, S)[2]) def test_it_is_generated_from_the_record_alone(self): words_in_page = flat(self.section(ADDED, CHANGED, DECLINED)) for label in ("History of this record", "Basis:", "Asked by:", "Request declined"): self.assertIn(label, words_in_page) self.assertEqual(self.section(ADDED, CHANGED), self.section(ADDED, CHANGED)) def test_the_source_block_is_not_changed_by_it(self): self.assertNotIn("record-history", trail.about_data(with_history(ADDED), S)) def test_real_records_all_show_their_history(self): recs = records.load_records() self.assertEqual(len(recs), 199) for rec in recs: part = trail.history_section(rec) self.assertIn("History of this record", part, rec["id"]) self.assertEqual(part.count('
  • ', part) class CreditBoxPermissionTest(unittest.TestCase): def credit_box(self, rec): page = trail.render(rec, S)[2] start = page.index('
    ') return page[start:page.index("", start)] def test_the_credit_box_shows_the_quotation_who_when_and_a_link(self): box = self.credit_box(permission_record()) self.assertIn('

    It's completely public and free to share.

    ', box) self.assertIn("Said by the association's president on 2026-11-16.", box) self.assertIn('
    https://github.com/o/r/issues/2', box) def test_the_sentence_is_true_where_it_stands(self): box = self.credit_box(permission_record()) self.assertIn("The statement is quoted below.", box) self.assertLess(box.index("The statement is quoted below."), box.index("