A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
add conditions to unittests
phailb01cbc9aa08a
diff --git a/src/paperless_mail/tests/samples/sample.html.pdf.webp b/src/paperless_mail/tests/samples/sample.html.pdf.webpnew file mode 100644index 000000000..534157660Binary files /dev/null and b/src/paperless_mail/tests/samples/sample.html.pdf.webp differdiff --git a/src/paperless_mail/tests/test_parsers.py b/src/paperless_mail/tests/test_parsers.pyindex a0c19e6f5..2bc0fbdad 100644--- a/src/paperless_mail/tests/test_parsers.py+++ b/src/paperless_mail/tests/test_parsers.py@@ -268,7 +268,7 @@ class TestParser(TestCase):@mock.patch("documents.loggers.LoggingMixin.log") # Disable log outputdef test_tika_parse(self, m):html = '<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body><p>Some Text</p></body></html>'- expected_text = "\n\n\n\n\n\n\n\n\nSome Text\n"+ expected_text = "Some Text"tika_server_original = self.parser.tika_server@@ -285,7 +285,7 @@ class TestParser(TestCase):# Check successful parsingparsed = self.parser.tika_parse(html)- self.assertEqual(expected_text, parsed)+ self.assertEqual(expected_text, parsed.strip())@mock.patch("paperless_mail.parsers.MailDocumentParser.generate_pdf_from_mail")@mock.patch("paperless_mail.parsers.MailDocumentParser.generate_pdf_from_html")@@ -355,19 +355,46 @@ class TestParser(TestCase):) as html_expected_handle:self.assertHTMLEqual(html_expected_handle.read(), html_handle.read())- @mock.patch("documents.loggers.LoggingMixin.log") # Disable log output- def test_generate_pdf_from_mail(self, m):+ @mock.patch("paperless_mail.parsers.requests.post")+ @mock.patch("paperless_mail.parsers.MailDocumentParser.mail_to_html")+ def test_generate_pdf_from_mail(+ self,+ mock_mail_to_html: mock.MagicMock,+ mock_post: mock.MagicMock,+ ):+ mock_response = mock.MagicMock()+ mock_response.content = b"Content"+ mock_post.return_value = mock_response++ mock_mail_to_html.return_value = "Testresponse"+mail = self.parser.get_parsed(os.path.join(self.SAMPLE_FILES, "html.eml"))- pdf_path = os.path.join(self.parser.tempdir, "test_generate_pdf_from_mail.pdf")+ retval = self.parser.generate_pdf_from_mail(mail)+ self.assertEqual(b"Content", retval)- with open(pdf_path, "wb") as file:- file.write(self.parser.generate_pdf_from_mail(mail))- file.close()+ mock_generate_pdf_from_mail.assert_called_once_with(+ self.parser.get_parsed(None),+ )+ mock_generate_pdf_from_html.assert_called_once_with(+ self.parser.get_parsed(None).html,+ self.parser.get_parsed(None).attachments,+ )+ self.assertEqual(+ self.parser.gotenberg_server + "/forms/pdfengines/merge",+ mock_post.call_args.args[0],+ )+ self.assertEqual({}, mock_post.call_args.kwargs["headers"])+ self.assertEqual(+ b"Mail Return",+ mock_post.call_args.kwargs["files"]["1_mail.pdf"][1].read(),+ )+ self.assertEqual(+ b"HTML Return",+ mock_post.call_args.kwargs["files"]["2_html.pdf"][1].read(),+ )- extracted = extract_text(pdf_path)- expected = "From Name <[email protected]>\n\n2022-10-15 09:23\n\nSubject HTML Message\n\nTo [email protected]\n\nAttachments IntM6gnXFm00FEV5.png (6.89 KiB), 600+kbfile.txt (0.59 MiB)\n\nSome Text \n\nand an embedded image.\n\n\x0c"- self.assertEqual(expected, extracted)+ mock_response.raise_for_status.assert_called_once()def test_transform_inline_html(self):class MailAttachmentMock:@@ -391,13 +418,18 @@ class TestParser(TestCase):self.assertTrue(result[0][0] in resulting_html)self.assertFalse("<script" in resulting_html.lower())+ @mock.patch("paperless_mail.parsers.requests.post")@mock.patch("documents.loggers.LoggingMixin.log") # Disable log output- def test_generate_pdf_from_html(self, m):+ def test_generate_pdf_from_html(self, m, mock_post: mock.MagicMock):class MailAttachmentMock:def __init__(self, payload, content_id):self.payload = payloadself.content_id = content_id+ mock_response = mock.MagicMock()+ mock_response.content = b"Content"+ mock_post.return_value = mock_response+result = Nonewith open(os.path.join(self.SAMPLE_FILES, "sample.html")) as html_file:@@ -409,12 +441,30 @@ class TestParser(TestCase):]result = self.parser.generate_pdf_from_html(html, attachments)- pdf_path = os.path.join(self.parser.tempdir, "test_generate_pdf_from_html.pdf")+ self.assertEqual(+ self.parser.gotenberg_server + "/forms/chromium/convert/html",+ mock_post.call_args.args[0],+ )+ self.assertEqual({}, mock_post.call_args.kwargs["headers"])+ self.assertEqual(+ {+ "marginTop": "0.1",+ "marginBottom": "0.1",+ "marginLeft": "0.1",+ "marginRight": "0.1",+ "paperWidth": "8.27",+ "paperHeight": "11.7",+ "scale": "1.0",+ },+ mock_post.call_args.kwargs["data"],+ )- with open(pdf_path, "wb") as file:- file.write(result)- file.close()+ # read to assert it is a file like object.+ mock_post.call_args.kwargs["files"]["cidpart1pNdUSz0sD3NqVtPgexamplede"][+ 1+ ].read()+ mock_post.call_args.kwargs["files"]["index.html"][1].read()++ mock_response.raise_for_status.assert_called_once()- extracted = extract_text(pdf_path)- expected = "Some Text\n\n This image should not be shown.\n\nand an embedded image.\n\nParagraph unchanged.\n\n\x0c"- self.assertEqual(expected, extracted)+ self.assertEqual(b"Content", result)diff --git a/src/paperless_mail/tests/test_parsers_live.py b/src/paperless_mail/tests/test_parsers_live.pyindex 6676ebc1e..a0fa1f54d 100644--- a/src/paperless_mail/tests/test_parsers_live.py+++ b/src/paperless_mail/tests/test_parsers_live.py@@ -62,7 +62,7 @@ class TestParserLive(TestCase):@mock.patch("documents.loggers.LoggingMixin.log") # Disable log outputdef test_tika_parse(self, m):html = '<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body><p>Some Text</p></body></html>'- expected_text = "\n\n\n\n\n\n\n\n\nSome Text\n"+ expected_text = "Some Text"tika_server_original = self.parser.tika_server@@ -79,7 +79,7 @@ class TestParserLive(TestCase):# Check successful parsingparsed = self.parser.tika_parse(html)- self.assertEqual(expected_text, parsed)+ self.assertEqual(expected_text, parsed.strip())@pytest.mark.skipif("GOTENBERG_LIVE" not in os.environ,@@ -124,7 +124,10 @@ class TestParserLive(TestCase):file.write(self.parser.generate_pdf_from_mail(mail))file.close()- converted = os.path.join(parser.tempdir, "test_generate_pdf_from_mail.webp")+ converted = os.path.join(+ self.parser.tempdir,+ "test_generate_pdf_from_mail.webp",+ )run_convert(density=300,scale="500x5000>",@@ -149,6 +152,42 @@ class TestParserLive(TestCase):f"PDF looks different. Check if {converted} looks weird.",)+ @pytest.mark.skipif(+ "GOTENBERG_LIVE" not in os.environ,+ reason="No gotenberg server",+ )+ @mock.patch("documents.loggers.LoggingMixin.log") # Disable log output+ def test_generate_pdf_from_html_no_convert(self, m):+ class MailAttachmentMock:+ def __init__(self, payload, content_id):+ self.payload = payload+ self.content_id = content_id++ result = None++ with open(os.path.join(self.SAMPLE_FILES, "sample.html")) as html_file:+ with open(os.path.join(self.SAMPLE_FILES, "sample.png"), "rb") as png_file:+ html = html_file.read()+ png = png_file.read()+ attachments = [+ MailAttachmentMock(png, "[email protected]"),+ ]+ result = self.parser.generate_pdf_from_html(html, attachments)++ pdf_path = os.path.join(self.parser.tempdir, "test_generate_pdf_from_html.pdf")++ with open(pdf_path, "wb") as file:+ file.write(result)+ file.close()++ extracted = extract_text(pdf_path)+ expected = "Some Text\n\n This image should not be shown.\n\nand an embedded image.\n\nParagraph unchanged.\n\n\x0c"+ self.assertEqual(expected, extracted)++ @pytest.mark.skipif(+ "GOTENBERG_LIVE" not in os.environ,+ reason="No gotenberg server",+ )# Only run if convert is available@pytest.mark.skipif("PAPERLESS_TEST_SKIP_CONVERT" in os.environ,@@ -156,7 +195,6 @@ class TestParserLive(TestCase):)@mock.patch("documents.loggers.LoggingMixin.log") # Disable log outputdef test_generate_pdf_from_html(self, m):- # TODOclass MailAttachmentMock:def __init__(self, payload, content_id):self.payload = payload@@ -173,13 +211,13 @@ class TestParserLive(TestCase):]result = self.parser.generate_pdf_from_html(html, attachments)- pdf_path = os.path.join(self.parser.tempdir, "test_generate_pdf_from_html.pdf")+ pdf_path = os.path.join(self.parser.tempdir, "sample.html.pdf")with open(pdf_path, "wb") as file:file.write(result)file.close()- converted = os.path.join(parser.tempdir, "test_generate_pdf_from_html.webp")+ converted = os.path.join(self.parser.tempdir, "sample.html.pdf.webp")run_convert(density=300,scale="500x5000>",@@ -195,9 +233,10 @@ class TestParserLive(TestCase):thumb_hash = self.hashfile(converted)# The created pdf is not reproducible. But the converted image should always look the same.- expected_hash = (- "267d61f0ab8f128a037002a424b2cb4bfe18a81e17f0b70f15d241688ed47d1a"+ expected_hash = self.hashfile(+ os.path.join(self.SAMPLE_FILES, "sample.html.pdf.webp"),)+self.assertEqual(thumb_hash,expected_hash,@@ -206,6 +245,10 @@ class TestParserLive(TestCase):)@staticmethod+ @pytest.mark.skipif(+ "GOTENBERG_LIVE" not in os.environ,+ reason="No gotenberg server",+ )def test_is_online_image_still_available():"""A public image is used in the html sample file. We have no control