Download tests/test_directive.py from Beemer0/CanLex: direct link, hf CLI and curl.
- Browser
- Download file 3.9 kB
-
https://huggingface.co/spaces/Beemer0/CanLex/resolve/main/tests/test_directive.py
- Command line
-
hf download hf://spaces/Beemer0/CanLex/tests/test_directive.py
-
curl -L -o test_directive.py https://huggingface.co/spaces/Beemer0/CanLex/resolve/main/tests/test_directive.py
3.9 kB
| """Unit tests for the NJC directive ingester (canlex/directive.py). Offline only. | |
| The ingester is a full rebuild, so anything a run fails to parse disappears from | |
| directives.json -- the shape of the 2026-07-27 D-memo loss, one directive at a | |
| time. These cover which directives get their last-good chunks back, and the | |
| act_code identity that decision hangs on. | |
| """ | |
| import unittest | |
| from canlex import directive | |
| class PreserveFailedTests(unittest.TestCase): | |
| STORED = [{"act_code": "d4", "text": "relocation"}, | |
| {"act_code": "d4", "text": "part two"}, | |
| {"act_code": "fsd-dse", "text": "foreign service"}] | |
| def test_unparseable_directive_keeps_its_last_good_chunks(self): | |
| failures = [("d4", "NJC Relocation Directive", "no content parsed")] | |
| chunks, preserved = directive.preserve_failed([], failures, self.STORED) | |
| self.assertEqual(preserved, ["d4"]) | |
| self.assertEqual(len(chunks), 2) | |
| def test_freshly_parsed_chunks_win_over_stored(self): | |
| fresh = [{"act_code": "d4", "text": "new text"}] | |
| failures = [("d4", "NJC Relocation Directive", "404")] | |
| chunks, preserved = directive.preserve_failed(fresh, failures, | |
| self.STORED) | |
| self.assertEqual((chunks, preserved), (fresh, [])) | |
| def test_directive_dropped_from_the_index_is_not_resurrected(self): | |
| # Not in this run and not a failure: the NJC retired it on purpose. | |
| chunks, preserved = directive.preserve_failed([], [], self.STORED) | |
| self.assertEqual((chunks, preserved), ([], [])) | |
| def test_directive_we_never_stored_cannot_be_preserved(self): | |
| failures = [("d99", "Brand New Directive", "404")] | |
| chunks, preserved = directive.preserve_failed([], failures, self.STORED) | |
| self.assertEqual((chunks, preserved), ([], [])) | |
| def test_a_retitled_directive_is_still_matched(self): | |
| # Identity is the URL slug, so an NJC reword of the index title (which | |
| # is all `failures` used to carry) does not lose the stored chunks. | |
| failures = [("fsd-dse", "Foreign Service Directives (2026 edition)", | |
| "no content parsed")] | |
| chunks, preserved = directive.preserve_failed([], failures, self.STORED) | |
| self.assertEqual(preserved, ["fsd-dse"]) | |
| self.assertEqual([c["text"] for c in chunks], ["foreign service"]) | |
| def test_each_failed_directive_is_restored_once(self): | |
| failures = [("d4", "Relocation", "404"), | |
| ("fsd-dse", "Foreign Service Directives", "404")] | |
| chunks, preserved = directive.preserve_failed([], failures, self.STORED) | |
| self.assertEqual(preserved, ["d4", "fsd-dse"]) | |
| self.assertEqual(len(chunks), 3) | |
| class FailureIdentityTests(unittest.TestCase): | |
| """The failure tuple's code must be the one parse_directive stamps on | |
| chunks, or preservation silently matches nothing.""" | |
| URL = "https://www.njc-cnm.gc.ca/directive/d99/en" | |
| HTML = ("<main><h2>1.1 Purpose</h2><p>The purpose of this directive.</p>" | |
| "</main>") | |
| def test_parsed_chunks_carry_the_code_from_the_url(self): | |
| chunks = directive.parse_directive(self.HTML, self.URL, "Test Directive", | |
| "2026-01-01") | |
| self.assertTrue(chunks) | |
| self.assertEqual({c["act_code"] for c in chunks}, {"d99"}) | |
| def test_the_ingest_loop_derives_the_same_code(self): | |
| code = directive._CODE.search(self.URL).group(1) | |
| chunks = directive.parse_directive(self.HTML, self.URL, "Test Directive", | |
| "") | |
| stored = [dict(c) for c in chunks] | |
| _chunks, preserved = directive.preserve_failed( | |
| [], [(code, "Test Directive", "404")], stored) | |
| self.assertEqual(preserved, [code]) | |
| if __name__ == "__main__": | |
| unittest.main() | |