diff --git a/.dockerignore b/.dockerignore index b9c7d2f84..d378a026a 100644 --- a/.dockerignore +++ b/.dockerignore @@ -15,6 +15,8 @@ # Test related **/.pytest_cache **/tests +src/paperless_testing +src/conftest.py **/*.spec.ts **/htmlcov # Local folders diff --git a/pyproject.toml b/pyproject.toml index 1317146d3..c350bd65c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = [ diff --git a/src/conftest.py b/src/conftest.py new file mode 100644 index 000000000..9bfcbd199 --- /dev/null +++ b/src/conftest.py @@ -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() diff --git a/src/documents/tests/conftest.py b/src/documents/tests/conftest.py index fe868b249..9c49a02c1 100644 --- a/src/documents/tests/conftest.py +++ b/src/documents/tests/conftest.py @@ -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 diff --git a/src/paperless_testing/__init__.py b/src/paperless_testing/__init__.py new file mode 100644 index 000000000..ac0c18181 --- /dev/null +++ b/src/paperless_testing/__init__.py @@ -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. +"""