]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Extended tests for ASN barcode parsing
authorPeter Kappelt <kappelt.peter@gmail.com>
Mon, 16 Jan 2023 13:16:35 +0000 (14:16 +0100)
committerTrenton H <797416+stumpylog@users.noreply.github.com>
Tue, 24 Jan 2023 17:43:52 +0000 (09:43 -0800)
src/documents/tests/samples/barcodes/barcode-39-asn-123.pdf [new file with mode: 0644]
src/documents/tests/samples/barcodes/barcode-39-asn-123.png [new file with mode: 0644]
src/documents/tests/samples/barcodes/barcode-39-asn-custom-prefix.pdf [new file with mode: 0644]
src/documents/tests/samples/barcodes/barcode-39-asn-custom-prefix.png [new file with mode: 0644]
src/documents/tests/samples/barcodes/barcode-39-asn-invalid.pdf [new file with mode: 0644]
src/documents/tests/samples/barcodes/barcode-39-asn-invalid.png [new file with mode: 0644]
src/documents/tests/test_barcodes.py

diff --git a/src/documents/tests/samples/barcodes/barcode-39-asn-123.pdf b/src/documents/tests/samples/barcodes/barcode-39-asn-123.pdf
new file mode 100644 (file)
index 0000000..2e3dfef
Binary files /dev/null and b/src/documents/tests/samples/barcodes/barcode-39-asn-123.pdf differ
diff --git a/src/documents/tests/samples/barcodes/barcode-39-asn-123.png b/src/documents/tests/samples/barcodes/barcode-39-asn-123.png
new file mode 100644 (file)
index 0000000..e0f7359
Binary files /dev/null and b/src/documents/tests/samples/barcodes/barcode-39-asn-123.png differ
diff --git a/src/documents/tests/samples/barcodes/barcode-39-asn-custom-prefix.pdf b/src/documents/tests/samples/barcodes/barcode-39-asn-custom-prefix.pdf
new file mode 100644 (file)
index 0000000..5b1da4c
Binary files /dev/null and b/src/documents/tests/samples/barcodes/barcode-39-asn-custom-prefix.pdf differ
diff --git a/src/documents/tests/samples/barcodes/barcode-39-asn-custom-prefix.png b/src/documents/tests/samples/barcodes/barcode-39-asn-custom-prefix.png
new file mode 100644 (file)
index 0000000..5712c69
Binary files /dev/null and b/src/documents/tests/samples/barcodes/barcode-39-asn-custom-prefix.png differ
diff --git a/src/documents/tests/samples/barcodes/barcode-39-asn-invalid.pdf b/src/documents/tests/samples/barcodes/barcode-39-asn-invalid.pdf
new file mode 100644 (file)
index 0000000..d0f3a86
Binary files /dev/null and b/src/documents/tests/samples/barcodes/barcode-39-asn-invalid.pdf differ
diff --git a/src/documents/tests/samples/barcodes/barcode-39-asn-invalid.png b/src/documents/tests/samples/barcodes/barcode-39-asn-invalid.png
new file mode 100644 (file)
index 0000000..cc7f0d4
Binary files /dev/null and b/src/documents/tests/samples/barcodes/barcode-39-asn-invalid.png differ
index cdaffcb3bf563f7da78ba759be8f258cc52051be..15e7efd94fff14f8cd370e3075538f9d95726f1c 100644 (file)
@@ -110,6 +110,30 @@ class TestBarcode(DirectoriesMixin, TestCase):
         img = Image.open(test_file)
         self.assertEqual(barcodes.barcode_reader(img), ["CUSTOM BARCODE"])
 
+    def test_barcode_reader_asn_normal(self):
+        test_file = os.path.join(
+            self.BARCODE_SAMPLE_DIR,
+            "barcode-39-asn-123.png",
+        )
+        img = Image.open(test_file)
+        self.assertEqual(barcodes.barcode_reader(img), ["ASN00123"])
+
+    def test_barcode_reader_asn_invalid(self):
+        test_file = os.path.join(
+            self.BARCODE_SAMPLE_DIR,
+            "barcode-39-asn-invalid.png",
+        )
+        img = Image.open(test_file)
+        self.assertEqual(barcodes.barcode_reader(img), ["ASNXYZXYZ"])
+
+    def test_barcode_reader_asn_customprefix(self):
+        test_file = os.path.join(
+            self.BARCODE_SAMPLE_DIR,
+            "barcode-39-asn-custom-prefix.png",
+        )
+        img = Image.open(test_file)
+        self.assertEqual(barcodes.barcode_reader(img), ["CUSTOM-PREFIX-00123"])
+
     def test_get_mime_type(self):
         tiff_file = os.path.join(
             self.SAMPLE_DIR,
@@ -607,3 +631,57 @@ class TestBarcode(DirectoriesMixin, TestCase):
 
         self.assertEqual(pdf_file, test_file)
         self.assertListEqual(separator_page_numbers, [])
+
+    def test_scan_file_for_asn_barcode(self):
+        test_file = os.path.join(
+            self.BARCODE_SAMPLE_DIR,
+            "barcode-39-asn-123.pdf",
+        )
+        pdf_file, parsed_barcodes = barcodes.scan_file_for_barcodes(
+            test_file,
+        )
+        asn = barcodes.get_asn_from_barcodes(parsed_barcodes)
+
+        self.assertEqual(pdf_file, test_file)
+        self.assertEqual(asn, 123)
+
+    def test_scan_file_for_asn_not_existing(self):
+        test_file = os.path.join(
+            self.BARCODE_SAMPLE_DIR,
+            "patch-code-t.pdf",
+        )
+        pdf_file, parsed_barcodes = barcodes.scan_file_for_barcodes(
+            test_file,
+        )
+        asn = barcodes.get_asn_from_barcodes(parsed_barcodes)
+
+        self.assertEqual(pdf_file, test_file)
+        self.assertEqual(asn, None)
+
+    def test_scan_file_for_asn_barcode_invalid(self):
+        test_file = os.path.join(
+            self.BARCODE_SAMPLE_DIR,
+            "barcode-39-asn-invalid.pdf",
+        )
+        pdf_file, parsed_barcodes = barcodes.scan_file_for_barcodes(
+            test_file,
+        )
+
+        asn = barcodes.get_asn_from_barcodes(parsed_barcodes)
+
+        self.assertEqual(pdf_file, test_file)
+        self.assertEqual(asn, None)
+
+    @override_settings(CONSUMER_ASN_BARCODE_PREFIX="CUSTOM-PREFIX-")
+    def test_scan_file_for_asn_custom_prefix(self):
+        test_file = os.path.join(
+            self.BARCODE_SAMPLE_DIR,
+            "barcode-39-asn-custom-prefix.pdf",
+        )
+        pdf_file, parsed_barcodes = barcodes.scan_file_for_barcodes(
+            test_file,
+        )
+        asn = barcodes.get_asn_from_barcodes(parsed_barcodes)
+
+        self.assertEqual(pdf_file, test_file)
+        self.assertEqual(asn, 123)