|
| 1 | +"""Regression tests for ``package_annotated_docs`` skipping failed placeholders.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import base64 |
| 6 | +import io |
| 7 | +import json |
| 8 | +import zipfile |
| 9 | +from unittest.mock import patch |
| 10 | + |
| 11 | +from django.contrib.auth import get_user_model |
| 12 | +from django.test import TestCase |
| 13 | + |
| 14 | +from opencontractserver.corpuses.models import Corpus |
| 15 | +from opencontractserver.tasks.export_tasks import package_annotated_docs |
| 16 | +from opencontractserver.users.models import UserExport |
| 17 | + |
| 18 | +User = get_user_model() |
| 19 | + |
| 20 | + |
| 21 | +def _make_label(pk: str, text: str) -> dict: |
| 22 | + return { |
| 23 | + "id": pk, |
| 24 | + "color": "#FF0000", |
| 25 | + "description": "", |
| 26 | + "icon": "tag", |
| 27 | + "text": text, |
| 28 | + "label_type": "TOKEN_LABEL", |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | +def _make_doc_export(title: str) -> dict: |
| 33 | + return { |
| 34 | + "doc_labels": [], |
| 35 | + "labelled_text": [], |
| 36 | + "title": title, |
| 37 | + "description": "", |
| 38 | + "content": "", |
| 39 | + "pawls_file_content": [], |
| 40 | + "page_count": 1, |
| 41 | + "file_type": "application/pdf", |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | +class PackageAnnotatedDocsSkipTestCase(TestCase): |
| 46 | + """Verifies package_annotated_docs skips failed burn_doc_annotations tuples.""" |
| 47 | + |
| 48 | + def setUp(self) -> None: |
| 49 | + self.user = User.objects.create_user(username="bob", password="12345678") |
| 50 | + self.corpus = Corpus.objects.create( |
| 51 | + title="Test Corpus", |
| 52 | + description="For package_annotated_docs tests", |
| 53 | + creator=self.user, |
| 54 | + ) |
| 55 | + self.export = UserExport.objects.create( |
| 56 | + name="test-export", |
| 57 | + creator=self.user, |
| 58 | + ) |
| 59 | + |
| 60 | + # A tiny payload that survives base64 + zip round-trip. |
| 61 | + self.fake_pdf_bytes = b"%PDF-1.4\n% fake body\n" |
| 62 | + self.fake_pdf_b64 = base64.b64encode(self.fake_pdf_bytes).decode("utf-8") |
| 63 | + |
| 64 | + self.text_labels = {"1": _make_label("1", "Important")} |
| 65 | + self.doc_labels = {"2": _make_label("2", "Contract")} |
| 66 | + |
| 67 | + def _collect_finalize(self): |
| 68 | + """Return a side_effect that captures finalize_export's output_bytes.""" |
| 69 | + captured: dict = {} |
| 70 | + |
| 71 | + def _capture(export_id, filename, output_bytes, corpus_title): |
| 72 | + # finalize_export does output_bytes.seek(0) before saving; mirror |
| 73 | + # that so the captured buffer is ready to read in-test. |
| 74 | + output_bytes.seek(0) |
| 75 | + captured["bytes"] = output_bytes.getvalue() |
| 76 | + captured["filename"] = filename |
| 77 | + |
| 78 | + return captured, _capture |
| 79 | + |
| 80 | + def test_skips_failed_placeholder_and_packages_successful_docs(self) -> None: |
| 81 | + """Mixed input: one good doc + one failed placeholder -> only good doc lands in zip.""" |
| 82 | + good_doc = ( |
| 83 | + "good.pdf", |
| 84 | + self.fake_pdf_b64, |
| 85 | + _make_doc_export("Good Doc"), |
| 86 | + self.text_labels, |
| 87 | + self.doc_labels, |
| 88 | + ) |
| 89 | + # This is exactly what build_document_export returns on failure. |
| 90 | + failed_doc = ("", "", None, {}, {}) |
| 91 | + |
| 92 | + captured, capture_fn = self._collect_finalize() |
| 93 | + with patch( |
| 94 | + "opencontractserver.tasks.export_tasks.finalize_export", |
| 95 | + side_effect=capture_fn, |
| 96 | + ), self.assertLogs( |
| 97 | + "opencontractserver.tasks.export_tasks", level="WARNING" |
| 98 | + ) as log_cm: |
| 99 | + package_annotated_docs( |
| 100 | + burned_docs=(good_doc, failed_doc), |
| 101 | + export_id=self.export.id, |
| 102 | + corpus_pk=self.corpus.id, |
| 103 | + ) |
| 104 | + |
| 105 | + self.assertTrue( |
| 106 | + any("Skipping failed burned doc" in line for line in log_cm.output), |
| 107 | + f"Expected skip-warning log, got: {log_cm.output}", |
| 108 | + ) |
| 109 | + self.assertIn("bytes", captured, "finalize_export was not called") |
| 110 | + zf = zipfile.ZipFile(io.BytesIO(captured["bytes"])) |
| 111 | + names = set(zf.namelist()) |
| 112 | + |
| 113 | + # The failed placeholder's empty-string filename must NOT be in the zip. |
| 114 | + self.assertNotIn("", names) |
| 115 | + self.assertIn("good.pdf", names) |
| 116 | + self.assertIn("data.json", names) |
| 117 | + |
| 118 | + data = json.loads(zf.read("data.json").decode("utf-8")) |
| 119 | + self.assertIn("good.pdf", data["annotated_docs"]) |
| 120 | + self.assertNotIn("", data["annotated_docs"]) |
| 121 | + # No None values should leak into annotated_docs. |
| 122 | + self.assertTrue( |
| 123 | + all(v is not None for v in data["annotated_docs"].values()), |
| 124 | + "annotated_docs must not contain None values for failed exports", |
| 125 | + ) |
| 126 | + # Labels from the successful doc still propagate. |
| 127 | + self.assertEqual(data["text_labels"], self.text_labels) |
| 128 | + self.assertEqual(data["doc_labels"], self.doc_labels) |
| 129 | + |
| 130 | + def test_all_failed_produces_empty_annotated_docs_without_crashing(self) -> None: |
| 131 | + """Every doc failed -> no crash, empty annotated_docs, no bogus keys.""" |
| 132 | + failed_doc_a = ("", "", None, {}, {}) |
| 133 | + failed_doc_b = ("", "", None, {}, {}) |
| 134 | + |
| 135 | + captured, capture_fn = self._collect_finalize() |
| 136 | + with patch( |
| 137 | + "opencontractserver.tasks.export_tasks.finalize_export", |
| 138 | + side_effect=capture_fn, |
| 139 | + ): |
| 140 | + package_annotated_docs( |
| 141 | + burned_docs=(failed_doc_a, failed_doc_b), |
| 142 | + export_id=self.export.id, |
| 143 | + corpus_pk=self.corpus.id, |
| 144 | + ) |
| 145 | + |
| 146 | + self.assertIn("bytes", captured, "finalize_export was not called") |
| 147 | + zf = zipfile.ZipFile(io.BytesIO(captured["bytes"])) |
| 148 | + # Only data.json, no empty-filename entry. |
| 149 | + self.assertEqual(set(zf.namelist()), {"data.json"}) |
| 150 | + |
| 151 | + data = json.loads(zf.read("data.json").decode("utf-8")) |
| 152 | + self.assertEqual(data["annotated_docs"], {}) |
0 commit comments