mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-19 20:25:21 +00:00
Ensure reindex happens after extraction on manual runs
This commit is contained in:
6 files changed
+110
-23
No files matched your search
@@ -5,6 +5,7 @@ import subprocess
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.contrib.auth.models import User
|
||||
from django.db import IntegrityError
|
||||
@@ -17,6 +18,9 @@ from documents.models import CustomFieldInstance
|
||||
from documents.models import MatchingModel
|
||||
from documents.models import OcrTemplate
|
||||
from documents.models import OcrTemplateZone
|
||||
from documents.search import get_backend
|
||||
from documents.signals import document_consumption_finished
|
||||
from documents.tasks import extract_zones
|
||||
from paperless_testing.dirs import DirectoriesMixin
|
||||
from paperless_testing.factories import DocumentFactory
|
||||
from paperless_testing.permissions import grant_global
|
||||
@@ -25,6 +29,75 @@ from paperless_testing.permissions import grant_object
|
||||
CONTENT = "Northwind Power & Light statement"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@pytest.mark.parametrize("entry_point", ["ingestion", "single", "bulk"])
|
||||
def test_zone_ocr_indexes_extracted_metadata(
|
||||
entry_point,
|
||||
admin_client,
|
||||
paperless_dirs,
|
||||
django_capture_on_commit_callbacks,
|
||||
mocker,
|
||||
):
|
||||
document = DocumentFactory(
|
||||
title="Before OCR",
|
||||
content=CONTENT,
|
||||
mime_type="image/png",
|
||||
)
|
||||
with Image.new("RGB", (100, 100), "white") as image:
|
||||
image.save(document.source_path, format="PNG")
|
||||
template = OcrTemplate.objects.create(
|
||||
name="Invoice template",
|
||||
match="Northwind",
|
||||
matching_algorithm=MatchingModel.MATCH_ANY,
|
||||
source_width=100,
|
||||
source_height=100,
|
||||
)
|
||||
field = CustomField.objects.create(name="Invoice code", data_type="string")
|
||||
OcrTemplateZone.objects.create(
|
||||
template=template,
|
||||
name="Title",
|
||||
target="title",
|
||||
x=0,
|
||||
y=0,
|
||||
width=50,
|
||||
height=50,
|
||||
)
|
||||
OcrTemplateZone.objects.create(
|
||||
template=template,
|
||||
name="Invoice code",
|
||||
custom_field=field,
|
||||
x=0,
|
||||
y=50,
|
||||
width=50,
|
||||
height=50,
|
||||
order=1,
|
||||
)
|
||||
mocker.patch(
|
||||
"documents.zone_ocr.subprocess.run",
|
||||
side_effect=[mocker.Mock(stdout="After OCR"), mocker.Mock(stdout="INV42")],
|
||||
)
|
||||
|
||||
backend = get_backend()
|
||||
if entry_point != "ingestion":
|
||||
backend.add_or_update(document)
|
||||
assert backend.search_ids("title:before", user=None) == [document.pk]
|
||||
|
||||
with django_capture_on_commit_callbacks(execute=True):
|
||||
if entry_point == "ingestion":
|
||||
document_consumption_finished.send(sender=None, document=document)
|
||||
elif entry_point == "single":
|
||||
response = admin_client.post(f"/api/documents/{document.pk}/run-zone-ocr/")
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
else:
|
||||
extract_zones(document.pk, template.pk)
|
||||
|
||||
document.refresh_from_db()
|
||||
assert document.title == "After OCR"
|
||||
assert backend.search_ids("title:before", user=None) == []
|
||||
assert backend.search_ids("title:after", user=None) == [document.pk]
|
||||
assert backend.search_ids("custom_fields.value:INV42", user=None) == [document.pk]
|
||||
|
||||
|
||||
class TestOcrTemplatesAPI(DirectoriesMixin, APITestCase):
|
||||
ENDPOINT = "/api/ocr_templates/"
|
||||
|
||||
@@ -787,7 +860,7 @@ class TestOcrTemplatesAPI(DirectoriesMixin, APITestCase):
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.json()["status"], "completed")
|
||||
extract.assert_called_once_with(document, None, templates=[template])
|
||||
extract.assert_called_once_with(document, templates=[template], reindex=True)
|
||||
|
||||
@patch("documents.zone_ocr.run_zone_extraction")
|
||||
def test_run_zone_ocr_rejects_a_template_the_user_cannot_view(self, extract):
|
||||
|
||||
@@ -1941,7 +1941,7 @@ class TestBulkEditRunZoneOcr:
|
||||
|
||||
extract_zones(doc.id)
|
||||
|
||||
extract.assert_called_once_with(doc, None)
|
||||
extract.assert_called_once_with(doc, templates=None, reindex=True)
|
||||
|
||||
def test_task_runs_the_chosen_template(self, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
@@ -1962,7 +1962,7 @@ class TestBulkEditRunZoneOcr:
|
||||
|
||||
extract_zones(doc.id, template.pk)
|
||||
|
||||
extract.assert_called_once_with(doc, None, templates=[template])
|
||||
extract.assert_called_once_with(doc, templates=[template], reindex=True)
|
||||
|
||||
def test_task_skips_a_deleted_template(self, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user