Refactor: inline index_reindex into management command; promote needs_rebuild to public API

- Rename _needs_rebuild -> needs_rebuild and export from documents.search
- document_index command imports directly from documents.search, constructs
  the queryset and calls get_backend().rebuild() inline — no tasks.py indirection
- Optimize subcommand logs deprecation directly; no longer calls index_optimize
- Remove index_reindex from tasks.py
- Convert TestMakeIndex to pytest class (no TestCase); use mocker fixtures
- Simplify TestIndexReindex -> TestIndexOptimize (wrapper test removed)

Co-Authored-By: Antoine Mérino <3023499+Merinorus@users.noreply.github.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Trenton H
2026-03-30 14:41:25 -07:00
co-authored by Antoine Mérino Claude Sonnet 4.6
parent 6699679c29
commit 061099b064
6 changed files with 58 additions and 67 deletions
+26 -17
View File
@@ -103,36 +103,45 @@ class TestArchiver(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
@pytest.mark.management
class TestMakeIndex(TestCase):
@mock.patch("documents.management.commands.document_index.index_reindex")
def test_reindex(self, m) -> None:
@pytest.mark.django_db
class TestMakeIndex:
def test_reindex(self, mocker: MockerFixture) -> None:
mock_get_backend = mocker.patch(
"documents.management.commands.document_index.get_backend",
)
call_command("document_index", "reindex", skip_checks=True)
m.assert_called_once()
mock_get_backend.return_value.rebuild.assert_called_once()
@mock.patch("documents.management.commands.document_index.index_optimize")
def test_optimize(self, m) -> None:
def test_optimize(self) -> None:
call_command("document_index", "optimize", skip_checks=True)
m.assert_called_once()
@mock.patch("documents.management.commands.document_index.index_reindex")
@mock.patch("documents.search._schema._needs_rebuild", return_value=False)
def test_reindex_if_needed_skips_when_up_to_date(
self,
_needs_rebuild,
reindex,
mocker: MockerFixture,
) -> None:
mocker.patch(
"documents.management.commands.document_index.needs_rebuild",
return_value=False,
)
mock_get_backend = mocker.patch(
"documents.management.commands.document_index.get_backend",
)
call_command("document_index", "reindex", if_needed=True, skip_checks=True)
reindex.assert_not_called()
mock_get_backend.return_value.rebuild.assert_not_called()
@mock.patch("documents.management.commands.document_index.index_reindex")
@mock.patch("documents.search._schema._needs_rebuild", return_value=True)
def test_reindex_if_needed_runs_when_rebuild_needed(
self,
_needs_rebuild,
reindex,
mocker: MockerFixture,
) -> None:
mocker.patch(
"documents.management.commands.document_index.needs_rebuild",
return_value=True,
)
mock_get_backend = mocker.patch(
"documents.management.commands.document_index.get_backend",
)
call_command("document_index", "reindex", if_needed=True, skip_checks=True)
reindex.assert_called_once()
mock_get_backend.return_value.rebuild.assert_called_once()
@pytest.mark.management
+1 -22
View File
@@ -23,29 +23,8 @@ from documents.tests.utils import DirectoriesMixin
from documents.tests.utils import FileSystemAssertsMixin
class TestIndexReindex(DirectoriesMixin, TestCase):
def test_index_reindex(self) -> None:
Document.objects.create(
title="test",
content="my document",
checksum="wow",
added=timezone.now(),
created=timezone.now(),
modified=timezone.now(),
)
tasks.index_reindex()
class TestIndexOptimize(TestCase):
def test_index_optimize(self) -> None:
Document.objects.create(
title="test",
content="my document",
checksum="wow",
added=timezone.now(),
created=timezone.now(),
modified=timezone.now(),
)
tasks.index_optimize()