|
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 |
|
@@ -146,6 +146,29 @@ class SearchListView(generics.ListAPIView):
|
146 | 146 | {'id': 2, 'title': 'zz', 'text': 'bcd'}
|
147 | 147 | ]
|
148 | 148 |
|
| 149 | + |
| 150 | +@pytest.mark.skipif(not postgres_fields, reason='psycopg2 is not installed') |
| 151 | +class SearchPostgreSQLFilterTests(TestCase): |
| 152 | + def setUp(self): |
| 153 | + connections.databases['default']['ENGINE'] = 'django.db.backends.postgresql' |
| 154 | + connections.databases['default']['NAME'] = ':memory:' |
| 155 | + |
| 156 | + def test_unaccent_search(self): |
| 157 | + class SearchListView(generics.ListAPIView): |
| 158 | + queryset = SearchFilterModel.objects.all() |
| 159 | + serializer_class = SearchFilterSerializer |
| 160 | + filter_backends = (filters.SearchFilter,) |
| 161 | + search_fields = ('title', '&text') |
| 162 | + |
| 163 | + obj = SearchFilterModel(title='Accent títle', text='Accent téxt').save() |
| 164 | + view = SearchListView.as_view() |
| 165 | + lower_and_unaccent_text = 'accent text' |
| 166 | + request = factory.get('/', {'search': lower_and_unaccent_text}) |
| 167 | + response = view(request) |
| 168 | + assert response.data == [ |
| 169 | + {'id': obj.id, 'title': 'Accent títle', 'text': 'Accent téxt'} |
| 170 | + ] |
| 171 | + |
149 | 172 | def test_regexp_search(self):
|
150 | 173 | class SearchListView(generics.ListAPIView):
|
151 | 174 | queryset = SearchFilterModel.objects.all()
|
|
0 commit comments