Skip to content

Commit

Permalink
Enable unaccent test
Browse files Browse the repository at this point in the history
  • Loading branch information
daanvdk authored and Maciej Lewinski committed Feb 22, 2022
1 parent c67a74d commit 9f5345d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
14 changes: 12 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
}
else:
db_settings = {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'binder-test',
'HOST': 'localhost',
'USER': 'postgres',
Expand All @@ -51,6 +51,11 @@
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
*(
['django.contrib.postgres']
if db_settings['ENGINE'] == 'django.db.backends.postgresql' else
[]
),
'binder',
'binder.plugins.token_auth',
'tests',
Expand Down Expand Up @@ -116,7 +121,7 @@
# Do the dance to ensure the models are synched to the DB.
# This saves us from having to include migrations
from django.core.management.commands.migrate import Command as MigrationCommand # noqa
from django.db import connections # noqa
from django.db import connection, connections # noqa
from django.db.migrations.executor import MigrationExecutor # noqa

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


# Create postgres extensions
if db_settings['ENGINE'] == 'django.db.backends.postgresql':
with connection.cursor() as cursor:
cursor.execute('CREATE EXTENSION IF NOT EXISTS unaccent;')
25 changes: 14 additions & 11 deletions tests/filters/test_text_filters.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import unittest
import os
from django.test import TestCase, Client
from binder.json import jsonloads
from django.contrib.auth.models import User
from django.contrib.postgres.operations import UnaccentExtension

from ..testapp.models import Caretaker

class TextFiltersTest(TestCase):
def setUp(self):
super().setUp()
UnaccentExtension()
u = User(username='testuser', is_active=True, is_superuser=True)
u.set_password('test')
u.save()
Expand Down Expand Up @@ -37,14 +37,17 @@ def test_text_filter_exact_match(self):
self.assertEqual(0, len(result['data']))


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

# self.assertEqual(response.status_code, 200)
self.assertEqual(response.status_code, 200)

# result = jsonloads(response.content)
# self.assertEqual(0, len(result['data']))
result = jsonloads(response.content)
self.assertEqual(0, len(result['data']))

def test_text_filter_iexact(self):
response = self.client.get('/caretaker/', data={'.name:iexact': 'stefan'})
Expand Down Expand Up @@ -73,7 +76,7 @@ def test_text_filter_contains(self):
result = jsonloads(response.content)
print(result)
self.assertEqual(0, len(result['data']))

response = self.client.get('/caretaker/', data={'.name:contains': 'Stef'})

self.assertEqual(response.status_code, 200)
Expand Down Expand Up @@ -169,7 +172,7 @@ def test_text_filter_istartswith(self):
print(result)
self.assertEqual(1, len(result['data']))
self.assertEqual('Stefan', result['data'][0]['name'])

def test_text_filter_endswith(self):
response = self.client.get('/caretaker/', data={'.name:endswith': 'efa'})

Expand Down Expand Up @@ -222,4 +225,4 @@ def test_text_filter_iendswith(self):
print(result)
self.assertEqual(1, len(result['data']))
self.assertEqual('Stefan', result['data'][0]['name'])

0 comments on commit 9f5345d

Please sign in to comment.