mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-12-03 17:58:41 +00:00
92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
import shutil
|
|
from pathlib import Path
|
|
from typing import TYPE_CHECKING
|
|
|
|
import filelock
|
|
import pytest
|
|
|
|
from paperless_testing.factories import DocumentFactory
|
|
|
|
if TYPE_CHECKING:
|
|
from unittest.mock import MagicMock
|
|
|
|
from pytest_mock import MockerFixture
|
|
|
|
from documents.models import Document
|
|
from paperless_testing.dirs import PaperlessDirs
|
|
|
|
|
|
@pytest.fixture()
|
|
def sample_doc(
|
|
paperless_dirs: "PaperlessDirs",
|
|
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(
|
|
simple_digital_pdf_file,
|
|
paperless_dirs.originals_dir / "0000001.pdf",
|
|
)
|
|
shutil.copy(
|
|
multi_page_images_pdf_file,
|
|
paperless_dirs.archive_dir / "0000001.pdf",
|
|
)
|
|
shutil.copy(
|
|
thumbnail_webp_file,
|
|
paperless_dirs.thumbnail_dir / "0000001.webp",
|
|
)
|
|
|
|
return DocumentFactory(
|
|
title="test",
|
|
checksum="1093cf6e32adbd16b06969df09215d42c4a3a8938cc18b39455953f08d1ff2ab",
|
|
archive_checksum="706124ecde3c31616992fa979caed17a726b1c9ccdba70e82a4ff796cea97ccf",
|
|
content="test content",
|
|
pk=1,
|
|
filename="0000001.pdf",
|
|
mime_type="application/pdf",
|
|
archive_filename="0000001.pdf",
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def _search_index(paperless_dirs: "PaperlessDirs") -> None:
|
|
"""Point the search backend at a fresh, empty index directory.
|
|
|
|
paperless_dirs owns INDEX_DIR and resets the backend singleton on both
|
|
sides of the test, so requesting it is all that is needed.
|
|
"""
|
|
|
|
|
|
@pytest.fixture
|
|
def searchable_document(_search_index: None) -> "Document":
|
|
"""One searchable document, for tests about what the search endpoint
|
|
returns rather than about what it finds.
|
|
"""
|
|
from documents.search import get_backend
|
|
|
|
doc = DocumentFactory.create(title="quarterly invoice", content="acme corp")
|
|
get_backend().add_or_update(doc)
|
|
return doc
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_zone_ocr_run(mocker: "MockerFixture") -> "MagicMock":
|
|
"""Patch the subprocess runner used by zone OCR (tesseract and pdftoppm)."""
|
|
return mocker.patch("documents.zone_ocr.subprocess.run")
|
|
|
|
|
|
@pytest.fixture
|
|
def config_id(db: None) -> int:
|
|
"""
|
|
The id of the application configuration row. A migration creates it, but a test
|
|
that flushes the database in the same worker removes it, so make sure it exists.
|
|
"""
|
|
from paperless.models import ApplicationConfiguration
|
|
|
|
config = ApplicationConfiguration.objects.first()
|
|
if config is None:
|
|
config = ApplicationConfiguration.objects.create()
|
|
return config.pk
|