def test_add_search_filters_with_filters(self): """Test that when add_search_filters is called with a filter object\ then the search object contains every filter passed in""" q = Q("bool", should=[Q("match", Test="test")], minimum_should_match=1) query = XSEQueries('test', 'test') query.search = query.search.query(q) filters = { "page": 1, "color": ["red", "blue"], "model": "nike", "type": "shirt" } hasFilter = False query.add_search_filters(filters) result = query.search result_dict = result.to_dict() if "filter" in result_dict['query']['bool']: hasFilter = True for filtr in result_dict['query']['bool']['filter']: termObj = filtr['terms'] for filter_name in termObj: orig_name = filter_name.replace('.keyword', '') self.assertEqual(termObj[filter_name], filters[orig_name]) self.assertTrue(hasFilter)
def test_add_search_filters_no_filter(self): """Test that when add_search_filters is called with an empty filter\ object then no filter gets added to the search object""" q = Q("bool", should=[Q("match", Test="test")], minimum_should_match=1) filters = {"page": 1} hasFilter = False query = XSEQueries('test', 'test') query.search = query.search.query(q) query.add_search_filters(filters) result = query.search result_dict = result.to_dict() if "filter" in result_dict['query']['bool']: hasFilter = True self.assertFalse(hasFilter)