From 6624d6144b78621a43e2bac000ab573ee192c880 Mon Sep 17 00:00:00 2001 From: Pascal Krahmer <5699756+pkrahmer@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:07:58 +0100 Subject: [PATCH] Increased test coverage --- src/documents/barcodes.py | 4 ++-- src/documents/tests/test_barcodes.py | 24 ++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/documents/barcodes.py b/src/documents/barcodes.py index 61667715f..2b36b697f 100644 --- a/src/documents/barcodes.py +++ b/src/documents/barcodes.py @@ -333,8 +333,8 @@ class BarcodePlugin(ConsumeTaskPlugin): ) tags.append(tag.pk) - except ValueError as e: - logger.warning( + except Exception as e: + logger.error( f"Failed to find or create TAG '{raw}' because: {e}", ) diff --git a/src/documents/tests/test_barcodes.py b/src/documents/tests/test_barcodes.py index 402d438e7..3dd6d62ff 100644 --- a/src/documents/tests/test_barcodes.py +++ b/src/documents/tests/test_barcodes.py @@ -812,11 +812,13 @@ class TestTagBarcode(DirectoriesMixin, SampleDirMixin, GetReaderPluginMixin, Tes """ test_file = self.BARCODE_SAMPLE_DIR / "barcode-39-asn-custom-prefix.pdf" with self.get_reader(test_file) as reader: + reader.metadata.tag_ids = [99] reader.run() self.assertEqual(reader.pdf_file, test_file) tags = reader.metadata.tag_ids - self.assertEqual(len(tags), 1) - self.assertEqual(Tag.objects.get(name__iexact="00123").pk, tags[0]) + self.assertEqual(len(tags), 2) + self.assertEqual(tags[0], 99) + self.assertEqual(Tag.objects.get(name__iexact="00123").pk, tags[1]) @override_settings( CONSUMER_ENABLE_TAG_BARCODE=True, @@ -844,3 +846,21 @@ class TestTagBarcode(DirectoriesMixin, SampleDirMixin, GetReaderPluginMixin, Tes self.assertEqual(Tag.objects.get(name__iexact="00125").pk, tags[2]) self.assertEqual(Tag.objects.get(name__iexact="00126").pk, tags[3]) self.assertEqual(Tag.objects.get(name__iexact="00127").pk, tags[4]) + + @override_settings( + CONSUMER_ENABLE_TAG_BARCODE=True, + CONSUMER_TAG_BARCODE_MAPPING={"CUSTOM-PREFIX-(.*)": "\\g<3>"}, + ) + def test_scan_file_for_tag_raises_value_error(self): + """ + GIVEN: + - Any error occurs during tag barcode processing + THEN: + - The processing should be skipped and not break the import + """ + test_file = self.BARCODE_SAMPLE_DIR / "barcode-39-asn-custom-prefix.pdf" + with self.get_reader(test_file) as reader: + reader.run() + # expect error to be caught and logged only + tags = reader.metadata.tag_ids + self.assertEqual(tags, None)