diff --git a/pyproject.toml b/pyproject.toml index 5967a6fda..401f0bdb0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -256,8 +256,8 @@ isort.force-single-line = true [tool.codespell] ignore-words-list = "criterias,afterall,valeu,ureue,equest,ure,assertIn,Oktober,commitish,NIN,nin,reprot" skip = """\ - src-ui/src/locale/*,src-ui/pnpm-lock.yaml,src-ui/e2e/*,src/paperless_mail/tests/samples/*,src/paperless/tests/samples\ - /mail/*,src/documents/tests/samples/*,*.po,*.json\ + src-ui/src/locale/*,src-ui/pnpm-lock.yaml,src-ui/e2e/*,src/paperless/tests/samples/mail/*,src/documents/tests/samples\ + /*,src/paperless_testing/sample_files/*,*.po,*.json\ """ write-changes = true diff --git a/src-ui/e2e/backend.py b/src-ui/e2e/backend.py index e706b0f8a..0a4ed8fa3 100644 --- a/src-ui/e2e/backend.py +++ b/src-ui/e2e/backend.py @@ -134,7 +134,9 @@ def seed_database() -> None: originals.mkdir(parents=True) thumbnails = Path(os.environ["PAPERLESS_MEDIA_ROOT"]) / "documents" / "thumbnails" thumbnails.mkdir(parents=True) - sample_pdf = SOURCE_ROOT / "documents" / "tests" / "samples" / "simple.pdf" + sample_pdf = ( + SOURCE_ROOT / "paperless_testing" / "sample_files" / "simple-digital.pdf" + ) for document in documents: shutil.copyfile(sample_pdf, originals / document.filename) shutil.copyfile( diff --git a/src/conftest.py b/src/conftest.py index 781cdff61..91f5635a2 100644 --- a/src/conftest.py +++ b/src/conftest.py @@ -11,6 +11,8 @@ from typing import TYPE_CHECKING import pytest +from paperless_testing import samples + if TYPE_CHECKING: from collections.abc import Generator from pathlib import Path @@ -149,3 +151,39 @@ def fake_progress_manager( monkeypatch.setattr("documents.tasks.ProgressManager", FakeProgressManager) return FakeProgressManager + + +@pytest.fixture(scope="session") +def shared_samples_dir() -> Path: + """Directory of sample files used by more than one app's tests.""" + return samples.SHARED_SAMPLES_DIR + + +@pytest.fixture(scope="session") +def simple_digital_pdf_file() -> Path: + """One-page PDF with a text layer.""" + return samples.SIMPLE_DIGITAL_PDF + + +@pytest.fixture(scope="session") +def multi_page_digital_pdf_file() -> Path: + """Three-page PDF with a text layer.""" + return samples.MULTI_PAGE_DIGITAL_PDF + + +@pytest.fixture(scope="session") +def with_form_pdf_file() -> Path: + """PDF containing a fillable form.""" + return samples.WITH_FORM_PDF + + +@pytest.fixture(scope="session") +def multi_page_images_pdf_file() -> Path: + """Multi-page PDF of scanned images, no text layer.""" + return samples.MULTI_PAGE_IMAGES_PDF + + +@pytest.fixture(scope="session") +def thumbnail_webp_file() -> Path: + """Small WebP thumbnail.""" + return samples.THUMBNAIL_WEBP diff --git a/src/documents/tests/conftest.py b/src/documents/tests/conftest.py index 567342dcc..c41f200a2 100644 --- a/src/documents/tests/conftest.py +++ b/src/documents/tests/conftest.py @@ -12,29 +12,25 @@ if TYPE_CHECKING: from paperless_testing.dirs import PaperlessDirs -@pytest.fixture(scope="session") -def document_samples_dir() -> Path: - """Path to the shared test sample documents.""" - return Path(__file__).parent / "samples" / "documents" - - @pytest.fixture() def sample_doc( paperless_dirs: "PaperlessDirs", - document_samples_dir: Path, + simple_digital_pdf_file: Path, + multi_page_images_pdf_file: Path, + thumbnail_webp_file: Path, ) -> "Document": """Create a document with valid files and matching checksums.""" with filelock.FileLock(paperless_dirs.media_lock): shutil.copy( - document_samples_dir / "originals" / "0000001.pdf", + simple_digital_pdf_file, paperless_dirs.originals_dir / "0000001.pdf", ) shutil.copy( - document_samples_dir / "archive" / "0000001.pdf", + multi_page_images_pdf_file, paperless_dirs.archive_dir / "0000001.pdf", ) shutil.copy( - document_samples_dir / "thumbnails" / "0000001.webp", + thumbnail_webp_file, paperless_dirs.thumbnail_dir / "0000001.webp", ) diff --git a/src/documents/tests/examples/no-text.png b/src/documents/tests/examples/no-text.png deleted file mode 100644 index e78b22bfb..000000000 Binary files a/src/documents/tests/examples/no-text.png and /dev/null differ diff --git a/src/documents/tests/samples/barcodes/barcode-128-asn-too-large.pdf b/src/documents/tests/samples/barcodes/barcode-128-asn-too-large.pdf deleted file mode 100644 index 2d190c876..000000000 Binary files a/src/documents/tests/samples/barcodes/barcode-128-asn-too-large.pdf and /dev/null differ diff --git a/src/documents/tests/samples/documents/archive/0000001.pdf b/src/documents/tests/samples/documents/archive/0000001.pdf deleted file mode 100644 index ea08363bf..000000000 Binary files a/src/documents/tests/samples/documents/archive/0000001.pdf and /dev/null differ diff --git a/src/documents/tests/samples/documents/originals/0000001.pdf b/src/documents/tests/samples/documents/originals/0000001.pdf deleted file mode 100644 index e450de482..000000000 Binary files a/src/documents/tests/samples/documents/originals/0000001.pdf and /dev/null differ diff --git a/src/documents/tests/samples/documents/originals/0000002.pdf b/src/documents/tests/samples/documents/originals/0000002.pdf deleted file mode 100644 index 5e75266ca..000000000 Binary files a/src/documents/tests/samples/documents/originals/0000002.pdf and /dev/null differ diff --git a/src/documents/tests/samples/documents/originals/0000003.pdf b/src/documents/tests/samples/documents/originals/0000003.pdf deleted file mode 100644 index afbeef5c8..000000000 Binary files a/src/documents/tests/samples/documents/originals/0000003.pdf and /dev/null differ diff --git a/src/documents/tests/samples/documents/thumbnails/0000002.webp b/src/documents/tests/samples/documents/thumbnails/0000002.webp deleted file mode 100644 index a7ff623b2..000000000 Binary files a/src/documents/tests/samples/documents/thumbnails/0000002.webp and /dev/null differ diff --git a/src/documents/tests/samples/documents/thumbnails/0000003.webp b/src/documents/tests/samples/documents/thumbnails/0000003.webp deleted file mode 100644 index a7ff623b2..000000000 Binary files a/src/documents/tests/samples/documents/thumbnails/0000003.webp and /dev/null differ diff --git a/src/documents/tests/samples/documents/thumbnails/0000004.webp b/src/documents/tests/samples/documents/thumbnails/0000004.webp deleted file mode 100644 index a7ff623b2..000000000 Binary files a/src/documents/tests/samples/documents/thumbnails/0000004.webp and /dev/null differ diff --git a/src/documents/tests/samples/simple.pdf b/src/documents/tests/samples/simple.pdf deleted file mode 100644 index e450de482..000000000 Binary files a/src/documents/tests/samples/simple.pdf and /dev/null differ diff --git a/src/documents/tests/samples/simple.txt b/src/documents/tests/samples/simple.txt deleted file mode 100644 index 6de7b8c69..000000000 --- a/src/documents/tests/samples/simple.txt +++ /dev/null @@ -1 +0,0 @@ -This is a test file. diff --git a/src/documents/tests/test_api_bulk_download.py b/src/documents/tests/test_api_bulk_download.py index b031a5105..0f8dc7afe 100644 --- a/src/documents/tests/test_api_bulk_download.py +++ b/src/documents/tests/test_api_bulk_download.py @@ -53,7 +53,7 @@ class TestBulkDownload(DirectoriesMixin, SampleDirMixin, APITestCase): archive_checksum="D", ) - shutil.copy(self.SAMPLE_DIR / "simple.pdf", self.doc2.source_path) + shutil.copy(self.SIMPLE_PDF, self.doc2.source_path) shutil.copy(self.SAMPLE_DIR / "simple.png", self.doc2b.source_path) shutil.copy(self.SAMPLE_DIR / "simple.jpg", self.doc3.source_path) shutil.copy(self.SAMPLE_DIR / "test_with_bom.pdf", self.doc3.archive_path) diff --git a/src/documents/tests/test_api_documents.py b/src/documents/tests/test_api_documents.py index c37a8d3ee..eeb1ab59a 100644 --- a/src/documents/tests/test_api_documents.py +++ b/src/documents/tests/test_api_documents.py @@ -57,6 +57,8 @@ from paperless_testing.http import read_streaming_response from paperless_testing.permissions import grant_all_global from paperless_testing.permissions import grant_global from paperless_testing.permissions import grant_object +from paperless_testing.samples import SIMPLE_DIGITAL_PDF +from paperless_testing.samples import THUMBNAIL_WEBP class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): @@ -1917,7 +1919,11 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): id=str(uuid.uuid4()), ) - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SimpleUploadedFile( + "simple.pdf", + SIMPLE_DIGITAL_PDF.read_bytes(), + content_type="application/pdf", + ) as f: response = self.client.post( "/api/documents/post_document/", {"document": f}, @@ -1945,7 +1951,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): payload = SimpleUploadedFile( "../../outside.pdf", - (Path(__file__).parent / "samples" / "simple.pdf").read_bytes(), + SIMPLE_DIGITAL_PDF.read_bytes(), content_type="application/pdf", ) @@ -1974,7 +1980,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): id=str(uuid.uuid4()), ) - pdf_bytes = (Path(__file__).parent / "samples" / "simple.pdf").read_bytes() + pdf_bytes = SIMPLE_DIGITAL_PDF.read_bytes() boundary = "paperless-boundary" payload = ( ( @@ -2050,7 +2056,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): def test_upload_insufficient_permissions(self) -> None: self.client.force_authenticate(user=UserFactory(username="testuser2")) - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SIMPLE_DIGITAL_PDF.open("rb") as f: response = self.client.post( "/api/documents/post_document/", {"document": f}, @@ -2063,7 +2069,11 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): id=str(uuid.uuid4()), ) - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SimpleUploadedFile( + "simple.pdf", + SIMPLE_DIGITAL_PDF.read_bytes(), + content_type="application/pdf", + ) as f: response = self.client.post( "/api/documents/post_document/", { @@ -2096,7 +2106,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): id=str(uuid.uuid4()), ) - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SIMPLE_DIGITAL_PDF.open("rb") as f: response = self.client.post( "/api/documents/post_document/", {"documenst": f}, @@ -2122,7 +2132,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): id=str(uuid.uuid4()), ) - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SIMPLE_DIGITAL_PDF.open("rb") as f: response = self.client.post( "/api/documents/post_document/", {"document": f, "title": "my custom title"}, @@ -2142,7 +2152,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): ) c = Correspondent.objects.create(name="test-corres") - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SIMPLE_DIGITAL_PDF.open("rb") as f: response = self.client.post( "/api/documents/post_document/", {"document": f, "correspondent": c.id}, @@ -2161,7 +2171,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): id=str(uuid.uuid4()), ) - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SIMPLE_DIGITAL_PDF.open("rb") as f: response = self.client.post( "/api/documents/post_document/", {"document": f, "correspondent": 3456}, @@ -2176,7 +2186,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): ) dt = DocumentType.objects.create(name="invoice") - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SIMPLE_DIGITAL_PDF.open("rb") as f: response = self.client.post( "/api/documents/post_document/", {"document": f, "document_type": dt.id}, @@ -2195,7 +2205,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): id=str(uuid.uuid4()), ) - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SIMPLE_DIGITAL_PDF.open("rb") as f: response = self.client.post( "/api/documents/post_document/", {"document": f, "document_type": 34578}, @@ -2210,7 +2220,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): ) sp = StoragePath.objects.create(name="invoices") - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SIMPLE_DIGITAL_PDF.open("rb") as f: response = self.client.post( "/api/documents/post_document/", {"document": f, "storage_path": sp.id}, @@ -2229,7 +2239,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): id=str(uuid.uuid4()), ) - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SIMPLE_DIGITAL_PDF.open("rb") as f: response = self.client.post( "/api/documents/post_document/", {"document": f, "storage_path": 34578}, @@ -2245,7 +2255,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): t1 = Tag.objects.create(name="tag1") t2 = Tag.objects.create(name="tag2") - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SIMPLE_DIGITAL_PDF.open("rb") as f: response = self.client.post( "/api/documents/post_document/", {"document": f, "tags": [t2.id, t1.id]}, @@ -2266,7 +2276,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): t1 = Tag.objects.create(name="tag1") t2 = Tag.objects.create(name="tag2") - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SIMPLE_DIGITAL_PDF.open("rb") as f: response = self.client.post( "/api/documents/post_document/", {"document": f, "tags": [t2.id, t1.id, 734563]}, @@ -2290,7 +2300,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): 0, tzinfo=zoneinfo.ZoneInfo("America/Los_Angeles"), ) - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SIMPLE_DIGITAL_PDF.open("rb") as f: response = self.client.post( "/api/documents/post_document/", {"document": f, "created": created}, @@ -2306,7 +2316,11 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): id=str(uuid.uuid4()), ) - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SimpleUploadedFile( + "simple.pdf", + SIMPLE_DIGITAL_PDF.read_bytes(), + content_type="application/pdf", + ) as f: response = self.client.post( "/api/documents/post_document/", {"document": f, "archive_serial_number": 500}, @@ -2333,7 +2347,11 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): data_type=CustomField.FieldDataType.STRING, ) - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SimpleUploadedFile( + "simple.pdf", + SIMPLE_DIGITAL_PDF.read_bytes(), + content_type="application/pdf", + ) as f: response = self.client.post( "/api/documents/post_document/", { @@ -2388,7 +2406,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): w1.actions.add(action1) w1.save() - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SIMPLE_DIGITAL_PDF.open("rb") as f: response = self.client.post( "/api/documents/post_document/", { @@ -2429,7 +2447,11 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): data_type=CustomField.FieldDataType.INT, ) - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SimpleUploadedFile( + "simple.pdf", + SIMPLE_DIGITAL_PDF.read_bytes(), + content_type="application/pdf", + ) as f: response = self.client.post( "/api/documents/post_document/", { @@ -2478,7 +2500,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): ] for payload in error_payloads: - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SIMPLE_DIGITAL_PDF.open("rb") as f: data = {"document": f, **payload} response = self.client.post( "/api/documents/post_document/", @@ -2536,7 +2558,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): id=str(uuid.uuid4()), ) - with (Path(__file__).parent / "samples" / "simple.pdf").open("rb") as f: + with SIMPLE_DIGITAL_PDF.open("rb") as f: response = self.client.post( "/api/documents/post_document/", {"document": f, "from_webui": True}, @@ -2575,14 +2597,8 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): archive_filename="archive.pdf", ) - source_file: Path = ( - Path(__file__).parent - / "samples" - / "documents" - / "thumbnails" - / "0000001.webp" - ) - archive_file: Path = Path(__file__).parent / "samples" / "simple.pdf" + source_file: Path = THUMBNAIL_WEBP + archive_file: Path = SIMPLE_DIGITAL_PDF shutil.copy(source_file, doc.source_path) shutil.copy(archive_file, doc.archive_path) @@ -2615,7 +2631,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): mime_type="application/pdf", ) - shutil.copy(Path(__file__).parent / "samples" / "simple.pdf", doc.source_path) + shutil.copy(SIMPLE_DIGITAL_PDF, doc.source_path) response = self.client.get(f"/api/documents/{doc.pk}/metadata/") self.assertEqual(response.status_code, status.HTTP_200_OK) @@ -4381,8 +4397,8 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): filename="test2.pdf", ) - archive_file = Path(__file__).parent / "samples" / "simple.pdf" - source_file = Path(__file__).parent / "samples" / "simple.pdf" + archive_file = SIMPLE_DIGITAL_PDF + source_file = SIMPLE_DIGITAL_PDF shutil.copy(archive_file, doc.archive_path) shutil.copy(source_file, doc2.source_path) diff --git a/src/documents/tests/test_api_email.py b/src/documents/tests/test_api_email.py index d26f26d85..c0a3fc9ac 100644 --- a/src/documents/tests/test_api_email.py +++ b/src/documents/tests/test_api_email.py @@ -12,6 +12,9 @@ from documents.tests.utils import SampleDirMixin from paperless_testing.dirs import DirectoriesMixin from paperless_testing.factories import UserFactory from paperless_testing.permissions import grant_global +from paperless_testing.samples import MULTI_PAGE_DIGITAL_PDF +from paperless_testing.samples import SIMPLE_DIGITAL_PDF +from paperless_testing.samples import WITH_FORM_PDF class TestEmail(DirectoriesMixin, SampleDirMixin, APITestCase): @@ -42,15 +45,15 @@ class TestEmail(DirectoriesMixin, SampleDirMixin, APITestCase): # Copy sample files to document paths (using different files to distinguish versions) shutil.copy( - self.SAMPLE_DIR / "documents" / "originals" / "0000001.pdf", + SIMPLE_DIGITAL_PDF, self.doc1.archive_path, ) shutil.copy( - self.SAMPLE_DIR / "documents" / "originals" / "0000002.pdf", + MULTI_PAGE_DIGITAL_PDF, self.doc1.source_path, ) shutil.copy( - self.SAMPLE_DIR / "documents" / "originals" / "0000003.pdf", + WITH_FORM_PDF, self.doc2.source_path, ) @@ -377,7 +380,7 @@ class TestEmail(DirectoriesMixin, SampleDirMixin, APITestCase): checksum="3", filename="test3.pdf", ) - shutil.copy(self.SAMPLE_DIR / "simple.pdf", doc3.source_path) + shutil.copy(self.SIMPLE_PDF, doc3.source_path) doc4 = Document.objects.create( title="test1", @@ -386,7 +389,7 @@ class TestEmail(DirectoriesMixin, SampleDirMixin, APITestCase): checksum="4", filename="test4.pdf", ) - shutil.copy(self.SAMPLE_DIR / "simple.pdf", doc4.source_path) + shutil.copy(self.SIMPLE_PDF, doc4.source_path) response = self.client.post( self.ENDPOINT, diff --git a/src/documents/tests/test_barcodes.py b/src/documents/tests/test_barcodes.py index 36322d2ab..9dc8f2353 100644 --- a/src/documents/tests/test_barcodes.py +++ b/src/documents/tests/test_barcodes.py @@ -169,7 +169,7 @@ class TestBarcode( - No barcodes detected - No pages to split on """ - test_file = self.SAMPLE_DIR / "simple.pdf" + test_file = self.SIMPLE_PDF with self.get_reader(test_file) as reader: reader.detect() separator_page_numbers = reader.get_separation_pages() @@ -461,7 +461,7 @@ class TestBarcode( THEN: - Nothing happens """ - test_file = self.SAMPLE_DIR / "simple.pdf" + test_file = self.SIMPLE_PDF with self.get_reader(test_file) as reader: try: diff --git a/src/documents/tests/test_bulk_edit.py b/src/documents/tests/test_bulk_edit.py index 9688b5cad..b341f5744 100644 --- a/src/documents/tests/test_bulk_edit.py +++ b/src/documents/tests/test_bulk_edit.py @@ -29,6 +29,9 @@ from documents.permissions import set_permissions_for_objects from paperless_testing.dirs import DirectoriesMixin from paperless_testing.factories import DocumentFactory from paperless_testing.permissions import grant_object +from paperless_testing.samples import MULTI_PAGE_DIGITAL_PDF +from paperless_testing.samples import SIMPLE_DIGITAL_PDF +from paperless_testing.samples import WITH_FORM_PDF class TestBulkEdit(DirectoriesMixin, TestCase): @@ -706,47 +709,27 @@ class TestPDFActions(DirectoriesMixin, TestCase): super().setUp() sample1 = self.dirs.scratch_dir / "sample.pdf" shutil.copy( - Path(__file__).parent - / "samples" - / "documents" - / "originals" - / "0000001.pdf", + SIMPLE_DIGITAL_PDF, sample1, ) sample1_archive = self.dirs.archive_dir / "sample_archive.pdf" shutil.copy( - Path(__file__).parent - / "samples" - / "documents" - / "originals" - / "0000001.pdf", + SIMPLE_DIGITAL_PDF, sample1_archive, ) sample2 = self.dirs.scratch_dir / "sample2.pdf" shutil.copy( - Path(__file__).parent - / "samples" - / "documents" - / "originals" - / "0000002.pdf", + MULTI_PAGE_DIGITAL_PDF, sample2, ) sample2_archive = self.dirs.archive_dir / "sample2_archive.pdf" shutil.copy( - Path(__file__).parent - / "samples" - / "documents" - / "originals" - / "0000002.pdf", + MULTI_PAGE_DIGITAL_PDF, sample2_archive, ) sample3 = self.dirs.scratch_dir / "sample3.pdf" shutil.copy( - Path(__file__).parent - / "samples" - / "documents" - / "originals" - / "0000003.pdf", + WITH_FORM_PDF, sample3, ) self.doc1 = Document.objects.create( @@ -780,11 +763,7 @@ class TestPDFActions(DirectoriesMixin, TestCase): ) img_doc_archive = self.dirs.archive_dir / "sample_image.pdf" shutil.copy( - Path(__file__).parent - / "samples" - / "documents" - / "originals" - / "0000001.pdf", + SIMPLE_DIGITAL_PDF, img_doc_archive, ) self.img_doc = Document.objects.create( diff --git a/src/documents/tests/test_consumer.py b/src/documents/tests/test_consumer.py index b9afd61a0..7c607e1e3 100644 --- a/src/documents/tests/test_consumer.py +++ b/src/documents/tests/test_consumer.py @@ -39,6 +39,9 @@ from paperless_testing.factories import TagFactory from paperless_testing.factories import UserFactory from paperless_testing.fakes.progress import FakeProgressManager from paperless_testing.permissions import grant_object +from paperless_testing.samples import MULTI_PAGE_DIGITAL_PDF +from paperless_testing.samples import MULTI_PAGE_IMAGES_PDF +from paperless_testing.samples import SIMPLE_DIGITAL_PDF class _BaseNewStyleParser: @@ -115,9 +118,7 @@ class _BaseNewStyleParser: class DummyParser(_BaseNewStyleParser): - _ARCHIVE_SRC = ( - Path(__file__).parent / "samples" / "documents" / "archive" / "0000001.pdf" - ) + _ARCHIVE_SRC = MULTI_PAGE_IMAGES_PDF def parse(self, document_path, mime_type, *, produce_archive: bool = True) -> None: self._text = "The Text" @@ -203,33 +204,19 @@ class TestConsumer( self.addCleanup(patcher.stop) def get_test_file(self): - src = ( - Path(__file__).parent - / "samples" - / "documents" - / "originals" - / "0000001.pdf" - ) + src = SIMPLE_DIGITAL_PDF dst = self.dirs.scratch_dir / "sample.pdf" shutil.copy(src, dst) return dst def get_test_file2(self): - src = ( - Path(__file__).parent - / "samples" - / "documents" - / "originals" - / "0000002.pdf" - ) + src = MULTI_PAGE_DIGITAL_PDF dst = self.dirs.scratch_dir / "sample2.pdf" shutil.copy(src, dst) return dst def get_test_archive_file(self): - src = ( - Path(__file__).parent / "samples" / "documents" / "archive" / "0000001.pdf" - ) + src = MULTI_PAGE_IMAGES_PDF dst = self.dirs.scratch_dir / "sample_archive.pdf" shutil.copy(src, dst) return dst @@ -1124,7 +1111,7 @@ class TestConsumer( @mock.patch("documents.consumer.get_parser_registry") def test_similar_filenames(self, m) -> None: shutil.copy( - Path(__file__).parent / "samples" / "simple.pdf", + SIMPLE_DIGITAL_PDF, settings.CONSUMPTION_DIR / "simple.pdf", ) shutil.copy( @@ -1665,13 +1652,7 @@ class TestConsumerRemoteOCR( self.addCleanup(patcher.stop) def _consume(self, *, overrides: DocumentMetadataOverrides | None = None) -> bool: - src = ( - Path(__file__).parent - / "samples" - / "documents" - / "originals" - / "0000001.pdf" - ) + src = SIMPLE_DIGITAL_PDF dst = self.dirs.scratch_dir / "sample.pdf" shutil.copy(src, dst) diff --git a/src/documents/tests/test_double_sided.py b/src/documents/tests/test_double_sided.py index 7ea806e93..c35b82486 100644 --- a/src/documents/tests/test_double_sided.py +++ b/src/documents/tests/test_double_sided.py @@ -37,12 +37,15 @@ class TestDoubleSided( self.double_sided_dir.mkdir() self.staging_file = self.dirs.scratch_dir / STAGING_FILE_NAME + def _sample(self, name: str) -> Path: + return self.SIMPLE_PDF if name == "simple.pdf" else self.SAMPLE_DIR / name + def consume_file(self, srcname, dstname: str | Path = "foo.pdf"): """ Starts the consume process and also ensures the destination file does not exist afterwards """ - src = self.SAMPLE_DIR / srcname + src = self._sample(srcname) dst = self.double_sided_dir / dstname dst.parent.mkdir(parents=True, exist_ok=True) shutil.copy(src, dst) @@ -57,7 +60,7 @@ class TestDoubleSided( return msg def create_staging_file(self, src="double-sided-odd.pdf", datetime=None) -> None: - shutil.copy(self.SAMPLE_DIR / src, self.staging_file) + shutil.copy(self._sample(src), self.staging_file) if datetime is None: datetime = dt.datetime.now() os.utime(str(self.staging_file), (datetime.timestamp(),) * 2) diff --git a/src/documents/tests/test_management.py b/src/documents/tests/test_management.py index 19ab4e269..0fe5d7eb5 100644 --- a/src/documents/tests/test_management.py +++ b/src/documents/tests/test_management.py @@ -23,8 +23,9 @@ from documents.tasks import update_document_content_maybe_archive_file from paperless_testing.assertions import FileSystemAssertsMixin from paperless_testing.dirs import DirectoriesMixin from paperless_testing.factories import DocumentFactory +from paperless_testing.samples import SIMPLE_DIGITAL_PDF -sample_file: Path = Path(__file__).parent / "samples" / "simple.pdf" +sample_file: Path = SIMPLE_DIGITAL_PDF @pytest.mark.management diff --git a/src/documents/tests/test_management_exporter.py b/src/documents/tests/test_management_exporter.py index b4cee95da..ec6a21b77 100644 --- a/src/documents/tests/test_management_exporter.py +++ b/src/documents/tests/test_management_exporter.py @@ -59,6 +59,7 @@ from paperless_testing.dirs import paperless_environment from paperless_testing.factories import DocumentBarcodeFactory from paperless_testing.factories import DocumentFactory from paperless_testing.permissions import grant_object +from paperless_testing.samples import install_document_samples @pytest.mark.management @@ -204,10 +205,7 @@ class TestExportImport( def test_exporter(self, *, use_filename_format=False) -> None: shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") num_permission_objects = Permission.objects.count() @@ -311,10 +309,7 @@ class TestExportImport( def test_exporter_with_filename_format(self) -> None: shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") with override_settings( FILENAME_FORMAT="{created_year}/{correspondent}/{title}", @@ -323,10 +318,7 @@ class TestExportImport( def test_exporter_includes_share_links_and_bundles(self) -> None: shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") share_link = ShareLink.objects.create( slug="share-link-slug", @@ -425,10 +417,7 @@ class TestExportImport( def test_update_export_changed_time(self) -> None: shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") self._do_export() self.assertIsFile(self.target / "manifest.json") @@ -464,10 +453,7 @@ class TestExportImport( def test_update_export_changed_checksum(self) -> None: shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") self._do_export() @@ -494,10 +480,7 @@ class TestExportImport( def test_update_export_deleted_document(self) -> None: shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") manifest = self._do_export() @@ -529,10 +512,7 @@ class TestExportImport( @override_settings(FILENAME_FORMAT="{title}/{correspondent}") def test_update_export_changed_location(self) -> None: shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") self._do_export(use_filename_format=True) self.assertIsFile(self.target / "wow1" / "c.pdf") @@ -574,10 +554,7 @@ class TestExportImport( - Zipfile contains exported files """ shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") args = ["document_exporter", self.target, "--zip"] @@ -606,10 +583,7 @@ class TestExportImport( - Zipfile contains exported files """ shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") args = ["document_exporter", self.target, "--zip", "--use-filename-format"] @@ -645,10 +619,7 @@ class TestExportImport( - The existing file and directory in target are removed """ shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") # Create stuff in target directory existing_file = self.target / "test.txt" @@ -744,10 +715,7 @@ class TestExportImport( - Documents can be imported again """ shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") manifest = self._do_export() has_archive = False @@ -790,10 +758,7 @@ class TestExportImport( - Documents can be imported again """ shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") manifest = self._do_export() has_thumbnail = False @@ -838,10 +803,7 @@ class TestExportImport( - Documents can be imported again """ shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") manifest = self._do_export(split_manifest=True) has_document = False @@ -866,10 +828,7 @@ class TestExportImport( def _export_import_barcodes(self, *, split_manifest: bool) -> None: shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") DocumentBarcodeFactory(document=self.d1, value="https://example.com") DocumentBarcodeFactory(document=self.d2, page=2, value="DE8937") @@ -905,10 +864,7 @@ class TestExportImport( - Documents can be imported again """ shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") self._do_export(use_folder_prefix=True) @@ -935,10 +891,7 @@ class TestExportImport( - Documents can be imported again """ shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") self._do_export(use_folder_prefix=True, split_manifest=True) @@ -965,10 +918,7 @@ class TestExportImport( """ shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") num_content_type_objects = ContentType.objects.count() num_permission_objects = Permission.objects.count() @@ -1005,10 +955,7 @@ class TestExportImport( def test_exporter_with_auditlog_disabled(self) -> None: shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") with override_settings( AUDIT_LOG_ENABLED=False, @@ -1028,10 +975,7 @@ class TestExportImport( survive the round-trip with deleted_at preserved """ shutil.rmtree(Path(self.dirs.media_dir) / "documents") - shutil.copytree( - Path(__file__).parent / "samples" / "documents", - Path(self.dirs.media_dir) / "documents", - ) + install_document_samples(Path(self.dirs.media_dir) / "documents") # d1 has self.note and self.cfi1 attached via setUp self.d1.delete() @@ -1075,10 +1019,7 @@ class TestExportImport( """ shutil.rmtree(self.dirs.media_dir / "documents") - shutil.copytree( - self.SAMPLE_DIR / "documents", - self.dirs.media_dir / "documents", - ) + install_document_samples(self.dirs.media_dir / "documents") _ = self._do_export(data_only=True) diff --git a/src/documents/tests/test_management_thumbnails.py b/src/documents/tests/test_management_thumbnails.py index 4da0caf5a..595aedc64 100644 --- a/src/documents/tests/test_management_thumbnails.py +++ b/src/documents/tests/test_management_thumbnails.py @@ -11,6 +11,7 @@ from documents.models import Document from documents.parsers import get_default_thumbnail from paperless_testing.assertions import FileSystemAssertsMixin from paperless_testing.dirs import DirectoriesMixin +from paperless_testing.samples import SIMPLE_DIGITAL_PDF @pytest.mark.management @@ -24,7 +25,7 @@ class TestMakeThumbnails(DirectoriesMixin, FileSystemAssertsMixin, TestCase): filename="test.pdf", ) shutil.copy( - Path(__file__).parent / "samples" / "simple.pdf", + SIMPLE_DIGITAL_PDF, self.d1.source_path, ) @@ -36,7 +37,7 @@ class TestMakeThumbnails(DirectoriesMixin, FileSystemAssertsMixin, TestCase): filename="test2.pdf", ) shutil.copy( - Path(__file__).parent / "samples" / "simple.pdf", + SIMPLE_DIGITAL_PDF, self.d2.source_path, ) diff --git a/src/documents/tests/test_pdf_ops.py b/src/documents/tests/test_pdf_ops.py index eb4cf7005..632ebc927 100644 --- a/src/documents/tests/test_pdf_ops.py +++ b/src/documents/tests/test_pdf_ops.py @@ -15,10 +15,11 @@ import pytest from documents import pdf_ops from documents.pdf_ops import PageSpec +from paperless_testing.samples import MULTI_PAGE_DIGITAL_PDF SRC_ROOT = Path(__file__).parents[2] SAMPLES = Path(__file__).parent / "samples" -THREE_PAGES = SAMPLES / "documents" / "originals" / "0000002.pdf" +THREE_PAGES = MULTI_PAGE_DIGITAL_PDF TWELVE_PAGES = SAMPLES / "barcodes" / "split-by-asn-2.pdf" ENCRYPTED = SAMPLES / "password-is-test.pdf" SIGNED = SRC_ROOT / "paperless" / "tests" / "samples" / "tesseract" / "signed.pdf" diff --git a/src/documents/tests/test_tasks.py b/src/documents/tests/test_tasks.py index 50d16233b..a5def0944 100644 --- a/src/documents/tests/test_tasks.py +++ b/src/documents/tests/test_tasks.py @@ -21,6 +21,7 @@ from documents.tests.helpers import dummy_preprocess from paperless_testing.assertions import FileSystemAssertsMixin from paperless_testing.dirs import DirectoriesMixin from paperless_testing.factories import DocumentFactory +from paperless_testing.samples import SIMPLE_DIGITAL_PDF @pytest.mark.django_db @@ -229,20 +230,12 @@ class TestUpdateContent(DirectoriesMixin, TestCase): """ sample1 = self.dirs.scratch_dir / "sample.pdf" shutil.copy( - Path(__file__).parent - / "samples" - / "documents" - / "originals" - / "0000001.pdf", + SIMPLE_DIGITAL_PDF, sample1, ) sample1_archive = self.dirs.archive_dir / "sample_archive.pdf" shutil.copy( - Path(__file__).parent - / "samples" - / "documents" - / "originals" - / "0000001.pdf", + SIMPLE_DIGITAL_PDF, sample1_archive, ) doc = Document.objects.create( @@ -270,11 +263,7 @@ class TestUpdateContent(DirectoriesMixin, TestCase): """ sample1 = self.dirs.scratch_dir / "sample.pdf" shutil.copy( - Path(__file__).parent - / "samples" - / "documents" - / "originals" - / "0000001.pdf", + SIMPLE_DIGITAL_PDF, sample1, ) doc = Document.objects.create( @@ -290,14 +279,7 @@ class TestUpdateContent(DirectoriesMixin, TestCase): def _create_root_with_version(self) -> tuple[Document, Document]: sample1 = self.dirs.scratch_dir / "sample.pdf" - shutil.copy( - Path(__file__).parent - / "samples" - / "documents" - / "originals" - / "0000001.pdf", - sample1, - ) + shutil.copy(SIMPLE_DIGITAL_PDF, sample1) root = DocumentFactory(content="root content", mime_type="application/pdf") version = DocumentFactory( content="my document", diff --git a/src/documents/tests/test_workflows.py b/src/documents/tests/test_workflows.py index 506fa6872..820882b49 100644 --- a/src/documents/tests/test_workflows.py +++ b/src/documents/tests/test_workflows.py @@ -178,7 +178,7 @@ class TestWorkflows( self.assertEqual(action.__str__(), "WorkflowAction 1") test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) @@ -290,7 +290,7 @@ class TestWorkflows( w.save() test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) @@ -409,7 +409,7 @@ class TestWorkflows( w2.save() test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) @@ -478,7 +478,7 @@ class TestWorkflows( w.save() test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) @@ -531,7 +531,7 @@ class TestWorkflows( w.save() test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) @@ -608,7 +608,7 @@ class TestWorkflows( w.save() test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) @@ -687,7 +687,7 @@ class TestWorkflows( w.save() test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) @@ -765,7 +765,7 @@ class TestWorkflows( w.save() test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) @@ -876,7 +876,7 @@ class TestWorkflows( w.save() test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) @@ -928,7 +928,7 @@ class TestWorkflows( generated = generate_unique_filename(doc) destination = (settings.ORIGINALS_DIR / generated).resolve() create_source_path_directory(destination) - shutil.copy(self.SAMPLE_DIR / "simple.pdf", destination) + shutil.copy(self.SIMPLE_PDF, destination) Document.objects.filter(pk=doc.pk).update(filename=generated.as_posix()) doc.refresh_from_db() @@ -2067,7 +2067,7 @@ class TestWorkflows( superuser = UserFactory(username="superuser", superuser=True) self.client.force_authenticate(user=superuser) test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) tasks.consume_file( @@ -3053,7 +3053,7 @@ class TestWorkflows( generated = generate_unique_filename(doc) destination = (settings.ORIGINALS_DIR / generated).resolve() create_source_path_directory(destination) - shutil.copy(self.SAMPLE_DIR / "simple.pdf", destination) + shutil.copy(self.SIMPLE_PDF, destination) Document.objects.filter(pk=doc.pk).update(filename=generated.as_posix()) doc.refresh_from_db() doc.tags.set([self.t1, self.t2]) @@ -3117,7 +3117,7 @@ class TestWorkflows( generated = generate_unique_filename(doc) destination = (settings.ORIGINALS_DIR / generated).resolve() create_source_path_directory(destination) - shutil.copy(self.SAMPLE_DIR / "simple.pdf", destination) + shutil.copy(self.SIMPLE_PDF, destination) Document.objects.filter(pk=doc.pk).update(filename=generated.as_posix()) doc.refresh_from_db() doc.tags.set([self.t1]) @@ -3266,7 +3266,7 @@ class TestWorkflows( w.save() test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) @@ -3390,7 +3390,7 @@ class TestWorkflows( w.save() test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) @@ -3540,7 +3540,7 @@ class TestWorkflows( workflow.actions.set([assignment_action, email_action]) temp_working_copy = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "working-copy.pdf", ) @@ -3640,7 +3640,7 @@ class TestWorkflows( # move the file test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) @@ -3748,7 +3748,7 @@ class TestWorkflows( generated = generate_unique_filename(doc) destination = (settings.ORIGINALS_DIR / generated).resolve() create_source_path_directory(destination) - shutil.copy(self.SAMPLE_DIR / "simple.pdf", destination) + shutil.copy(self.SIMPLE_PDF, destination) Document.objects.filter(pk=doc.pk).update(filename=generated.as_posix()) run_workflows(WorkflowTrigger.WorkflowTriggerType.DOCUMENT_UPDATED, doc) @@ -3959,7 +3959,7 @@ class TestWorkflows( w.save() test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) @@ -4080,7 +4080,7 @@ class TestWorkflows( w.save() test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) @@ -4431,7 +4431,7 @@ class TestWorkflows( w.save() test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) @@ -4879,7 +4879,7 @@ class TestWorkflows( w.save() test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) @@ -5071,7 +5071,7 @@ class TestWorkflows( # Create a test file to be consumed test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) test_file_path = Path(test_file) @@ -5135,7 +5135,7 @@ class TestWorkflows( # Create a test file to be consumed test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple2.pdf", ) test_file_path = Path(test_file) @@ -5639,7 +5639,7 @@ class TestDateWorkflowLocalization( ) test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, tmp_path / "simple.pdf", ) @@ -5706,7 +5706,7 @@ class TestRemoteOCRWorkflowAction(DirectoriesMixin, SampleDirMixin, APITestCase) self._make_workflow(WorkflowTrigger.WorkflowTriggerType.CONSUMPTION) test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) overrides = DocumentMetadataOverrides() @@ -5924,7 +5924,7 @@ class TestApplyAISuggestionsWorkflowAction( ) test_file = shutil.copy( - self.SAMPLE_DIR / "simple.pdf", + self.SIMPLE_PDF, self.dirs.scratch_dir / "simple.pdf", ) diff --git a/src/documents/tests/utils.py b/src/documents/tests/utils.py index 63e907855..18769dfc9 100644 --- a/src/documents/tests/utils.py +++ b/src/documents/tests/utils.py @@ -11,6 +11,7 @@ from documents.data_models import ConsumableDocument from documents.data_models import DocumentMetadataOverrides from documents.data_models import DocumentSource from paperless_testing.fakes.progress import FakeProgressManager +from paperless_testing.samples import SIMPLE_DIGITAL_PDF class ConsumeTaskMixin: @@ -53,6 +54,8 @@ class SampleDirMixin: BARCODE_SAMPLE_DIR = SAMPLE_DIR / "barcodes" + SIMPLE_PDF = SIMPLE_DIGITAL_PDF + class GetConsumerMixin: @contextmanager diff --git a/src/paperless/tests/parsers/conftest.py b/src/paperless/tests/parsers/conftest.py index 343e9ea8a..5e834e959 100644 --- a/src/paperless/tests/parsers/conftest.py +++ b/src/paperless/tests/parsers/conftest.py @@ -432,30 +432,6 @@ def tesseract_samples_dir(parser_samples_dir: Path) -> Path: return parser_samples_dir / "tesseract" -@pytest.fixture(scope="session") -def multi_page_images_pdf_file(tesseract_samples_dir: Path) -> Path: - """Path to a multi-page PDF with images. - - Returns - ------- - Path - Absolute path to ``tesseract/multi-page-images.pdf``. - """ - return tesseract_samples_dir / "multi-page-images.pdf" - - -@pytest.fixture(scope="session") -def simple_digital_pdf_file(tesseract_samples_dir: Path) -> Path: - """Path to a simple digital PDF sample file. - - Returns - ------- - Path - Absolute path to ``tesseract/simple-digital.pdf``. - """ - return tesseract_samples_dir / "simple-digital.pdf" - - @pytest.fixture(scope="session") def simple_no_dpi_png_file(tesseract_samples_dir: Path) -> Path: """Path to a simple PNG without DPI information. @@ -480,6 +456,18 @@ def simple_png_file(tesseract_samples_dir: Path) -> Path: return tesseract_samples_dir / "simple.png" +@pytest.fixture(scope="session") +def rotated_pdf_file(tesseract_samples_dir: Path) -> Path: + """Path to a landscape (rotated) PDF sample file. + + Returns + ------- + Path + Absolute path to ``tesseract/rotated.pdf``. + """ + return tesseract_samples_dir / "rotated.pdf" + + # ------------------------------------------------------------------ # Tesseract parser instance and settings helpers # ------------------------------------------------------------------ diff --git a/src/paperless/tests/parsers/test_tesseract_parser.py b/src/paperless/tests/parsers/test_tesseract_parser.py index f057ad086..a44d23bec 100644 --- a/src/paperless/tests/parsers/test_tesseract_parser.py +++ b/src/paperless/tests/parsers/test_tesseract_parser.py @@ -161,11 +161,11 @@ class TestGetPageCount: def test_single_page_pdf( self, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + simple_digital_pdf_file: Path, ) -> None: assert ( tesseract_parser.get_page_count( - tesseract_samples_dir / "simple-digital.pdf", + simple_digital_pdf_file, "application/pdf", ) == 1 @@ -188,7 +188,7 @@ class TestGetPageCount: self, mocker: MockerFixture, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + simple_digital_pdf_file: Path, caplog, ) -> None: """ @@ -204,7 +204,7 @@ class TestGetPageCount: with caplog.at_level(logging.WARNING): page_count = tesseract_parser.get_page_count( - tesseract_samples_dir / "simple-digital.pdf", + simple_digital_pdf_file, "application/pdf", ) assert page_count is None @@ -273,26 +273,26 @@ class TestGetThumbnail: def test_thumbnail_is_file( self, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + simple_digital_pdf_file: Path, ) -> None: thumb = tesseract_parser.get_thumbnail( - tesseract_samples_dir / "simple-digital.pdf", + simple_digital_pdf_file, "application/pdf", ) assert thumb.is_file() @pytest.mark.parametrize( - ("filename", "expected_height"), + ("sample_fixture", "expected_height"), [ - pytest.param("simple-digital.pdf", 647, id="portrait-letter"), - pytest.param("rotated.pdf", 386, id="landscape"), + pytest.param("simple_digital_pdf_file", 647, id="portrait-letter"), + pytest.param("rotated_pdf_file", 386, id="landscape"), ], ) def test_thumbnail_is_correct_format_and_size( self, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, - filename: str, + request: pytest.FixtureRequest, + sample_fixture: str, expected_height: int, ) -> None: """ @@ -304,7 +304,7 @@ class TestGetThumbnail: - A 500px wide WebP keeping the page's aspect ratio """ thumb = tesseract_parser.get_thumbnail( - tesseract_samples_dir / filename, + request.getfixturevalue(sample_fixture), "application/pdf", ) with Image.open(thumb) as im: @@ -316,7 +316,7 @@ class TestGetThumbnail: self, mocker: MockerFixture, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + simple_digital_pdf_file: Path, ) -> None: """ GIVEN: @@ -326,7 +326,7 @@ class TestGetThumbnail: THEN: - The PDF is repaired with qpdf and a real thumbnail is rendered """ - original = tesseract_samples_dir / "simple-digital.pdf" + original = simple_digital_pdf_file def _fail_on_original(in_path: Path, out_path: Path, **kwargs) -> None: if in_path == original: @@ -368,11 +368,11 @@ class TestExtractText: def test_extract_text_from_digital_pdf( self, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + simple_digital_pdf_file: Path, ) -> None: text = tesseract_parser.extract_text( None, - tesseract_samples_dir / "simple-digital.pdf", + simple_digital_pdf_file, ) assert text is not None assert "This is a test document." in text.strip() @@ -387,7 +387,7 @@ class TestParsePdf: def test_simple_digital_creates_archive( self, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + multi_page_digital_pdf_file: Path, ) -> None: """ GIVEN: @@ -401,7 +401,7 @@ class TestParsePdf: - Text is extracted """ tesseract_parser.parse( - tesseract_samples_dir / "multi-page-digital.pdf", + multi_page_digital_pdf_file, "application/pdf", ) assert tesseract_parser.archive_path is not None @@ -416,10 +416,10 @@ class TestParsePdf: def test_with_form_default( self, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + with_form_pdf_file: Path, ) -> None: tesseract_parser.parse( - tesseract_samples_dir / "with-form.pdf", + with_form_pdf_file, "application/pdf", ) assert tesseract_parser.archive_path is not None @@ -432,11 +432,11 @@ class TestParsePdf: def test_with_form_redo_no_archive_when_not_requested( self, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + with_form_pdf_file: Path, ) -> None: tesseract_parser.settings.mode = ModeChoices.REDO tesseract_parser.parse( - tesseract_samples_dir / "with-form.pdf", + with_form_pdf_file, "application/pdf", produce_archive=False, ) @@ -449,11 +449,11 @@ class TestParsePdf: def test_with_form_force( self, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + with_form_pdf_file: Path, ) -> None: tesseract_parser.settings.mode = ModeChoices.FORCE tesseract_parser.parse( - tesseract_samples_dir / "with-form.pdf", + with_form_pdf_file, "application/pdf", ) assert_ordered_substrings( @@ -494,7 +494,7 @@ class TestParsePdf: self, mocker: MockerFixture, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + simple_digital_pdf_file: Path, ) -> None: mocker.patch( "ocrmypdf.ocr", @@ -502,7 +502,7 @@ class TestParsePdf: ) with pytest.raises(ParseError): tesseract_parser.parse( - tesseract_samples_dir / "simple-digital.pdf", + simple_digital_pdf_file, "application/pdf", ) @@ -578,10 +578,10 @@ class TestParseMultiPage: def test_multi_page_digital( self, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + multi_page_digital_pdf_file: Path, ) -> None: tesseract_parser.parse( - tesseract_samples_dir / "multi-page-digital.pdf", + multi_page_digital_pdf_file, "application/pdf", ) assert tesseract_parser.archive_path is not None @@ -605,12 +605,12 @@ class TestParseMultiPage: self, mode: str, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + multi_page_digital_pdf_file: Path, ) -> None: tesseract_parser.settings.pages = 2 tesseract_parser.settings.mode = mode tesseract_parser.parse( - tesseract_samples_dir / "multi-page-digital.pdf", + multi_page_digital_pdf_file, "application/pdf", ) assert tesseract_parser.archive_path is not None @@ -624,11 +624,11 @@ class TestParseMultiPage: def test_multi_page_images_skip( self, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + multi_page_images_pdf_file: Path, ) -> None: tesseract_parser.settings.mode = ModeChoices.AUTO tesseract_parser.parse( - tesseract_samples_dir / "multi-page-images.pdf", + multi_page_images_pdf_file, "application/pdf", ) assert tesseract_parser.archive_path is not None @@ -642,7 +642,7 @@ class TestParseMultiPage: def test_multi_page_images_redo_pages_2( self, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + multi_page_images_pdf_file: Path, ) -> None: """ GIVEN: @@ -657,7 +657,7 @@ class TestParseMultiPage: tesseract_parser.settings.pages = 2 tesseract_parser.settings.mode = ModeChoices.REDO tesseract_parser.parse( - tesseract_samples_dir / "multi-page-images.pdf", + multi_page_images_pdf_file, "application/pdf", ) assert tesseract_parser.archive_path is not None @@ -670,7 +670,7 @@ class TestParseMultiPage: def test_multi_page_images_force_page_1( self, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + multi_page_images_pdf_file: Path, ) -> None: """ GIVEN: @@ -685,7 +685,7 @@ class TestParseMultiPage: tesseract_parser.settings.pages = 1 tesseract_parser.settings.mode = ModeChoices.FORCE tesseract_parser.parse( - tesseract_samples_dir / "multi-page-images.pdf", + multi_page_images_pdf_file, "application/pdf", ) assert tesseract_parser.archive_path is not None @@ -781,7 +781,7 @@ class TestSkipArchive: def test_skip_noarchive_with_text_layer( self, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + multi_page_digital_pdf_file: Path, ) -> None: """ GIVEN: @@ -795,7 +795,7 @@ class TestSkipArchive: """ tesseract_parser.settings.mode = ModeChoices.AUTO tesseract_parser.parse( - tesseract_samples_dir / "multi-page-digital.pdf", + multi_page_digital_pdf_file, "application/pdf", produce_archive=False, ) @@ -810,7 +810,7 @@ class TestSkipArchive: def test_skip_noarchive_image_only_creates_archive( self, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + multi_page_images_pdf_file: Path, ) -> None: """ GIVEN: @@ -823,7 +823,7 @@ class TestSkipArchive: """ tesseract_parser.settings.mode = ModeChoices.AUTO tesseract_parser.parse( - tesseract_samples_dir / "multi-page-images.pdf", + multi_page_images_pdf_file, "application/pdf", ) assert tesseract_parser.archive_path is not None @@ -835,29 +835,29 @@ class TestSkipArchive: ) @pytest.mark.parametrize( - ("produce_archive", "filename", "expect_archive"), + ("produce_archive", "sample_fixture", "expect_archive"), [ pytest.param( True, - "multi-page-digital.pdf", + "multi_page_digital_pdf_file", True, id="produce-archive-with-text", ), pytest.param( True, - "multi-page-images.pdf", + "multi_page_images_pdf_file", True, id="produce-archive-no-text", ), pytest.param( False, - "multi-page-digital.pdf", + "multi_page_digital_pdf_file", False, id="no-archive-with-text-layer", ), pytest.param( False, - "multi-page-images.pdf", + "multi_page_images_pdf_file", False, id="no-archive-no-text-layer", ), @@ -866,10 +866,10 @@ class TestSkipArchive: def test_produce_archive_flag( self, produce_archive: bool, # noqa: FBT001 - filename: str, + sample_fixture: str, expect_archive: bool, # noqa: FBT001 tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + request: pytest.FixtureRequest, ) -> None: """ GIVEN: @@ -882,8 +882,9 @@ class TestSkipArchive: - Text is always extracted """ tesseract_parser.settings.mode = ModeChoices.AUTO + sample = request.getfixturevalue(sample_fixture) tesseract_parser.parse( - tesseract_samples_dir / filename, + sample, "application/pdf", produce_archive=produce_archive, ) @@ -900,7 +901,7 @@ class TestSkipArchive: self, mocker: MockerFixture, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + simple_digital_pdf_file: Path, ) -> None: """ GIVEN: @@ -916,7 +917,7 @@ class TestSkipArchive: tesseract_parser.settings.mode = ModeChoices.AUTO mock_ocr = mocker.patch("ocrmypdf.ocr") tesseract_parser.parse( - tesseract_samples_dir / "simple-digital.pdf", + simple_digital_pdf_file, "application/pdf", produce_archive=False, ) @@ -955,7 +956,7 @@ class TestSkipArchive: def test_tagged_pdf_produces_pdfa_archive_without_ocr( self, tesseract_parser: RasterisedDocumentParser, - tesseract_samples_dir: Path, + simple_digital_pdf_file: Path, ) -> None: """ GIVEN: @@ -970,7 +971,7 @@ class TestSkipArchive: """ tesseract_parser.settings.mode = ModeChoices.AUTO tesseract_parser.parse( - tesseract_samples_dir / "simple-digital.pdf", + simple_digital_pdf_file, "application/pdf", produce_archive=True, ) diff --git a/src/paperless/tests/samples/mail/sample.html b/src/paperless/tests/samples/mail/sample.html deleted file mode 100644 index 9d535dba6..000000000 --- a/src/paperless/tests/samples/mail/sample.html +++ /dev/null @@ -1,19 +0,0 @@ - - - - - -

