mirror of
https://github.com/suitenumerique/docs.git
synced 2026-08-17 21:25:43 +02:00
♻️(backend) rename DocumentViewset::_title_search in _search_title
We rename the private method _title_search in _search_title in the DocumentViewset for having the search methods prefixed with _search
This commit is contained in:
@@ -1434,21 +1434,27 @@ class DocumentViewSet(
|
||||
params.is_valid(raise_exception=True)
|
||||
search_type = self._get_search_type()
|
||||
if search_type == SearchType.TITLE:
|
||||
return self._title_search(request, params.validated_data, *args, **kwargs)
|
||||
return self._search_using_database(
|
||||
request, params.validated_data, *args, **kwargs
|
||||
)
|
||||
|
||||
indexer = get_document_indexer()
|
||||
if indexer is None:
|
||||
# fallback on title search if the indexer is not configured
|
||||
return self._title_search(request, params.validated_data, *args, **kwargs)
|
||||
return self._search_using_database(
|
||||
request, params.validated_data, *args, **kwargs
|
||||
)
|
||||
|
||||
try:
|
||||
return self._search_with_indexer(
|
||||
return self._search_using_indexer(
|
||||
indexer, request, params=params, search_type=search_type
|
||||
)
|
||||
except requests.exceptions.RequestException as e:
|
||||
logger.error("Error while searching documents with indexer: %s", e)
|
||||
# fallback on title search if the indexer is not reached
|
||||
return self._title_search(request, params.validated_data, *args, **kwargs)
|
||||
return self._search_using_database(
|
||||
request, params.validated_data, *args, **kwargs
|
||||
)
|
||||
|
||||
def _get_search_type(self) -> SearchType:
|
||||
"""
|
||||
@@ -1464,7 +1470,7 @@ class DocumentViewSet(
|
||||
return SearchType.TITLE
|
||||
|
||||
@staticmethod
|
||||
def _search_with_indexer(indexer, request, params, search_type):
|
||||
def _search_using_indexer(indexer, request, params, search_type):
|
||||
"""
|
||||
Returns a list of documents matching the query (q) according to the configured indexer.
|
||||
"""
|
||||
@@ -1491,7 +1497,7 @@ class DocumentViewSet(
|
||||
}
|
||||
)
|
||||
|
||||
def _title_search(self, request, validated_data, *args, **kwargs):
|
||||
def _search_using_database(self, request, validated_data, *args, **kwargs):
|
||||
"""
|
||||
Fallback search method when no indexer is configured.
|
||||
Only searches in the title field of documents.
|
||||
|
||||
@@ -142,8 +142,10 @@ def test_api_documents_search_fallback_on_search_list_sub_docs(
|
||||
assert response.json() == mocked_response
|
||||
|
||||
|
||||
@mock.patch("core.api.viewsets.DocumentViewSet._title_search")
|
||||
def test_api_documents_search_indexer_crashes(mock_title_search, indexer_settings):
|
||||
@mock.patch("core.api.viewsets.DocumentViewSet._search_using_database")
|
||||
def test_api_documents_search_indexer_crashes(
|
||||
mock_search_using_database, indexer_settings
|
||||
):
|
||||
"""
|
||||
When indexer is configured but crashes -> falls back on title_search
|
||||
"""
|
||||
@@ -170,7 +172,7 @@ def test_api_documents_search_indexer_crashes(mock_title_search, indexer_setting
|
||||
"previous": None,
|
||||
"results": [{"title": "mocked title_search result"}],
|
||||
}
|
||||
mock_title_search.return_value = drf_response.Response(mocked_response)
|
||||
mock_search_using_database.return_value = drf_response.Response(mocked_response)
|
||||
|
||||
parent = factories.DocumentFactory(title="parent", users=[user])
|
||||
q = "alpha"
|
||||
@@ -181,9 +183,9 @@ def test_api_documents_search_indexer_crashes(mock_title_search, indexer_setting
|
||||
# the search endpoint did not crash
|
||||
assert response.status_code == 200
|
||||
# fallback on title_search
|
||||
assert mock_title_search.call_count == 1
|
||||
assert mock_title_search.call_args[0][0].GET.get("q") == q
|
||||
assert mock_title_search.call_args[0][0].GET.get("path") == parent.path
|
||||
assert mock_search_using_database.call_count == 1
|
||||
assert mock_search_using_database.call_args[0][0].GET.get("q") == q
|
||||
assert mock_search_using_database.call_args[0][0].GET.get("path") == parent.path
|
||||
assert response.json() == mocked_response
|
||||
|
||||
|
||||
|
||||
@@ -18,13 +18,13 @@ pytestmark = pytest.mark.django_db
|
||||
|
||||
|
||||
@responses.activate
|
||||
@mock.patch("core.api.viewsets.DocumentViewSet._title_search")
|
||||
@mock.patch("core.api.viewsets.DocumentViewSet._search_with_indexer")
|
||||
@mock.patch("core.api.viewsets.DocumentViewSet._search_using_database")
|
||||
@mock.patch("core.api.viewsets.DocumentViewSet._search_using_indexer")
|
||||
@pytest.mark.parametrize(
|
||||
"activated_flags,"
|
||||
"expected_search_type,"
|
||||
"expected_search_with_indexer_called,"
|
||||
"expected_title_search_called",
|
||||
"expected_search_using_indexer_called,"
|
||||
"expected_search_using_database_called",
|
||||
[
|
||||
([], SearchType.TITLE, False, True),
|
||||
([FeatureFlag.FLAG_FIND_HYBRID_SEARCH], SearchType.HYBRID, True, False),
|
||||
@@ -40,15 +40,15 @@ pytestmark = pytest.mark.django_db
|
||||
([FeatureFlag.FLAG_FIND_FULL_TEXT_SEARCH], SearchType.FULL_TEXT, True, False),
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("indexer_settings")
|
||||
# pylint: disable=too-many-arguments, too-many-positional-arguments
|
||||
def test_api_documents_search_success( # noqa : PLR0913
|
||||
mock_search_with_indexer,
|
||||
mock_title_search,
|
||||
mock_search_using_indexer,
|
||||
mock_search_using_database,
|
||||
activated_flags,
|
||||
expected_search_type,
|
||||
expected_search_with_indexer_called,
|
||||
expected_title_search_called,
|
||||
indexer_settings,
|
||||
expected_search_using_indexer_called,
|
||||
expected_search_using_database_called,
|
||||
):
|
||||
"""
|
||||
Test that the API endpoint for searching documents returns a successful response
|
||||
@@ -57,8 +57,8 @@ def test_api_documents_search_success( # noqa : PLR0913
|
||||
"""
|
||||
assert get_document_indexer() is not None
|
||||
|
||||
mock_search_with_indexer.return_value = HttpResponse()
|
||||
mock_title_search.return_value = HttpResponse()
|
||||
mock_search_using_indexer.return_value = HttpResponse()
|
||||
mock_search_using_database.return_value = HttpResponse()
|
||||
|
||||
with override_flag(
|
||||
FeatureFlag.FLAG_FIND_HYBRID_SEARCH,
|
||||
@@ -74,17 +74,17 @@ def test_api_documents_search_success( # noqa : PLR0913
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
if expected_search_with_indexer_called:
|
||||
mock_search_with_indexer.assert_called_once()
|
||||
if expected_search_using_indexer_called:
|
||||
mock_search_using_indexer.assert_called_once()
|
||||
assert (
|
||||
mock_search_with_indexer.call_args.kwargs["search_type"]
|
||||
mock_search_using_indexer.call_args.kwargs["search_type"]
|
||||
== expected_search_type
|
||||
)
|
||||
else:
|
||||
assert not mock_search_with_indexer.called
|
||||
assert not mock_search_using_indexer.called
|
||||
|
||||
if expected_title_search_called:
|
||||
if expected_search_using_database_called:
|
||||
assert SearchType.TITLE == expected_search_type
|
||||
mock_title_search.assert_called_once()
|
||||
mock_search_using_database.assert_called_once()
|
||||
else:
|
||||
assert not mock_title_search.called
|
||||
assert not mock_search_using_database.called
|
||||
|
||||
Reference in New Issue
Block a user