mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-19 20:25:21 +00:00
Report actual OCR extraction outcomes instead of existing field values
Co-authored-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
5 files changed
+206
-71
No files matched your search
@@ -8,6 +8,7 @@ from unittest.mock import patch
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.contrib.auth.models import User
|
||||
from django.db import IntegrityError
|
||||
from PIL import Image
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
@@ -947,14 +948,16 @@ class TestOcrTemplatesAPI(DirectoriesMixin, APITestCase):
|
||||
)
|
||||
extract.assert_not_called()
|
||||
|
||||
@patch("documents.zone_ocr.run_zone_extraction")
|
||||
@patch("documents.zone_ocr.subprocess.run")
|
||||
def test_run_zone_ocr_reports_only_templates_visible_to_the_document_owner(
|
||||
self,
|
||||
extract,
|
||||
run,
|
||||
):
|
||||
owner = User.objects.create_user(username="document_owner")
|
||||
document = DocumentFactory(content=CONTENT, owner=owner)
|
||||
document.source_path.write_bytes(b"test PDF")
|
||||
with Image.new("RGB", (100, 100), "white") as image:
|
||||
image.save(document.source_path, format="PNG")
|
||||
run.return_value.stdout = "extracted"
|
||||
for name, template_owner in [
|
||||
("Owned", owner),
|
||||
("Unowned", None),
|
||||
@@ -987,7 +990,7 @@ class TestOcrTemplatesAPI(DirectoriesMixin, APITestCase):
|
||||
[result["template"] for result in response.json()["results"]],
|
||||
["Owned", "Unowned", "Shared"],
|
||||
)
|
||||
extract.assert_called_once()
|
||||
self.assertEqual(run.call_count, 3)
|
||||
|
||||
def test_shared_editor_cannot_transfer_ownership_or_change_sharing(self):
|
||||
editor = User.objects.create_user(username="shared_editor")
|
||||
@@ -1035,13 +1038,15 @@ class TestOcrTemplatesAPI(DirectoriesMixin, APITestCase):
|
||||
self.assertEqual(response.json(), {"error": "Document file not found"})
|
||||
extract.assert_not_called()
|
||||
|
||||
@patch("documents.zone_ocr.run_zone_extraction")
|
||||
def test_run_zone_ocr_reports_completed_results(self, extract):
|
||||
@patch("documents.zone_ocr.subprocess.run")
|
||||
def test_run_zone_ocr_reports_completed_results(self, run):
|
||||
document = DocumentFactory(
|
||||
content=CONTENT,
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
document.source_path.write_bytes(b"test PDF")
|
||||
with Image.new("RGB", (100, 100), "white") as image:
|
||||
image.save(document.source_path, format="PNG")
|
||||
run.return_value.stdout = "Extracted title"
|
||||
template = OcrTemplate.objects.create(
|
||||
name="Template",
|
||||
match="Northwind",
|
||||
@@ -1070,17 +1075,61 @@ class TestOcrTemplatesAPI(DirectoriesMixin, APITestCase):
|
||||
"template": template.name,
|
||||
"zone": "Title zone",
|
||||
"custom_field": "Title",
|
||||
"value": document.title,
|
||||
"value": "Extracted title",
|
||||
"reason": None,
|
||||
},
|
||||
],
|
||||
},
|
||||
)
|
||||
extract.assert_called_once()
|
||||
run.assert_called_once()
|
||||
|
||||
@patch("documents.zone_ocr.run_zone_extraction")
|
||||
def test_run_zone_ocr_reports_all_target_types_and_missing_values(self, extract):
|
||||
@patch("documents.zone_ocr.subprocess.run")
|
||||
def test_run_zone_ocr_does_not_report_an_old_title_when_extraction_is_empty(
|
||||
self,
|
||||
run,
|
||||
):
|
||||
document = DocumentFactory(title="Existing title", content=CONTENT)
|
||||
with Image.new("RGB", (100, 100), "white") as image:
|
||||
image.save(document.source_path, format="PNG")
|
||||
template = OcrTemplate.objects.create(
|
||||
name="Template",
|
||||
match="Northwind",
|
||||
source_width=100,
|
||||
source_height=100,
|
||||
)
|
||||
OcrTemplateZone.objects.create(
|
||||
template=template,
|
||||
name="Title",
|
||||
target="title",
|
||||
x=0,
|
||||
y=0,
|
||||
width=20,
|
||||
height=20,
|
||||
)
|
||||
run.return_value.stdout = ""
|
||||
response = self.client.post(f"/api/documents/{document.pk}/run-zone-ocr/")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()["results"][0]["reason"], "no_value")
|
||||
self.assertIsNone(response.json()["results"][0]["value"])
|
||||
document.refresh_from_db()
|
||||
self.assertEqual(document.title, "Existing title")
|
||||
|
||||
@patch("documents.zone_ocr.subprocess.run")
|
||||
def test_run_zone_ocr_reports_all_target_types_and_missing_values(self, run):
|
||||
document = DocumentFactory(content=CONTENT, archive_serial_number=123)
|
||||
document.source_path.write_bytes(b"test PDF")
|
||||
with Image.new("RGB", (100, 100), "white") as image:
|
||||
image.save(document.source_path, format="PNG")
|
||||
run.side_effect = [
|
||||
subprocess.CompletedProcess([], 0, stdout=value)
|
||||
for value in [
|
||||
"New invoice",
|
||||
"",
|
||||
"orphan",
|
||||
"New title",
|
||||
"456",
|
||||
"2026-04-13",
|
||||
]
|
||||
]
|
||||
template = OcrTemplate.objects.create(
|
||||
name="Test",
|
||||
source_width=100,
|
||||
@@ -1121,12 +1170,12 @@ class TestOcrTemplatesAPI(DirectoriesMixin, APITestCase):
|
||||
self.assertEqual(
|
||||
values,
|
||||
{
|
||||
"Present": "INV-123",
|
||||
"Present": "New invoice",
|
||||
"Missing": None,
|
||||
"Unassigned": None,
|
||||
"Title": document.title,
|
||||
"ASN": 123,
|
||||
"Created": document.created.isoformat(),
|
||||
"Title": "New title",
|
||||
"ASN": 456,
|
||||
"Created": "2026-04-13",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@@ -303,6 +303,70 @@ class TestSavedZoneExtraction(DirectoriesMixin, TestCase):
|
||||
self.document.refresh_from_db()
|
||||
self.assertEqual(self.document.title, "Second")
|
||||
|
||||
@patch("documents.zone_ocr.subprocess.run")
|
||||
def test_skipped_results_never_report_existing_document_values(self, run):
|
||||
zones = [
|
||||
self.zone("Empty"),
|
||||
self.zone("Rejected", validation_regex="accepted"),
|
||||
self.zone("Invalid", "asn"),
|
||||
self.zone("Unassigned", "custom_field"),
|
||||
]
|
||||
run.side_effect = [
|
||||
MagicMock(stdout=value) for value in ["", "rejected", "abc", "orphan"]
|
||||
]
|
||||
results = _process_template(self.document, self.page, self.template, zones)
|
||||
self.assertEqual(
|
||||
[result.reason for result in results],
|
||||
[
|
||||
"no_value",
|
||||
"regex_mismatch",
|
||||
"invalid_value",
|
||||
"unassigned",
|
||||
],
|
||||
)
|
||||
self.assertEqual([result.value for result in results], [None] * 4)
|
||||
self.assertEqual(self.document.title, "Original")
|
||||
|
||||
@patch("documents.zone_ocr.subprocess.run")
|
||||
def test_failed_template_reports_rollback_and_next_template_still_runs(self, run):
|
||||
DocumentFactory(archive_serial_number=42)
|
||||
self.zone("Title")
|
||||
self.zone("Conflict", "asn")
|
||||
second = OcrTemplate.objects.create(
|
||||
name="Second",
|
||||
source_width=100,
|
||||
source_height=50,
|
||||
)
|
||||
first = self.template
|
||||
self.template = second
|
||||
self.zone("Second")
|
||||
run.side_effect = [
|
||||
MagicMock(stdout=value) for value in ["Rolled back", "42", "Applied"]
|
||||
]
|
||||
results = run_zone_extraction(
|
||||
self.document,
|
||||
self.page,
|
||||
templates=[first, second],
|
||||
)
|
||||
self.assertEqual(
|
||||
[result.reason for result in results],
|
||||
["template_failed", "template_failed", None],
|
||||
)
|
||||
self.assertEqual([result.value for result in results], [None, None, "Applied"])
|
||||
self.document.refresh_from_db()
|
||||
self.assertEqual(self.document.title, "Applied")
|
||||
self.assertIsNone(self.document.archive_serial_number)
|
||||
|
||||
@patch("documents.zone_ocr.subprocess.run")
|
||||
def test_literal_format_does_not_write_when_every_zone_failed(self, run):
|
||||
zone = self.zone("Title")
|
||||
self.template.combine_formats = {"title": "Invoice {Title}"}
|
||||
run.return_value.stdout = ""
|
||||
results = _process_template(self.document, self.page, self.template, [zone])
|
||||
self.assertEqual(results[0].reason, "no_value")
|
||||
self.assertIsNone(results[0].value)
|
||||
self.assertEqual(self.document.title, "Original")
|
||||
|
||||
@patch("documents.zone_ocr.subprocess.run")
|
||||
def test_saved_extraction_matches_preview_and_rejects_invalid_value(self, run):
|
||||
zone = self.zone(
|
||||
@@ -326,15 +390,13 @@ class TestSavedZoneExtraction(DirectoriesMixin, TestCase):
|
||||
|
||||
class TestCombineFieldValue(SimpleTestCase):
|
||||
def test_default_format_preserves_zone_order_and_duplicate_names(self):
|
||||
zones = [
|
||||
(OcrTemplateZone(name="Same"), value) for value in ["one", None, "two"]
|
||||
]
|
||||
zones = [("Same", value) for value in ["one", None, "two"]]
|
||||
self.assertEqual(_combine_field_value("", zones), "one two")
|
||||
|
||||
def test_format_preserves_existing_missing_token_cleanup(self):
|
||||
zones = [
|
||||
(OcrTemplateZone(name="First"), "one"),
|
||||
(OcrTemplateZone(name="Second"), None),
|
||||
("First", "one"),
|
||||
("Second", None),
|
||||
]
|
||||
self.assertEqual(
|
||||
_combine_field_value("{ First } / {Second} / {Unknown}", zones),
|
||||
|
||||
Reference in new issue
Block a user