Some Text

-

- Has to be rewritten to work.. - This image should not be shown. -

- -

and an embedded image.
-

-

Paragraph unchanged.

- - - diff --git a/src/paperless/tests/samples/mail/sample.html.pdf b/src/paperless/tests/samples/mail/sample.html.pdf deleted file mode 100644 index 24c2dbf87..000000000 Binary files a/src/paperless/tests/samples/mail/sample.html.pdf and /dev/null differ diff --git a/src/paperless/tests/samples/mail/sample.html.pdf.webp b/src/paperless/tests/samples/mail/sample.html.pdf.webp deleted file mode 100644 index 534157660..000000000 Binary files a/src/paperless/tests/samples/mail/sample.html.pdf.webp and /dev/null differ diff --git a/src/paperless/tests/samples/mail/sample.png b/src/paperless/tests/samples/mail/sample.png deleted file mode 100644 index 162aadad0..000000000 Binary files a/src/paperless/tests/samples/mail/sample.png and /dev/null differ diff --git a/src/paperless/tests/samples/tesseract/no-text-alpha.png b/src/paperless/tests/samples/tesseract/no-text-alpha.png deleted file mode 100644 index e78b22bfb..000000000 Binary files a/src/paperless/tests/samples/tesseract/no-text-alpha.png and /dev/null differ diff --git a/src/paperless/tests/test_parser_utils.py b/src/paperless/tests/test_parser_utils.py index 7bae1f78d..ed20a024b 100644 --- a/src/paperless/tests/test_parser_utils.py +++ b/src/paperless/tests/test_parser_utils.py @@ -18,8 +18,6 @@ from paperless.parsers.utils import read_file_handle_unicode_errors if TYPE_CHECKING: from pytest_mock import MockerFixture -SAMPLES = Path(__file__).parent / "samples" / "tesseract" - class TestReadFileHandleUnicodeErrors: def test_plain_utf8(self, tmp_path: Path) -> None: @@ -57,11 +55,11 @@ class TestReadFileHandleUnicodeErrors: class TestIsTaggedPdf: - def test_tagged_pdf_returns_true(self) -> None: - assert is_tagged_pdf(SAMPLES / "simple-digital.pdf") is True + def test_tagged_pdf_returns_true(self, simple_digital_pdf_file: Path) -> None: + assert is_tagged_pdf(simple_digital_pdf_file) is True - def test_untagged_pdf_returns_false(self) -> None: - assert is_tagged_pdf(SAMPLES / "multi-page-images.pdf") is False + def test_untagged_pdf_returns_false(self, multi_page_images_pdf_file: Path) -> None: + assert is_tagged_pdf(multi_page_images_pdf_file) is False def test_nonexistent_path_returns_false(self) -> None: assert is_tagged_pdf(Path("/nonexistent/file.pdf")) is False @@ -95,7 +93,7 @@ class TestGetPdfFirstPageSizePoints: pdf.save(path) return path - def test_letter_sample(self) -> None: + def test_letter_sample(self, simple_digital_pdf_file: Path) -> None: """ GIVEN: - A US Letter sample PDF with no CropBox and no rotation @@ -104,7 +102,7 @@ class TestGetPdfFirstPageSizePoints: THEN: - The MediaBox size in points is returned """ - assert get_pdf_first_page_size_points(SAMPLES / "simple-digital.pdf") == ( + assert get_pdf_first_page_size_points(simple_digital_pdf_file) == ( 612.0, 792.0, ) @@ -199,6 +197,7 @@ class TestGetPdfFirstPageSizePoints: def test_pdfinfo_not_installed_returns_none( self, mocker: MockerFixture, + simple_digital_pdf_file: Path, ) -> None: """ GIVEN: @@ -209,7 +208,7 @@ class TestGetPdfFirstPageSizePoints: - None is returned and nothing is raised """ mocker.patch("subprocess.run", side_effect=FileNotFoundError("pdfinfo")) - assert get_pdf_first_page_size_points(SAMPLES / "simple-digital.pdf") is None + assert get_pdf_first_page_size_points(simple_digital_pdf_file) is None def test_degenerate_box_returns_none(self, tmp_path: Path) -> None: """ @@ -251,7 +250,7 @@ class TestGetPdfFirstPageSizePoints: bad.write_bytes(b"not a pdf") assert get_pdf_first_page_size_points(bad) is None - def test_encrypted_pdf_returns_none(self) -> None: + def test_encrypted_pdf_returns_none(self, parser_samples_dir: Path) -> None: """ GIVEN: - A password protected PDF @@ -260,7 +259,8 @@ class TestGetPdfFirstPageSizePoints: THEN: - None is returned and nothing is raised """ - assert get_pdf_first_page_size_points(SAMPLES / "encrypted.pdf") is None + encrypted = parser_samples_dir / "tesseract" / "encrypted.pdf" + assert get_pdf_first_page_size_points(encrypted) is None class TestPostProcessText: diff --git a/src/paperless/tests/samples/tesseract/multi-page-digital.pdf b/src/paperless_testing/sample_files/multi-page-digital.pdf similarity index 100% rename from src/paperless/tests/samples/tesseract/multi-page-digital.pdf rename to src/paperless_testing/sample_files/multi-page-digital.pdf diff --git a/src/paperless/tests/samples/tesseract/multi-page-images.pdf b/src/paperless_testing/sample_files/multi-page-images.pdf similarity index 100% rename from src/paperless/tests/samples/tesseract/multi-page-images.pdf rename to src/paperless_testing/sample_files/multi-page-images.pdf diff --git a/src/paperless/tests/samples/tesseract/simple-digital.pdf b/src/paperless_testing/sample_files/simple-digital.pdf similarity index 100% rename from src/paperless/tests/samples/tesseract/simple-digital.pdf rename to src/paperless_testing/sample_files/simple-digital.pdf diff --git a/src/documents/tests/samples/documents/thumbnails/0000001.webp b/src/paperless_testing/sample_files/thumbnail.webp similarity index 100% rename from src/documents/tests/samples/documents/thumbnails/0000001.webp rename to src/paperless_testing/sample_files/thumbnail.webp diff --git a/src/paperless/tests/samples/tesseract/with-form.pdf b/src/paperless_testing/sample_files/with-form.pdf similarity index 100% rename from src/paperless/tests/samples/tesseract/with-form.pdf rename to src/paperless_testing/sample_files/with-form.pdf diff --git a/src/paperless_testing/samples.py b/src/paperless_testing/samples.py new file mode 100644 index 000000000..2e823041a --- /dev/null +++ b/src/paperless_testing/samples.py @@ -0,0 +1,48 @@ +"""Test sample files shared across tests. + +A sample used by a single app stays in that app's ``tests/samples`` tree. +Samples needed by more than one app, or stored under more than one name, live +here exactly once. +""" + +import shutil +from pathlib import Path + +SHARED_SAMPLES_DIR = (Path(__file__).parent / "sample_files").resolve() + +SIMPLE_DIGITAL_PDF = SHARED_SAMPLES_DIR / "simple-digital.pdf" +MULTI_PAGE_DIGITAL_PDF = SHARED_SAMPLES_DIR / "multi-page-digital.pdf" +WITH_FORM_PDF = SHARED_SAMPLES_DIR / "with-form.pdf" +MULTI_PAGE_IMAGES_PDF = SHARED_SAMPLES_DIR / "multi-page-images.pdf" +THUMBNAIL_WEBP = SHARED_SAMPLES_DIR / "thumbnail.webp" + +# The fake media tree under documents/tests/samples/documents keeps only the +# files unique to it. The files that duplicate a shared sample are laid back +# down under their historical opaque names (DB rows and exporter manifests +# refer to them) by install_document_samples(). +_DOCUMENT_SAMPLES_DIR = ( + Path(__file__).parent.parent / "documents" / "tests" / "samples" / "documents" +).resolve() + +_SHARED_AS_MEDIA = ( + ("originals/0000001.pdf", SIMPLE_DIGITAL_PDF), + ("originals/0000002.pdf", MULTI_PAGE_DIGITAL_PDF), + ("originals/0000003.pdf", WITH_FORM_PDF), + ("archive/0000001.pdf", MULTI_PAGE_IMAGES_PDF), + ("thumbnails/0000001.webp", THUMBNAIL_WEBP), + ("thumbnails/0000002.webp", THUMBNAIL_WEBP), + ("thumbnails/0000003.webp", THUMBNAIL_WEBP), + ("thumbnails/0000004.webp", THUMBNAIL_WEBP), +) + + +def install_document_samples(dest: Path) -> None: + """Populate ``dest`` with the full fake media tree. + + Replaces ``shutil.copytree(/documents, dest)`` in tests. + """ + shutil.copytree(_DOCUMENT_SAMPLES_DIR, dest, dirs_exist_ok=True) + for relpath, source in _SHARED_AS_MEDIA: + target = dest / relpath + target.parent.mkdir(parents=True, exist_ok=True) + shutil.copy2(source, target)