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 1718cf6504
commit 0df91c31f1
14 changed files with 275 additions and 253 deletions

View File

@@ -13,13 +13,14 @@ from documents.file_handling import generate_filename
from documents.models import Document
from documents.tasks import update_document_archive_file
from documents.tests.utils import DirectoriesMixin
from documents.tests.utils import FileSystemAssertsMixin
sample_file = os.path.join(os.path.dirname(__file__), "samples", "simple.pdf")
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
class TestArchiver(DirectoriesMixin, TestCase):
class TestArchiver(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
def make_models(self):
return Document.objects.create(
checksum="A",
@@ -52,8 +53,8 @@ class TestArchiver(DirectoriesMixin, TestCase):
self.assertIsNotNone(doc.checksum)
self.assertIsNotNone(doc.archive_checksum)
self.assertTrue(os.path.isfile(doc.archive_path))
self.assertTrue(os.path.isfile(doc.source_path))
self.assertIsFile(doc.archive_path)
self.assertIsFile(doc.source_path)
self.assertTrue(filecmp.cmp(sample_file, doc.source_path))
self.assertEqual(doc.archive_filename, "none/A.pdf")
@@ -70,7 +71,7 @@ class TestArchiver(DirectoriesMixin, TestCase):
self.assertIsNotNone(doc.checksum)
self.assertIsNone(doc.archive_checksum)
self.assertIsNone(doc.archive_filename)
self.assertTrue(os.path.isfile(doc.source_path))
self.assertIsFile(doc.source_path)
@override_settings(FILENAME_FORMAT="{title}")
def test_naming_priorities(self):
@@ -104,7 +105,7 @@ class TestArchiver(DirectoriesMixin, TestCase):
self.assertEqual(doc2.archive_filename, "document_01.pdf")
class TestDecryptDocuments(TestCase):
class TestDecryptDocuments(FileSystemAssertsMixin, TestCase):
@override_settings(
ORIGINALS_DIR=os.path.join(os.path.dirname(__file__), "samples", "originals"),
THUMBNAIL_DIR=os.path.join(os.path.dirname(__file__), "samples", "thumb"),
@@ -161,10 +162,10 @@ class TestDecryptDocuments(TestCase):
self.assertEqual(doc.storage_type, Document.STORAGE_TYPE_UNENCRYPTED)
self.assertEqual(doc.filename, "0000004.pdf")
self.assertTrue(os.path.isfile(os.path.join(originals_dir, "0000004.pdf")))
self.assertTrue(os.path.isfile(doc.source_path))
self.assertTrue(os.path.isfile(os.path.join(thumb_dir, f"{doc.id:07}.webp")))
self.assertTrue(os.path.isfile(doc.thumbnail_path))
self.assertIsFile(os.path.join(originals_dir, "0000004.pdf"))
self.assertIsFile(doc.source_path)
self.assertIsFile(os.path.join(thumb_dir, f"{doc.id:07}.webp"))
self.assertIsFile(doc.thumbnail_path)
with doc.source_file as f:
checksum = hashlib.md5(f.read()).hexdigest()
@@ -183,7 +184,7 @@ class TestMakeIndex(TestCase):
m.assert_called_once()
class TestRenamer(DirectoriesMixin, TestCase):
class TestRenamer(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
@override_settings(FILENAME_FORMAT="")
def test_rename(self):
doc = Document.objects.create(title="test", mime_type="image/jpeg")
@@ -201,10 +202,10 @@ class TestRenamer(DirectoriesMixin, TestCase):
self.assertEqual(doc2.filename, "none/test.jpg")
self.assertEqual(doc2.archive_filename, "none/test.pdf")
self.assertFalse(os.path.isfile(doc.source_path))
self.assertFalse(os.path.isfile(doc.archive_path))
self.assertTrue(os.path.isfile(doc2.source_path))
self.assertTrue(os.path.isfile(doc2.archive_path))
self.assertIsNotFile(doc.source_path)
self.assertIsNotFile(doc.archive_path)
self.assertIsFile(doc2.source_path)
self.assertIsFile(doc2.archive_path)
class TestCreateClassifier(TestCase):