diff --git a/src/paperless_tesseract/tests/samples/multi-page-mixed.pdf b/src/paperless_tesseract/tests/samples/multi-page-mixed.pdf new file mode 100644 index 000000000..cca8db419 Binary files /dev/null and b/src/paperless_tesseract/tests/samples/multi-page-mixed.pdf differ diff --git a/src/paperless_tesseract/tests/test_parser.py b/src/paperless_tesseract/tests/test_parser.py index 4fd314667..b64abb382 100644 --- a/src/paperless_tesseract/tests/test_parser.py +++ b/src/paperless_tesseract/tests/test_parser.py @@ -7,7 +7,7 @@ from django.test import TestCase, override_settings from documents.parsers import ParseError, run_convert from documents.tests.utils import DirectoriesMixin -from paperless_tesseract.parsers import RasterisedDocumentParser, get_text_from_pdf, strip_excess_whitespace +from paperless_tesseract.parsers import RasterisedDocumentParser, strip_excess_whitespace image_to_string_calls = [] @@ -38,7 +38,12 @@ class TestParser(DirectoriesMixin, TestCase): def assertContainsStrings(self, content, strings): # Asserts that all strings appear in content, in the given order. - indices = [content.index(s) for s in strings] + indices = [] + for s in strings: + if s in content: + indices.append(content.index(s)) + else: + self.fail(f"'{s}' is not in '{content}'") self.assertListEqual(indices, sorted(indices)) text_cases = [ @@ -69,7 +74,8 @@ class TestParser(DirectoriesMixin, TestCase): SAMPLE_FILES = os.path.join(os.path.dirname(__file__), "samples") def test_get_text_from_pdf(self): - text = get_text_from_pdf(os.path.join(self.SAMPLE_FILES, 'simple-digital.pdf')) + parser = RasterisedDocumentParser(uuid.uuid4()) + text = parser.extract_text(None, os.path.join(self.SAMPLE_FILES, 'simple-digital.pdf')) self.assertContainsStrings(text.strip(), ["This is a test document."]) @@ -129,15 +135,21 @@ class TestParser(DirectoriesMixin, TestCase): self.assertIsNone(parser.archive_path) self.assertContainsStrings(parser.get_text(), ["Please enter your name in here:", "This is a PDF document with a form."]) - @override_settings(OCR_MODE="redo") - @mock.patch("paperless_tesseract.parsers.get_text_from_pdf", lambda _: None) - def test_with_form_error_notext(self): + @override_settings(OCR_MODE="skip") + def test_encrypted(self): parser = RasterisedDocumentParser(None) - def f(): - parser.parse(os.path.join(self.SAMPLE_FILES, "with-form.pdf"), "application/pdf") + parser.parse(os.path.join(self.SAMPLE_FILES, "encrypted.pdf"), "application/pdf") - self.assertRaises(ParseError, f) + self.assertIsNone(parser.archive_path) + self.assertContainsStrings(parser.get_text(), ["This is a digitally signed PDF, created with Acrobat Pro for the Paperless project to enable", "automated testing of signed/encrypted PDFs"]) + + @override_settings(OCR_MODE="redo") + def test_with_form_error_notext(self): + parser = RasterisedDocumentParser(None) + parser.parse(os.path.join(self.SAMPLE_FILES, "with-form.pdf"), "application/pdf") + + self.assertContainsStrings(parser.get_text(), ["Please enter your name in here:", "This is a PDF document with a form."]) @override_settings(OCR_MODE="force") def test_with_form_force(self): @@ -253,9 +265,41 @@ class TestParser(DirectoriesMixin, TestCase): def test_skip_noarchive_notext(self): parser = RasterisedDocumentParser(None) parser.parse(os.path.join(self.SAMPLE_FILES, "multi-page-images.pdf"), "application/pdf") - self.assertTrue(os.path.join(parser.archive_path)) + self.assertTrue(os.path.isfile(parser.archive_path)) self.assertContainsStrings(parser.get_text().lower(), ["page 1", "page 2", "page 3"]) + @override_settings(OCR_MODE="skip") + def test_multi_page_mixed(self): + parser = RasterisedDocumentParser(None) + parser.parse(os.path.join(self.SAMPLE_FILES, "multi-page-mixed.pdf"), "application/pdf") + self.assertTrue(os.path.isfile(parser.archive_path)) + self.assertContainsStrings(parser.get_text().lower(), ["page 1", "page 2", "page 3", "page 4", "page 5", "page 6"]) + + with open(os.path.join(parser.tempdir, "sidecar.txt")) as f: + sidecar = f.read() + + self.assertIn("[OCR skipped on page 4]", sidecar) + self.assertIn("[OCR skipped on page 5]", sidecar) + self.assertIn("[OCR skipped on page 6]", sidecar) + + @override_settings(OCR_MODE="skip_noarchive") + def test_multi_page_mixed_no_archive(self): + parser = RasterisedDocumentParser(None) + parser.parse(os.path.join(self.SAMPLE_FILES, "multi-page-mixed.pdf"), "application/pdf") + self.assertIsNone(parser.archive_path) + self.assertContainsStrings(parser.get_text().lower(), ["page 4", "page 5", "page 6"]) + + @override_settings(OCR_MODE="skip", OCR_ROTATE_PAGES=True) + def test_rotate(self): + parser = RasterisedDocumentParser(None) + parser.parse(os.path.join(self.SAMPLE_FILES, "rotated.pdf"), "application/pdf") + self.assertContainsStrings(parser.get_text(), [ + "This is the text that appears on the first page. It’s a lot of text.", + "Even if the pages are rotated, OCRmyPDF still gets the job done.", + "This is a really weird file with lots of nonsense text.", + "If you read this, it’s your own fault. Also check your screen orientation." + ]) + class TestParserFileTypes(DirectoriesMixin, TestCase):