diff --git a/tests/__init__.py b/tests/__init__.py index 4dcaf8bd..30bbb438 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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', @@ -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', @@ -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.... @@ -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;') diff --git a/tests/filters/test_text_filters.py b/tests/filters/test_text_filters.py index 82cf5b33..a4ddb525 100644 --- a/tests/filters/test_text_filters.py +++ b/tests/filters/test_text_filters.py @@ -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() @@ -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'}) @@ -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) @@ -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'}) @@ -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']) - \ No newline at end of file +