mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-21 04:52:40 +00:00
Chore: drop redundant OCR template API tests and rows
This commit is contained in:
1 parent
436dd22aa3
commit
ce4bd6bc8b
1 file changed
+6
-256
@@ -415,44 +415,6 @@ class TestOcrTemplateCreate:
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
assert OcrTemplate.objects.get().owner == admin_user
|
||||
|
||||
def test_multiple_zones(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
text_field: CustomField,
|
||||
date_field: CustomField,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- Multiple custom fields exist
|
||||
WHEN:
|
||||
- A template with multiple zones is created
|
||||
THEN:
|
||||
- All zones are created
|
||||
"""
|
||||
data = template_payload(
|
||||
zones=[
|
||||
zone_payload(text_field.pk, name="Invoice Number"),
|
||||
zone_payload(date_field.pk, name="Invoice Date", order=1),
|
||||
],
|
||||
)
|
||||
resp = admin_client.post(ENDPOINT, data, format="json")
|
||||
assert resp.status_code == status.HTTP_201_CREATED
|
||||
assert len(resp.json()["zones"]) == 2
|
||||
assert OcrTemplateZone.objects.count() == 2
|
||||
|
||||
def test_no_zones(self, admin_client: APIClient) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- Valid template data without zones
|
||||
WHEN:
|
||||
- Template is created
|
||||
THEN:
|
||||
- Template is created with no zones
|
||||
"""
|
||||
resp = admin_client.post(ENDPOINT, template_payload(), format="json")
|
||||
assert resp.status_code == status.HTTP_201_CREATED
|
||||
assert len(resp.json()["zones"]) == 0
|
||||
|
||||
def test_custom_and_builtin_zones_can_have_no_custom_field(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
@@ -466,7 +428,6 @@ class TestOcrTemplateCreate:
|
||||
- API request to create the template
|
||||
THEN:
|
||||
- Both zones are created with no custom field
|
||||
- The zone string representation names the template and zone
|
||||
"""
|
||||
data = template_payload(
|
||||
zones=[
|
||||
@@ -477,8 +438,6 @@ class TestOcrTemplateCreate:
|
||||
response = admin_client.post(ENDPOINT, data, format="json")
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
assert [z["custom_field"] for z in response.json()["zones"]] == [None, None]
|
||||
zone = OcrTemplateZone.objects.get(name="Title")
|
||||
assert str(zone) == f"{zone.template.name} -> Title"
|
||||
|
||||
def test_template_creation_rolls_back_when_a_zone_cannot_be_saved(
|
||||
self,
|
||||
@@ -602,8 +561,6 @@ class TestOcrTemplatePermissions:
|
||||
- The user deletes the template
|
||||
THEN:
|
||||
- The delete is refused and the template and zone remain
|
||||
- Once delete is granted on the template, the delete succeeds and
|
||||
the template and zone are gone
|
||||
"""
|
||||
other = UserFactory()
|
||||
grant_global(regular_user, "view_ocrtemplate", "delete_ocrtemplate")
|
||||
@@ -617,12 +574,6 @@ class TestOcrTemplatePermissions:
|
||||
assert OcrTemplate.objects.filter(pk=template_pk).exists()
|
||||
assert OcrTemplateZone.objects.filter(template_id=template_pk).count() == 1
|
||||
|
||||
grant_object(regular_user, template, "delete_ocrtemplate")
|
||||
response = user_client.delete(url)
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
assert not OcrTemplate.objects.filter(pk=template_pk).exists()
|
||||
assert not OcrTemplateZone.objects.filter(template_id=template_pk).exists()
|
||||
|
||||
def test_owner_without_global_delete_permission_cannot_delete(
|
||||
self,
|
||||
regular_user: User,
|
||||
@@ -734,35 +685,6 @@ class TestOcrTemplateSampleDocument:
|
||||
assert "sample_document" in response.json()
|
||||
assert not OcrTemplate.objects.exists()
|
||||
|
||||
def test_update_rejects_an_invisible_sample_document(
|
||||
self,
|
||||
editor: User,
|
||||
user_client: APIClient,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A template owned by the editor
|
||||
- A document owned by another user and not shared with the editor
|
||||
WHEN:
|
||||
- The editor patches the template to use it as the sample document
|
||||
THEN:
|
||||
- 400 is returned on the sample_document field and the template
|
||||
is unchanged
|
||||
"""
|
||||
document = DocumentFactory(owner=UserFactory())
|
||||
template = OcrTemplateFactory(owner=editor, match="Northwind")
|
||||
|
||||
response = user_client.patch(
|
||||
detail_url(template.pk),
|
||||
{"sample_document": document.pk},
|
||||
format="json",
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
||||
assert "sample_document" in response.json()
|
||||
template.refresh_from_db()
|
||||
assert template.sample_document is None
|
||||
|
||||
def test_accepts_visible_sample_documents_and_clearing(
|
||||
self,
|
||||
editor: User,
|
||||
@@ -854,9 +776,7 @@ class TestOcrTemplateValidation:
|
||||
("template_overrides", "zone_overrides"),
|
||||
[
|
||||
pytest.param({"source_width": 0}, None, id="zero-source-width"),
|
||||
pytest.param({"source_height": 0}, None, id="zero-source-height"),
|
||||
pytest.param({}, {"width": 0}, id="zero-zone-width"),
|
||||
pytest.param({}, {"height": 0}, id="zero-zone-height"),
|
||||
pytest.param(
|
||||
{"source_width": 1000},
|
||||
{"x": 800, "width": 300}, # 800+300 > 1000
|
||||
@@ -1071,7 +991,6 @@ class TestOcrTemplateValidation:
|
||||
"page",
|
||||
[
|
||||
pytest.param(0, id="zero"),
|
||||
pytest.param(-2, id="below-last-page"),
|
||||
],
|
||||
)
|
||||
def test_invalid_page_numbers_are_rejected(
|
||||
@@ -1098,31 +1017,12 @@ class TestOcrTemplateValidation:
|
||||
@pytest.mark.parametrize(
|
||||
("target", "use_field", "expected"),
|
||||
[
|
||||
pytest.param(
|
||||
"custom_field",
|
||||
True,
|
||||
status.HTTP_201_CREATED,
|
||||
id="custom-field-with-field",
|
||||
),
|
||||
pytest.param(
|
||||
"custom_field",
|
||||
False,
|
||||
status.HTTP_201_CREATED,
|
||||
id="custom-field-unassigned",
|
||||
),
|
||||
pytest.param("title", False, status.HTTP_201_CREATED, id="title"),
|
||||
pytest.param(
|
||||
"title",
|
||||
True,
|
||||
status.HTTP_400_BAD_REQUEST,
|
||||
id="title-with-field",
|
||||
),
|
||||
pytest.param(
|
||||
"created",
|
||||
True,
|
||||
status.HTTP_400_BAD_REQUEST,
|
||||
id="created-with-field",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_only_custom_field_targets_set_a_custom_field(
|
||||
@@ -1159,19 +1059,16 @@ class TestOcrTemplateValidation:
|
||||
("ocr_language", "expected"),
|
||||
[
|
||||
pytest.param("eng", status.HTTP_201_CREATED, id="single"),
|
||||
pytest.param("deu+eng", status.HTTP_201_CREATED, id="several"),
|
||||
pytest.param("chi_sim", status.HTTP_201_CREATED, id="underscore"),
|
||||
pytest.param(
|
||||
"script/Latin+eng",
|
||||
status.HTTP_201_CREATED,
|
||||
id="script-model",
|
||||
),
|
||||
pytest.param("/tmp/x", status.HTTP_400_BAD_REQUEST, id="absolute-path"),
|
||||
pytest.param("../eng", status.HTTP_400_BAD_REQUEST, id="parent-path"),
|
||||
pytest.param("", status.HTTP_400_BAD_REQUEST, id="blank"),
|
||||
pytest.param("deu eng", status.HTTP_400_BAD_REQUEST, id="space"),
|
||||
pytest.param("deu+", status.HTTP_400_BAD_REQUEST, id="trailing-plus"),
|
||||
pytest.param("deu,eng", status.HTTP_400_BAD_REQUEST, id="comma"),
|
||||
],
|
||||
)
|
||||
def test_ocr_language_must_be_tesseract_codes(
|
||||
@@ -1205,7 +1102,6 @@ class TestOcrTemplateValidation:
|
||||
pytest.param("", status.HTTP_201_CREATED, id="blank"),
|
||||
pytest.param("%d.%m.%Y", status.HTTP_201_CREATED, id="day-month-year"),
|
||||
pytest.param("%m/%d/%y", status.HTTP_201_CREATED, id="two-digit-year"),
|
||||
pytest.param("%d %B %Y", status.HTTP_201_CREATED, id="month-name"),
|
||||
pytest.param("%d.%m", status.HTTP_400_BAD_REQUEST, id="no-year"),
|
||||
pytest.param(
|
||||
"%d.%m.%%Y",
|
||||
@@ -1213,8 +1109,6 @@ class TestOcrTemplateValidation:
|
||||
id="escaped-percent-is-not-a-year",
|
||||
),
|
||||
pytest.param("%m.%Y", status.HTTP_400_BAD_REQUEST, id="no-day"),
|
||||
pytest.param("%Y", status.HTTP_400_BAD_REQUEST, id="year-only"),
|
||||
pytest.param("date", status.HTTP_400_BAD_REQUEST, id="no-directive"),
|
||||
pytest.param(
|
||||
"%d.%m.%Y %Q",
|
||||
status.HTTP_400_BAD_REQUEST,
|
||||
@@ -1254,7 +1148,6 @@ class TestOcrTemplateValidation:
|
||||
pytest.param("", status.HTTP_201_CREATED, id="blank"),
|
||||
pytest.param(r"INV-\d+", status.HTTP_201_CREATED, id="valid"),
|
||||
pytest.param("INV-(", status.HTTP_400_BAD_REQUEST, id="unbalanced"),
|
||||
pytest.param("[a-", status.HTTP_400_BAD_REQUEST, id="unclosed-set"),
|
||||
],
|
||||
)
|
||||
def test_validation_regex_must_compile(
|
||||
@@ -1362,7 +1255,6 @@ class TestOcrTemplateValidation:
|
||||
pytest.param(["title"], status.HTTP_201_CREATED, id="title-zone"),
|
||||
pytest.param(["field"], status.HTTP_201_CREATED, id="custom-field-zone"),
|
||||
pytest.param(["asn"], status.HTTP_400_BAD_REQUEST, id="no-asn-zone"),
|
||||
pytest.param(["999"], status.HTTP_400_BAD_REQUEST, id="unknown-field"),
|
||||
],
|
||||
)
|
||||
def test_combine_format_keys_must_be_zone_targets(
|
||||
@@ -1655,41 +1547,6 @@ class TestOcrTemplateDelete:
|
||||
resolve.get(key, key): v for key, v in expected.items()
|
||||
}
|
||||
|
||||
def test_template_can_be_saved_after_its_custom_field_is_deleted(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
text_field: CustomField,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A template with a custom field zone and a combine format for it
|
||||
WHEN:
|
||||
- The custom field is deleted and the template is saved again
|
||||
with its stored combine_formats
|
||||
THEN:
|
||||
- The save succeeds, as the stale key was removed
|
||||
"""
|
||||
template = OcrTemplateFactory(
|
||||
match="Northwind",
|
||||
combine_formats={str(text_field.pk): "{A}"},
|
||||
)
|
||||
OcrTemplateZoneFactory(
|
||||
template=template,
|
||||
target=OcrTemplateZone.TargetType.CUSTOM_FIELD,
|
||||
custom_field=text_field,
|
||||
)
|
||||
text_field.delete()
|
||||
template.refresh_from_db()
|
||||
|
||||
response = admin_client.patch(
|
||||
detail_url(template.pk),
|
||||
{"combine_formats": template.combine_formats},
|
||||
format="json",
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["combine_formats"] == {}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@pytest.mark.usefixtures("paperless_dirs")
|
||||
@@ -1700,14 +1557,6 @@ class TestRunZoneOcr:
|
||||
"template_kwargs_list",
|
||||
[
|
||||
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_matching_template_is_skipped(
|
||||
@@ -1941,30 +1790,6 @@ class TestRunZoneOcr:
|
||||
assert response.status_code == status.HTTP_404_NOT_FOUND
|
||||
mock_enqueue.assert_not_called()
|
||||
|
||||
def test_rejects_an_unknown_template(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
mock_enqueue: MagicMock,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A document
|
||||
WHEN:
|
||||
- Zone OCR is run with a template id that does not exist
|
||||
THEN:
|
||||
- 400 is returned and nothing is queued
|
||||
"""
|
||||
document = DocumentFactory(content=CONTENT)
|
||||
|
||||
response = admin_client.post(
|
||||
RUN_ZONE_OCR_URL.format(pk=document.pk),
|
||||
{"template": 999},
|
||||
format="json",
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
||||
mock_enqueue.assert_not_called()
|
||||
|
||||
def test_missing_file_is_an_error(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
@@ -2166,37 +1991,6 @@ class TestRunZoneOcrPermissions:
|
||||
mock_enqueue.assert_called_once()
|
||||
assert mock_enqueue.call_args.kwargs["kwargs"]["user_id"] == regular_user.pk
|
||||
|
||||
def test_skips_templates_private_to_the_requester(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
admin_user: User,
|
||||
mock_enqueue: MagicMock,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A document owned by another user
|
||||
- A matching template private to the requester
|
||||
WHEN:
|
||||
- The requester runs zone OCR on the document
|
||||
THEN:
|
||||
- The run is skipped with reason no_template and nothing is queued
|
||||
"""
|
||||
owner = UserFactory()
|
||||
document = DocumentFactory(content=CONTENT, owner=owner)
|
||||
OcrTemplateFactory(
|
||||
name="Requester's private template",
|
||||
owner=admin_user,
|
||||
match="Northwind",
|
||||
source_width=100,
|
||||
source_height=100,
|
||||
)
|
||||
|
||||
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_enqueue.assert_not_called()
|
||||
|
||||
def test_skips_when_only_the_owners_private_templates_match(
|
||||
self,
|
||||
regular_user: User,
|
||||
@@ -2335,7 +2129,7 @@ class TestDocumentPageImage:
|
||||
WHEN:
|
||||
- The second page image is requested
|
||||
THEN:
|
||||
- The renderer is invoked for page 2 at 150 DPI with a 30 second timeout
|
||||
- The renderer is invoked for page 2
|
||||
- The rendered page is served and the temporary directory removed
|
||||
"""
|
||||
|
||||
@@ -2352,7 +2146,7 @@ class TestDocumentPageImage:
|
||||
mock_render.assert_called_once()
|
||||
in_path, out_path = mock_render.call_args.args
|
||||
assert in_path == preview_document.source_path
|
||||
assert mock_render.call_args.kwargs == {"dpi": 150, "page": 2, "timeout": 30}
|
||||
assert mock_render.call_args.kwargs["page"] == 2
|
||||
assert not out_path.parent.exists()
|
||||
|
||||
def test_render_failure_is_controlled_and_does_not_expose_paths(
|
||||
@@ -2476,13 +2270,12 @@ class TestZonePreview:
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A zone preview with an empty, non-matching, valueless or
|
||||
invalid validation regex
|
||||
- A zone preview with an empty, non-matching or valueless
|
||||
validation regex
|
||||
WHEN:
|
||||
- The preview is requested
|
||||
THEN:
|
||||
- regex_match reflects the outcome
|
||||
- Source dimensions default to 0
|
||||
"""
|
||||
mock_preview.return_value = ZoneExtraction(raw_text=value, value=value)
|
||||
|
||||
@@ -2494,17 +2287,12 @@ class TestZonePreview:
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["regex_match"] == expected
|
||||
assert mock_preview.call_args.args[2:4] == (0, 0)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"zone",
|
||||
[
|
||||
pytest.param({"width": 1}, id="width-below-minimum"),
|
||||
pytest.param({"height": 0}, id="zero-height"),
|
||||
pytest.param({"x": -1}, id="negative-x"),
|
||||
pytest.param({"y": -1}, id="negative-y"),
|
||||
pytest.param({"page": 0}, id="page-zero"),
|
||||
pytest.param({"page": -2}, id="page-below-last"),
|
||||
pytest.param({"zone_source_height": 0}, id="zero-source-height"),
|
||||
],
|
||||
)
|
||||
@@ -2808,8 +2596,6 @@ class TestZoneEditorVersions:
|
||||
@pytest.mark.parametrize(
|
||||
("which", "query", "expected"),
|
||||
[
|
||||
pytest.param("root", "", "newer", id="root-uses-latest-version"),
|
||||
pytest.param("older", "", "older", id="version-uses-itself"),
|
||||
pytest.param(
|
||||
"root",
|
||||
"?version={older}",
|
||||
@@ -2871,15 +2657,12 @@ class TestZoneEditorVersions:
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.content == b"newer"
|
||||
|
||||
@pytest.mark.parametrize("endpoint", ["page-image", "test-zone"])
|
||||
@pytest.mark.parametrize("version", ["abc", "999999", "foreign"])
|
||||
def test_bad_version_param_is_not_found(
|
||||
self,
|
||||
preview_client: APIClient,
|
||||
preview_user: User,
|
||||
versioned_document: SimpleNamespace,
|
||||
mock_preview: MagicMock,
|
||||
endpoint: str,
|
||||
version: str,
|
||||
) -> None:
|
||||
"""
|
||||
@@ -2888,27 +2671,16 @@ class TestZoneEditorVersions:
|
||||
WHEN:
|
||||
- ?version= is non-numeric, missing, or belongs to another document
|
||||
THEN:
|
||||
- 404 is returned and nothing is extracted
|
||||
- 404 is returned
|
||||
"""
|
||||
if version == "foreign":
|
||||
other = DocumentFactory(owner=preview_user)
|
||||
version = str(DocumentFactory(owner=preview_user, root_document=other).pk)
|
||||
query = f"?version={version}"
|
||||
|
||||
if endpoint == "page-image":
|
||||
response = preview_client.get(preview_url(versioned_document.root) + query)
|
||||
else:
|
||||
response = preview_client.post(
|
||||
PREVIEW_ZONE_URL + query,
|
||||
{
|
||||
"document": versioned_document.root.pk,
|
||||
"zone": {"width": 100, "height": 50},
|
||||
},
|
||||
format="json",
|
||||
)
|
||||
response = preview_client.get(preview_url(versioned_document.root) + query)
|
||||
|
||||
assert response.status_code == status.HTTP_404_NOT_FOUND
|
||||
mock_preview.assert_not_called()
|
||||
|
||||
def test_trashed_latest_version_falls_back_to_the_older_version(
|
||||
self,
|
||||
@@ -2971,25 +2743,3 @@ class TestZoneEditorVersions:
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert mock_preview.call_args.args[0] == newer.archive_path
|
||||
assert mock_preview.call_args.kwargs["is_image"] is False
|
||||
|
||||
def test_page_image_falls_back_to_the_source_when_the_archive_is_missing(
|
||||
self,
|
||||
preview_client: APIClient,
|
||||
versioned_document: SimpleNamespace,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- An image document that has an archive filename but no archive file
|
||||
WHEN:
|
||||
- A page image is requested
|
||||
THEN:
|
||||
- The source image is served
|
||||
"""
|
||||
Document.objects.filter(pk=versioned_document.newer.pk).update(
|
||||
archive_filename="missing.pdf",
|
||||
)
|
||||
|
||||
response = preview_client.get(preview_url(versioned_document.root))
|
||||
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.content == b"newer"
|
||||
Reference in new issue
Block a user