From 765b96734c374ae8612720a6f71c0094430e6cf7 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Fri, 21 Apr 2023 09:59:01 +0200 Subject: [PATCH] Added SchemaTests._add_ci_collation() hook. --- tests/schema/tests.py | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 5a03ac6fd5..58c87904f7 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -1293,6 +1293,21 @@ class SchemaTests(TransactionTestCase): with self.assertRaisesMessage(DataError, msg): editor.alter_field(ArrayModel, old_field, new_field, strict=True) + def _add_ci_collation(self): + ci_collation = "case_insensitive" + + def drop_collation(): + with connection.cursor() as cursor: + cursor.execute(f"DROP COLLATION IF EXISTS {ci_collation}") + + with connection.cursor() as cursor: + cursor.execute( + f"CREATE COLLATION IF NOT EXISTS {ci_collation} (provider=icu, " + f"locale='und-u-ks-level2', deterministic=false)" + ) + self.addCleanup(drop_collation) + return ci_collation + @isolate_apps("schema") @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific") @skipUnlessDBFeature( @@ -1302,20 +1317,9 @@ class SchemaTests(TransactionTestCase): def test_db_collation_arrayfield(self): from django.contrib.postgres.fields import ArrayField - ci_collation = "case_insensitive" + ci_collation = self._add_ci_collation() cs_collation = "en-x-icu" - def drop_collation(): - with connection.cursor() as cursor: - cursor.execute(f"DROP COLLATION IF EXISTS {ci_collation}") - - with connection.cursor() as cursor: - cursor.execute( - f"CREATE COLLATION IF NOT EXISTS {ci_collation} (provider = icu, " - f"locale = 'und-u-ks-level2', deterministic = false)" - ) - self.addCleanup(drop_collation) - class ArrayModel(Model): field = ArrayField(CharField(max_length=16, db_collation=ci_collation)) @@ -1349,18 +1353,7 @@ class SchemaTests(TransactionTestCase): "supports_non_deterministic_collations", ) def test_unique_with_collation_charfield(self): - ci_collation = "case_insensitive" - - def drop_collation(): - with connection.cursor() as cursor: - cursor.execute(f"DROP COLLATION IF EXISTS {ci_collation}") - - with connection.cursor() as cursor: - cursor.execute( - f"CREATE COLLATION IF NOT EXISTS {ci_collation} (provider = icu, " - f"locale = 'und-u-ks-level2', deterministic = false)" - ) - self.addCleanup(drop_collation) + ci_collation = self._add_ci_collation() class CiCharModel(Model): field = CharField(max_length=16, db_collation=ci_collation, unique=True)