| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# Unit tests for the `drf_haystack.filters` classes. |
| 4 |
# |
| 5 |
|
| 6 |
from __future__ import absolute_import, unicode_literals |
| 7 |
|
| 8 |
import json |
| 9 |
from datetime import date, datetime, timedelta |
| 10 |
|
| 11 |
from unittest import skipIf |
| 12 |
|
| 13 |
from django.test import TestCase |
| 14 |
|
| 15 |
from rest_framework import status |
| 16 |
from rest_framework import serializers |
| 17 |
from rest_framework.test import APIRequestFactory |
| 18 |
|
| 19 |
from drf_haystack.viewsets import HaystackViewSet |
| 20 |
from drf_haystack.serializers import HaystackSerializer, HaystackFacetSerializer |
| 21 |
from drf_haystack.filters import ( |
| 22 |
HaystackAutocompleteFilter, HaystackBoostFilter, |
| 23 |
HaystackFacetFilter, HaystackFilter, |
| 24 |
HaystackGEOSpatialFilter, HaystackHighlightFilter, |
| 25 |
HaystackOrderingFilter |
| 26 |
) |
| 27 |
from drf_haystack.mixins import FacetMixin |
| 28 |
|
| 29 |
from . import geospatial_support, elasticsearch_version |
| 30 |
from .constants import MOCKLOCATION_DATA_SET_SIZE, MOCKPERSON_DATA_SET_SIZE |
| 31 |
from .mixins import WarningTestCaseMixin |
| 32 |
from .mockapp.models import MockAllField, MockLocation, MockPerson |
| 33 |
from .mockapp.search_indexes import MockAllFieldIndex, MockLocationIndex, MockPersonIndex |
| 34 |
|
| 35 |
factory = APIRequestFactory() |
| 36 |
|
| 37 |
|
| 38 |
class HaystackFilterTestCase(TestCase): |
| 39 |
|
| 40 |
fixtures = ["mockperson", "mockallfield"] |
| 41 |
|
| 42 |
def setUp(self): |
| 43 |
MockAllFieldIndex().reindex() |
| 44 |
MockPersonIndex().reindex() |
| 45 |
|
| 46 |
class Serializer1(HaystackSerializer): |
| 47 |
class Meta: |
| 48 |
index_classes = [MockPersonIndex] |
| 49 |
fields = ["text", "firstname", "lastname", |
| 50 |
"full_name", "birthdate", "autocomplete"] |
| 51 |
field_aliases = { |
| 52 |
"q": "autocomplete", |
| 53 |
"name": "full_name" |
| 54 |
} |
| 55 |
|
| 56 |
class Serializer2(HaystackSerializer): |
| 57 |
class Meta: |
| 58 |
index_classes = [MockLocationIndex] |
| 59 |
exclude = ["lastname"] |
| 60 |
|
| 61 |
class Serializer4(serializers.Serializer): |
| 62 |
# This is not allowed. Must implement a `Meta` class. |
| 63 |
pass |
| 64 |
|
| 65 |
class Serializer5(HaystackSerializer): |
| 66 |
class Meta: |
| 67 |
index_classes = [MockAllFieldIndex] |
| 68 |
fields = ["integerfield"] |
| 69 |
|
| 70 |
class ViewSet1(HaystackViewSet): |
| 71 |
index_models = [MockPerson] |
| 72 |
serializer_class = Serializer1 |
| 73 |
# No need to specify `filter_backends`, defaults to HaystackFilter |
| 74 |
|
| 75 |
class ViewSet2(ViewSet1): |
| 76 |
serializer_class = Serializer2 |
| 77 |
|
| 78 |
class ViewSet3(ViewSet1): |
| 79 |
serializer_class = Serializer4 |
| 80 |
|
| 81 |
class ViewSet4(HaystackViewSet): |
| 82 |
serializer_class = Serializer5 |
| 83 |
|
| 84 |
self.view1 = ViewSet1 |
| 85 |
self.view2 = ViewSet2 |
| 86 |
self.view3 = ViewSet3 |
| 87 |
self.view4 = ViewSet4 |
| 88 |
|
| 89 |
def tearDown(self): |
| 90 |
MockPersonIndex().clear() |
| 91 |
|
| 92 |
def test_filter_view_has_default_filter(self): |
| 93 |
self.assertEqual(self.view1.filter_backends, [HaystackFilter]) |
| 94 |
|
| 95 |
def test_filter_no_query_parameters(self): |
| 96 |
request = factory.get(path="/", data="", content_type="application/json") |
| 97 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 98 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 99 |
self.assertEqual(len(response.data), MOCKPERSON_DATA_SET_SIZE) |
| 100 |
|
| 101 |
def test_filter_single_field(self): |
| 102 |
request = factory.get(path="/", data={"firstname": "John"}) # Should return 3 results |
| 103 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 104 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 105 |
self.assertEqual(len(response.data), 3) |
| 106 |
|
| 107 |
def test_filter_single_field_with_lookup(self): |
| 108 |
request = factory.get(path="/", data={"firstname__startswith": "John"}) # Should return 3 results |
| 109 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 110 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 111 |
self.assertEqual(len(response.data), 3) |
| 112 |
|
| 113 |
def test_filter_aliased_field(self): |
| 114 |
request = factory.get(path="/", data={"name": "John McClane"}, content_type="application/json") |
| 115 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 116 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 117 |
self.assertEqual(len(response.data), 1) |
| 118 |
|
| 119 |
def test_filter_aliased_field_with_lookup(self): |
| 120 |
request = factory.get(path="/", data={"name__contains": "John McClane"}, content_type="application/json") |
| 121 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 122 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 123 |
self.assertEqual(len(response.data), 1) |
| 124 |
|
| 125 |
def test_filter_single_field_OR(self): |
| 126 |
# Test filtering a single field for multiple values. The parameters should be OR'ed |
| 127 |
request = factory.get(path="/", data={"lastname": "Hickman,Hood"}) # Should return 3 results |
| 128 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 129 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 130 |
self.assertEqual(len(response.data), 3) |
| 131 |
|
| 132 |
def test_filter_single_field_OR_custom_lookup_sep(self): |
| 133 |
setattr(self.view1, "lookup_sep", ";") |
| 134 |
request = factory.get(path="/", data={"lastname": "Hickman;Hood"}) # Should return 3 results |
| 135 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 136 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 137 |
self.assertEqual(len(response.data), 3) |
| 138 |
|
| 139 |
# Reset the `lookup_sep` |
| 140 |
setattr(self.view1, "lookup_sep", ",") |
| 141 |
|
| 142 |
def test_filter_multiple_fields(self): |
| 143 |
# Test filtering multiple fields. The parameters should be AND'ed |
| 144 |
request = factory.get(path="/", data={"lastname": "Hood", "firstname": "Bruno"}) # Should return 1 result |
| 145 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 146 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 147 |
self.assertEqual(len(response.data), 1) |
| 148 |
|
| 149 |
def test_filter_multiple_fields_OR_same_fields(self): |
| 150 |
# Test filtering multiple fields for multiple values. The values should be OR'ed between |
| 151 |
# same parameters, and AND'ed between them |
| 152 |
request = factory.get(path="/", data={ |
| 153 |
"lastname": "Hickman,Hood", |
| 154 |
"firstname": "Walker,Bruno" |
| 155 |
}) # Should return 2 result |
| 156 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 157 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 158 |
self.assertEqual(len(response.data), 2) |
| 159 |
|
| 160 |
def test_filter_excluded_field(self): |
| 161 |
request = factory.get(path="/", data={"lastname": "Hood"}, content_type="application/json") |
| 162 |
response = self.view2.as_view(actions={"get": "list"})(request) |
| 163 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 164 |
self.assertEqual(len(response.data), MOCKPERSON_DATA_SET_SIZE) # Should return all results since, field is ignored |
| 165 |
|
| 166 |
def test_filter_with_non_searched_excluded_field(self): |
| 167 |
request = factory.get(path="/", data={"text": "John"}, content_type="application/json") |
| 168 |
response = self.view2.as_view(actions={"get": "list"})(request) |
| 169 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 170 |
self.assertEqual(len(response.data), 3) |
| 171 |
|
| 172 |
def test_filter_unicode_characters(self): |
| 173 |
request = factory.get(path="/", data={"firstname": "åsmund", "lastname": "sørensen"}, |
| 174 |
content_type="application/json") |
| 175 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 176 |
self.assertEqual(len(response.data), 1) |
| 177 |
|
| 178 |
def test_filter_negated_field(self): |
| 179 |
request = factory.get(path="/", data={"text__not": "John"}, content_type="application/json") |
| 180 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 181 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 182 |
self.assertEqual(len(response.data), 97) |
| 183 |
|
| 184 |
def test_filter_negated_field_with_lookup(self): |
| 185 |
request = factory.get(path="/", data={"name__not__contains": "John McClane"}, content_type="application/json") |
| 186 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 187 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 188 |
self.assertEqual(len(response.data), 99) |
| 189 |
|
| 190 |
def test_filter_negated_field_with_other_field(self): |
| 191 |
request = factory.get(path="/", data={"firstname": "John", "lastname__not": "McClane"}, content_type="application/json") |
| 192 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 193 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 194 |
self.assertEqual(len(response.data), 2) |
| 195 |
|
| 196 |
def test_filter_gt_date_field(self): |
| 197 |
request = factory.get(path="/", data={"birthdate__gt": "1980-01-01"}, content_type="application/json") |
| 198 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 199 |
self.assertEqual(len(response.data), MockPerson.objects.filter(birthdate__gt=date(1980, 1, 1)).count()) |
| 200 |
|
| 201 |
def test_filter_lt_date_field(self): |
| 202 |
request = factory.get(path="/", data={"birthdate__lt": "1980-01-01"}, content_type="application/json") |
| 203 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 204 |
self.assertEqual(len(response.data), MockPerson.objects.filter(birthdate__lt=date(1980, 1, 1)).count()) |
| 205 |
|
| 206 |
def test_filter_in_integerfield(self): |
| 207 |
request = factory.get(path="/", data={"integerfield__in": "48,57"}, content_type="application/json") |
| 208 |
response = self.view4.as_view(actions={"get": "list"})(request) |
| 209 |
self.assertEqual(len(response.data), MockAllField.objects.filter(integerfield__in=[48, 57]).count()) |
| 210 |
|
| 211 |
def test_filter_range_integerfield(self): |
| 212 |
request = factory.get(path="/", data={"integerfield__range": "300,500"}, content_type="application/json") |
| 213 |
response = self.view4.as_view(actions={"get": "list"})(request) |
| 214 |
self.assertEqual(len(response.data), MockAllField.objects.filter(integerfield__range=[300, 500]).count()) |
| 215 |
|
| 216 |
|
| 217 |
class HaystackAutocompleteFilterTestCase(TestCase): |
| 218 |
|
| 219 |
fixtures = ["mockperson"] |
| 220 |
|
| 221 |
def setUp(self): |
| 222 |
MockPersonIndex().reindex() |
| 223 |
|
| 224 |
class Serializer(HaystackSerializer): |
| 225 |
|
| 226 |
class Meta: |
| 227 |
index_classes = [MockPersonIndex] |
| 228 |
fields = ["text", "firstname", "lastname", "autocomplete"] |
| 229 |
|
| 230 |
class ViewSet(HaystackViewSet): |
| 231 |
index_models = [MockPerson] |
| 232 |
serializer_class = Serializer |
| 233 |
filter_backends = [HaystackAutocompleteFilter] |
| 234 |
|
| 235 |
self.view = ViewSet |
| 236 |
|
| 237 |
def tearDown(self): |
| 238 |
MockPersonIndex().clear() |
| 239 |
|
| 240 |
def test_filter_autocomplete_single_term(self): |
| 241 |
# Test querying the autocomplete field for a partial term. Should return 4 results |
| 242 |
request = factory.get(path="/", data={"autocomplete": "jer"}, content_type="application/json") |
| 243 |
response = self.view.as_view(actions={"get": "list"})(request) |
| 244 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 245 |
self.assertEqual(len(response.data), 4) |
| 246 |
|
| 247 |
def test_filter_autocomplete_multiple_terms(self): |
| 248 |
# Test querying the autocomplete field for multiple terms. |
| 249 |
# Make sure the filter AND's the terms on spaces, thus reduce the results. |
| 250 |
request = factory.get(path="/", data={"autocomplete": "joh mc"}, content_type="application/json") |
| 251 |
response = self.view.as_view(actions={"get": "list"})(request) |
| 252 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 253 |
self.assertEqual(len(response.data), 2) |
| 254 |
|
| 255 |
def test_filter_autocomplete_multiple_parameters(self): |
| 256 |
request = factory.get(path="/", data={"autocomplete": "jer fowler", "firstname": "jeremy"}, |
| 257 |
content_type="application/json") |
| 258 |
response = self.view.as_view(actions={"get": "list"})(request) |
| 259 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 260 |
|
| 261 |
def test_filter_autocomplete_single_field_OR(self): |
| 262 |
request = factory.get(path="/", data={"autocomplete": "jer,fowl"}, content_type="application/json") |
| 263 |
response = self.view.as_view(actions={"get": "list"})(request) |
| 264 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 265 |
|
| 266 |
|
| 267 |
@skipIf(not geospatial_support, "Skipped due to lack of GEO spatial features") |
| 268 |
class HaystackGEOSpatialFilterTestCase(TestCase): |
| 269 |
|
| 270 |
fixtures = ["mocklocation"] |
| 271 |
|
| 272 |
def setUp(self): |
| 273 |
MockLocationIndex().reindex() |
| 274 |
|
| 275 |
class Serializer(HaystackSerializer): |
| 276 |
|
| 277 |
class Meta: |
| 278 |
index_classes = [MockLocationIndex] |
| 279 |
fields = [ |
| 280 |
"text", "address", "city", "zip_code", |
| 281 |
"coordinates", |
| 282 |
] |
| 283 |
|
| 284 |
class ViewSet(HaystackViewSet): |
| 285 |
index_models = [MockLocation] |
| 286 |
serializer_class = Serializer |
| 287 |
filter_backends = [HaystackGEOSpatialFilter] |
| 288 |
|
| 289 |
self.view = ViewSet |
| 290 |
|
| 291 |
def tearDown(self): |
| 292 |
MockLocationIndex().clear() |
| 293 |
|
| 294 |
def test_filter_dwithin(self): |
| 295 |
request = factory.get(path="/", data={"from": "59.923396,10.739370", "km": 1}, content_type="application/json") |
| 296 |
response = self.view.as_view(actions={"get": "list"})(request) |
| 297 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 298 |
self.assertEqual(len(response.data), 4) |
| 299 |
|
| 300 |
def test_filter_dwithin_without_range_unit(self): |
| 301 |
# If no range unit is supplied, no filtering will occur. Make sure we |
| 302 |
# get the entire data set. |
| 303 |
request = factory.get(path="/", data={"from": "59.923396,10.739370"}, content_type="application/json") |
| 304 |
response = self.view.as_view(actions={"get": "list"})(request) |
| 305 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 306 |
self.assertEqual(len(response.data), MOCKLOCATION_DATA_SET_SIZE) |
| 307 |
|
| 308 |
def test_filter_dwithin_invalid_params(self): |
| 309 |
request = factory.get(path="/", data={"from": "i am not numeric,10.739370", "km": 1}, content_type="application/json") |
| 310 |
self.assertRaises( |
| 311 |
ValueError, |
| 312 |
self.view.as_view(actions={"get": "list"}), request |
| 313 |
) |
| 314 |
|
| 315 |
|
| 316 |
class HaystackHighlightFilterTestCase(TestCase): |
| 317 |
|
| 318 |
fixtures = ["mockperson"] |
| 319 |
|
| 320 |
def setUp(self): |
| 321 |
MockPersonIndex().reindex() |
| 322 |
|
| 323 |
class Serializer(HaystackSerializer): |
| 324 |
|
| 325 |
class Meta: |
| 326 |
index_classes = [MockPersonIndex] |
| 327 |
fields = ["firstname", "lastname"] |
| 328 |
|
| 329 |
class ViewSet(HaystackViewSet): |
| 330 |
index_models = [MockPerson] |
| 331 |
serializer_class = Serializer |
| 332 |
filter_backends = [HaystackHighlightFilter] |
| 333 |
|
| 334 |
self.view = ViewSet |
| 335 |
|
| 336 |
def tearDown(self): |
| 337 |
MockPersonIndex().clear() |
| 338 |
|
| 339 |
@skipIf(not elasticsearch_version < (2, ), "Highlighting is not yet supported for the Elasticsearch2 backend") |
| 340 |
def test_filter_highlighter_filter(self): |
| 341 |
request = factory.get(path="/", data={"firstname": "jeremy"}, content_type="application/json") |
| 342 |
response = self.view.as_view(actions={"get": "list"})(request) |
| 343 |
response.render() |
| 344 |
for result in json.loads(response.content.decode()): |
| 345 |
self.assertTrue("highlighted" in result) |
| 346 |
self.assertEqual( |
| 347 |
result["highlighted"], |
| 348 |
" ".join(("<em>Jeremy</em>", "%s\n" % result["lastname"])) |
| 349 |
) |
| 350 |
|
| 351 |
|
| 352 |
class HaystackBoostFilterTestCase(TestCase): |
| 353 |
|
| 354 |
fixtures = ["mockperson"] |
| 355 |
|
| 356 |
def setUp(self): |
| 357 |
MockPersonIndex().reindex() |
| 358 |
|
| 359 |
class Serializer(HaystackSerializer): |
| 360 |
|
| 361 |
class Meta: |
| 362 |
index_classes = [MockPersonIndex] |
| 363 |
fields = ["firstname", "lastname"] |
| 364 |
|
| 365 |
class ViewSet(HaystackViewSet): |
| 366 |
index_models = [MockPerson] |
| 367 |
serializer_class = Serializer |
| 368 |
filter_backends = [HaystackBoostFilter] |
| 369 |
|
| 370 |
self.view = ViewSet |
| 371 |
|
| 372 |
def tearDown(self): |
| 373 |
MockPersonIndex().clear() |
| 374 |
|
| 375 |
# Skipping the boost filter test case because it fails. |
| 376 |
# I strongly believe that this has to be fixed upstream, and |
| 377 |
# that the drf-haystack code works as it should. |
| 378 |
|
| 379 |
# def test_filter_boost(self): |
| 380 |
# |
| 381 |
# # This test will fail |
| 382 |
# # See https://github.com/django-haystack/django-haystack/issues/1235 |
| 383 |
# |
| 384 |
# request = factory.get(path="/", data={"lastname": "hood"}, content_type="application/json") |
| 385 |
# response = self.view.as_view(actions={"get": "list"})(request) |
| 386 |
# response.render() |
| 387 |
# data = json.loads(response.content.decode()) |
| 388 |
# self.assertEqual(len(response.data), 2) |
| 389 |
# self.assertEqual(data[0]["firstname"], "Bruno") |
| 390 |
# self.assertEqual(data[1]["firstname"], "Walker") |
| 391 |
# |
| 392 |
# # We're boosting walter slightly which should put him first in the results |
| 393 |
# request = factory.get(path="/", data={"lastname": "hood", "boost": "walker,1.1"}, |
| 394 |
# content_type="application/json") |
| 395 |
# response = self.view.as_view(actions={"get": "list"})(request) |
| 396 |
# response.render() |
| 397 |
# data = json.loads(response.content.decode()) |
| 398 |
# self.assertEqual(len(response.data), 2) |
| 399 |
# self.assertEqual(data[0]["firstname"], "Walker") |
| 400 |
# self.assertEqual(data[1]["firstname"], "Bruno") |
| 401 |
|
| 402 |
def test_filter_boost_valid_params(self): |
| 403 |
request = factory.get(path="/", data={"boost": "bruno,1.2"}, content_type="application/json") |
| 404 |
response = self.view.as_view(actions={"get": "list"})(request) |
| 405 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 406 |
|
| 407 |
def test_filter_boost_invalid_non_numeric(self): |
| 408 |
request = factory.get(path="/", data={"boost": "bruno,i am not numeric!"}, content_type="application/json") |
| 409 |
try: |
| 410 |
self.view.as_view(actions={"get": "list"})(request) |
| 411 |
self.fail("Did not raise ValueError when called with a non-numeric boost value.") |
| 412 |
except ValueError as e: |
| 413 |
self.assertEqual( |
| 414 |
str(e), |
| 415 |
"Cannot convert boost to float value. Make sure to provide a numerical boost value." |
| 416 |
) |
| 417 |
|
| 418 |
def test_filter_boost_invalid_malformed_query_params(self): |
| 419 |
request = factory.get(path="/", data={"boost": "bruno"}, content_type="application/json") |
| 420 |
try: |
| 421 |
self.view.as_view(actions={"get": "list"})(request) |
| 422 |
self.fail("Did not raise ValueError when called with a malformed query parameters.") |
| 423 |
except ValueError as e: |
| 424 |
self.assertEqual( |
| 425 |
str(e), |
| 426 |
"Cannot convert the '%s' query parameter to a valid boost filter." |
| 427 |
% HaystackBoostFilter.query_param |
| 428 |
) |
| 429 |
|
| 430 |
|
| 431 |
class HaystackFacetFilterTestCase(WarningTestCaseMixin, TestCase): |
| 432 |
|
| 433 |
fixtures = ["mockperson"] |
| 434 |
|
| 435 |
def setUp(self): |
| 436 |
MockPersonIndex().reindex() |
| 437 |
|
| 438 |
class FacetSerializer1(HaystackFacetSerializer): |
| 439 |
|
| 440 |
class Meta: |
| 441 |
index_classes = [MockPersonIndex] |
| 442 |
fields = ["firstname", "lastname", "created"] |
| 443 |
|
| 444 |
class FacetSerializer2(HaystackFacetSerializer): |
| 445 |
|
| 446 |
class Meta: |
| 447 |
index_classes = [MockPersonIndex] |
| 448 |
fields = ["firstname", "lastname", "created"] |
| 449 |
field_options = { |
| 450 |
"firstname": {}, |
| 451 |
"lastname": {}, |
| 452 |
"created": { |
| 453 |
"start_date": datetime.now() - timedelta(days=3 * 365), |
| 454 |
"end_date": datetime.now(), |
| 455 |
"gap_by": "day", |
| 456 |
"gap_amount": 10 |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
class ViewSet1(FacetMixin, HaystackViewSet): |
| 461 |
index_models = [MockPerson] |
| 462 |
facet_serializer_class = FacetSerializer1 |
| 463 |
|
| 464 |
class ViewSet2(FacetMixin, HaystackViewSet): |
| 465 |
index_models = [MockPerson] |
| 466 |
facet_serializer_class = FacetSerializer2 |
| 467 |
|
| 468 |
self.view1 = ViewSet1 |
| 469 |
self.view2 = ViewSet2 |
| 470 |
|
| 471 |
def tearDown(self): |
| 472 |
MockPersonIndex().clear() |
| 473 |
|
| 474 |
def test_filter_view_has_default_facet_filter(self): |
| 475 |
self.assertEqual(self.view1.facet_filter_backends, [HaystackFacetFilter]) |
| 476 |
|
| 477 |
def test_filter_facet_no_field_options(self): |
| 478 |
request = factory.get("/", data={}, content_type="application/json") |
| 479 |
response = self.view1.as_view(actions={"get": "facets"})(request) |
| 480 |
response.render() |
| 481 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 482 |
self.assertEqual(json.loads(response.content.decode()), {}) |
| 483 |
|
| 484 |
def test_filter_facet_serializer_no_field_options_missing_required_query_parameters(self): |
| 485 |
request = factory.get("/", data={"created": "start_date:Oct 3rd 2015"}, content_type="application/json") |
| 486 |
try: |
| 487 |
self.view1.as_view(actions={"get": "facets"})(request) |
| 488 |
self.fail("Did not raise ValueError when called without all required " |
| 489 |
"attributes and no default field_options is set.") |
| 490 |
except ValueError as e: |
| 491 |
self.assertEqual( |
| 492 |
str(e), |
| 493 |
"Date faceting requires at least 'start_date', 'end_date' and 'gap_by' to be set." |
| 494 |
) |
| 495 |
|
| 496 |
def test_filter_facet_no_field_options_valid_required_query_parameters(self): |
| 497 |
request = factory.get( |
| 498 |
"/", |
| 499 |
data={"created": "start_date:Jan 1th 2010,end_date:Dec 31th 2020,gap_by:month,gap_amount:1"}, |
| 500 |
content_type="application/json" |
| 501 |
) |
| 502 |
response = self.view1.as_view(actions={"get": "facets"})(request) |
| 503 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 504 |
|
| 505 |
def test_filter_facet_with_field_options(self): |
| 506 |
request = factory.get("/", data={}, content_type="application/json") |
| 507 |
response = self.view2.as_view(actions={"get": "facets"})(request) |
| 508 |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 509 |
|
| 510 |
def test_filter_facet_warn_on_inproperly_formatted_token(self): |
| 511 |
request = factory.get("/", data={"firstname": "token"}, content_type="application/json") |
| 512 |
self.assertWarning(UserWarning, self.view2.as_view(actions={"get": "facets"}), request) |
| 513 |
|
| 514 |
|
| 515 |
class OrderedHaystackViewSetTestCase(TestCase): |
| 516 |
|
| 517 |
fixtures = ["mockallfield"] |
| 518 |
|
| 519 |
def setUp(self): |
| 520 |
MockAllFieldIndex().reindex() |
| 521 |
|
| 522 |
class Serializer(HaystackSerializer): |
| 523 |
class Meta: |
| 524 |
fields = ("charfield", "integerfield", "floatfield", |
| 525 |
"decimalfield", "boolfield") |
| 526 |
index_classes = [MockAllFieldIndex] |
| 527 |
|
| 528 |
class ViewSet1(HaystackViewSet): |
| 529 |
index_models = [MockAllField] |
| 530 |
serializer_class = Serializer |
| 531 |
filter_backends = (HaystackOrderingFilter,) |
| 532 |
ordering_fields = "__all__" |
| 533 |
ordering = ("integerfield",) |
| 534 |
|
| 535 |
class ViewSet2(HaystackViewSet): |
| 536 |
index_models = [MockAllField] |
| 537 |
serializer_class = Serializer |
| 538 |
filter_backends = (HaystackOrderingFilter,) |
| 539 |
ordering_fields = "__all__" |
| 540 |
ordering = ("-integerfield",) |
| 541 |
|
| 542 |
self.view1 = ViewSet1 |
| 543 |
self.view2 = ViewSet2 |
| 544 |
|
| 545 |
def tearDown(self): |
| 546 |
MockAllFieldIndex().clear() |
| 547 |
|
| 548 |
def test_viewset_default_ordering(self): |
| 549 |
request = factory.get(path="/", content_type="application/json") |
| 550 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 551 |
|
| 552 |
response.render() |
| 553 |
content = json.loads(response.content.decode()) |
| 554 |
|
| 555 |
self.assertEqual( |
| 556 |
[result["integerfield"] for result in content], |
| 557 |
list(MockAllField.objects.values_list("integerfield", flat=True).order_by("integerfield")) |
| 558 |
) |
| 559 |
|
| 560 |
def test_viewset_default_reverse_ordering(self): |
| 561 |
request = factory.get(path="/", content_type="application/json") |
| 562 |
response = self.view2.as_view(actions={"get": "list"})(request) |
| 563 |
|
| 564 |
response.render() |
| 565 |
content = json.loads(response.content.decode()) |
| 566 |
|
| 567 |
self.assertEqual( |
| 568 |
[result["integerfield"] for result in content], |
| 569 |
list(MockAllField.objects.values_list("integerfield", flat=True).order_by("-integerfield")) |
| 570 |
) |
| 571 |
|
| 572 |
def test_viewset_order_by_single_query_param(self): |
| 573 |
request = factory.get(path="/", data={"ordering": "integerfield"}, content_type="application/json") |
| 574 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 575 |
|
| 576 |
response.render() |
| 577 |
content = json.loads(response.content.decode()) |
| 578 |
|
| 579 |
self.assertEqual( |
| 580 |
[result["integerfield"] for result in content], |
| 581 |
list(MockAllField.objects.values_list("integerfield", flat=True).order_by("integerfield")) |
| 582 |
) |
| 583 |
|
| 584 |
def test_viewset_order_by_multiple_query_params(self): |
| 585 |
request = factory.get(path="/", data={"ordering": "integerfield,boolfield"}, content_type="application/json") |
| 586 |
response = self.view1.as_view(actions={"get": "list"})(request) |
| 587 |
|
| 588 |
response.render() |
| 589 |
content = json.loads(response.content.decode()) |
| 590 |
|
| 591 |
self.assertEqual( |
| 592 |
[result["integerfield"] for result in content], |
| 593 |
list(MockAllField.objects.values_list("integerfield", flat=True).order_by("integerfield", "boolfield")) |
| 594 |
) |