Chore: Add a shared test support layer

All four Django apps have test code in common, but the only place to put it
was the documents app's own tests package, so paperless, paperless_ai and
paperless_mail each reached across an app boundary to import helpers and
relied on fixtures that were only defined for the documents test path.

This adds a root src/conftest.py holding the fixtures every app needs and an
new src/paperless_testing package for shared helpers a test names
This commit is contained in:
stumpylog
2026-09-20 16:32:31 -07:00
committed by GitHub
parent 46db7217ee
commit 4abdd6d8b6
5 changed files with 45 additions and 26 deletions
+2
View File
@@ -15,6 +15,8 @@
# Test related
**/.pytest_cache
**/tests
src/paperless_testing
src/conftest.py
**/*.spec.ts
**/htmlcov
# Local folders
+3
View File
@@ -317,6 +317,7 @@ testpaths = [
"src/paperless/tests/",
"src/paperless_mail/tests/",
"src/paperless_ai/tests",
"src/paperless_testing/tests/",
]
[tool.pytest_env]
@@ -336,6 +337,8 @@ omit = [
"manage.py",
"paperless/wsgi.py",
"paperless/auth.py",
"src/paperless_testing/*",
"src/conftest.py",
]
[tool.coverage.report]
exclude_also = [
+35
View File
@@ -0,0 +1,35 @@
"""Fixtures available to every Paperless-ngx app.
Loaded automatically for every test path. Keep module-scope imports minimal:
this file is imported for every session, so anything heavy belongs inside
the fixture body that needs it.
"""
import pytest
@pytest.fixture(scope="session", autouse=True)
def faker_session_locale() -> str:
"""Set Faker locale for reproducibility."""
return "en_US"
@pytest.fixture(scope="session", autouse=True)
def faker_seed() -> int:
return 12345
@pytest.fixture(autouse=True)
def _clear_content_type_caches() -> None:
"""Clear Django's ContentType cache and guardian's lru_cache before each test.
Tests that delete and reinsert ContentType/Permission rows (e.g. the
importer) corrupt both caches. Without this fixture a subsequent test on
the same xdist worker sees stale ContentType objects and guardian raises
MixedContentTypeError.
"""
from django.contrib.contenttypes.models import ContentType
from guardian.shortcuts import clear_ct_cache
ContentType.objects.clear_cache()
clear_ct_cache()
-26
View File
@@ -7,8 +7,6 @@ from typing import TYPE_CHECKING
import filelock
import pytest
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from guardian.shortcuts import clear_ct_cache
from pytest_django.fixtures import Settings
from rest_framework.test import APIClient
@@ -154,30 +152,6 @@ def user_client(rest_api_client: APIClient, regular_user: UserModelT) -> APIClie
return rest_api_client
@pytest.fixture(autouse=True)
def _clear_content_type_caches() -> None:
"""Clear Django's ContentType cache and guardian's lru_cache before each test.
Tests that delete and reinsert ContentType/Permission rows (e.g. the
importer) corrupt both caches. Without this fixture a subsequent test on
the same xdist worker sees stale ContentType objects and guardian raises
MixedContentTypeError.
"""
ContentType.objects.clear_cache()
clear_ct_cache()
@pytest.fixture(scope="session", autouse=True)
def faker_session_locale():
"""Set Faker locale for reproducibility."""
return "en_US"
@pytest.fixture(scope="session", autouse=True)
def faker_seed():
return 12345
@pytest.fixture
def indexed_document(_search_index: None) -> "Document":
"""One searchable document, for tests about what the search endpoint
+5
View File
@@ -0,0 +1,5 @@
"""Shared test support for every Paperless-ngx app.
Fixtures live in ``src/conftest.py``; anything a test imports by name lives
here. App-specific helpers stay in that app's ``tests`` package.
"""