|
| 1 | +""" |
| 2 | +Tests for the reindex_studio management command and the rebuild_index_incremental Celery task. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from unittest.mock import MagicMock, Mock, patch |
| 8 | + |
| 9 | +import pytest |
| 10 | +from django.core.management import CommandError, call_command |
| 11 | +from django.test import TestCase, override_settings |
| 12 | + |
| 13 | +from openedx.core.djangolib.testing.utils import skip_unless_cms |
| 14 | + |
| 15 | +try: |
| 16 | + from .. import api |
| 17 | + from ..tasks import rebuild_index_incremental |
| 18 | +except RuntimeError: |
| 19 | + pass |
| 20 | + |
| 21 | + |
| 22 | +@skip_unless_cms |
| 23 | +@override_settings(MEILISEARCH_ENABLED=True) |
| 24 | +class TestReindexStudioCommand(TestCase): |
| 25 | + """Tests for the reindex_studio management command.""" |
| 26 | + |
| 27 | + @patch("openedx.core.djangoapps.content.search.tasks.rebuild_index_incremental.delay") |
| 28 | + def test_enqueues_task(self, mock_delay): |
| 29 | + """Command enqueues the incremental rebuild task.""" |
| 30 | + mock_delay.return_value = Mock(id="fake-task-id") |
| 31 | + |
| 32 | + call_command("reindex_studio") |
| 33 | + |
| 34 | + mock_delay.assert_called_once_with() |
| 35 | + |
| 36 | + @override_settings(MEILISEARCH_ENABLED=False) |
| 37 | + def test_disabled(self): |
| 38 | + """Command raises error when Meilisearch is disabled.""" |
| 39 | + with pytest.raises(CommandError, match="not enabled"): |
| 40 | + call_command("reindex_studio") |
| 41 | + |
| 42 | + def test_reset_flag_removed(self): |
| 43 | + """Passing --reset raises a clear error.""" |
| 44 | + with pytest.raises(CommandError, match="--reset flag has been removed"): |
| 45 | + call_command("reindex_studio", "--reset") |
| 46 | + |
| 47 | + def test_init_flag_removed(self): |
| 48 | + """Passing --init raises a clear error.""" |
| 49 | + with pytest.raises(CommandError, match="--init flag has been removed"): |
| 50 | + call_command("reindex_studio", "--init") |
| 51 | + |
| 52 | + @patch("openedx.core.djangoapps.content.search.tasks.rebuild_index_incremental.delay") |
| 53 | + @patch("openedx.core.djangoapps.content.search.management.commands.reindex_studio.log") |
| 54 | + def test_incremental_flag_accepted_with_warning(self, mock_log, mock_delay): |
| 55 | + """Passing --incremental logs a warning but still enqueues the task.""" |
| 56 | + mock_delay.return_value = Mock(id="fake-task-id") |
| 57 | + |
| 58 | + call_command("reindex_studio", "--incremental") |
| 59 | + |
| 60 | + mock_log.warning.assert_called_once() |
| 61 | + mock_delay.assert_called_once_with() |
| 62 | + |
| 63 | + |
| 64 | +@skip_unless_cms |
| 65 | +@override_settings(MEILISEARCH_ENABLED=True) |
| 66 | +@patch("openedx.core.djangoapps.content.search.api._wait_for_meili_task", new=MagicMock(return_value=None)) |
| 67 | +@patch("openedx.core.djangoapps.content.search.api.MeilisearchClient") |
| 68 | +class TestRebuildIndexIncrementalTask(TestCase): |
| 69 | + """Tests for the rebuild_index_incremental Celery task.""" |
| 70 | + |
| 71 | + def setUp(self): |
| 72 | + super().setUp() |
| 73 | + api.clear_meilisearch_client() |
| 74 | + |
| 75 | + @patch("openedx.core.djangoapps.content.search.api.rebuild_index") |
| 76 | + def test_calls_rebuild_incremental(self, mock_rebuild, mock_meilisearch): |
| 77 | + """Task calls api.rebuild_index with incremental=True.""" |
| 78 | + rebuild_index_incremental() |
| 79 | + |
| 80 | + mock_rebuild.assert_called_once() |
| 81 | + _, kwargs = mock_rebuild.call_args |
| 82 | + assert kwargs["incremental"] is True |
| 83 | + |
| 84 | + @patch("openedx.core.djangoapps.content.search.api.rebuild_index") |
| 85 | + def test_rebuild_already_in_progress(self, mock_rebuild, mock_meilisearch): |
| 86 | + """Task exits gracefully if rebuild lock is already held.""" |
| 87 | + mock_rebuild.side_effect = RuntimeError("Rebuild already in progress") |
| 88 | + |
| 89 | + # Should not raise |
| 90 | + rebuild_index_incremental() |
| 91 | + |
| 92 | + @patch("openedx.core.djangoapps.content.search.api.rebuild_index") |
| 93 | + def test_other_runtime_error_raised(self, mock_rebuild, mock_meilisearch): |
| 94 | + """Task re-raises RuntimeError if it's not about lock contention.""" |
| 95 | + mock_rebuild.side_effect = RuntimeError("Something else went wrong") |
| 96 | + |
| 97 | + with pytest.raises(RuntimeError, match="Something else went wrong"): |
| 98 | + rebuild_index_incremental() |
| 99 | + |
| 100 | + @patch("openedx.core.djangoapps.content.search.api.rebuild_index") |
| 101 | + def test_idempotent(self, mock_rebuild, mock_meilisearch): |
| 102 | + """Task can be called multiple times safely.""" |
| 103 | + rebuild_index_incremental() |
| 104 | + rebuild_index_incremental() |
| 105 | + |
| 106 | + assert mock_rebuild.call_count == 2 |
0 commit comments