2018-11-01 15:14:34 +00:00
|
|
|
from django.db import connection
|
2014-09-05 20:53:11 +00:00
|
|
|
|
2015-05-30 21:18:01 +00:00
|
|
|
from . import PostgreSQLTestCase
|
2014-09-05 20:53:11 +00:00
|
|
|
from .models import CharFieldModel, TextFieldModel
|
|
|
|
|
|
|
|
|
2015-05-30 21:18:01 +00:00
|
|
|
class UnaccentTest(PostgreSQLTestCase):
|
2014-09-05 20:53:11 +00:00
|
|
|
Model = CharFieldModel
|
|
|
|
|
2018-11-24 01:59:38 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.Model.objects.bulk_create(
|
|
|
|
[
|
|
|
|
cls.Model(field="àéÖ"),
|
|
|
|
cls.Model(field="aeO"),
|
|
|
|
cls.Model(field="aeo"),
|
2014-09-05 20:53:11 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_unaccent(self):
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2014-09-05 20:53:11 +00:00
|
|
|
self.Model.objects.filter(field__unaccent="aeO"),
|
|
|
|
["àéÖ", "aeO"],
|
|
|
|
transform=lambda instance: instance.field,
|
|
|
|
ordered=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_unaccent_chained(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
Unaccent can be used chained with a lookup (which should be the case
|
|
|
|
since unaccent implements the Transform API)
|
2014-09-05 20:53:11 +00:00
|
|
|
"""
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2014-09-05 20:53:11 +00:00
|
|
|
self.Model.objects.filter(field__unaccent__iexact="aeO"),
|
|
|
|
["àéÖ", "aeO", "aeo"],
|
|
|
|
transform=lambda instance: instance.field,
|
|
|
|
ordered=False,
|
|
|
|
)
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2014-09-05 20:53:11 +00:00
|
|
|
self.Model.objects.filter(field__unaccent__endswith="éÖ"),
|
|
|
|
["àéÖ", "aeO"],
|
|
|
|
transform=lambda instance: instance.field,
|
|
|
|
ordered=False,
|
|
|
|
)
|
|
|
|
|
2018-11-01 15:14:34 +00:00
|
|
|
def test_unaccent_with_conforming_strings_off(self):
|
|
|
|
"""SQL is valid when standard_conforming_strings is off."""
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
cursor.execute("SHOW standard_conforming_strings")
|
|
|
|
disable_conforming_strings = cursor.fetchall()[0][0] == "on"
|
|
|
|
if disable_conforming_strings:
|
|
|
|
cursor.execute("SET standard_conforming_strings TO off")
|
|
|
|
try:
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2018-11-01 15:14:34 +00:00
|
|
|
self.Model.objects.filter(field__unaccent__endswith="éÖ"),
|
|
|
|
["àéÖ", "aeO"],
|
|
|
|
transform=lambda instance: instance.field,
|
|
|
|
ordered=False,
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
if disable_conforming_strings:
|
|
|
|
cursor.execute("SET standard_conforming_strings TO on")
|
|
|
|
|
2014-09-05 20:53:11 +00:00
|
|
|
def test_unaccent_accentuated_needle(self):
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2014-09-05 20:53:11 +00:00
|
|
|
self.Model.objects.filter(field__unaccent="aéÖ"),
|
|
|
|
["àéÖ", "aeO"],
|
|
|
|
transform=lambda instance: instance.field,
|
|
|
|
ordered=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class UnaccentTextFieldTest(UnaccentTest):
|
|
|
|
"""
|
|
|
|
TextField should have the exact same behavior as CharField
|
|
|
|
regarding unaccent lookups.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2014-09-05 20:53:11 +00:00
|
|
|
Model = TextFieldModel
|