def test_classic_query(self, mock_Elasticsearch, mock_Search): """:class:`.index.search` supports :class:`SimpleQuery`.""" mock_results = mock.MagicMock() mock_results.__getitem__.return_value = {"total": 53} rdata = mock_rdata() mock_result = mock.MagicMock(_d_=rdata, **rdata) mock_result.meta.score = 1 mock_results.__iter__.return_value = [mock_result] mock_Search.execute.return_value = mock_results # Support the chaining API for py-ES. mock_Search.return_value = mock_Search mock_Search.filter.return_value = mock_Search mock_Search.highlight.return_value = mock_Search mock_Search.highlight_options.return_value = mock_Search mock_Search.query.return_value = mock_Search mock_Search.sort.return_value = mock_Search mock_Search.__getitem__.return_value = mock_Search query = ClassicAPIQuery( phrase=Term(Field.Author, "copernicus"), order=SortOrder(by=SortBy.relevance), size=10, ) document_set = index.SearchSession.search(query) # self.assertIsInstance(document_set, DocumentSet) self.assertEqual(document_set["metadata"]["start"], 0) self.assertEqual(document_set["metadata"]["total_results"], 53) self.assertEqual(document_set["metadata"]["current_page"], 1) self.assertEqual(document_set["metadata"]["total_pages"], 6) self.assertEqual(document_set["metadata"]["size"], 10) self.assertEqual(len(document_set["results"]), 1)
def test_all_valid_field_values(self): for field in Field: result = parse_classic_query(f"{field}:some_text") self.assertEqual(result, Term(field, "some_text"))
from search.domain import Field, Operator, Phrase, Term from search.services.index.classic_api.query_builder import ( query_builder, FIELD_TERM_MAPPING as FTM, ) @dataclass class Case: message: str phrase: Phrase query: Q TEST_CASES: List[Case] = [ Case(message="Empty query", phrase=Term(Field.All, ""), query=Q()), Case( message="Empty query in conjunction.", phrase=( Operator.AND, Term(Field.All, ""), Term(Field.All, "electron"), ), query=FTM[Field.All]("electron"), ), Case( message="Double empty query in conjunction.", phrase=(Operator.AND, Term(Field.All, ""), Term(Field.All, ""),), query=Q(), ), Case(
from werkzeug.exceptions import BadRequest from unittest import TestCase @dataclass class Case: message: str query: str phrase: Phrase = None TEST_PARSE_OK_CASES: List[Case] = [ Case( message="Empty query.", query="", phrase=Term(Field.All, ""), ), Case( message="Empty query full of spaces.", query='au:" "', phrase=Term(Field.Author, ""), ), Case( message="Empty query in conjunct.", query='all:electron AND au:""', phrase=( Operator.AND, Term(Field.All, "electron"), Term(Field.Author, ""), ), ),