]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
add tests
authorFlorian Brandes <florian.brandes@posteo.de>
Sun, 17 Apr 2022 14:10:26 +0000 (16:10 +0200)
committerFlorian Brandes <florian.brandes@posteo.de>
Sun, 17 Apr 2022 14:10:53 +0000 (16:10 +0200)
fix indention
add more documentation

Signed-off-by: Florian Brandes <florian.brandes@posteo.de>
docs/configuration.rst
src/documents/tasks.py
src/documents/tests/test_tasks.py

index 248d7a4920db79177a2b63d0f002c38b924127c4..20cd2941f4a4cc6a6e0bc5ac3a59077cb0c56d04 100644 (file)
@@ -624,12 +624,17 @@ PAPERLESS_CONSUMER_ENABLE_BARCODES=<bool>
     If no barcodes are detected in the uploaded file, no page separation
     will happen.
 
+    The original document will be removed and the separated pages will be
+    saved as pdf.
+
     Defaults to false.
 
 PAPERLESS_CONSUMER_BARCODE_TIFF_SUPPORT=<bool>
     Whether TIFF image files should be scanned for barcodes.
     This will automatically convert any TIFF image(s) to pdfs for later
     processing.
+    This only has an effect, if PAPERLESS_CONSUMER_ENABLE_BARCODES has been
+    enabled.
 
     Defaults to false.
 
index dfa2c6bcd12a6a5c80aab0bb38826272d15e0e86..e20554f7625226dba00bde9d0292f86be0ecc4ff 100644 (file)
@@ -119,20 +119,21 @@ def convert_from_tiff_to_pdf(filepath: str) -> str:
             images[0].save(newpath)
         else:
             images[0].save(newpath, save_all=True, append_images=images[1:])
-        os.unlink(filepath)
     except OSError as e:
         logger.warning(
             f"Could not save the file as pdf. "
             f"The original image file was not deleted. Error: "
             f"{str(e)}",
         )
+        return ""
+    os.unlink(filepath)
     image.close()
     return newpath
 
 
 def scan_file_for_separating_barcodes(filepath: str) -> List[int]:
     """
-    Scan the provided file for page separating barcodes
+    Scan the provided pdf file for page separating barcodes
     Returns a list of pagenumbers, which separate the file
     """
     separator_page_numbers = []
@@ -149,7 +150,7 @@ def scan_file_for_separating_barcodes(filepath: str) -> List[int]:
 
 def separate_pages(filepath: str, pages_to_split_on: List[int]) -> List[str]:
     """
-    Separate the provided file on the pages_to_split_on.
+    Separate the provided pdf file on the pages_to_split_on.
     The pages which are defined by page_numbers will be removed.
     Returns a list of (temporary) filepaths to consume.
     These will need to be deleted later.
index df9e10077ab4d14929270773285c104daad508cd..baff2f5341794f870fd95406679b75ece1c756b9 100644 (file)
@@ -445,6 +445,43 @@ class TestTasks(DirectoriesMixin, TestCase):
 
         self.assertEqual(tasks.consume_file(dst), "File successfully split")
 
+    @override_settings(
+        CONSUMER_ENABLE_BARCODES=True,
+        CONSUMER_BARCODE_TIFF_SUPPORT=True,
+    )
+    @mock.patch("documents.consumer.Consumer.try_consume_file")
+    def test_consume_barcode_unsupported_jpg_file(self, m):
+        """
+        This test assumes barcode and TIFF support are enabled and
+        the user uploads an unsupported image file (e.g. jpg)
+
+        The function shouldn't try to scan for separating barcodes
+        and continue archiving the file as is.
+        """
+        test_file = os.path.join(
+            os.path.dirname(__file__),
+            "samples",
+            "simple.jpg",
+        )
+        dst = os.path.join(settings.SCRATCH_DIR, "simple.jpg")
+        shutil.copy(test_file, dst)
+        with self.assertLogs("paperless.tasks", level="WARNING") as cm:
+            self.assertIn("Success", tasks.consume_file(dst))
+        self.assertEqual(
+            cm.output,
+            [
+                "WARNING:paperless.tasks:Unsupported file format for barcode reader: .jpg",
+            ],
+        )
+        m.assert_called_once()
+
+        args, kwargs = m.call_args
+        self.assertIsNone(kwargs["override_filename"])
+        self.assertIsNone(kwargs["override_title"])
+        self.assertIsNone(kwargs["override_correspondent_id"])
+        self.assertIsNone(kwargs["override_document_type_id"])
+        self.assertIsNone(kwargs["override_tag_ids"])
+
     @mock.patch("documents.tasks.sanity_checker.check_sanity")
     def test_sanity_check_success(self, m):
         m.return_value = SanityCheckMessages()