From df02e6073f0a0999765d953988e623febae1a560 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 8 Oct 2026 09:54:06 -0700 Subject: [PATCH] Cover LLM index update when reprocessing a version --- src/documents/tests/test_tasks.py | 70 ++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/src/documents/tests/test_tasks.py b/src/documents/tests/test_tasks.py index 472f998fa..c1bea1b43 100644 --- a/src/documents/tests/test_tasks.py +++ b/src/documents/tests/test_tasks.py @@ -20,6 +20,7 @@ from documents.sanity_checker import SanityCheckMessages from documents.tests.helpers import dummy_preprocess from paperless_testing.assertions import FileSystemAssertsMixin from paperless_testing.dirs import DirectoriesMixin +from paperless_testing.factories import DocumentFactory @pytest.mark.django_db @@ -287,6 +288,26 @@ class TestUpdateContent(DirectoriesMixin, TestCase): tasks.update_document_content_maybe_archive_file(doc.pk) self.assertNotEqual(Document.objects.get(pk=doc.pk).content, "test") + def _create_root_with_version(self) -> tuple[Document, Document]: + sample1 = self.dirs.scratch_dir / "sample.pdf" + shutil.copy( + Path(__file__).parent + / "samples" + / "documents" + / "originals" + / "0000001.pdf", + sample1, + ) + root = DocumentFactory(content="root content", mime_type="application/pdf") + version = DocumentFactory( + content="my document", + filename=sample1, + mime_type="application/pdf", + root_document=root, + version_index=1, + ) + return root, version + @mock.patch("documents.tasks.clear_document_caches") @mock.patch("documents.search.get_backend") def test_update_content_version_indexes_root( @@ -304,30 +325,7 @@ class TestUpdateContent(DirectoriesMixin, TestCase): - The root document is indexed rather than the version - Caches are cleared for both """ - sample1 = self.dirs.scratch_dir / "sample.pdf" - shutil.copy( - Path(__file__).parent - / "samples" - / "documents" - / "originals" - / "0000001.pdf", - sample1, - ) - root = Document.objects.create( - title="test", - content="root content", - checksum="root", - mime_type="application/pdf", - ) - version = Document.objects.create( - title="test", - content="my document", - checksum="wow", - filename=sample1, - mime_type="application/pdf", - root_document=root, - version_index=1, - ) + root, version = self._create_root_with_version() tasks.update_document_content_maybe_archive_file(version.pk) @@ -342,6 +340,30 @@ class TestUpdateContent(DirectoriesMixin, TestCase): [mock.call(version.pk), mock.call(root.pk)], ) + @override_settings(AI_ENABLED=True, LLM_EMBEDDING_BACKEND="huggingface") + @mock.patch("documents.tasks.llm_index_add_or_update_document") + @mock.patch("documents.search.get_backend") + def test_update_content_version_updates_llm_index_for_root( + self, + mock_get_backend: mock.Mock, + mock_llm_index: mock.Mock, + ) -> None: + """ + GIVEN: + - A root document with a version + - The LLM index is enabled + WHEN: + - Update content task is called for the version + THEN: + - The LLM index is updated for the root document, not the version + """ + root, version = self._create_root_with_version() + + tasks.update_document_content_maybe_archive_file(version.pk) + + mock_llm_index.assert_called_once() + self.assertEqual(mock_llm_index.call_args.args[0].pk, root.pk) + class TestUpdateContentRemoteOCR(DirectoriesMixin, TestCase): """