Skip to content

Commit

Permalink
chore:add unit tests for _create_search_query in search utility
Browse files Browse the repository at this point in the history
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
hareshkainthdbt committed Nov 26, 2024
1 parent 664e5e9 commit 8cde7be
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 4 deletions.
Empty file.
118 changes: 118 additions & 0 deletions orp/orp_search/tests/test_search.py
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()
8 changes: 4 additions & 4 deletions orp/orp_search/utils/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
logger = logging.getLogger(__name__)


def _create_search_query(search_string):
def create_search_query(search_string):
"""
Create a search query from a search string with AND/OR operators
Expand Down Expand Up @@ -49,9 +49,9 @@ def _create_search_query(search_string):
preprocess_query = search_query
else:
if current_operator == "&":
preprocess_query &= search_query
preprocess_query.__and__(search_query)
elif current_operator == "|":
preprocess_query |= search_query
preprocess_query.__or__(search_query)

return preprocess_query

Expand All @@ -75,7 +75,7 @@ def search_database(
logger.debug(f"sanitized search query: {query_str}")

# Generate query object
query_objs = _create_search_query(query_str)
query_objs = create_search_query(query_str)
logger.debug(f"search query objects: {query_objs}")

# Search across specific fields
Expand Down
3 changes: 3 additions & 0 deletions pytest.ini
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"

0 comments on commit 8cde7be

Please sign in to comment.