Skip to content

Commit bde3cb6

Browse files
author
Daan van der Kallen
committed
Enable unaccent test
1 parent 2a523e6 commit bde3cb6

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

tests/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
else:
2727
db_settings = {
28-
'ENGINE': 'django.db.backends.postgresql_psycopg2',
28+
'ENGINE': 'django.db.backends.postgresql',
2929
'NAME': 'binder-test',
3030
'HOST': 'localhost',
3131
'USER': 'postgres',
@@ -51,6 +51,11 @@
5151
'django.contrib.auth',
5252
'django.contrib.contenttypes',
5353
'django.contrib.sessions',
54+
*(
55+
['django.contrib.postgres']
56+
if db_settings['ENGINE'] == 'django.db.backends.postgresql' else
57+
[]
58+
),
5459
'binder',
5560
'binder.plugins.token_auth',
5661
'tests',
@@ -116,7 +121,7 @@
116121
# Do the dance to ensure the models are synched to the DB.
117122
# This saves us from having to include migrations
118123
from django.core.management.commands.migrate import Command as MigrationCommand # noqa
119-
from django.db import connections # noqa
124+
from django.db import connection, connections # noqa
120125
from django.db.migrations.executor import MigrationExecutor # noqa
121126

122127
# This is oh so hacky....
@@ -132,3 +137,8 @@
132137
Permission.objects.get_or_create(content_type=content_type, codename='view_country')
133138
call_command('define_groups')
134139

140+
141+
# Create postgres extensions
142+
if db_settings['ENGINE'] == 'django.db.backends.postgresql':
143+
with connection.cursor() as cursor:
144+
cursor.execute('CREATE EXTENSION IF NOT EXISTS unaccent;')

tests/filters/test_text_filters.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
import unittest
2+
import os
13
from django.test import TestCase, Client
24
from binder.json import jsonloads
35
from django.contrib.auth.models import User
4-
from django.contrib.postgres.operations import UnaccentExtension
56

67
from ..testapp.models import Caretaker
78

89
class TextFiltersTest(TestCase):
910
def setUp(self):
1011
super().setUp()
11-
UnaccentExtension()
1212
u = User(username='testuser', is_active=True, is_superuser=True)
1313
u.set_password('test')
1414
u.save()
@@ -37,14 +37,17 @@ def test_text_filter_exact_match(self):
3737
self.assertEqual(0, len(result['data']))
3838

3939

40-
# [TODO] unaccent needs to be installed as an extension on postgres to make it work
41-
# def test_text_filter_unaccent_chained_qualifier_match(self):
42-
# response = self.client.get('/caretaker/', data={'.name:unaccent': 'Śtefan'})
40+
@unittest.skipIf(
41+
os.environ.get('BINDER_TEST_MYSQL', '0') != '0',
42+
"Only available with PostgreSQL"
43+
)
44+
def test_text_filter_unaccent_chained_qualifier_match(self):
45+
response = self.client.get('/caretaker/', data={'.name:unaccent': 'Śtefan'})
4346

44-
# self.assertEqual(response.status_code, 200)
47+
self.assertEqual(response.status_code, 200)
4548

46-
# result = jsonloads(response.content)
47-
# self.assertEqual(0, len(result['data']))
49+
result = jsonloads(response.content)
50+
self.assertEqual(0, len(result['data']))
4851

4952
def test_text_filter_iexact(self):
5053
response = self.client.get('/caretaker/', data={'.name:iexact': 'stefan'})
@@ -73,7 +76,7 @@ def test_text_filter_contains(self):
7376
result = jsonloads(response.content)
7477
print(result)
7578
self.assertEqual(0, len(result['data']))
76-
79+
7780
response = self.client.get('/caretaker/', data={'.name:contains': 'Stef'})
7881

7982
self.assertEqual(response.status_code, 200)
@@ -169,7 +172,7 @@ def test_text_filter_istartswith(self):
169172
print(result)
170173
self.assertEqual(1, len(result['data']))
171174
self.assertEqual('Stefan', result['data'][0]['name'])
172-
175+
173176
def test_text_filter_endswith(self):
174177
response = self.client.get('/caretaker/', data={'.name:endswith': 'efa'})
175178

@@ -222,4 +225,4 @@ def test_text_filter_iendswith(self):
222225
print(result)
223226
self.assertEqual(1, len(result['data']))
224227
self.assertEqual('Stefan', result['data'][0]['name'])
225-
228+

0 commit comments

Comments
 (0)