-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore:add unit tests for _create_search_query in search utility
Created comprehensive unit tests for the _create_search_query function within the search utility, validating behavior for single-word, AND/OR operators, and phrase queries. Additionally, fixed the method of combining search queries using __and__ and __or__ instead of the bitwise operators. This enhances the robustness and correctness of the search functionality.
- Loading branch information
1 parent
664e5e9
commit 8cde7be
Showing
4 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import unittest | ||
|
||
from unittest.mock import MagicMock, call, patch | ||
|
||
from orp_search.utils.search import create_search_query | ||
|
||
|
||
class TestCreateSearchQuery(unittest.TestCase): | ||
|
||
@patch("orp_search.utils.search.SearchQuery", autospec=True) | ||
def test_single_word_query(self, mock_search_query): | ||
result = create_search_query("test") | ||
mock_search_query.assert_called_with("test", search_type="plain") | ||
self.assertEqual(result, mock_search_query.return_value) | ||
|
||
@patch("orp_search.utils.search.SearchQuery", autospec=True) | ||
def test_and_search_operator_query(self, mock_search_query): | ||
# Mock SearchQuery instances | ||
mock_query1 = MagicMock(name="MockQuery1") | ||
mock_query2 = MagicMock(name="MockQuery2") | ||
|
||
# Configure the mock to return mock objects | ||
mock_search_query.side_effect = [mock_query1, mock_query2] | ||
|
||
# Call the function | ||
_ = create_search_query("test AND trial") | ||
|
||
# Assert that SearchQuery was called with expected arguments | ||
calls = [ | ||
call("test", search_type="plain"), | ||
call("trial", search_type="plain"), | ||
] | ||
mock_search_query.assert_has_calls(calls, any_order=False) | ||
|
||
# Assert the AND operation was applied | ||
mock_query1.__and__.assert_called_once_with(mock_query2) | ||
|
||
@patch("orp_search.utils.search.SearchQuery", autospec=True) | ||
def test_multiple_and_search_operator_query(self, mock_search_query): | ||
# Mock SearchQuery instances | ||
mock_query1 = MagicMock(name="MockQuery1") | ||
mock_query2 = MagicMock(name="MockQuery2") | ||
mock_query3 = MagicMock(name="MockQuery3") | ||
|
||
# Configure the mock to return mock objects | ||
mock_search_query.side_effect = [mock_query1, mock_query2, mock_query3] | ||
|
||
# Call the function | ||
_ = create_search_query("test AND trial AND error") | ||
|
||
# Assert that SearchQuery was called with expected arguments | ||
calls = [ | ||
call("test", search_type="plain"), | ||
call("trial", search_type="plain"), | ||
call("error", search_type="plain"), | ||
] | ||
mock_search_query.assert_has_calls(calls, any_order=False) | ||
|
||
# Assert the AND operation was applied | ||
mock_query1.__and__.assert_called_with(mock_query3) | ||
|
||
@patch("orp_search.utils.search.SearchQuery", autospec=True) | ||
def test_or_search_operator_query(self, mock_search_query): | ||
# Mock SearchQuery instances | ||
mock_query1 = MagicMock(name="MockQuery1") | ||
mock_query2 = MagicMock(name="MockQuery2") | ||
|
||
# Configure the mock to return mock objects | ||
mock_search_query.side_effect = [mock_query1, mock_query2] | ||
|
||
# Call the function | ||
_ = create_search_query("test OR trial") | ||
|
||
# Assert that SearchQuery was called with expected arguments | ||
calls = [ | ||
call("test", search_type="plain"), | ||
call("trial", search_type="plain"), | ||
] | ||
mock_search_query.assert_has_calls(calls, any_order=False) | ||
|
||
# Assert the AND operation was applied | ||
mock_query1.__or__.assert_called_once_with(mock_query2) | ||
|
||
@patch("orp_search.utils.search.SearchQuery", autospec=True) | ||
def test_multiple_or_search_operator_query(self, mock_search_query): | ||
# Mock SearchQuery instances | ||
mock_query1 = MagicMock(name="MockQuery1") | ||
mock_query2 = MagicMock(name="MockQuery2") | ||
mock_query3 = MagicMock(name="MockQuery3") | ||
|
||
# Configure the mock to return mock objects | ||
mock_search_query.side_effect = [mock_query1, mock_query2, mock_query3] | ||
|
||
# Call the function | ||
_ = create_search_query("test OR trial OR error") | ||
|
||
# Assert that SearchQuery was called with expected arguments | ||
calls = [ | ||
call("test", search_type="plain"), | ||
call("trial", search_type="plain"), | ||
call("error", search_type="plain"), | ||
] | ||
mock_search_query.assert_has_calls(calls, any_order=False) | ||
|
||
# Assert the AND operation was applied | ||
mock_query1.__or__.assert_called_with(mock_query3) | ||
|
||
@patch("orp_search.utils.search.SearchQuery", autospec=True) | ||
def test_phrase_search_query(self, mock_search_query): | ||
result = create_search_query('"test trial"') | ||
mock_search_query.assert_called_with( | ||
"test trial", search_type="phrase" | ||
) | ||
self.assertEqual(result, mock_search_query.return_value) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[pytest] | ||
DJANGO_SETTINGS_MODULE = config.settings.local | ||
DJANGO_FIND_PROJECT = false" |