diff --git a/src/documents/tests/test_api_ocr_templates.py b/src/documents/tests/test_api_ocr_templates.py index 7d9bdb5450..0f5de5e053 100644 --- a/src/documents/tests/test_api_ocr_templates.py +++ b/src/documents/tests/test_api_ocr_templates.py @@ -813,27 +813,30 @@ class TestOcrTemplateValidation: assert response.status_code == 200 @pytest.mark.parametrize( - "changes", + ("changes", "expected_status"), [ - pytest.param({"x": 900, "width": 200}, id="exceeds-width"), - pytest.param({"y": 990, "height": 50}, id="exceeds-height"), + pytest.param({"x": 900, "width": 200}, 400, id="exceeds-width"), + pytest.param({"y": 990, "height": 50}, 400, id="exceeds-height"), + pytest.param({}, 200, id="within-bounds"), ], ) - def test_partial_zone_replacement_uses_saved_dimensions( + def test_partial_zone_replacement_checks_saved_dimensions( self, admin_client: APIClient, admin_user: User, make_zone: Callable[..., dict[str, Any]], changes: dict[str, int], + expected_status: int, ) -> None: """ GIVEN: - A saved 1000x1000 template WHEN: - - A PATCH replaces zones with one extending beyond the saved - source dimensions + - A PATCH replaces zones with one that either extends beyond the + saved source dimensions or lies within them THEN: - - The request is rejected + - The request is rejected when the zone is out of bounds and + succeeds when it is within them """ template = OcrTemplateFactory( name="Test", @@ -844,32 +847,7 @@ class TestOcrTemplateValidation: {"zones": [make_zone(**changes)]}, format="json", ) - assert response.status_code == 400 - - def test_partial_zone_replacement_within_saved_dimensions_accepted( - self, - admin_client: APIClient, - admin_user: User, - make_zone: Callable[..., dict[str, Any]], - ) -> None: - """ - GIVEN: - - A saved 1000x1000 template - WHEN: - - A PATCH replaces zones with one inside the saved dimensions - THEN: - - The request succeeds - """ - template = OcrTemplateFactory( - name="Test", - owner=admin_user, - ) - response = admin_client.patch( - f"{ENDPOINT}{template.pk}/", - {"zones": [make_zone()]}, - format="json", - ) - assert response.status_code == 200 + assert response.status_code == expected_status def test_partial_zone_replacement_requires_complete_zones( self, @@ -1249,59 +1227,38 @@ class TestOcrTemplateDelete: @pytest.mark.django_db @pytest.mark.usefixtures("paperless_dirs") class TestRunZoneOcr: - def test_without_matching_template_is_skipped( - self, - admin_client: APIClient, - mock_extract: MagicMock, - ) -> None: - """ - GIVEN: - - A document and a template that does not match it - WHEN: - - Zone OCR is run on the document - THEN: - - The run is skipped with reason no_template and nothing is extracted - """ - document = DocumentFactory(content=CONTENT) - OcrTemplateFactory( - name="Other sender", - match="Contoso", - ) - - response = admin_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk)) - - assert response.status_code == status.HTTP_200_OK - assert response.json() == SKIPPED_NO_TEMPLATE - mock_extract.assert_not_called() - @pytest.mark.parametrize( - "with_disabled_template", + "template_kwargs_list", [ - pytest.param(False, id="no-templates"), - pytest.param(True, id="disabled-template"), + pytest.param([], id="no-templates"), + pytest.param( + [{"name": "Disabled template", "match": "Northwind", "enabled": False}], + id="disabled-template", + ), + pytest.param( + [{"name": "Other sender", "match": "Contoso"}], + id="non-matching-template", + ), ], ) - def test_without_enabled_template_is_skipped( + def test_without_enabled_matching_template_is_skipped( self, admin_client: APIClient, mock_extract: MagicMock, - with_disabled_template: bool, # noqa: FBT001 + template_kwargs_list: list[dict[str, Any]], ) -> None: """ GIVEN: - - A document and either no templates or a matching disabled template + - A document and either no templates, only a matching disabled + template, or only a template that does not match the document WHEN: - Zone OCR is run on the document THEN: - The run is skipped with reason no_template and nothing is extracted """ document = DocumentFactory(content=CONTENT) - if with_disabled_template: - OcrTemplateFactory( - name="Disabled template", - match="Northwind", - enabled=False, - ) + for template_kwargs in template_kwargs_list: + OcrTemplateFactory(**template_kwargs) response = admin_client.post(RUN_ZONE_OCR_URL.format(pk=document.pk)) diff --git a/src/documents/tests/test_zone_ocr.py b/src/documents/tests/test_zone_ocr.py index 9ca856d06d..09689ed5eb 100644 --- a/src/documents/tests/test_zone_ocr.py +++ b/src/documents/tests/test_zone_ocr.py @@ -265,45 +265,64 @@ class TestZoneExtraction: assert result.raw_text is None assert result.value is None - def test_barcode_failure_returns_no_value( + @pytest.mark.parametrize( + "reader_kwargs", + [ + pytest.param( + {"side_effect": RuntimeError("reader failed")}, + id="reader-raises", + ), + pytest.param({"return_value": []}, id="nothing-found"), + ], + ) + def test_barcode_without_value_returns_no_value( self, mocker: MockerFixture, page: Path, zone: OcrTemplateZone, tmp_path: Path, + reader_kwargs: dict[str, Any], ) -> None: """ GIVEN: - - A QR code zone and a barcode reader that raises + - A QR code zone and a barcode reader that either raises or finds + no barcode WHEN: - The zone is extracted THEN: - No text or value is returned """ - mocker.patch( - "zxingcpp.read_barcodes", - side_effect=RuntimeError("reader failed"), - ) + mocker.patch("zxingcpp.read_barcodes", **reader_kwargs) zone.transform = "qr_code" result = _extract_zone(page, zone, 100, 50, tmp_path) assert result.raw_text is None assert result.value is None - def test_preview_tiny_crop_returns_empty_result( + @pytest.mark.parametrize( + ("attribute", "value"), + [ + pytest.param("width", 0, id="zero-width"), + pytest.param("page", 2, id="unavailable-page"), + ], + ) + def test_preview_unreadable_zone_returns_empty_result( self, page: Path, zone: OcrTemplateZone, mock_zone_ocr_run: MagicMock, + attribute: str, + value: int, ) -> None: """ GIVEN: - - A zone with zero width + - A zone that cannot be read, either because its crop has zero + width or because it targets a page that is not available WHEN: - A preview is requested THEN: - An empty result is returned and the reader is never run """ - zone.width = 0 + setattr(zone, attribute, value) assert extract_zone_preview(page, zone, 100, 50, 1) == { "raw_text": None, "value": None, @@ -366,27 +385,6 @@ class TestZoneExtraction: with pytest.raises(ValueError): read_barcodes.call_args.args[0].getpixel((0, 0)) - def test_missing_barcode_returns_no_value( - self, - mocker: MockerFixture, - page: Path, - zone: OcrTemplateZone, - tmp_path: Path, - ) -> None: - """ - GIVEN: - - A QR code zone and a barcode reader that finds nothing - WHEN: - - The zone is extracted - THEN: - - No text or value is returned - """ - mocker.patch("zxingcpp.read_barcodes", return_value=[]) - zone.transform = "qr_code" - result = _extract_zone(page, zone, 100, 50, tmp_path) - assert result.raw_text is None - assert result.value is None - def test_preview_uses_image_dimensions_without_writing( self, page: Path, @@ -407,27 +405,6 @@ class TestZoneExtraction: "value": "ACCOUNT ABC", } - def test_preview_unavailable_page_skips_reader( - self, - page: Path, - zone: OcrTemplateZone, - mock_zone_ocr_run: MagicMock, - ) -> None: - """ - GIVEN: - - A zone targeting a page that is not available - WHEN: - - A preview is requested - THEN: - - An empty result is returned and the reader is never run - """ - zone.page = 2 - assert extract_zone_preview(page, zone, 100, 50, 1) == { - "raw_text": None, - "value": None, - } - mock_zone_ocr_run.assert_not_called() - def make_template(name: str, **kwargs: Any) -> OcrTemplate: """Build a saved template, 100x50 by default.""" @@ -2035,43 +2012,32 @@ class TestRunZoneExtraction: return _make - def test_skips_template_that_does_not_match( + @pytest.mark.parametrize( + ("template_name", "template_kwargs"), + [ + pytest.param("Other sender", {"match": "Contoso"}, id="non-matching"), + pytest.param("Disabled", {"enabled": False}, id="disabled"), + ], + ) + def test_skips_ineligible_template( self, mock_process: MagicMock, template_with_zone: Callable[..., OcrTemplate], document: Document, pdf: Path, + template_name: str, + template_kwargs: dict[str, Any], ) -> None: """ GIVEN: - - A template whose match text is not in the document content + - A template whose match text is not in the document content, or a + disabled template that would otherwise match WHEN: - - Zone extraction runs + - Zone extraction runs against a real document file THEN: - The template is not processed """ - template_with_zone("Other sender", match="Contoso") - - run_zone_extraction(document, pdf) - - mock_process.assert_not_called() - - def test_skips_disabled_template( - self, - template_with_zone: Callable[..., OcrTemplate], - document: Document, - pdf: Path, - mock_process: MagicMock, - ) -> None: - """ - GIVEN: - - A disabled template matching the document - WHEN: - - Zone extraction runs - THEN: - - The template is not processed - """ - template_with_zone("Disabled", enabled=False) + template_with_zone(template_name, **template_kwargs) run_zone_extraction(document, pdf) @@ -2101,27 +2067,6 @@ class TestRunZoneExtraction: assert results == [] mock_process.assert_not_called() - def test_calls_process_for_enabled_template( - self, - mock_process: MagicMock, - template_with_zone: Callable[..., OcrTemplate], - document: Document, - pdf: Path, - ) -> None: - """ - GIVEN: - - An enabled matching template with a zone - WHEN: - - Zone extraction runs - THEN: - - The template is processed - """ - template_with_zone("Active", enabled=True) - - run_zone_extraction(document, pdf) - - assert mock_process.called - def test_handles_process_exception_gracefully( self, mock_process: MagicMock, @@ -2175,24 +2120,37 @@ class TestRunZoneExtraction: ] mock_process.assert_not_called() - def test_multiple_templates_all_process( + @pytest.mark.parametrize( + "template_names", + [ + pytest.param(["Active"], id="one-template"), + pytest.param( + ["Template 0", "Template 1", "Template 2"], + id="three-templates", + ), + ], + ) + def test_processes_every_enabled_template( self, mock_process: MagicMock, template_with_zone: Callable[..., OcrTemplate], document: Document, pdf: Path, + template_names: list[str], ) -> None: """ GIVEN: - - Three enabled matching templates, each with a zone + - One or more enabled matching templates, each with a zone WHEN: - Zone extraction runs THEN: - - All three templates are processed + - Each template is processed exactly once """ - for i in range(3): - template_with_zone(f"Template {i}", enabled=True) + for name in template_names: + template_with_zone(name, enabled=True) run_zone_extraction(document, pdf) - assert mock_process.call_count == 3 + processed = [call.args[2].name for call in mock_process.call_args_list] + assert mock_process.call_count == len(template_names) + assert sorted(processed) == sorted(template_names)