|
3 | 3 |
|
4 | 4 | import pytest
|
5 | 5 | from django.core.exceptions import ImproperlyConfigured
|
6 |
| -from django.db import models |
| 6 | +from django.db import connections, models |
7 | 7 | from django.db.models import CharField, Transform
|
8 | 8 | from django.db.models.functions import Concat, Upper
|
9 | 9 | from django.test import SimpleTestCase, TestCase
|
10 | 10 | from django.test.utils import override_settings
|
11 | 11 |
|
12 | 12 | from rest_framework import filters, generics, serializers
|
13 |
| -from rest_framework.compat import coreschema |
| 13 | +from rest_framework.compat import coreschema, postgres_fields |
14 | 14 | from rest_framework.exceptions import ValidationError
|
15 | 15 | from rest_framework.test import APIRequestFactory
|
16 | 16 |
|
@@ -305,6 +305,29 @@ class SearchListView(generics.ListAPIView):
|
305 | 305 | ]
|
306 | 306 |
|
307 | 307 |
|
| 308 | +@pytest.mark.skipif(not postgres_fields, reason='psycopg2 is not installed') |
| 309 | +class SearchPostgreSQLFilterTests(TestCase): |
| 310 | + def setUp(self): |
| 311 | + connections.databases['default']['ENGINE'] = 'django.db.backends.postgresql' |
| 312 | + connections.databases['default']['NAME'] = ':memory:' |
| 313 | + |
| 314 | + def test_unaccent_search(self): |
| 315 | + class SearchListView(generics.ListAPIView): |
| 316 | + queryset = SearchFilterModel.objects.all() |
| 317 | + serializer_class = SearchFilterSerializer |
| 318 | + filter_backends = (filters.SearchFilter,) |
| 319 | + search_fields = ('title', '&text') |
| 320 | + |
| 321 | + obj = SearchFilterModel(title='Accent títle', text='Accent téxt').save() |
| 322 | + view = SearchListView.as_view() |
| 323 | + lower_and_unaccent_text = 'accent text' |
| 324 | + request = factory.get('/', {'search': lower_and_unaccent_text}) |
| 325 | + response = view(request) |
| 326 | + assert response.data == [ |
| 327 | + {'id': obj.id, 'title': 'Accent títle', 'text': 'Accent téxt'} |
| 328 | + ] |
| 329 | + |
| 330 | + |
308 | 331 | class AttributeModel(models.Model):
|
309 | 332 | label = models.CharField(max_length=32)
|
310 | 333 |
|
|
0 commit comments