Chore: share zone OCR mock fixtures and helpers

This commit is contained in:
stumpylog committed 2026-11-18 12:41:46 -08:00
1 parent 9ff48ecfce
commit e03faab730
3 files changed
+254 -271

No files matched your search

+10
View File
@@ -8,6 +8,10 @@ import pytest
from paperless_testing.factories import DocumentFactory
if TYPE_CHECKING:
from unittest.mock import MagicMock
from pytest_mock import MockerFixture
from documents.models import Document
from paperless_testing.dirs import PaperlessDirs
@@ -65,3 +69,9 @@ def searchable_document(_search_index: None) -> "Document":
doc = DocumentFactory.create(title="quarterly invoice", content="acme corp")
get_backend().add_or_update(doc)
return doc
@pytest.fixture
def mock_zone_ocr_run(mocker: "MockerFixture") -> "MagicMock":
"""Patch the subprocess runner used by zone OCR (tesseract and pdftoppm)."""
return mocker.patch("documents.zone_ocr.subprocess.run")
+148 -158
View File
@@ -34,6 +34,7 @@ from paperless_testing.permissions import grant_object
if TYPE_CHECKING:
from collections.abc import Callable
from unittest.mock import MagicMock
from django.contrib.auth.models import User
from pytest_django.fixtures import DjangoCaptureOnCommitCallbacks
@@ -48,6 +49,7 @@ CONTENT = "Northwind Power & Light statement"
ENDPOINT = "/api/ocr_templates/"
RUN_ZONE_OCR_URL = "/api/documents/{pk}/run-zone-ocr/"
PREVIEW_ZONE_URL = f"{ENDPOINT}test-zone/"
SKIPPED_NO_TEMPLATE = {"status": "skipped", "reason": "no_template", "results": []}
def make_template_data(**overrides: Any) -> dict[str, Any]:
@@ -111,6 +113,48 @@ def make_zone(text_field: CustomField) -> Callable[..., dict[str, Any]]:
return functools.partial(make_zone_data, text_field.pk)
def write_blank_png(path: Path) -> None:
"""Write a blank 100x100 white PNG to the given path."""
with Image.new("RGB", (100, 100), "white") as image:
image.save(path, format="PNG")
def title_template(
name: str,
owner: User | None = None,
**kwargs: Any,
) -> OcrTemplate:
"""Build a template matching "Northwind" with one 20x20 title zone."""
template = OcrTemplateFactory(
name=name,
owner=owner,
match="Northwind",
**kwargs,
)
OcrTemplateZoneFactory(
template=template,
name=name,
width=20,
height=20,
)
return template
@pytest.fixture
def mock_extract(mocker: MockerFixture) -> MagicMock:
return mocker.patch("documents.zone_ocr.run_zone_extraction")
@pytest.fixture
def mock_preview(mocker: MockerFixture) -> MagicMock:
return mocker.patch("documents.zone_ocr.extract_zone_preview")
@pytest.fixture
def mock_render(mocker: MockerFixture) -> MagicMock:
return mocker.patch("documents.views.subprocess.run")
def preview_url(document: Document, page: int = 0) -> str:
return f"{ENDPOINT}document-page-image/{document.pk}/{page}/"
@@ -178,7 +222,7 @@ class TestZoneOcrIndexing:
versioned: bool, # noqa: FBT001
admin_client: APIClient,
django_capture_on_commit_callbacks: DjangoCaptureOnCommitCallbacks,
mocker: MockerFixture,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -208,8 +252,7 @@ class TestZoneOcrIndexing:
mime_type="image/png",
page_count=1,
)
with Image.new("RGB", (100, 100), "white") as image:
image.save(file_document.source_path, format="PNG")
write_blank_png(file_document.source_path)
template = OcrTemplateFactory(
name="Invoice template",
match="Northwind",
@@ -233,10 +276,20 @@ class TestZoneOcrIndexing:
width=50,
order=1,
)
mocker.patch(
"documents.zone_ocr.subprocess.run",
side_effect=[mocker.Mock(stdout="After OCR"), mocker.Mock(stdout="INV42")],
)
mock_zone_ocr_run.side_effect = [
subprocess.CompletedProcess(
args=[],
returncode=0,
stdout="After OCR",
stderr="",
),
subprocess.CompletedProcess(
args=[],
returncode=0,
stdout="INV42",
stderr="",
),
]
backend = get_backend()
if entry_point != "ingestion":
@@ -442,17 +495,10 @@ class TestOcrTemplatePermissions:
user = UserFactory()
grant_global(user, "view_ocrtemplate")
def template(name: str, owner: User | None = None) -> OcrTemplate:
return OcrTemplateFactory(
name=name,
match="Northwind",
owner=owner,
)
template("Unowned")
template("Mine", owner=user)
template("Private", owner=other)
grant_object(user, template("Shared", owner=other), "view_ocrtemplate")
title_template("Unowned")
title_template("Mine", owner=user)
title_template("Private", owner=other)
grant_object(user, title_template("Shared", owner=other), "view_ocrtemplate")
rest_api_client.force_authenticate(user=user)
response = rest_api_client.get(ENDPOINT)
@@ -545,7 +591,7 @@ class TestOcrTemplatePermissions:
def test_test_zone_requires_view_permission_on_the_document(
self,
rest_api_client: APIClient,
mocker: MockerFixture,
mock_preview: MagicMock,
) -> None:
"""
GIVEN:
@@ -558,7 +604,6 @@ class TestOcrTemplatePermissions:
- Afterwards it passes the permission check and reaches the missing file
- No preview extraction runs
"""
preview = mocker.patch("documents.zone_ocr.extract_zone_preview")
owner = UserFactory()
user = UserFactory()
grant_global(user, "add_ocrtemplate", "view_document")
@@ -576,7 +621,7 @@ class TestOcrTemplatePermissions:
response = rest_api_client.post(PREVIEW_ZONE_URL, data, format="json")
# Past the permission check; the factory document has no file on disk.
assert response.status_code == status.HTTP_404_NOT_FOUND
preview.assert_not_called()
mock_preview.assert_not_called()
@pytest.mark.parametrize(
"make_data",
@@ -1208,7 +1253,7 @@ class TestRunZoneOcr:
def test_without_matching_template_is_skipped(
self,
admin_client: APIClient,
mocker: MockerFixture,
mock_extract: MagicMock,
) -> None:
"""
GIVEN:
@@ -1218,7 +1263,6 @@ class TestRunZoneOcr:
THEN:
- The run is skipped with reason no_template and nothing is extracted
"""
extract = mocker.patch("documents.zone_ocr.run_zone_extraction")
document = DocumentFactory(content=CONTENT)
OcrTemplateFactory(
name="Other sender",
@@ -1228,12 +1272,8 @@ class TestRunZoneOcr:
response = admin_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk))
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
"status": "skipped",
"reason": "no_template",
"results": [],
}
extract.assert_not_called()
assert response.json() == SKIPPED_NO_TEMPLATE
mock_extract.assert_not_called()
@pytest.mark.parametrize(
"with_disabled_template",
@@ -1245,7 +1285,7 @@ class TestRunZoneOcr:
def test_without_enabled_template_is_skipped(
self,
admin_client: APIClient,
mocker: MockerFixture,
mock_extract: MagicMock,
with_disabled_template: bool, # noqa: FBT001
) -> None:
"""
@@ -1256,7 +1296,6 @@ class TestRunZoneOcr:
THEN:
- The run is skipped with reason no_template and nothing is extracted
"""
extract = mocker.patch("documents.zone_ocr.run_zone_extraction")
document = DocumentFactory(content=CONTENT)
if with_disabled_template:
OcrTemplateFactory(
@@ -1268,17 +1307,13 @@ class TestRunZoneOcr:
response = admin_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk))
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
"status": "skipped",
"reason": "no_template",
"results": [],
}
extract.assert_not_called()
assert response.json() == SKIPPED_NO_TEMPLATE
mock_extract.assert_not_called()
def test_applies_a_chosen_template(
self,
admin_client: APIClient,
mocker: MockerFixture,
mock_extract: MagicMock,
) -> None:
"""
GIVEN:
@@ -1288,7 +1323,6 @@ class TestRunZoneOcr:
THEN:
- The template is applied anyway, since it was chosen explicitly
"""
extract = mocker.patch("documents.zone_ocr.run_zone_extraction")
document = DocumentFactory(content=CONTENT, mime_type="application/pdf")
document.source_path.write_bytes(b"test PDF")
template = OcrTemplateFactory(
@@ -1305,7 +1339,11 @@ class TestRunZoneOcr:
assert response.status_code == status.HTTP_200_OK
assert response.json()["status"] == "completed"
extract.assert_called_once_with(document, templates=[template], reindex=True)
mock_extract.assert_called_once_with(
document,
templates=[template],
reindex=True,
)
@pytest.mark.parametrize(
"version",
@@ -1317,7 +1355,7 @@ class TestRunZoneOcr:
def test_matches_the_requested_file_version(
self,
admin_client: APIClient,
mocker: MockerFixture,
mock_extract: MagicMock,
version: str,
) -> None:
"""
@@ -1328,10 +1366,7 @@ class TestRunZoneOcr:
THEN:
- The template is applied to the requested file document
"""
extract = mocker.patch(
"documents.zone_ocr.run_zone_extraction",
return_value=[],
)
mock_extract.return_value = []
document = DocumentFactory(content=CONTENT)
older = DocumentFactory(
root_document=document,
@@ -1354,7 +1389,7 @@ class TestRunZoneOcr:
assert response.status_code == status.HTTP_200_OK
assert response.json()["status"] == "completed"
extract.assert_called_once_with(
mock_extract.assert_called_once_with(
file_document,
templates=[template],
reindex=True,
@@ -1371,7 +1406,7 @@ class TestRunZoneOcr:
def test_rejects_invalid_or_unavailable_versions(
self,
admin_client: APIClient,
mocker: MockerFixture,
mock_extract: MagicMock,
version: str,
) -> None:
"""
@@ -1382,7 +1417,6 @@ class TestRunZoneOcr:
THEN:
- 404 is returned and nothing is extracted
"""
extract = mocker.patch("documents.zone_ocr.run_zone_extraction")
document = DocumentFactory()
unrelated = DocumentFactory()
deleted = DocumentFactory(
@@ -1401,7 +1435,7 @@ class TestRunZoneOcr:
)
assert response.status_code == status.HTTP_404_NOT_FOUND
extract.assert_not_called()
mock_extract.assert_not_called()
def test_rejects_an_unknown_template(self, admin_client: APIClient) -> None:
"""
@@ -1438,7 +1472,7 @@ class TestRunZoneOcr:
def test_missing_file_is_an_error(
self,
admin_client: APIClient,
mocker: MockerFixture,
mock_extract: MagicMock,
) -> None:
"""
GIVEN:
@@ -1448,7 +1482,6 @@ class TestRunZoneOcr:
THEN:
- 404 is returned with a file not found error and nothing is extracted
"""
extract = mocker.patch("documents.zone_ocr.run_zone_extraction")
document = DocumentFactory(content=CONTENT)
OcrTemplateFactory(
name="Template",
@@ -1459,12 +1492,12 @@ class TestRunZoneOcr:
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json() == {"error": "Document file not found"}
extract.assert_not_called()
mock_extract.assert_not_called()
def test_reports_completed_results(
self,
admin_client: APIClient,
mocker: MockerFixture,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -1474,14 +1507,12 @@ class TestRunZoneOcr:
THEN:
- The completed result lists the extracted title value
"""
run = mocker.patch("documents.zone_ocr.subprocess.run")
document = DocumentFactory(
content=CONTENT,
mime_type="application/pdf",
)
with Image.new("RGB", (100, 100), "white") as image:
image.save(document.source_path, format="PNG")
run.return_value.stdout = "Extracted title"
write_blank_png(document.source_path)
mock_zone_ocr_run.return_value.stdout = "Extracted title"
template = OcrTemplateFactory(
name="Template",
match="Northwind",
@@ -1507,12 +1538,12 @@ class TestRunZoneOcr:
},
],
}
run.assert_called_once()
mock_zone_ocr_run.assert_called_once()
def test_does_not_report_an_old_title_when_extraction_is_empty(
self,
admin_client: APIClient,
mocker: MockerFixture,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -1524,10 +1555,8 @@ class TestRunZoneOcr:
- The result has reason no_value and no value
- The document title is unchanged
"""
run = mocker.patch("documents.zone_ocr.subprocess.run")
document = DocumentFactory(title="Existing title", content=CONTENT)
with Image.new("RGB", (100, 100), "white") as image:
image.save(document.source_path, format="PNG")
write_blank_png(document.source_path)
template = OcrTemplateFactory(
name="Template",
match="Northwind",
@@ -1540,7 +1569,7 @@ class TestRunZoneOcr:
width=20,
height=20,
)
run.return_value.stdout = ""
mock_zone_ocr_run.return_value.stdout = ""
response = admin_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk))
@@ -1553,7 +1582,7 @@ class TestRunZoneOcr:
def test_reports_all_target_types_and_missing_values(
self,
admin_client: APIClient,
mocker: MockerFixture,
mock_zone_ocr_run: MagicMock,
text_field: CustomField,
date_field: CustomField,
) -> None:
@@ -1566,12 +1595,10 @@ class TestRunZoneOcr:
THEN:
- Each zone reports the value for its target, or None when missing
"""
run = mocker.patch("documents.zone_ocr.subprocess.run")
document = DocumentFactory(content=CONTENT, archive_serial_number=123)
with Image.new("RGB", (100, 100), "white") as image:
image.save(document.source_path, format="PNG")
run.side_effect = [
subprocess.CompletedProcess([], 0, stdout=value)
write_blank_png(document.source_path)
mock_zone_ocr_run.side_effect = [
subprocess.CompletedProcess(args=[], returncode=0, stdout=value, stderr="")
for value in [
"New invoice",
"",
@@ -1634,7 +1661,7 @@ class TestRunZoneOcrPermissions:
def test_rejects_a_template_the_user_cannot_view(
self,
rest_api_client: APIClient,
mocker: MockerFixture,
mock_extract: MagicMock,
) -> None:
"""
GIVEN:
@@ -1645,7 +1672,6 @@ class TestRunZoneOcrPermissions:
THEN:
- 403 is returned and nothing is extracted
"""
extract = mocker.patch("documents.zone_ocr.run_zone_extraction")
user = UserFactory()
grant_global(user, "view_document", "change_document")
document = DocumentFactory(content=CONTENT, owner=user)
@@ -1662,12 +1688,12 @@ class TestRunZoneOcrPermissions:
)
assert response.status_code == status.HTTP_403_FORBIDDEN
extract.assert_not_called()
mock_extract.assert_not_called()
def test_requires_change_permission_on_the_document(
self,
rest_api_client: APIClient,
mocker: MockerFixture,
mock_extract: MagicMock,
) -> None:
"""
GIVEN:
@@ -1678,7 +1704,6 @@ class TestRunZoneOcrPermissions:
THEN:
- 403 is returned and nothing is extracted
"""
extract = mocker.patch("documents.zone_ocr.run_zone_extraction")
owner = UserFactory()
user = UserFactory()
grant_global(user, "view_document", "change_document")
@@ -1688,12 +1713,12 @@ class TestRunZoneOcrPermissions:
response = rest_api_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk))
assert response.status_code == status.HTTP_403_FORBIDDEN
extract.assert_not_called()
mock_extract.assert_not_called()
def test_requires_global_change_permission(
self,
rest_api_client: APIClient,
mocker: MockerFixture,
mock_extract: MagicMock,
) -> None:
"""
GIVEN:
@@ -1704,7 +1729,6 @@ class TestRunZoneOcrPermissions:
THEN:
- 403 is returned and nothing is extracted
"""
extract = mocker.patch("documents.zone_ocr.run_zone_extraction")
user = UserFactory()
grant_global(user, "view_document", "add_document")
document = DocumentFactory(content=CONTENT, owner=user)
@@ -1713,12 +1737,12 @@ class TestRunZoneOcrPermissions:
response = rest_api_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk))
assert response.status_code == status.HTTP_403_FORBIDDEN
extract.assert_not_called()
mock_extract.assert_not_called()
def test_does_not_require_document_upload_permission(
self,
rest_api_client: APIClient,
mocker: MockerFixture,
mock_extract: MagicMock,
) -> None:
"""
GIVEN:
@@ -1729,7 +1753,6 @@ class TestRunZoneOcrPermissions:
THEN:
- The request succeeds and extraction runs
"""
extract = mocker.patch("documents.zone_ocr.run_zone_extraction")
user = UserFactory()
grant_global(user, "change_document")
document = DocumentFactory(owner=user, content=CONTENT)
@@ -1746,13 +1769,13 @@ class TestRunZoneOcrPermissions:
response = rest_api_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk))
assert response.status_code == 200
extract.assert_called_once()
mock_extract.assert_called_once()
def test_skips_templates_private_to_the_requester(
self,
admin_client: APIClient,
admin_user: User,
mocker: MockerFixture,
mock_extract: MagicMock,
) -> None:
"""
GIVEN:
@@ -1763,7 +1786,6 @@ class TestRunZoneOcrPermissions:
THEN:
- The run is skipped with reason no_template and nothing is extracted
"""
extract = mocker.patch("documents.zone_ocr.run_zone_extraction")
owner = UserFactory()
document = DocumentFactory(content=CONTENT, owner=owner)
OcrTemplateFactory(
@@ -1777,18 +1799,14 @@ class TestRunZoneOcrPermissions:
response = admin_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk))
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
"status": "skipped",
"reason": "no_template",
"results": [],
}
extract.assert_not_called()
assert response.json() == SKIPPED_NO_TEMPLATE
mock_extract.assert_not_called()
def test_reports_only_templates_visible_to_the_document_owner(
self,
admin_client: APIClient,
admin_user: User,
mocker: MockerFixture,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -1800,31 +1818,22 @@ class TestRunZoneOcrPermissions:
THEN:
- Only templates visible to the document owner run and are reported
"""
run = mocker.patch("documents.zone_ocr.subprocess.run")
owner = UserFactory()
document = DocumentFactory(content=CONTENT, owner=owner)
with Image.new("RGB", (100, 100), "white") as image:
image.save(document.source_path, format="PNG")
run.return_value.stdout = "extracted"
write_blank_png(document.source_path)
mock_zone_ocr_run.return_value.stdout = "extracted"
for name, template_owner in [
("Owned", owner),
("Unowned", None),
("Shared", admin_user),
("Private", admin_user),
]:
template = OcrTemplateFactory(
name=name,
owner=template_owner,
match="Northwind",
template = title_template(
name,
template_owner,
source_width=100,
source_height=100,
)
OcrTemplateZoneFactory(
template=template,
name=name,
width=20,
height=20,
)
if name == "Shared":
grant_object(owner, template, "view_ocrtemplate")
@@ -1836,12 +1845,12 @@ class TestRunZoneOcrPermissions:
"Shared",
"Unowned",
]
assert run.call_count == 3
assert mock_zone_ocr_run.call_count == 3
def test_hides_the_owners_private_templates_from_an_editor(
self,
rest_api_client: APIClient,
mocker: MockerFixture,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -1852,33 +1861,24 @@ class TestRunZoneOcrPermissions:
THEN:
- Only templates the editor can also view run and are reported
"""
run = mocker.patch("documents.zone_ocr.subprocess.run")
owner = UserFactory()
editor = UserFactory()
grant_global(editor, "view_document", "change_document")
document = DocumentFactory(content=CONTENT, owner=owner)
grant_object(editor, document, "view_document", "change_document")
with Image.new("RGB", (100, 100), "white") as image:
image.save(document.source_path, format="PNG")
run.return_value.stdout = "extracted"
write_blank_png(document.source_path)
mock_zone_ocr_run.return_value.stdout = "extracted"
for name, template_owner in [
("Owner private", owner),
("Unowned", None),
("Shared", owner),
]:
template = OcrTemplateFactory(
name=name,
owner=template_owner,
match="Northwind",
template = title_template(
name,
template_owner,
source_width=100,
source_height=100,
)
OcrTemplateZoneFactory(
template=template,
name=name,
width=20,
height=20,
)
if name == "Shared":
grant_object(editor, template, "view_ocrtemplate")
rest_api_client.force_authenticate(editor)
@@ -1890,7 +1890,7 @@ class TestRunZoneOcrPermissions:
"Shared",
"Unowned",
]
assert run.call_count == 2
assert mock_zone_ocr_run.call_count == 2
@pytest.mark.django_db
@@ -1917,7 +1917,7 @@ class TestDocumentPageImage:
self,
preview_client: APIClient,
preview_user: User,
mocker: MockerFixture,
mock_render: MagicMock,
) -> None:
"""
GIVEN:
@@ -1927,7 +1927,6 @@ class TestDocumentPageImage:
THEN:
- The original file is served and no render subprocess runs
"""
render = mocker.patch("documents.views.subprocess.run")
document = DocumentFactory(owner=preview_user, mime_type="image/png")
document.source_path.write_bytes(b"image contents")
@@ -1936,7 +1935,7 @@ class TestDocumentPageImage:
assert response.status_code == 200
assert response["Content-Type"] == "image/png"
assert response.content == b"image contents"
render.assert_not_called()
mock_render.assert_not_called()
def test_renders_pdf_archive_for_an_image_upload(
self,
@@ -1974,7 +1973,7 @@ class TestDocumentPageImage:
self,
preview_client: APIClient,
preview_document: Document,
mocker: MockerFixture,
mock_render: MagicMock,
) -> None:
"""
GIVEN:
@@ -1985,19 +1984,18 @@ class TestDocumentPageImage:
- pdftoppm is invoked for page 2 with the expected arguments
- The rendered page is served and the temporary directory removed
"""
render = mocker.patch("documents.views.subprocess.run")
def render_page(command: list[str], **kwargs: Any) -> None:
Path(command[-1] + "-2.png").write_bytes(b"rendered page")
render.side_effect = render_page
mock_render.side_effect = render_page
response = preview_client.get(preview_url(preview_document, 1))
assert response.status_code == 200
assert response["Content-Type"] == "image/png"
assert response.content == b"rendered page"
command = render.call_args.args[0]
command = mock_render.call_args.args[0]
assert command[:9] == [
"pdftoppm",
"-png",
@@ -2009,7 +2007,7 @@ class TestDocumentPageImage:
"2",
str(preview_document.source_path),
]
assert render.call_args.kwargs == {
assert mock_render.call_args.kwargs == {
"check": True,
"capture_output": True,
"timeout": 30,
@@ -2035,7 +2033,7 @@ class TestDocumentPageImage:
self,
preview_client: APIClient,
preview_document: Document,
mocker: MockerFixture,
mock_render: MagicMock,
error: Exception,
) -> None:
"""
@@ -2046,19 +2044,18 @@ class TestDocumentPageImage:
THEN:
- 404 is returned without leaking the subprocess error output
"""
render = mocker.patch("documents.views.subprocess.run")
render.side_effect = error
mock_render.side_effect = error
response = preview_client.get(preview_url(preview_document))
assert response.status_code == 404
assert "sensitive server path" not in str(response.content)
@pytest.mark.usefixtures("mock_render")
def test_missing_render_output_returns_not_found(
self,
preview_client: APIClient,
preview_document: Document,
mocker: MockerFixture,
) -> None:
"""
GIVEN:
@@ -2068,7 +2065,6 @@ class TestDocumentPageImage:
THEN:
- 404 is returned
"""
mocker.patch("documents.views.subprocess.run")
assert preview_client.get(preview_url(preview_document)).status_code == 404
@@ -2080,7 +2076,7 @@ class TestZonePreview:
self,
preview_client: APIClient,
preview_document: Document,
mocker: MockerFixture,
mock_preview: MagicMock,
) -> None:
"""
GIVEN:
@@ -2092,8 +2088,7 @@ class TestZonePreview:
- The result includes the regex match
- No zone is saved
"""
extract = mocker.patch("documents.zone_ocr.extract_zone_preview")
extract.return_value = {"raw_text": " 123 ", "value": "123"}
mock_preview.return_value = {"raw_text": " 123 ", "value": "123"}
response = preview_zone(
preview_client,
@@ -2115,7 +2110,7 @@ class TestZonePreview:
"regex": r"\d+",
"regex_match": True,
}
path, zone, width, height, page_count = extract.call_args.args
path, zone, width, height, page_count = mock_preview.call_args.args
assert path == preview_document.source_path
assert (zone.x, zone.y, zone.width, zone.height, zone.page) == (
10,
@@ -2143,7 +2138,7 @@ class TestZonePreview:
self,
preview_client: APIClient,
preview_document: Document,
mocker: MockerFixture,
mock_preview: MagicMock,
regex: str,
value: str | None,
expected: bool | None, # noqa: FBT001
@@ -2158,8 +2153,7 @@ class TestZonePreview:
- regex_match reflects the outcome
- Source dimensions default to 0
"""
extract = mocker.patch("documents.zone_ocr.extract_zone_preview")
extract.return_value = {"raw_text": value, "value": value}
mock_preview.return_value = {"raw_text": value, "value": value}
response = preview_zone(
preview_client,
@@ -2169,7 +2163,7 @@ class TestZonePreview:
assert response.status_code == 200
assert response.json()["regex_match"] == expected
assert extract.call_args.args[2:4] == (0, 0)
assert mock_preview.call_args.args[2:4] == (0, 0)
@pytest.mark.parametrize(
"zone",
@@ -2195,7 +2189,7 @@ class TestZonePreview:
self,
preview_client: APIClient,
preview_document: Document,
mocker: MockerFixture,
mock_preview: MagicMock,
zone: dict[str, Any],
) -> None:
"""
@@ -2206,10 +2200,9 @@ class TestZonePreview:
THEN:
- 400 is returned and extraction does not run
"""
extract = mocker.patch("documents.zone_ocr.extract_zone_preview")
assert preview_zone(preview_client, preview_document, **zone).status_code == 400
extract.assert_not_called()
mock_preview.assert_not_called()
@pytest.mark.parametrize(
"make_data",
@@ -2237,7 +2230,7 @@ class TestZonePreview:
self,
preview_client: APIClient,
preview_document: Document,
mocker: MockerFixture,
mock_preview: MagicMock,
make_data: Callable[[Document], dict[str, Any]],
) -> None:
"""
@@ -2248,7 +2241,6 @@ class TestZonePreview:
THEN:
- 400 is returned and extraction does not run
"""
extract = mocker.patch("documents.zone_ocr.extract_zone_preview")
response = preview_client.post(
PREVIEW_ZONE_URL,
@@ -2257,7 +2249,7 @@ class TestZonePreview:
)
assert response.status_code == 400
extract.assert_not_called()
mock_preview.assert_not_called()
@pytest.mark.django_db
@@ -2273,7 +2265,7 @@ class TestOcrTemplatePreviewPermissions:
def test_previews_require_global_document_view_permission(
self,
rest_api_client: APIClient,
mocker: MockerFixture,
mock_preview: MagicMock,
action: str,
) -> None:
"""
@@ -2286,13 +2278,12 @@ class TestOcrTemplatePreviewPermissions:
THEN:
- 403 is returned and no extraction runs
"""
extract = mocker.patch("documents.zone_ocr.extract_zone_preview")
user = UserFactory()
grant_global(user, "view_ocrtemplate", "add_ocrtemplate")
rest_api_client.force_authenticate(user)
document = DocumentFactory(owner=None, mime_type="image/png")
document.source_path.write_bytes(b"document contents")
extract.return_value = {"raw_text": "document contents", "value": None}
mock_preview.return_value = {"raw_text": "document contents", "value": None}
if action == "document-page-image":
response = rest_api_client.get(preview_url(document))
@@ -2304,13 +2295,14 @@ class TestOcrTemplatePreviewPermissions:
)
assert response.status_code == status.HTTP_403_FORBIDDEN
extract.assert_not_called()
mock_preview.assert_not_called()
def test_versions_are_authorized_through_the_root_document(
self,
preview_client: APIClient,
preview_user: User,
mocker: MockerFixture,
mock_preview: MagicMock,
mock_render: MagicMock,
) -> None:
"""
GIVEN:
@@ -2322,8 +2314,6 @@ class TestOcrTemplatePreviewPermissions:
- Both are refused until the user is granted view on the root
- Afterwards both succeed
"""
extract = mocker.patch("documents.zone_ocr.extract_zone_preview")
render = mocker.patch("documents.views.subprocess.run")
other = UserFactory()
root = DocumentFactory(owner=other)
version = DocumentFactory(
@@ -2339,11 +2329,11 @@ class TestOcrTemplatePreviewPermissions:
preview_client.post(PREVIEW_ZONE_URL, data, format="json").status_code
== 403
)
render.assert_not_called()
extract.assert_not_called()
mock_render.assert_not_called()
mock_preview.assert_not_called()
grant_object(preview_user, root, "view_document")
extract.return_value = {"raw_text": None, "value": None}
mock_preview.return_value = {"raw_text": None, "value": None}
assert preview_client.get(preview_url(version)).status_code == 200
assert (
preview_client.post(PREVIEW_ZONE_URL, data, format="json").status_code
+96 -113
View File
@@ -77,10 +77,6 @@ class TestZoneExtraction:
def scratch_dir(self, settings: SettingsWrapper, tmp_path: Path) -> None:
settings.SCRATCH_DIR = tmp_path
@pytest.fixture
def mock_run(self, mocker: MockerFixture) -> MagicMock:
return mocker.patch("documents.zone_ocr.subprocess.run")
def test_crop_scales_coordinates(
self,
page: Path,
@@ -138,7 +134,7 @@ class TestZoneExtraction:
page: Path,
zone: OcrTemplateZone,
tmp_path: Path,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -152,14 +148,14 @@ class TestZoneExtraction:
result = _extract_zone(page, zone, 100, 50, tmp_path)
assert result.raw_text is None
assert result.value is None
mock_run.assert_not_called()
mock_zone_ocr_run.assert_not_called()
def test_unreadable_image_skips_reader(
self,
page: Path,
zone: OcrTemplateZone,
tmp_path: Path,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -173,15 +169,14 @@ class TestZoneExtraction:
result = _extract_zone(page, zone, 100, 50, tmp_path)
assert result.raw_text is None
assert result.value is None
mock_run.assert_not_called()
mock_zone_ocr_run.assert_not_called()
def test_text_extraction_retains_raw_text_and_transforms(
self,
mocker: MockerFixture,
page: Path,
zone: OcrTemplateZone,
tmp_path: Path,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -193,25 +188,33 @@ class TestZoneExtraction:
- The stripped raw text is retained and the transformed value returned
"""
def read_crop(args: list[str], **kwargs: object) -> MagicMock:
def read_crop(
args: list[str],
**kwargs: object,
) -> subprocess.CompletedProcess:
with Image.open(args[1]) as crop:
assert crop.size == (40, 20)
assert crop.getpixel((0, 0)) == (0, 0, 0)
return mocker.MagicMock(stdout=" account abc\n")
return subprocess.CompletedProcess(
args=[],
returncode=0,
stdout=" account abc\n",
stderr="",
)
mock_run.side_effect = read_crop
mock_zone_ocr_run.side_effect = read_crop
result = _extract_zone(page, zone, 100, 50, tmp_path)
assert result.raw_text == "account abc"
assert result.value == "ACCOUNT ABC"
assert mock_run.call_args.args[0][-4:] == ["-l", "eng", "--psm", "6"]
assert mock_run.call_args.kwargs["timeout"] == 30
assert mock_zone_ocr_run.call_args.args[0][-4:] == ["-l", "eng", "--psm", "6"]
assert mock_zone_ocr_run.call_args.kwargs["timeout"] == 30
def test_no_text_returns_no_value(
self,
page: Path,
zone: OcrTemplateZone,
tmp_path: Path,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -221,7 +224,7 @@ class TestZoneExtraction:
THEN:
- No text or value is returned
"""
mock_run.return_value.stdout = " \n"
mock_zone_ocr_run.return_value.stdout = " \n"
result = _extract_zone(page, zone, 100, 50, tmp_path)
assert result.raw_text is None
assert result.value is None
@@ -246,7 +249,7 @@ class TestZoneExtraction:
page: Path,
zone: OcrTemplateZone,
tmp_path: Path,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
error: Exception,
) -> None:
"""
@@ -257,7 +260,7 @@ class TestZoneExtraction:
THEN:
- No text or value is returned
"""
mock_run.side_effect = error
mock_zone_ocr_run.side_effect = error
result = _extract_zone(page, zone, 100, 50, tmp_path)
assert result.raw_text is None
assert result.value is None
@@ -290,7 +293,7 @@ class TestZoneExtraction:
self,
page: Path,
zone: OcrTemplateZone,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -305,14 +308,14 @@ class TestZoneExtraction:
"raw_text": None,
"value": None,
}
mock_run.assert_not_called()
mock_zone_ocr_run.assert_not_called()
def test_date_transform_uses_zone_format(
self,
page: Path,
zone: OcrTemplateZone,
tmp_path: Path,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -324,7 +327,7 @@ class TestZoneExtraction:
"""
zone.transform = "date"
zone.date_format = "%d/%m/%Y"
mock_run.return_value.stdout = "25/12/2025"
mock_zone_ocr_run.return_value.stdout = "25/12/2025"
result = _extract_zone(page, zone, 100, 50, tmp_path)
assert result.raw_text == "25/12/2025"
assert result.value == "2025-12-25"
@@ -335,7 +338,7 @@ class TestZoneExtraction:
page: Path,
zone: OcrTemplateZone,
tmp_path: Path,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -359,7 +362,7 @@ class TestZoneExtraction:
result = _extract_zone(page, zone, 100, 50, tmp_path)
assert result.raw_text == " https://example.com "
assert result.value == "https://example.com"
mock_run.assert_not_called()
mock_zone_ocr_run.assert_not_called()
with pytest.raises(ValueError):
read_barcodes.call_args.args[0].getpixel((0, 0))
@@ -388,7 +391,7 @@ class TestZoneExtraction:
self,
page: Path,
zone: OcrTemplateZone,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -398,7 +401,7 @@ class TestZoneExtraction:
THEN:
- The raw text and transformed value are returned
"""
mock_run.return_value.stdout = "account abc"
mock_zone_ocr_run.return_value.stdout = "account abc"
assert extract_zone_preview(page, zone, 0, 0, 1) == {
"raw_text": "account abc",
"value": "ACCOUNT ABC",
@@ -408,7 +411,7 @@ class TestZoneExtraction:
self,
page: Path,
zone: OcrTemplateZone,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -423,7 +426,7 @@ class TestZoneExtraction:
"raw_text": None,
"value": None,
}
mock_run.assert_not_called()
mock_zone_ocr_run.assert_not_called()
def make_template(name: str, **kwargs: Any) -> OcrTemplate:
@@ -470,14 +473,10 @@ class TestSavedZoneExtraction:
def template(self) -> OcrTemplate:
return make_template("Account")
@pytest.fixture
def mock_run(self, mocker: MockerFixture) -> MagicMock:
return mocker.patch("documents.zone_ocr.subprocess.run")
def test_combines_zones_and_writes_each_field_once(
self,
mocker: MockerFixture,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
paperless_dirs: PaperlessDirs,
page: Path,
document: Document,
@@ -493,9 +492,9 @@ class TestSavedZoneExtraction:
"""
zones = [make_zone(template, "First"), make_zone(template, "Second")]
template.combine_formats = {"title": "{Second} / {First}"}
mock_run.side_effect = [
mocker.MagicMock(stdout="one"),
mocker.MagicMock(stdout="two"),
mock_zone_ocr_run.side_effect = [
subprocess.CompletedProcess(args=[], returncode=0, stdout="one", stderr=""),
subprocess.CompletedProcess(args=[], returncode=0, stdout="two", stderr=""),
]
save = mocker.patch.object(document, "save", wraps=document.save)
@@ -513,8 +512,7 @@ class TestSavedZoneExtraction:
def test_converts_custom_and_builtin_fields_before_saving(
self,
mocker: MockerFixture,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
paperless_dirs: PaperlessDirs,
page: Path,
document: Document,
@@ -535,8 +533,8 @@ class TestSavedZoneExtraction:
make_zone(template, "Created", "created"),
make_zone(template, "Paid", "custom_field", custom_field=field),
]
mock_run.side_effect = [
mocker.MagicMock(stdout=value)
mock_zone_ocr_run.side_effect = [
subprocess.CompletedProcess(args=[], returncode=0, stdout=value, stderr="")
for value in ["New title", "ASN 42", "2026-04-13", "no"]
]
@@ -556,8 +554,7 @@ class TestSavedZoneExtraction:
def test_invalid_field_values_preserve_existing_values(
self,
mocker: MockerFixture,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
paperless_dirs: PaperlessDirs,
page: Path,
document: Document,
@@ -587,8 +584,8 @@ class TestSavedZoneExtraction:
make_zone(template, "Unassigned", "custom_field"),
]
original_created = document.created
mock_run.side_effect = [
mocker.MagicMock(stdout=value)
mock_zone_ocr_run.side_effect = [
subprocess.CompletedProcess(args=[], returncode=0, stdout=value, stderr="")
for value in [
"Valid",
"9999999999999999999999999999999",
@@ -615,7 +612,7 @@ class TestSavedZoneExtraction:
def test_write_failure_rolls_back_template_and_restores_document(
self,
mocker: MockerFixture,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
paperless_dirs: PaperlessDirs,
page: Path,
document: Document,
@@ -636,9 +633,14 @@ class TestSavedZoneExtraction:
make_zone(template, "Title"),
make_zone(template, "Number", "custom_field", custom_field=field),
]
mock_run.side_effect = [
mocker.MagicMock(stdout="New title"),
mocker.MagicMock(stdout="42"),
mock_zone_ocr_run.side_effect = [
subprocess.CompletedProcess(
args=[],
returncode=0,
stdout="New title",
stderr="",
),
subprocess.CompletedProcess(args=[], returncode=0, stdout="42", stderr=""),
]
save = document.save
@@ -664,8 +666,7 @@ class TestSavedZoneExtraction:
def test_last_successful_template_wins(
self,
mocker: MockerFixture,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
page: Path,
document: Document,
template: OcrTemplate,
@@ -684,8 +685,9 @@ class TestSavedZoneExtraction:
third = make_template("Third")
make_zone(third, "Third", validation_regex="accepted")
templates = list(OcrTemplate.objects.order_by("pk"))
mock_run.side_effect = [
mocker.MagicMock(stdout=value) for value in ["First", "Second", "rejected"]
mock_zone_ocr_run.side_effect = [
subprocess.CompletedProcess(args=[], returncode=0, stdout=value, stderr="")
for value in ["First", "Second", "rejected"]
]
run_zone_extraction(document, page, templates=templates)
@@ -695,7 +697,7 @@ class TestSavedZoneExtraction:
def test_document_run_renders_each_page_once_across_templates(
self,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
paperless_dirs: PaperlessDirs,
document: Document,
template: OcrTemplate,
@@ -727,12 +729,14 @@ class TestSavedZoneExtraction:
return subprocess.CompletedProcess(args, 0)
return subprocess.CompletedProcess(args, 0, stdout="extracted")
mock_run.side_effect = execute
mock_zone_ocr_run.side_effect = execute
results = run_zone_extraction(document, pdf, templates=[template, second])
render_calls = [
call for call in mock_run.call_args_list if call.args[0][0] == "pdftoppm"
call
for call in mock_zone_ocr_run.call_args_list
if call.args[0][0] == "pdftoppm"
]
assert len(render_calls) == 2
assert [
@@ -743,7 +747,7 @@ class TestSavedZoneExtraction:
def test_zone_dimensions_override_template_dimensions(
self,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
paperless_dirs: PaperlessDirs,
page: Path,
document: Document,
@@ -769,7 +773,7 @@ class TestSavedZoneExtraction:
assert crop.size == (20, 10)
return subprocess.CompletedProcess(args, 0, stdout="extracted")
mock_run.side_effect = read_crop
mock_zone_ocr_run.side_effect = read_crop
_process_template(
document,
@@ -831,7 +835,7 @@ class TestSavedZoneExtraction:
def test_empty_combination_cannot_clear_a_field(
self,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
paperless_dirs: PaperlessDirs,
page: Path,
document: Document,
@@ -847,7 +851,7 @@ class TestSavedZoneExtraction:
"""
zone = make_zone(template, "Title")
template.combine_formats = {"title": "{Unknown}"}
mock_run.return_value.stdout = "extracted"
mock_zone_ocr_run.return_value.stdout = "extracted"
results = _process_template(
document,
@@ -863,8 +867,7 @@ class TestSavedZoneExtraction:
def test_skipped_results_never_report_existing_document_values(
self,
mocker: MockerFixture,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
paperless_dirs: PaperlessDirs,
page: Path,
document: Document,
@@ -885,8 +888,8 @@ class TestSavedZoneExtraction:
make_zone(template, "Invalid", "asn"),
make_zone(template, "Unassigned", "custom_field"),
]
mock_run.side_effect = [
mocker.MagicMock(stdout=value)
mock_zone_ocr_run.side_effect = [
subprocess.CompletedProcess(args=[], returncode=0, stdout=value, stderr="")
for value in ["", "rejected", "abc", "orphan"]
]
@@ -909,8 +912,7 @@ class TestSavedZoneExtraction:
def test_failed_template_reports_rollback_and_next_template_still_runs(
self,
mocker: MockerFixture,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
page: Path,
document: Document,
template: OcrTemplate,
@@ -930,8 +932,9 @@ class TestSavedZoneExtraction:
make_zone(template, "Conflict", "asn")
second = make_template("Second")
make_zone(second, "Second")
mock_run.side_effect = [
mocker.MagicMock(stdout=value) for value in ["Rolled back", "42", "Applied"]
mock_zone_ocr_run.side_effect = [
subprocess.CompletedProcess(args=[], returncode=0, stdout=value, stderr="")
for value in ["Rolled back", "42", "Applied"]
]
results = run_zone_extraction(document, page, templates=[template, second])
@@ -948,7 +951,7 @@ class TestSavedZoneExtraction:
def test_literal_format_does_not_write_when_every_zone_failed(
self,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
paperless_dirs: PaperlessDirs,
page: Path,
document: Document,
@@ -964,7 +967,7 @@ class TestSavedZoneExtraction:
"""
zone = make_zone(template, "Title")
template.combine_formats = {"title": "Invoice {Title}"}
mock_run.return_value.stdout = ""
mock_zone_ocr_run.return_value.stdout = ""
results = _process_template(
document,
@@ -980,7 +983,7 @@ class TestSavedZoneExtraction:
def test_saved_extraction_matches_preview_and_rejects_invalid_value(
self,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
paperless_dirs: PaperlessDirs,
page: Path,
document: Document,
@@ -1004,7 +1007,7 @@ class TestSavedZoneExtraction:
)
def preview_and_save(raw_text: str) -> None:
mock_run.return_value.stdout = raw_text
mock_zone_ocr_run.return_value.stdout = raw_text
preview = extract_zone_preview(page, zone, 100, 50, 1)
assert preview["raw_text"] == raw_text
assert preview["value"] == raw_text.upper()
@@ -1772,15 +1775,11 @@ class TestRenderPages:
pdf.write_bytes(b"%PDF-1.4")
return pdf
@pytest.fixture
def mock_run(self, mocker: MockerFixture) -> MagicMock:
return mocker.patch("documents.zone_ocr.subprocess.run")
def test_renders_requested_pages_in_order_and_skips_out_of_range(
self,
pdf: Path,
tmp_path: Path,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -1795,11 +1794,11 @@ class TestRenderPages:
def render(args: list[str], **kwargs: object) -> None:
Path(args[-1]).with_suffix(".png").write_bytes(b"rendered")
mock_run.side_effect = render
mock_zone_ocr_run.side_effect = render
pages = _render_pages(pdf, {-1, 0, 2, 3}, tmp_path, 3)
assert list(pages) == [0, 2]
assert mock_run.call_count == 2
for page, call in zip([1, 3], mock_run.call_args_list, strict=True):
assert mock_zone_ocr_run.call_count == 2
for page, call in zip([1, 3], mock_zone_ocr_run.call_args_list, strict=True):
args = call.args[0]
assert "-singlefile" in args
assert args[args.index("-f") + 1] == str(page)
@@ -1807,7 +1806,7 @@ class TestRenderPages:
assert call.kwargs["timeout"] == 60
assert call.kwargs["check"]
@pytest.mark.usefixtures("mock_run")
@pytest.mark.usefixtures("mock_zone_ocr_run")
def test_missing_render_output_is_not_a_page(
self,
pdf: Path,
@@ -1841,7 +1840,7 @@ class TestRenderPages:
self,
pdf: Path,
tmp_path: Path,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
error: Exception,
) -> None:
"""
@@ -1858,14 +1857,14 @@ class TestRenderPages:
raise error
Path(args[-1]).with_suffix(".png").write_bytes(b"rendered")
mock_run.side_effect = render
mock_zone_ocr_run.side_effect = render
assert list(_render_pages(pdf, {0, 1}, tmp_path, 2)) == [1]
def test_missing_renderer_stops_without_retrying_every_page(
self,
pdf: Path,
tmp_path: Path,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -1875,14 +1874,14 @@ class TestRenderPages:
THEN:
- No pages are returned and the renderer is only attempted once
"""
mock_run.side_effect = FileNotFoundError("pdftoppm")
mock_zone_ocr_run.side_effect = FileNotFoundError("pdftoppm")
assert _render_pages(pdf, {0, 1}, tmp_path, 2) == {}
mock_run.assert_called_once()
mock_zone_ocr_run.assert_called_once()
def test_image_is_used_directly_only_for_first_page(
self,
tmp_path: Path,
mock_run: MagicMock,
mock_zone_ocr_run: MagicMock,
) -> None:
"""
GIVEN:
@@ -1898,7 +1897,7 @@ class TestRenderPages:
image.save(image_path)
assert _render_pages(image_path, {0}, tmp_path, 1) == {0: image_path}
assert _render_pages(image_path, {1}, tmp_path, 1) == {}
mock_run.assert_not_called()
mock_zone_ocr_run.assert_not_called()
class TestDetectMime:
@@ -2008,13 +2007,6 @@ class TestResolveDocPath:
class TestRunZoneExtraction:
"""Tests for the full extraction pipeline."""
@pytest.fixture
def custom_field(self) -> CustomField:
return CustomFieldFactory(
name="Invoice Number",
data_type=CustomField.FieldDataType.STRING,
)
@pytest.fixture
def document(self) -> Document:
return DocumentFactory(
@@ -2034,24 +2026,13 @@ class TestRunZoneExtraction:
return mocker.patch("documents.zone_ocr._process_template")
@pytest.fixture
def template_with_zone(
self,
custom_field: CustomField,
) -> Callable[..., OcrTemplate]:
"""Build a template with one custom field zone, matching "test" by default."""
def template_with_zone(self) -> Callable[..., OcrTemplate]:
"""Build a template with one default zone, matching "test" by default."""
def _make(name: str, **kwargs: Any) -> OcrTemplate:
kwargs.setdefault("match", "test")
template = OcrTemplateFactory(
name=name,
**kwargs,
)
OcrTemplateZoneFactory(
template=template,
name="Zone",
target=OcrTemplateZone.TargetType.CUSTOM_FIELD,
custom_field=custom_field,
)
template = OcrTemplateFactory(name=name, **kwargs)
OcrTemplateZoneFactory(template=template)
return template
return _make
@@ -2081,6 +2062,8 @@ class TestRunZoneExtraction:
self,
template_with_zone: Callable[..., OcrTemplate],
document: Document,
pdf: Path,
mock_process: MagicMock,
) -> None:
"""
GIVEN:
@@ -2088,13 +2071,13 @@ class TestRunZoneExtraction:
WHEN:
- Zone extraction runs
THEN:
- No custom field instances are created
- The template is not processed
"""
template_with_zone("Disabled", enabled=False)
run_zone_extraction(document, Path("/nonexistent"))
run_zone_extraction(document, pdf)
assert CustomFieldInstance.objects.count() == 0
mock_process.assert_not_called()
def test_skips_template_with_no_zones(
self,