Creates a mix-in for asserting file system states

This commit is contained in:
Trenton Holmes
2023-02-19 18:00:45 -08:00
committed by Trenton H
parent be6c2cfcbd
commit acfa7d633d
14 changed files with 275 additions and 253 deletions

View File

@@ -14,6 +14,7 @@ from django.db import DatabaseError
from django.test import TestCase
from documents.models import Correspondent
from documents.tests.utils import DirectoriesMixin
from documents.tests.utils import FileSystemAssertsMixin
from imap_tools import EmailAddress
from imap_tools import FolderInfo
from imap_tools import MailboxFolderSelectError
@@ -241,7 +242,7 @@ def fake_magic_from_buffer(buffer, mime=False):
@mock.patch("paperless_mail.mail.magic.from_buffer", fake_magic_from_buffer)
class TestMail(DirectoriesMixin, TestCase):
class TestMail(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
def setUp(self):
patcher = mock.patch("paperless_mail.mail.MailBox")
m = patcher.start()
@@ -389,12 +390,12 @@ class TestMail(DirectoriesMixin, TestCase):
args1, kwargs1 = self.async_task.call_args_list[0]
args2, kwargs2 = self.async_task.call_args_list[1]
self.assertTrue(os.path.isfile(kwargs1["path"]), kwargs1["path"])
self.assertIsFile(kwargs1["path"])
self.assertEqual(kwargs1["override_title"], "file_0")
self.assertEqual(kwargs1["override_filename"], "file_0.pdf")
self.assertTrue(os.path.isfile(kwargs2["path"]), kwargs1["path"])
self.assertIsFile(kwargs2["path"])
self.assertEqual(kwargs2["override_title"], "file_1")
self.assertEqual(kwargs2["override_filename"], "file_1.pdf")
@@ -435,7 +436,7 @@ class TestMail(DirectoriesMixin, TestCase):
self.assertEqual(self.async_task.call_count, 1)
args, kwargs = self.async_task.call_args
self.assertTrue(os.path.isfile(kwargs["path"]), kwargs["path"])
self.assertIsFile(kwargs["path"])
self.assertEqual(kwargs["override_filename"], "f1.pdf")
def test_handle_disposition(self):

View File

@@ -4,10 +4,11 @@ from unittest import mock
from django.test import TestCase
from documents.parsers import ParseError
from documents.tests.utils import FileSystemAssertsMixin
from paperless_mail.parsers import MailDocumentParser
class TestParser(TestCase):
class TestParser(FileSystemAssertsMixin, TestCase):
SAMPLE_FILES = os.path.join(os.path.dirname(__file__), "samples")
def setUp(self) -> None:
@@ -331,7 +332,7 @@ class TestParser(TestCase):
)
@mock.patch("paperless_mail.parsers.MailDocumentParser.generate_pdf")
def test_parse_simple_eml(self, n):
def test_parse_simple_eml(self, m: mock.MagicMock):
"""
GIVEN:
- Fresh start
@@ -361,8 +362,8 @@ class TestParser(TestCase):
self.parser.date,
)
# Just check if file exists, the unittest for generate_pdf() goes deeper.
self.assertTrue(os.path.isfile(self.parser.archive_path))
# Just check if tried to generate archive, the unittest for generate_pdf() goes deeper.
m.assert_called()
@mock.patch("paperless_mail.parsers.parser.from_buffer")
def test_tika_parse_unsuccessful(self, mock_from_buffer: mock.MagicMock):
@@ -494,7 +495,7 @@ class TestParser(TestCase):
mock_response.content = b"Content"
mock_post.return_value = mock_response
pdf_path = self.parser.generate_pdf(os.path.join(self.SAMPLE_FILES, "html.eml"))
self.assertTrue(os.path.isfile(pdf_path))
self.assertIsFile(pdf_path)
mock_generate_pdf_from_mail.assert_called_once_with(
self.parser.get_parsed(None),

View File

@@ -7,13 +7,14 @@ from urllib.request import urlopen
import pytest
from django.test import TestCase
from documents.parsers import run_convert
from documents.tests.utils import FileSystemAssertsMixin
from imagehash import average_hash
from paperless_mail.parsers import MailDocumentParser
from pdfminer.high_level import extract_text
from PIL import Image
class TestParserLive(TestCase):
class TestParserLive(FileSystemAssertsMixin, TestCase):
SAMPLE_FILES = os.path.join(os.path.dirname(__file__), "samples")
def setUp(self) -> None:
@@ -85,7 +86,7 @@ class TestParserLive(TestCase):
os.path.join(self.SAMPLE_FILES, "simple_text.eml"),
"message/rfc822",
)
self.assertTrue(os.path.isfile(thumb))
self.assertIsFile(thumb)
expected = os.path.join(self.SAMPLE_FILES, "simple_text.eml.pdf.webp")
@@ -161,7 +162,7 @@ class TestParserLive(TestCase):
self.parser.generate_pdf,
[os.path.join(self.SAMPLE_FILES, "html.eml")],
)
self.assertTrue(os.path.isfile(pdf_path))
self.assertIsFile(pdf_path)
extracted = extract_text(pdf_path)
expected = (
@@ -232,7 +233,7 @@ class TestParserLive(TestCase):
output_file=converted,
logging_group=None,
)
self.assertTrue(os.path.isfile(converted))
self.assertIsFile(converted)
thumb_hash = self.imagehash(converted)
# The created pdf is not reproducible. But the converted image should always look the same.
@@ -337,7 +338,7 @@ class TestParserLive(TestCase):
output_file=converted,
logging_group=None,
)
self.assertTrue(os.path.isfile(converted))
self.assertIsFile(converted)
thumb_hash = self.imagehash(converted)
# The created pdf is not reproducible. But the converted image should always look the same.