from documents import tasks
from documents.consumer import ConsumerError
from documents.tests.utils import DirectoriesMixin
+from documents.tests.utils import FileSystemAssertsMixin
from PIL import Image
-class TestBarcode(DirectoriesMixin, TestCase):
+class TestBarcode(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
SAMPLE_DIR = os.path.join(
os.path.dirname(__file__),
shutil.copy(test_file, dst)
target_file = barcodes.convert_from_tiff_to_pdf(dst)
file_extension = os.path.splitext(os.path.basename(target_file))[1]
- self.assertTrue(os.path.isfile(target_file))
+ self.assertIsFile(target_file)
self.assertEqual(file_extension, ".pdf")
def test_convert_error_from_pdf_to_pdf(self):
)
barcodes.save_to_dir(test_file, target_dir=settings.SCRATCH_DIR)
target_file = os.path.join(settings.SCRATCH_DIR, "patch-code-t.pdf")
- self.assertTrue(os.path.isfile(target_file))
+ self.assertIsFile(target_file)
def test_save_to_dir_not_existing(self):
"""
"patch-code-t.pdf",
)
nonexistingdir = "/nowhere"
- if os.path.isdir(nonexistingdir):
- self.fail("non-existing dir exists")
+ self.assertIsNotDir(nonexistingdir)
with self.assertLogs("paperless.barcodes", level="WARNING") as cm:
barcodes.save_to_dir(test_file, target_dir=nonexistingdir)
target_dir=settings.SCRATCH_DIR,
)
target_file = os.path.join(settings.SCRATCH_DIR, "newname.pdf")
- self.assertTrue(os.path.isfile(target_file))
+ self.assertIsFile(target_file)
def test_barcode_splitter(self):
"""
"patch-code-t-middle_document_1.pdf",
)
- self.assertTrue(os.path.isfile(target_file1))
- self.assertTrue(os.path.isfile(target_file2))
+ self.assertIsFile(target_file1)
+ self.assertIsFile(target_file2)
@override_settings(CONSUMER_ENABLE_BARCODES=True)
def test_consume_barcode_file(self):
from ..parsers import ParseError
from ..tasks import sanity_check
from .utils import DirectoriesMixin
+from documents.tests.utils import FileSystemAssertsMixin
class TestAttributes(TestCase):
@mock.patch("documents.consumer.magic.from_file", fake_magic_from_file)
-class TestConsumer(DirectoriesMixin, TestCase):
+class TestConsumer(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
def _assert_first_last_send_progress(
self,
first_status="STARTING",
self.assertEqual(document.filename, "0000001.pdf")
self.assertEqual(document.archive_filename, "0000001.pdf")
- self.assertTrue(os.path.isfile(document.source_path))
+ self.assertIsFile(document.source_path)
- self.assertTrue(os.path.isfile(document.thumbnail_path))
+ self.assertIsFile(document.thumbnail_path)
- self.assertTrue(os.path.isfile(document.archive_path))
+ self.assertIsFile(document.archive_path)
self.assertEqual(document.checksum, "42995833e01aea9b3edee44bbfdd7ce1")
self.assertEqual(document.archive_checksum, "62acb0bcbfbcaa62ca6ad3668e4e404b")
- self.assertFalse(os.path.isfile(filename))
+ self.assertIsNotFile(filename)
self._assert_first_last_send_progress()
shutil.copy(filename, shadow_file)
- self.assertTrue(os.path.isfile(shadow_file))
+ self.assertIsFile(shadow_file)
document = self.consumer.try_consume_file(filename)
- self.assertTrue(os.path.isfile(document.source_path))
+ self.assertIsFile(document.source_path)
- self.assertFalse(os.path.isfile(shadow_file))
- self.assertFalse(os.path.isfile(filename))
+ self.assertIsNotFile(shadow_file)
+ self.assertIsNotFile(filename)
def testOverrideFilename(self):
filename = self.get_test_file()
self._assert_first_last_send_progress(last_status="FAILED")
# file not deleted
- self.assertTrue(os.path.isfile(filename))
+ self.assertIsFile(filename)
# Database empty
self.assertEqual(len(Document.objects.all()), 0)
document = self.consumer.try_consume_file(filename, override_title="new docs")
self.assertEqual(document.title, "new docs")
- self.assertIsNotNone(os.path.isfile(document.title))
- self.assertTrue(os.path.isfile(document.source_path))
- self.assertTrue(os.path.isfile(document.archive_path))
+ self.assertIsNotNone(document.title)
+ self.assertIsFile(document.source_path)
+ self.assertIsFile(document.archive_path)
self._assert_first_last_send_progress()
@override_settings(CONSUMER_DELETE_DUPLICATES=True)
def test_delete_duplicate(self):
dst = self.get_test_file()
- self.assertTrue(os.path.isfile(dst))
+ self.assertIsFile(dst)
doc = self.consumer.try_consume_file(dst)
self._assert_first_last_send_progress()
- self.assertFalse(os.path.isfile(dst))
+ self.assertIsNotFile(dst)
self.assertIsNotNone(doc)
self._send_progress.reset_mock()
dst = self.get_test_file()
- self.assertTrue(os.path.isfile(dst))
+ self.assertIsFile(dst)
self.assertRaises(ConsumerError, self.consumer.try_consume_file, dst)
- self.assertFalse(os.path.isfile(dst))
+ self.assertIsNotFile(dst)
self._assert_first_last_send_progress(last_status="FAILED")
@override_settings(CONSUMER_DELETE_DUPLICATES=False)
def test_no_delete_duplicate(self):
dst = self.get_test_file()
- self.assertTrue(os.path.isfile(dst))
+ self.assertIsFile(dst)
doc = self.consumer.try_consume_file(dst)
- self.assertFalse(os.path.isfile(dst))
+ self.assertIsNotFile(dst)
self.assertIsNotNone(doc)
dst = self.get_test_file()
- self.assertTrue(os.path.isfile(dst))
+ self.assertIsFile(dst)
self.assertRaises(ConsumerError, self.consumer.try_consume_file, dst)
- self.assertTrue(os.path.isfile(dst))
+ self.assertIsFile(dst)
self._assert_first_last_send_progress(last_status="FAILED")
from django.test import override_settings
from django.test import TestCase
from django.utils import timezone
+from documents.tests.utils import FileSystemAssertsMixin
-from ..bulk_edit import bulk_update_documents
from ..file_handling import create_source_path_directory
from ..file_handling import delete_empty_directories
from ..file_handling import generate_filename
from .utils import DirectoriesMixin
-class TestFileHandling(DirectoriesMixin, TestCase):
+class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
@override_settings(FILENAME_FORMAT="")
def test_generate_source_filename(self):
document = Document()
# Test default source_path
self.assertEqual(
document.source_path,
- settings.ORIGINALS_DIR + f"/{document.pk:07d}.pdf",
+ os.path.join(settings.ORIGINALS_DIR, f"{document.pk:07d}.pdf"),
)
document.filename = generate_filename(document)
# test that creating dirs for the source_path creates the correct directory
create_source_path_directory(document.source_path)
Path(document.source_path).touch()
- self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), True)
+ self.assertIsDir(os.path.join(settings.ORIGINALS_DIR, "none"))
# Set a correspondent and save the document
document.correspondent = Correspondent.objects.get_or_create(name="test")[0]
document.save()
# Check proper handling of files
- self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/test"), True)
- self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), False)
- self.assertEqual(
- os.path.isfile(settings.ORIGINALS_DIR + "/test/test.pdf.gpg"),
- True,
+ self.assertIsDir(os.path.join(settings.ORIGINALS_DIR, "test"))
+ self.assertIsNotDir(os.path.join(settings.ORIGINALS_DIR, "none"))
+ self.assertIsFile(
+ os.path.join(settings.ORIGINALS_DIR, "test/test.pdf.gpg"),
)
@override_settings(FILENAME_FORMAT="{correspondent}/{correspondent}")
# Test source_path
self.assertEqual(
document.source_path,
- settings.ORIGINALS_DIR + "/none/none.pdf",
+ os.path.join(settings.ORIGINALS_DIR, "none/none.pdf"),
)
# Make the folder read- and execute-only (no writing and no renaming)
- os.chmod(settings.ORIGINALS_DIR + "/none", 0o555)
+ os.chmod(os.path.join(settings.ORIGINALS_DIR, "none"), 0o555)
# Set a correspondent and save the document
document.correspondent = Correspondent.objects.get_or_create(name="test")[0]
document.save()
# Check proper handling of files
- self.assertEqual(
- os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"),
- True,
+ self.assertIsFile(
+ os.path.join(settings.ORIGINALS_DIR, "none/none.pdf"),
)
self.assertEqual(document.filename, "none/none.pdf")
- os.chmod(settings.ORIGINALS_DIR + "/none", 0o777)
+ os.chmod(os.path.join(settings.ORIGINALS_DIR, "none"), 0o777)
@override_settings(FILENAME_FORMAT="{correspondent}/{correspondent}")
def test_file_renaming_database_error(self):
Path(document.source_path).touch()
# Test source_path
- self.assertTrue(os.path.isfile(document.source_path))
+ self.assertIsFile(document.source_path)
# Set a correspondent and save the document
document.correspondent = Correspondent.objects.get_or_create(name="test")[0]
document.save()
# Check proper handling of files
- self.assertTrue(os.path.isfile(document.source_path))
- self.assertEqual(
- os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"),
- True,
+ self.assertIsFile(document.source_path)
+ self.assertIsFile(
+ os.path.join(settings.ORIGINALS_DIR, "none/none.pdf"),
)
self.assertEqual(document.filename, "none/none.pdf")
# Ensure file deletion after delete
pk = document.pk
document.delete()
- self.assertEqual(
- os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"),
- False,
+ self.assertIsNotFile(
+ os.path.join(settings.ORIGINALS_DIR, "none", "none.pdf"),
)
- self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), False)
+ self.assertIsNotDir(os.path.join(settings.ORIGINALS_DIR, "none"))
@override_settings(
FILENAME_FORMAT="{correspondent}/{correspondent}",
Path(document.source_path).touch()
# Ensure file was moved to trash after delete
- self.assertEqual(os.path.isfile(settings.TRASH_DIR + "/none/none.pdf"), False)
+ self.assertIsNotFile(os.path.join(settings.TRASH_DIR, "none", "none.pdf"))
document.delete()
- self.assertEqual(
- os.path.isfile(settings.ORIGINALS_DIR + "/none/none.pdf"),
- False,
+ self.assertIsNotFile(
+ os.path.join(settings.ORIGINALS_DIR, "none", "none.pdf"),
)
- self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), False)
- self.assertEqual(os.path.isfile(settings.TRASH_DIR + "/none.pdf"), True)
- self.assertEqual(os.path.isfile(settings.TRASH_DIR + "/none_01.pdf"), False)
+ self.assertIsNotDir(os.path.join(settings.ORIGINALS_DIR, "none"))
+ self.assertIsFile(os.path.join(settings.TRASH_DIR, "none.pdf"))
+ self.assertIsNotFile(os.path.join(settings.TRASH_DIR, "none_01.pdf"))
# Create an identical document and ensure it is trashed under a new name
document = Document()
create_source_path_directory(document.source_path)
Path(document.source_path).touch()
document.delete()
- self.assertEqual(os.path.isfile(settings.TRASH_DIR + "/none_01.pdf"), True)
+ self.assertIsFile(os.path.join(settings.TRASH_DIR, "none_01.pdf"))
@override_settings(FILENAME_FORMAT="{correspondent}/{correspondent}")
def test_document_delete_nofile(self):
document.save()
# Check proper handling of files
- self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/test"), True)
- self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), True)
- self.assertTrue(os.path.isfile(important_file))
+ self.assertIsDir(os.path.join(settings.ORIGINALS_DIR, "test"))
+ self.assertIsDir(os.path.join(settings.ORIGINALS_DIR, "none"))
+ self.assertIsFile(important_file)
@override_settings(FILENAME_FORMAT="{document_type} - {title}")
def test_document_type(self):
Path(document.source_path).touch()
# Check proper handling of files
- self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none/none"), True)
+ self.assertIsDir(os.path.join(settings.ORIGINALS_DIR, "none/none"))
pk = document.pk
document.delete()
- self.assertEqual(
- os.path.isfile(settings.ORIGINALS_DIR + "/none/none/none.pdf"),
- False,
+ self.assertIsNotFile(
+ os.path.join(settings.ORIGINALS_DIR, "none/none/none.pdf"),
)
- self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none/none"), False)
- self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR + "/none"), False)
- self.assertEqual(os.path.isdir(settings.ORIGINALS_DIR), True)
+ self.assertIsNotDir(os.path.join(settings.ORIGINALS_DIR, "none/none"))
+ self.assertIsNotDir(os.path.join(settings.ORIGINALS_DIR, "none"))
+ self.assertIsDir(settings.ORIGINALS_DIR)
@override_settings(FILENAME_FORMAT=None)
def test_format_none(self):
os.path.join(tmp, "notempty", "empty"),
root=settings.ORIGINALS_DIR,
)
- self.assertEqual(os.path.isdir(os.path.join(tmp, "notempty")), True)
- self.assertEqual(os.path.isfile(os.path.join(tmp, "notempty", "file")), True)
- self.assertEqual(os.path.isdir(os.path.join(tmp, "notempty", "empty")), False)
+ self.assertIsDir(os.path.join(tmp, "notempty"))
+ self.assertIsFile(os.path.join(tmp, "notempty", "file"))
+ self.assertIsNotDir(os.path.join(tmp, "notempty", "empty"))
@override_settings(FILENAME_FORMAT="{created/[title]")
def test_invalid_format(self):
document.filename = "0000001.pdf"
document.save()
- self.assertTrue(os.path.isfile(document.source_path))
+ self.assertIsFile(document.source_path)
self.assertEqual(document.filename, "qwe.pdf")
document2.filename = "0000002.pdf"
document2.save()
- self.assertTrue(os.path.isfile(document.source_path))
+ self.assertIsFile(document.source_path)
self.assertEqual(document2.filename, "qwe_01.pdf")
# saving should not change the file names.
document.save()
- self.assertTrue(os.path.isfile(document.source_path))
+ self.assertIsFile(document.source_path)
self.assertEqual(document.filename, "qwe.pdf")
document2.save()
- self.assertTrue(os.path.isfile(document.source_path))
+ self.assertIsFile(document.source_path)
self.assertEqual(document2.filename, "qwe_01.pdf")
document.delete()
- self.assertFalse(os.path.isfile(document.source_path))
+ self.assertIsNotFile(document.source_path)
# filename free, should remove _01 suffix
document2.save()
- self.assertTrue(os.path.isfile(document.source_path))
+ self.assertIsFile(document.source_path)
self.assertEqual(document2.filename, "qwe.pdf")
@override_settings(FILENAME_FORMAT="{title}")
m.assert_not_called()
-class TestFileHandlingWithArchive(DirectoriesMixin, TestCase):
+class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
@override_settings(FILENAME_FORMAT=None)
def test_create_no_format(self):
original = os.path.join(settings.ORIGINALS_DIR, "0000001.pdf")
archive_checksum="B",
)
- self.assertTrue(os.path.isfile(original))
- self.assertTrue(os.path.isfile(archive))
- self.assertTrue(os.path.isfile(doc.source_path))
- self.assertTrue(os.path.isfile(doc.archive_path))
+ self.assertIsFile(original)
+ self.assertIsFile(archive)
+ self.assertIsFile(doc.source_path)
+ self.assertIsFile(doc.archive_path)
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
def test_create_with_format(self):
archive_filename="0000001.pdf",
)
- self.assertFalse(os.path.isfile(original))
- self.assertFalse(os.path.isfile(archive))
- self.assertTrue(os.path.isfile(doc.source_path))
- self.assertTrue(os.path.isfile(doc.archive_path))
+ self.assertIsNotFile(original)
+ self.assertIsNotFile(archive)
+ self.assertIsFile(doc.source_path)
+ self.assertIsFile(doc.archive_path)
self.assertEqual(
doc.source_path,
os.path.join(settings.ORIGINALS_DIR, "none", "my_doc.pdf"),
archive_filename="0000001.pdf",
)
- self.assertTrue(os.path.isfile(original))
- self.assertFalse(os.path.isfile(archive))
- self.assertTrue(os.path.isfile(doc.source_path))
- self.assertFalse(os.path.isfile(doc.archive_path))
+ self.assertIsFile(original)
+ self.assertIsNotFile(archive)
+ self.assertIsFile(doc.source_path)
+ self.assertIsNotFile(doc.archive_path)
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
def test_move_archive_exists(self):
archive_filename="0000001.pdf",
)
- self.assertFalse(os.path.isfile(original))
- self.assertFalse(os.path.isfile(archive))
- self.assertTrue(os.path.isfile(doc.source_path))
- self.assertTrue(os.path.isfile(doc.archive_path))
- self.assertTrue(os.path.isfile(existing_archive_file))
+ self.assertIsNotFile(original)
+ self.assertIsNotFile(archive)
+ self.assertIsFile(doc.source_path)
+ self.assertIsFile(doc.archive_path)
+ self.assertIsFile(existing_archive_file)
self.assertEqual(doc.archive_filename, "none/my_doc_01.pdf")
@override_settings(FILENAME_FORMAT="{title}")
self.assertEqual(doc.filename, "document.pdf")
self.assertEqual(doc.archive_filename, "document.pdf")
- self.assertTrue(os.path.isfile(doc.source_path))
- self.assertTrue(os.path.isfile(doc.archive_path))
+ self.assertIsFile(doc.source_path)
+ self.assertIsFile(doc.archive_path)
@override_settings(FILENAME_FORMAT="{title}")
def test_move_archive_only(self):
self.assertEqual(doc.filename, "document.pdf")
self.assertEqual(doc.archive_filename, "document.pdf")
- self.assertTrue(os.path.isfile(doc.source_path))
- self.assertTrue(os.path.isfile(doc.archive_path))
+ self.assertIsFile(doc.source_path)
+ self.assertIsFile(doc.archive_path)
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
@mock.patch("documents.signals.handlers.os.rename")
)
m.assert_called()
- self.assertTrue(os.path.isfile(original))
- self.assertTrue(os.path.isfile(archive))
- self.assertTrue(os.path.isfile(doc.source_path))
- self.assertTrue(os.path.isfile(doc.archive_path))
+ self.assertIsFile(original)
+ self.assertIsFile(archive)
+ self.assertIsFile(doc.source_path)
+ self.assertIsFile(doc.archive_path)
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
def test_move_file_gone(self):
archive_checksum="B",
)
- self.assertFalse(os.path.isfile(original))
- self.assertTrue(os.path.isfile(archive))
- self.assertFalse(os.path.isfile(doc.source_path))
- self.assertTrue(os.path.isfile(doc.archive_path))
+ self.assertIsNotFile(original)
+ self.assertIsFile(archive)
+ self.assertIsNotFile(doc.source_path)
+ self.assertIsFile(doc.archive_path)
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
@mock.patch("documents.signals.handlers.os.rename")
)
m.assert_called()
- self.assertTrue(os.path.isfile(original))
- self.assertTrue(os.path.isfile(archive))
- self.assertTrue(os.path.isfile(doc.source_path))
- self.assertTrue(os.path.isfile(doc.archive_path))
+ self.assertIsFile(original)
+ self.assertIsFile(archive)
+ self.assertIsFile(doc.source_path)
+ self.assertIsFile(doc.archive_path)
@override_settings(FILENAME_FORMAT="")
def test_archive_deleted(self):
archive_filename="0000001.pdf",
)
- self.assertTrue(os.path.isfile(original))
- self.assertTrue(os.path.isfile(archive))
- self.assertTrue(os.path.isfile(doc.source_path))
- self.assertTrue(os.path.isfile(doc.archive_path))
+ self.assertIsFile(original)
+ self.assertIsFile(archive)
+ self.assertIsFile(doc.source_path)
+ self.assertIsFile(doc.archive_path)
doc.delete()
- self.assertFalse(os.path.isfile(original))
- self.assertFalse(os.path.isfile(archive))
- self.assertFalse(os.path.isfile(doc.source_path))
- self.assertFalse(os.path.isfile(doc.archive_path))
+ self.assertIsNotFile(original)
+ self.assertIsNotFile(archive)
+ self.assertIsNotFile(doc.source_path)
+ self.assertIsNotFile(doc.archive_path)
@override_settings(FILENAME_FORMAT="{title}")
def test_archive_deleted2(self):
checksum="C",
)
- self.assertTrue(os.path.isfile(doc1.source_path))
- self.assertTrue(os.path.isfile(doc1.archive_path))
- self.assertTrue(os.path.isfile(doc2.source_path))
+ self.assertIsFile(doc1.source_path)
+ self.assertIsFile(doc1.archive_path)
+ self.assertIsFile(doc2.source_path)
doc2.delete()
- self.assertTrue(os.path.isfile(doc1.source_path))
- self.assertTrue(os.path.isfile(doc1.archive_path))
- self.assertFalse(os.path.isfile(doc2.source_path))
+ self.assertIsFile(doc1.source_path)
+ self.assertIsFile(doc1.archive_path)
+ self.assertIsNotFile(doc2.source_path)
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
def test_database_error(self):
m.side_effect = DatabaseError()
doc.save()
- self.assertTrue(os.path.isfile(original))
- self.assertTrue(os.path.isfile(archive))
- self.assertTrue(os.path.isfile(doc.source_path))
- self.assertTrue(os.path.isfile(doc.archive_path))
+ self.assertIsFile(original)
+ self.assertIsFile(archive)
+ self.assertIsFile(doc.source_path)
+ self.assertIsFile(doc.archive_path)
class TestFilenameGeneration(DirectoriesMixin, TestCase):
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",
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")
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):
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"),
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()
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")
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):
from documents.sanity_checker import check_sanity
from documents.settings import EXPORTER_FILE_NAME
from documents.tests.utils import DirectoriesMixin
+from documents.tests.utils import FileSystemAssertsMixin
from documents.tests.utils import paperless_environment
-class TestExportImport(DirectoriesMixin, TestCase):
+class TestExportImport(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
def setUp(self) -> None:
self.target = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.target)
4,
)
- self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))
+ self.assertIsFile(os.path.join(self.target, "manifest.json"))
self.assertEqual(
self._get_document_from_manifest(manifest, self.d1.id)["fields"]["title"],
self.target,
element[document_exporter.EXPORTER_FILE_NAME],
)
- self.assertTrue(os.path.exists(fname))
- self.assertTrue(
- os.path.exists(
- os.path.join(
- self.target,
- element[document_exporter.EXPORTER_THUMBNAIL_NAME],
- ),
+ self.assertIsFile(fname)
+ self.assertIsFile(
+ os.path.join(
+ self.target,
+ element[document_exporter.EXPORTER_THUMBNAIL_NAME],
),
)
self.target,
element[document_exporter.EXPORTER_ARCHIVE_NAME],
)
- self.assertTrue(os.path.exists(fname))
+ self.assertIsFile(fname)
with open(fname, "rb") as f:
checksum = hashlib.md5(f.read()).hexdigest()
)
self._do_export()
- self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))
+ self.assertIsFile(os.path.join(self.target, "manifest.json"))
st_mtime_1 = os.stat(os.path.join(self.target, "manifest.json")).st_mtime
self._do_export()
m.assert_not_called()
- self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))
+ self.assertIsFile(os.path.join(self.target, "manifest.json"))
st_mtime_2 = os.stat(os.path.join(self.target, "manifest.json")).st_mtime
Path(self.d1.source_path).touch()
self.assertEqual(m.call_count, 1)
st_mtime_3 = os.stat(os.path.join(self.target, "manifest.json")).st_mtime
- self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))
+ self.assertIsFile(os.path.join(self.target, "manifest.json"))
self.assertNotEqual(st_mtime_1, st_mtime_2)
self.assertNotEqual(st_mtime_2, st_mtime_3)
self._do_export()
- self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))
+ self.assertIsFile(os.path.join(self.target, "manifest.json"))
with mock.patch(
"documents.management.commands.document_exporter.shutil.copy2",
self._do_export()
m.assert_not_called()
- self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))
+ self.assertIsFile(os.path.join(self.target, "manifest.json"))
self.d2.checksum = "asdfasdgf3"
self.d2.save()
self._do_export(compare_checksums=True)
self.assertEqual(m.call_count, 1)
- self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))
+ self.assertIsFile(os.path.join(self.target, "manifest.json"))
def test_update_export_deleted_document(self):
shutil.rmtree(os.path.join(self.dirs.media_dir, "documents"))
self.assertTrue(len(manifest), 7)
doc_from_manifest = self._get_document_from_manifest(manifest, self.d3.id)
- self.assertTrue(
- os.path.isfile(
- os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME]),
- ),
+ self.assertIsFile(
+ os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME]),
)
self.d3.delete()
manifest,
self.d3.id,
)
- self.assertTrue(
- os.path.isfile(
- os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME]),
- ),
+ self.assertIsFile(
+ os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME]),
)
manifest = self._do_export(delete=True)
- self.assertFalse(
- os.path.isfile(
- os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME]),
- ),
+ self.assertIsNotFile(
+ os.path.join(self.target, doc_from_manifest[EXPORTER_FILE_NAME]),
)
self.assertTrue(len(manifest), 6)
)
m = self._do_export(use_filename_format=True)
- self.assertTrue(os.path.isfile(os.path.join(self.target, "wow1", "c.pdf")))
+ self.assertIsFile(os.path.join(self.target, "wow1", "c.pdf"))
- self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))
+ self.assertIsFile(os.path.join(self.target, "manifest.json"))
self.d1.title = "new_title"
self.d1.save()
self._do_export(use_filename_format=True, delete=True)
- self.assertFalse(os.path.isfile(os.path.join(self.target, "wow1", "c.pdf")))
- self.assertFalse(os.path.isdir(os.path.join(self.target, "wow1")))
- self.assertTrue(os.path.isfile(os.path.join(self.target, "new_title", "c.pdf")))
- self.assertTrue(os.path.exists(os.path.join(self.target, "manifest.json")))
- self.assertTrue(os.path.isfile(os.path.join(self.target, "wow2", "none.pdf")))
- self.assertTrue(
- os.path.isfile(os.path.join(self.target, "wow2", "none_01.pdf")),
+ self.assertIsNotFile(os.path.join(self.target, "wow1", "c.pdf"))
+ self.assertIsNotDir(os.path.join(self.target, "wow1"))
+ self.assertIsFile(os.path.join(self.target, "new_title", "c.pdf"))
+ self.assertIsFile(os.path.join(self.target, "manifest.json"))
+ self.assertIsFile(os.path.join(self.target, "wow2", "none.pdf"))
+ self.assertIsFile(
+ (os.path.join(self.target, "wow2", "none_01.pdf")),
)
def test_export_missing_files(self):
f"export-{timezone.localdate().isoformat()}.zip",
)
- self.assertTrue(os.path.isfile(expected_file))
+ self.assertIsFile(expected_file)
with ZipFile(expected_file) as zip:
self.assertEqual(len(zip.namelist()), 11)
f"export-{timezone.localdate().isoformat()}.zip",
)
- self.assertTrue(os.path.isfile(expected_file))
+ self.assertIsFile(expected_file)
with ZipFile(expected_file) as zip:
# Extras are from the directories, which also appear in the listing
from documents.management.commands.document_thumbnails import _process_document
from documents.models import Document
from documents.tests.utils import DirectoriesMixin
+from documents.tests.utils import FileSystemAssertsMixin
-class TestMakeThumbnails(DirectoriesMixin, TestCase):
+class TestMakeThumbnails(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
def make_models(self):
self.d1 = Document.objects.create(
checksum="A",
self.make_models()
def test_process_document(self):
- self.assertFalse(os.path.isfile(self.d1.thumbnail_path))
+ self.assertIsNotFile(self.d1.thumbnail_path)
_process_document(self.d1.id)
- self.assertTrue(os.path.isfile(self.d1.thumbnail_path))
+ self.assertIsFile(self.d1.thumbnail_path)
@mock.patch("documents.management.commands.document_thumbnails.shutil.move")
def test_process_document_invalid_mime_type(self, m):
m.assert_not_called()
def test_command(self):
- self.assertFalse(os.path.isfile(self.d1.thumbnail_path))
- self.assertFalse(os.path.isfile(self.d2.thumbnail_path))
+ self.assertIsNotFile(self.d1.thumbnail_path)
+ self.assertIsNotFile(self.d2.thumbnail_path)
call_command("document_thumbnails")
- self.assertTrue(os.path.isfile(self.d1.thumbnail_path))
- self.assertTrue(os.path.isfile(self.d2.thumbnail_path))
+ self.assertTrue(self.d1.thumbnail_path)
+ self.assertTrue(self.d2.thumbnail_path)
def test_command_documentid(self):
- self.assertFalse(os.path.isfile(self.d1.thumbnail_path))
- self.assertFalse(os.path.isfile(self.d2.thumbnail_path))
+ self.assertIsNotFile(self.d1.thumbnail_path)
+ self.assertIsNotFile(self.d2.thumbnail_path)
call_command("document_thumbnails", "-d", f"{self.d1.id}")
- self.assertTrue(os.path.isfile(self.d1.thumbnail_path))
- self.assertFalse(os.path.isfile(self.d2.thumbnail_path))
+ self.assertIsFile(self.d1.thumbnail_path)
+ self.assertIsNotFile(self.d2.thumbnail_path)
from django.test import override_settings
from documents.parsers import ParseError
from documents.tests.utils import DirectoriesMixin
+from documents.tests.utils import FileSystemAssertsMixin
from documents.tests.utils import TestMigrations
@override_settings(FILENAME_FORMAT="")
-class TestMigrateArchiveFiles(DirectoriesMixin, TestMigrations):
+class TestMigrateArchiveFiles(DirectoriesMixin, FileSystemAssertsMixin, TestMigrations):
migrate_from = "1011_auto_20210101_2340"
migrate_to = "1012_fix_archive_files"
for doc in Document.objects.all():
if doc.archive_checksum:
self.assertIsNotNone(doc.archive_filename)
- self.assertTrue(os.path.isfile(archive_path_new(doc)))
+ self.assertIsFile(archive_path_new(doc))
else:
self.assertIsNone(doc.archive_filename)
self.assertEqual(original_checksum, doc.checksum)
if doc.archive_checksum:
- self.assertTrue(os.path.isfile(archive_path_new(doc)))
+ self.assertIsFile(archive_path_new(doc))
with open(archive_path_new(doc), "rb") as f:
archive_checksum = hashlib.md5(f.read()).hexdigest()
self.assertEqual(archive_checksum, doc.archive_checksum)
@override_settings(FILENAME_FORMAT="")
-class TestMigrateArchiveFilesBackwards(DirectoriesMixin, TestMigrations):
+class TestMigrateArchiveFilesBackwards(
+ DirectoriesMixin,
+ FileSystemAssertsMixin,
+ TestMigrations,
+):
migrate_from = "1012_fix_archive_files"
migrate_to = "1011_auto_20210101_2340"
for doc in Document.objects.all():
if doc.archive_checksum:
- self.assertTrue(os.path.isfile(archive_path_old(doc)))
+ self.assertIsFile(archive_path_old(doc))
with open(source_path(doc), "rb") as f:
original_checksum = hashlib.md5(f.read()).hexdigest()
self.assertEqual(original_checksum, doc.checksum)
if doc.archive_checksum:
- self.assertTrue(os.path.isfile(archive_path_old(doc)))
+ self.assertIsFile(archive_path_old(doc))
with open(archive_path_old(doc), "rb") as f:
archive_checksum = hashlib.md5(f.read()).hexdigest()
self.assertEqual(archive_checksum, doc.archive_checksum)
from documents.sanity_checker import SanityCheckMessages
from documents.tests.test_classifier import dummy_preprocess
from documents.tests.utils import DirectoriesMixin
+from documents.tests.utils import FileSystemAssertsMixin
class TestIndexReindex(DirectoriesMixin, TestCase):
tasks.index_optimize()
-class TestClassifier(DirectoriesMixin, TestCase):
+class TestClassifier(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
@mock.patch("documents.tasks.load_classifier")
def test_train_classifier_no_auto_matching(self, load_classifier):
tasks.train_classifier()
Tag.objects.create(matching_algorithm=Tag.MATCH_AUTO, name="test")
tasks.train_classifier()
load_classifier.assert_called_once()
- self.assertFalse(os.path.isfile(settings.MODEL_FILE))
+ self.assertIsNotFile(settings.MODEL_FILE)
@mock.patch("documents.tasks.load_classifier")
def test_train_classifier_with_auto_type(self, load_classifier):
DocumentType.objects.create(matching_algorithm=Tag.MATCH_AUTO, name="test")
tasks.train_classifier()
load_classifier.assert_called_once()
- self.assertFalse(os.path.isfile(settings.MODEL_FILE))
+ self.assertIsNotFile(settings.MODEL_FILE)
@mock.patch("documents.tasks.load_classifier")
def test_train_classifier_with_auto_correspondent(self, load_classifier):
Correspondent.objects.create(matching_algorithm=Tag.MATCH_AUTO, name="test")
tasks.train_classifier()
load_classifier.assert_called_once()
- self.assertFalse(os.path.isfile(settings.MODEL_FILE))
+ self.assertIsNotFile(settings.MODEL_FILE)
def test_train_classifier(self):
c = Correspondent.objects.create(matching_algorithm=Tag.MATCH_AUTO, name="test")
doc = Document.objects.create(correspondent=c, content="test", title="test")
- self.assertFalse(os.path.isfile(settings.MODEL_FILE))
+ self.assertIsNotFile(settings.MODEL_FILE)
with mock.patch(
"documents.classifier.DocumentClassifier.preprocess_content",
pre_proc_mock.side_effect = dummy_preprocess
tasks.train_classifier()
- self.assertTrue(os.path.isfile(settings.MODEL_FILE))
+ self.assertIsFile(settings.MODEL_FILE)
mtime = os.stat(settings.MODEL_FILE).st_mtime
tasks.train_classifier()
- self.assertTrue(os.path.isfile(settings.MODEL_FILE))
+ self.assertIsFile(settings.MODEL_FILE)
mtime2 = os.stat(settings.MODEL_FILE).st_mtime
self.assertEqual(mtime, mtime2)
doc.content = "test2"
doc.save()
tasks.train_classifier()
- self.assertTrue(os.path.isfile(settings.MODEL_FILE))
+ self.assertIsFile(settings.MODEL_FILE)
mtime3 = os.stat(settings.MODEL_FILE).st_mtime
self.assertNotEqual(mtime2, mtime3)
import tempfile
from collections import namedtuple
from contextlib import contextmanager
+from os import PathLike
+from pathlib import Path
+from typing import Union
from unittest import mock
from django.apps import apps
remove_dirs(self.dirs)
+class FileSystemAssertsMixin:
+ def assertIsFile(self, path: Union[PathLike, str]):
+ if not Path(path).resolve().is_file():
+ raise AssertionError(f"File does not exist: {path}")
+
+ def assertIsNotFile(self, path: Union[PathLike, str]):
+ if Path(path).resolve().is_file():
+ raise AssertionError(f"File does exist: {path}")
+
+ def assertIsDir(self, path: Union[PathLike, str]):
+ if not Path(path).resolve().is_dir():
+ raise AssertionError(f"Dir does not exist: {path}")
+
+ def assertIsNotDir(self, path: Union[PathLike, str]):
+ if Path(path).resolve().is_dir():
+ raise AssertionError(f"Dir does exist: {path}")
+
+
class ConsumerProgressMixin:
def setUp(self) -> None:
self.send_progress_patcher = mock.patch(
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
@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()
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")
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):
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:
)
@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
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):
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),
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:
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")
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 = (
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.
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.
from documents.parsers import ParseError
from documents.parsers import run_convert
from documents.tests.utils import DirectoriesMixin
+from documents.tests.utils import FileSystemAssertsMixin
from paperless_tesseract.parsers import post_process_text
from paperless_tesseract.parsers import RasterisedDocumentParser
return os.path.basename(self.fname)
-class TestParser(DirectoriesMixin, TestCase):
+class TestParser(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
SAMPLE_FILES = os.path.join(os.path.dirname(__file__), "samples")
os.path.join(self.SAMPLE_FILES, "simple-digital.pdf"),
"application/pdf",
)
- self.assertTrue(os.path.isfile(thumb))
+ self.assertIsFile(thumb)
@mock.patch("documents.parsers.run_convert")
def test_thumbnail_fallback(self, m):
os.path.join(self.SAMPLE_FILES, "simple-digital.pdf"),
"application/pdf",
)
- self.assertTrue(os.path.isfile(thumb))
+ self.assertIsFile(thumb)
def test_thumbnail_encrypted(self):
parser = RasterisedDocumentParser(uuid.uuid4())
os.path.join(self.SAMPLE_FILES, "encrypted.pdf"),
"application/pdf",
)
- self.assertTrue(os.path.isfile(thumb))
+ self.assertIsFile(thumb)
def test_get_dpi(self):
parser = RasterisedDocumentParser(None)
"application/pdf",
)
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(parser.get_text(), ["This is a test document."])
"application/pdf",
)
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(
parser.get_text(),
parser.parse(os.path.join(self.SAMPLE_FILES, "simple.png"), "image/png")
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(parser.get_text(), ["This is a test document."])
parser.parse(dest_file, "image/png")
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(parser.get_text(), ["This is a test document."])
parser.parse(os.path.join(self.SAMPLE_FILES, "simple-no-dpi.png"), "image/png")
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(
parser.get_text().lower(),
os.path.join(self.SAMPLE_FILES, "multi-page-digital.pdf"),
"application/pdf",
)
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(
parser.get_text().lower(),
["page 1", "page 2", "page 3"],
os.path.join(self.SAMPLE_FILES, "multi-page-digital.pdf"),
"application/pdf",
)
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(
parser.get_text().lower(),
["page 1", "page 2", "page 3"],
os.path.join(self.SAMPLE_FILES, "multi-page-digital.pdf"),
"application/pdf",
)
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(
parser.get_text().lower(),
["page 1", "page 2", "page 3"],
os.path.join(self.SAMPLE_FILES, "multi-page-digital.pdf"),
"application/pdf",
)
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(
parser.get_text().lower(),
["page 1", "page 2", "page 3"],
os.path.join(self.SAMPLE_FILES, "multi-page-images.pdf"),
"application/pdf",
)
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(
parser.get_text().lower(),
["page 1", "page 2", "page 3"],
os.path.join(self.SAMPLE_FILES, "multi-page-images.pdf"),
"application/pdf",
)
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(parser.get_text().lower(), ["page 1", "page 2"])
self.assertNotIn("page 3", parser.get_text().lower())
os.path.join(self.SAMPLE_FILES, "multi-page-images.pdf"),
"application/pdf",
)
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(parser.get_text().lower(), ["page 1"])
self.assertNotIn("page 2", parser.get_text().lower())
self.assertNotIn("page 3", parser.get_text().lower())
"application/pdf",
)
self.assertIsNotNone(parser.archive_path)
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(
parser.get_text().lower(),
["page 1", "page 2", "page 3", "page 4", "page 5", "page 6"],
"application/pdf",
)
self.assertIsNotNone(parser.archive_path)
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(
parser.get_text().lower(),
[
os.path.join(self.SAMPLE_FILES, "multi-page-images.tiff"),
"image/tiff",
)
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(
parser.get_text().lower(),
["page 1", "page 2", "page 3"],
tmp_file.name,
"image/tiff",
)
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(
parser.get_text().lower(),
["page 1", "page 2", "page 3"],
tmp_file.name,
"image/tiff",
)
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertContainsStrings(
parser.get_text().lower(),
["page 1", "page 2", "page 3"],
self.assertIn("ةﯾﻠﺧﺎدﻻ ةرازو", parser.get_text())
-class TestParserFileTypes(DirectoriesMixin, TestCase):
+class TestParserFileTypes(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
SAMPLE_FILES = os.path.join(os.path.dirname(__file__), "samples")
def test_bmp(self):
parser = RasterisedDocumentParser(None)
parser.parse(os.path.join(self.SAMPLE_FILES, "simple.bmp"), "image/bmp")
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertIn("this is a test document", parser.get_text().lower())
def test_jpg(self):
parser = RasterisedDocumentParser(None)
parser.parse(os.path.join(self.SAMPLE_FILES, "simple.jpg"), "image/jpeg")
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertIn("this is a test document", parser.get_text().lower())
@override_settings(OCR_IMAGE_DPI=200)
def test_gif(self):
parser = RasterisedDocumentParser(None)
parser.parse(os.path.join(self.SAMPLE_FILES, "simple.gif"), "image/gif")
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertIn("this is a test document", parser.get_text().lower())
def test_tiff(self):
parser = RasterisedDocumentParser(None)
parser.parse(os.path.join(self.SAMPLE_FILES, "simple.tif"), "image/tiff")
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
self.assertIn("this is a test document", parser.get_text().lower())
@override_settings(OCR_IMAGE_DPI=72)
def test_webp(self):
parser = RasterisedDocumentParser(None)
parser.parse(os.path.join(self.SAMPLE_FILES, "document.webp"), "image/webp")
- self.assertTrue(os.path.isfile(parser.archive_path))
+ self.assertIsFile(parser.archive_path)
# OCR consistent mangles this space, oh well
self.assertIn(
"this is awebp document, created 11/14/2022.",
from django.test import TestCase
from documents.tests.utils import DirectoriesMixin
+from documents.tests.utils import FileSystemAssertsMixin
from paperless_text.parsers import TextDocumentParser
-class TestTextParser(DirectoriesMixin, TestCase):
+class TestTextParser(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
def test_thumbnail(self):
parser = TextDocumentParser(None)
os.path.join(os.path.dirname(__file__), "samples", "test.txt"),
"text/plain",
)
- self.assertTrue(os.path.isfile(f))
+ self.assertIsFile(f)
def test_parse(self):