mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-09-21 17:08:33 +00:00
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:
@@ -15,6 +15,8 @@
|
||||
# Test related
|
||||
**/.pytest_cache
|
||||
**/tests
|
||||
src/paperless_testing
|
||||
src/conftest.py
|
||||
**/*.spec.ts
|
||||
**/htmlcov
|
||||
# Local folders
|
||||
|
||||
@@ -339,6 +339,8 @@ omit = [
|
||||
"manage.py",
|
||||
"paperless/auth.py",
|
||||
"paperless/wsgi.py",
|
||||
"src/conftest.py",
|
||||
"src/paperless_testing/*",
|
||||
]
|
||||
[tool.coverage.report]
|
||||
exclude_also = [
|
||||
|
||||
@@ -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()
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
"""
|
||||
Reference in New Issue
Block a user