def test_model_not_set(self): """Test handling of no model""" view = ElasticListAPIView() view.Meta.model = None with self.assertRaises(AssertionError): view.get_queryset()
def test_model_set(self): """Test handling when model is set""" expectation = Search().query("match", field="value") view = ElasticListAPIView() view.Meta.model = MagicMock() view.Meta.model.return_value = "Some" view.Meta.model.search.return_value = expectation self.assertEqual(view.get_queryset(), expectation)
def test_model_set(self): """Test handling when model is set""" expectation = Search().query('match', field='value') view = ElasticListAPIView() view.Meta.model = MagicMock() view.Meta.model.return_value = 'Some' view.Meta.model.search.return_value = expectation self.assertEqual(view.get_queryset(), expectation)
def test_filter_queryset(self): """Test search object filtering.""" view = ElasticListAPIView() view.Meta.model = MagicMock() view.Meta.model.field_has_raw.return_value = False elasticfilter = ElasticFilter() request = MagicMock() # Set up the query terms for the test. request.query_params = QueryDict('', mutable=True) request.query_params.update({ view.pagination_class.page_query_param: 'ignored', 'name': 'value', 'name__terms': '["value1","value2"]' }) result = \ elasticfilter.filter_queryset(request, Search(), view).to_dict() EXPECTED = [{ 'terms': { u'name': ['value1', 'value2'] } }, { 'match': { u'name': 'value' } }] for item in EXPECTED: self.assertTrue(item in result['query']['bool']['must'])
def test__add_query_no_raw(self): """Test proper handling of mapping without raw field.""" view = ElasticListAPIView() view.Meta.model = MagicMock() view.Meta.model.field_has_raw.return_value = False expectation = {'query': {'match': {'param1': 'value1'}}} elasticfilter = ElasticFilter() queryset = Search() # pylint: disable=W0212 result = elasticfilter._add_query('param1', 'value1', view, queryset) self.assertTrue(view.Meta.model.field_has_raw.called) self.assertEqual(result.to_dict(), expectation)
def test_filter_terms_regexp(self): """Test search object filtering using terms and regexp. Because their values are dicts, these take a different code path than "standard" match queries. """ view = ElasticListAPIView() view.Meta.model = MagicMock() view.Meta.model.field_has_raw.return_value = False EXPECTED = [{ 'regexp': { 'this.is.a.nested.field': '.+12345666666666' } }, { 'terms': { '_type': ["audit.http.request", "audit.http.response", "identity.*"] } }] elasticfilter = ElasticFilter() request = MagicMock() # Set up the query terms for the test. request.query_params = QueryDict('', mutable=True) request.query_params.update( { view.pagination_class.page_query_param: 'ignored', 'terms': { "_type": [ "audit.http.request", "audit.http.response", "identity.*" ], }, 'regexp': { "this.is.a.nested.field": ".+12345666666666" }, }, ) result = \ elasticfilter.filter_queryset(request, Search(), view).to_dict() for item in EXPECTED: self.assertTrue(item in result['query']['bool']['must'])