def test_search_with_aggregation_params(self, mock_es): def side_effect(*args, **kwargs): if "size" in kwargs: return self.es_search_response else: return self.es_aggregation_response mock_es.side_effect = side_effect response = self.app.get( '/api/search?q=act1&category=gene&go_names=cytoplasm') es_query = build_search_query( "act1", self.search_fields, "gene", self.category_filters, ImmutableMultiDict([("q", "act1"), ("category", "gene"), ("go_names", "cytoplasm")])) mock_es.assert_has_calls([ mock.call(index=self.index, body=build_es_search_body_request( "act1", "gene", es_query, self.json_response_fields, self.search_fields, ""), size=10, from_=0, preference='p_act1') ]) mock_es.assert_has_calls([ mock.call(index=self.index, body=build_es_aggregation_body_request( es_query, "gene", self.category_filters)) ]) self.assertEqual(response.status_code, 200)
def test_build_es_aggregation_body_request_should_aggregate_each_subcategory(self): query = "" fields = ["name", "symbol"] category = "genes" category_filters = { "genes": ['go_ids', 'go_names'], "go": ['gene'], } args = ImmutableMultiDict([ ('go_names', 'A'), ('go_names', 'B'), ('go_names', 'C') ]) es_query = build_search_query(query, fields, category, category_filters, args) self.assertEqual(build_es_aggregation_body_request(es_query, category, category_filters), { 'query': es_query, 'size': 0, 'aggs': { 'go_ids': { 'terms': {'field': 'go_ids.raw', 'size': 999} }, 'go_names': { 'terms': {'field': 'go_names.raw', 'size': 999} } } })
def test_search_default_params(self, mock_es): def side_effect(*args, **kwargs): if "size" in kwargs: return self.es_search_response else: return self.es_aggregation_response mock_es.side_effect = side_effect response = self.app.get('/api/search') es_query = build_search_query('', self.search_fields, '', self.category_filters, {}) mock_es.assert_has_calls([ mock.call(index=self.index, body=build_es_search_body_request( '', '', es_query, self.json_response_fields, self.search_fields, ''), size=10, from_=0, preference='p_') ]) mock_es.assert_has_calls([ mock.call(index=self.index, body=build_es_aggregation_body_request( es_query, '', self.category_filters)) ]) self.assertEqual(response.status_code, 200)
def test_build_es_search_body_should_not_randomize_results_if_category_is_defined(self): query = "" fields = ["name", "symbol"] category = "genes" category_filters = { "genes": ['go_ids', 'go_names'], "go": ['gene'], } sort_by = "alphabetical" args = ImmutableMultiDict([ ('go_names', 'A'), ('go_names', 'B'), ('go_names', 'C') ]) search_fields = ["name", "symbol"] json_response_fields = ['name', 'symbol', 'synonym'] es_query = build_search_query(query, fields, category, category_filters, args) self.assertEqual(build_es_search_body_request(query, category, es_query, json_response_fields, search_fields, sort_by), { '_source': json_response_fields, 'highlight': { 'fields': {'name': {}, 'symbol': {}} }, 'query': es_query, 'sort': [ { "name.raw": { "order": "asc" } } ] })
def test_build_es_search_body_should_add_highlighting(self): query = "" fields = ["name", "symbol"] category = "" category_filters = { "genes": ['go_ids', 'go_names'], "go": ['gene'], } sort_by = "relevance" args = ImmutableMultiDict([ ('go_names', 'A'), ('go_names', 'B'), ('go_names', 'C') ]) search_fields = ["name", "symbol"] json_response_fields = ['name', 'symbol', 'synonym'] es_query = build_search_query(query, fields, category, category_filters, args) self.assertEqual(build_es_search_body_request(query, category, es_query, json_response_fields, search_fields, sort_by), { '_source': json_response_fields, 'highlight': { 'fields': {'name': {}, 'symbol': {}} }, 'query': { "function_score": { "query": es_query, "random_score": {"seed": 12345} } } })
def test_build_search_query_should_filter_subcategories_if_passed(self): query = "act1" fields = ["name", "symbol"] category = "genes" category_filters = { "genes": ['go_ids', 'go_names'], "go": ['gene'], } args = ImmutableMultiDict([ ('go_names', 'A'), ('go_names', 'B'), ('go_names', 'C') ]) es_query = build_search_params(query, fields) self.assertEqual(build_search_query(query, fields, category, category_filters, args), { 'filtered': { 'query': es_query, 'filter': { 'bool': { 'must': [ {'term': {'category': category}}, {'term': {'go_names.raw': 'A'}}, {'term': {'go_names.raw': 'B'}}, {'term': {'go_names.raw': 'C'}} ] } } } })
def test_search_with_custom_params(self, mock_es): def side_effect(*args, **kwargs): if "size" in kwargs: return self.es_search_response else: return self.es_aggregation_response mock_es.side_effect = side_effect response = self.app.get( '/api/search?q=act1&category=gene&limit=25&offset=10&sort_by=alphabetical' ) es_query = build_search_query( "act1", self.search_fields, "gene", self.category_filters, ImmutableMultiDict([("q", "act1"), ("category", "gene"), ("limit", 25), ("offset", 10), ("sort_by", "alphabetical")])) mock_es.assert_has_calls([ mock.call(index=self.index, body=build_es_search_body_request( "act1", "gene", es_query, self.json_response_fields, self.search_fields, "alphabetical"), size=25, from_=10) ]) mock_es.assert_has_calls([ mock.call(index=self.index, body=build_es_aggregation_body_request( es_query, "gene", self.category_filters)) ]) self.assertEqual(response.status_code, 200)
def test_build_search_query_should_return_search_params_only_for_no_category(self): query = "gene" fields = ["name", "symbol"] category = '' category_filters = { "genes": ['go_ids', 'go_names'], "go": ['gene'], } args = ImmutableMultiDict() es_query = build_search_params(query, fields) self.assertEqual(build_search_query(query, fields, category, category_filters, args), es_query)
def test_build_es_aggregation_body_request_should_return_empty_for_invalid_category(self): query = "" fields = ["name", "symbol"] category = "invalid_category" category_filters = { "genes": ['go_ids', 'go_names'], "go": ['gene'], } args = ImmutableMultiDict([ ('go_names', 'A'), ('go_names', 'B'), ('go_names', 'C') ]) es_query = build_search_query(query, fields, category, category_filters, args) self.assertEqual(build_es_aggregation_body_request(es_query, category, category_filters), {})
def test_build_search_query_should_filter_by_category(self): query = "gene" fields = ["name", "symbol"] category = 'genes' category_filters = { "genes": ['go_ids', 'go_names'], "go": ['gene'] } args = ImmutableMultiDict() es_query = build_search_params(query, fields) self.assertEqual(build_search_query(query, fields, category, category_filters, args), { 'filtered': { 'query': es_query, 'filter': { 'bool': { 'must': [{'term': {'category': category}}] } } } })