Test(beta): use pytest-django fixtures and drop needless DB markers in taxonomy hint tests

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
stumpylog
2026-06-13 06:48:21 -07:00
parent f3f55e3866
commit d06f66b618
4 changed files with 16 additions and 22 deletions
+7 -2
View File
@@ -1,5 +1,6 @@
import json
from types import SimpleNamespace
from typing import cast
from unittest.mock import MagicMock
from unittest.mock import patch
@@ -265,11 +266,15 @@ def test_get_context_for_document_no_similar_docs(mock_document):
assert result == ""
@pytest.mark.django_db
class TestPromptHints:
@pytest.fixture
def config(self) -> AIConfig:
return AIConfig()
# build_prompt_* only read these two numeric settings off config;
# a stand-in avoids constructing a DB-backed AIConfig.
return cast(
"AIConfig",
SimpleNamespace(llm_embedding_chunk_size=1000, llm_context_size=8000),
)
def test_without_rag_includes_hints_block(
self,
+6 -11
View File
@@ -1,4 +1,5 @@
import difflib
from types import SimpleNamespace
from unittest.mock import patch
import pytest
@@ -89,16 +90,14 @@ class TestAIMatching(TestCase):
self.assertEqual(result[1].name, "Test Tag 2")
@pytest.mark.django_db
class TestHintedMatching:
def test_hinted_verbatim_skips_fuzzy(
self,
mocker: pytest_mock.MockerFixture,
) -> None:
Tag.objects.create(name="Bloodwork")
mocker.patch(
"paperless_ai.matching.get_objects_for_user_owner_aware",
return_value=Tag.objects.all(),
return_value=[SimpleNamespace(name="Bloodwork")],
)
spy = mocker.spy(difflib, "get_close_matches")
@@ -115,10 +114,9 @@ class TestHintedMatching:
self,
mocker: pytest_mock.MockerFixture,
) -> None:
Tag.objects.create(name="Bloodwork")
mocker.patch(
"paperless_ai.matching.get_objects_for_user_owner_aware",
return_value=Tag.objects.all(),
return_value=[SimpleNamespace(name="Bloodwork")],
)
# "Bloodwrok" is a typo not in hints -> fuzzy still maps it to Bloodwork.
@@ -134,10 +132,9 @@ class TestHintedMatching:
self,
mocker: pytest_mock.MockerFixture,
) -> None:
Tag.objects.create(name="Bloodwork")
mocker.patch(
"paperless_ai.matching.get_objects_for_user_owner_aware",
return_value=Tag.objects.all(),
return_value=[SimpleNamespace(name="Bloodwork")],
)
spy = mocker.spy(difflib, "get_close_matches")
@@ -155,10 +152,9 @@ class TestHintedMatching:
mocker: pytest_mock.MockerFixture,
) -> None:
# A hint with no exact object must not fall through to fuzzy.
Tag.objects.create(name="Bloodwork")
mocker.patch(
"paperless_ai.matching.get_objects_for_user_owner_aware",
return_value=Tag.objects.all(),
return_value=[SimpleNamespace(name="Bloodwork")],
)
result = match_tags_by_name(
@@ -173,10 +169,9 @@ class TestHintedMatching:
self,
mocker: pytest_mock.MockerFixture,
) -> None:
Tag.objects.create(name="Test Tag 1")
mocker.patch(
"paperless_ai.matching.get_objects_for_user_owner_aware",
return_value=Tag.objects.all(),
return_value=[SimpleNamespace(name="Test Tag 1")],
)
result = match_tags_by_name(["Test Tag 1", "Nonexistent"], user=None)
-2
View File
@@ -1,6 +1,5 @@
from types import SimpleNamespace
import pytest
import pytest_mock
from paperless_ai.taxonomy import TaxonomyHints
@@ -133,7 +132,6 @@ class TestFormatHintsForPrompt:
assert result.count("Prefer existing names from these lists verbatim") == 1
@pytest.mark.django_db
class TestGetTaxonomyHintsForDocument:
def test_returns_none_when_embedding_backend_off(
self,
@@ -11,11 +11,7 @@ from documents.models import Document
@pytest.mark.django_db
class TestSuggestionsHintWiring:
@pytest.fixture
def user(self) -> User:
return User.objects.create_superuser(username="admin", password="pw")
@pytest.fixture
def document(self, user: User) -> Document:
def document(self) -> Document:
return Document.objects.create(
title="Doc",
content="content",
@@ -24,9 +20,9 @@ class TestSuggestionsHintWiring:
)
@pytest.fixture
def api_client(self, user: User) -> APIClient:
def api_client(self, admin_user: User) -> APIClient:
client = APIClient()
client.force_authenticate(user=user)
client.force_authenticate(user=admin_user)
return client
def test_hints_passed_to_classifier_and_matchers(