import shutil from pathlib import Path import pytest from pytest_django.fixtures import Settings from documents import tasks from documents.models import Correspondent from documents.models import Document from documents.utils import compute_checksum from paperless_testing.dirs import PaperlessDirs from paperless_testing.samples import SIMPLE_DIGITAL_PDF @pytest.mark.django_db class TestReprocessArchiveCleanup: def test_reprocess_with_pending_rename_leaves_one_archive( self, paperless_dirs: PaperlessDirs, settings: Settings, ) -> None: """ GIVEN: - A document whose committed metadata already generates a different filename than the one its files still have, because its file move has not run yet WHEN: - The document is reprocessed and a new archive file is produced THEN: - Exactly one archive file remains, the one the document points at - The superseded archive file is not left behind """ settings.FILENAME_FORMAT = "{{ correspondent }}/{{ title }}" settings.ARCHIVE_FILE_GENERATION = "always" old_correspondent = Correspondent.objects.create(name="old") new_correspondent = Correspondent.objects.create(name="new") original = paperless_dirs.originals_dir / "old" / "t.pdf" archive = paperless_dirs.archive_dir / "old" / "t.pdf" for path in (original, archive): path.parent.mkdir(parents=True) shutil.copy(SIMPLE_DIGITAL_PDF, path) doc = Document.objects.create( title="t", content="my document", checksum=compute_checksum(original), archive_checksum="stale", filename="old/t.pdf", archive_filename="old/t.pdf", mime_type="application/pdf", correspondent=old_correspondent, ) # A queryset update sends no signals, so no file move is requested, as # with a committed edit whose deferred move has not run yet. Document.objects.filter(pk=doc.pk).update(correspondent=new_correspondent) tasks.update_document_content_maybe_archive_file(doc.pk) refreshed = Document.objects.get(pk=doc.pk) archives = sorted( p.relative_to(paperless_dirs.archive_dir).as_posix() for p in Path(paperless_dirs.archive_dir).rglob("*.pdf") ) assert archives == [refreshed.archive_filename] assert refreshed.archive_filename == "new/t.pdf" assert (paperless_dirs.originals_dir / refreshed.filename).is_file() def test_reprocess_without_pending_rename_keeps_archive_in_place( self, paperless_dirs: PaperlessDirs, settings: Settings, ) -> None: """ GIVEN: - A document whose files already match its generated filename WHEN: - The document is reprocessed and a new archive file is produced THEN: - The new archive replaces the old one at the same path and is not deleted as a superseded file """ settings.FILENAME_FORMAT = "{{ correspondent }}/{{ title }}" settings.ARCHIVE_FILE_GENERATION = "always" correspondent = Correspondent.objects.create(name="same") original = paperless_dirs.originals_dir / "same" / "t.pdf" archive = paperless_dirs.archive_dir / "same" / "t.pdf" for path in (original, archive): path.parent.mkdir(parents=True) shutil.copy(SIMPLE_DIGITAL_PDF, path) doc = Document.objects.create( title="t", content="my document", checksum=compute_checksum(original), archive_checksum="stale", filename="same/t.pdf", archive_filename="same/t.pdf", mime_type="application/pdf", correspondent=correspondent, ) tasks.update_document_content_maybe_archive_file(doc.pk) refreshed = Document.objects.get(pk=doc.pk) assert refreshed.archive_filename == "same/t.pdf" assert archive.is_file() assert compute_checksum(archive) == refreshed.archive_checksum