| import contextlib |
| import io |
| import unittest |
| from unittest import mock |
|
|
| from sec_parser import sec_parser as sp |
|
|
|
|
| class NormalizeTextMarkupTests(unittest.TestCase): |
| def test_valid_utf8_bypasses_detwingle(self): |
| payload = "Revenue — 2025".encode("utf-8") |
|
|
| with mock.patch.object( |
| sp.UnicodeDammit, |
| "detwingle", |
| side_effect=AssertionError("detwingle should not be called for valid utf-8"), |
| ): |
| normalized = sp.normalize_text_markup(payload) |
|
|
| self.assertEqual(normalized, "Revenue — 2025") |
|
|
| def test_invalid_utf8_still_uses_detwingle_path(self): |
| payload = b'\x93quoted\x94' |
|
|
| with mock.patch.object(sp.UnicodeDammit, "detwingle", wraps=sp.UnicodeDammit.detwingle) as detwingle: |
| normalized = sp.normalize_text_markup(payload) |
|
|
| self.assertTrue(detwingle.called) |
| self.assertIn("quoted", normalized) |
|
|
|
|
| class DebugPrintGatingTests(unittest.TestCase): |
| def test_parse_html_filing_stage_debug_is_off_by_default(self): |
| html = "<html><body><p>Hello world</p></body></html>" |
| stdout = io.StringIO() |
|
|
| with mock.patch.dict("os.environ", {}, clear=False): |
| with mock.patch.object(sp, "is_document_layout_positioned", return_value=False): |
| with mock.patch.object(sp, "parse_positioned_html_islands_via_ocr", return_value=(None, False)): |
| with contextlib.redirect_stdout(stdout): |
| sp.parse_html_filing(html, form_type="") |
|
|
| self.assertNotIn("→ stage 0 (raw):", stdout.getvalue()) |
|
|
| def test_parse_html_filing_stage_debug_can_be_enabled(self): |
| html = "<html><body><p>Hello world</p></body></html>" |
| stdout = io.StringIO() |
|
|
| with mock.patch.dict("os.environ", {"SEC_PARSER_DEBUG": "1"}, clear=False): |
| with mock.patch.object(sp, "is_document_layout_positioned", return_value=False): |
| with mock.patch.object(sp, "parse_positioned_html_islands_via_ocr", return_value=(None, False)): |
| with contextlib.redirect_stdout(stdout): |
| sp.parse_html_filing(html, form_type="") |
|
|
| self.assertIn("→ stage 0 (raw):", stdout.getvalue()) |
|
|
|
|
| if __name__ == "__main__": |
| unittest.main() |
|
|