mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-21 04:52:40 +00:00
Chore: log a warning when a zone title is cut to 128 characters
This commit is contained in:
1 parent
a7d4c04a4b
commit
d6ae3e1ec6
2 files changed
+41
No files matched your search
@@ -2797,6 +2797,41 @@ class TestPrepareFieldUpdate:
|
||||
with pytest.raises(ValueError, match="unknown"):
|
||||
_prepare_field_update(OcrTemplateZone(target="unknown"), "value")
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("length", "warns"),
|
||||
[
|
||||
pytest.param(128, False, id="at-the-limit"),
|
||||
pytest.param(129, True, id="over-the-limit"),
|
||||
],
|
||||
)
|
||||
def test_title_is_cut_to_128_characters_with_a_warning(
|
||||
self,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
length: int,
|
||||
*,
|
||||
warns: bool,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A zone targeting the title and extracted text at or over 128 characters
|
||||
WHEN:
|
||||
- A field update is prepared for it
|
||||
THEN:
|
||||
- At most the first 128 characters are kept
|
||||
- A warning is logged only when the text was cut
|
||||
"""
|
||||
zone = OcrTemplateZoneFactory(
|
||||
target=OcrTemplateZone.TargetType.TITLE,
|
||||
name="Heading",
|
||||
)
|
||||
|
||||
with caplog.at_level(logging.WARNING, logger="paperless.zone_ocr"):
|
||||
update = _prepare_field_update(zone, "x" * length)
|
||||
|
||||
assert update is not None
|
||||
assert update.value == "x" * 128
|
||||
assert any("Heading" in message for message in caplog.messages) is warns
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("ocr_language", "expected"),
|
||||
[
|
||||
|
||||
@@ -1078,6 +1078,12 @@ def _prepare_field_update(
|
||||
)
|
||||
model = CustomFieldInstance
|
||||
case OcrTemplateZone.TargetType.TITLE:
|
||||
if len(value) > 128:
|
||||
logger.warning(
|
||||
"Zone OCR: title from zone '%s' is %d characters, keeping the first 128",
|
||||
zone.name,
|
||||
len(value),
|
||||
)
|
||||
field_name, typed_value = "title", value[:128]
|
||||
model = Document
|
||||
case OcrTemplateZone.TargetType.ASN:
|
||||
|
||||
Reference in new issue
Block a user