From 999cddd58d30469f3ee85278985313fdf528323d Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Tue, 6 Oct 2020 12:51:35 +0200 Subject: [PATCH] Fixed #32073 -- Skipped collation tests on PostgreSQL < 10. PostgreSQL < 10 doesn't support ICU collations. Thanks Hannes Ljungberg for the report. --- django/db/backends/postgresql/features.py | 14 ++++++++++---- tests/inspectdb/tests.py | 2 ++ tests/schema/tests.py | 23 ++++++++++++++++++----- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/django/db/backends/postgresql/features.py b/django/db/backends/postgresql/features.py index e70ef4e95d..7615c2b4c3 100644 --- a/django/db/backends/postgresql/features.py +++ b/django/db/backends/postgresql/features.py @@ -58,10 +58,16 @@ class DatabaseFeatures(BaseDatabaseFeatures): supports_deferrable_unique_constraints = True has_json_operators = True json_key_contains_list_matching_requires_list = True - test_collations = { - 'non_default': 'sv-x-icu', - 'swedish_ci': 'sv-x-icu', - } + + @cached_property + def test_collations(self): + # PostgreSQL < 10 doesn't support ICU collations. + if self.is_postgresql_10: + return { + 'non_default': 'sv-x-icu', + 'swedish_ci': 'sv-x-icu', + } + return {} @cached_property def introspected_field_types(self): diff --git a/tests/inspectdb/tests.py b/tests/inspectdb/tests.py index 6815629e95..b2da1c0a26 100644 --- a/tests/inspectdb/tests.py +++ b/tests/inspectdb/tests.py @@ -105,6 +105,7 @@ class InspectDBTestCase(TestCase): self.assertIn('null_json_field = models.JSONField(blank=True, null=True)', output) @skipUnlessDBFeature('supports_collation_on_charfield') + @skipUnless(test_collation, 'Language collations are not supported.') def test_char_field_db_collation(self): out = StringIO() call_command('inspectdb', 'inspectdb_charfielddbcollation', stdout=out) @@ -123,6 +124,7 @@ class InspectDBTestCase(TestCase): ) @skipUnlessDBFeature('supports_collation_on_textfield') + @skipUnless(test_collation, 'Language collations are not supported.') def test_text_field_db_collation(self): out = StringIO() call_command('inspectdb', 'inspectdb_textfielddbcollation', stdout=out) diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 396d7c7c4f..49fda069d3 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -3236,7 +3236,9 @@ class SchemaTests(TransactionTestCase): @isolate_apps('schema') @skipUnlessDBFeature('supports_collation_on_charfield') def test_db_collation_charfield(self): - collation = connection.features.test_collations['non_default'] + collation = connection.features.test_collations.get('non_default') + if not collation: + self.skipTest('Language collations are not supported.') class Foo(Model): field = CharField(max_length=255, db_collation=collation) @@ -3256,7 +3258,9 @@ class SchemaTests(TransactionTestCase): @isolate_apps('schema') @skipUnlessDBFeature('supports_collation_on_textfield') def test_db_collation_textfield(self): - collation = connection.features.test_collations['non_default'] + collation = connection.features.test_collations.get('non_default') + if not collation: + self.skipTest('Language collations are not supported.') class Foo(Model): field = TextField(db_collation=collation) @@ -3275,10 +3279,13 @@ class SchemaTests(TransactionTestCase): @skipUnlessDBFeature('supports_collation_on_charfield') def test_add_field_db_collation(self): + collation = connection.features.test_collations.get('non_default') + if not collation: + self.skipTest('Language collations are not supported.') + with connection.schema_editor() as editor: editor.create_model(Author) - collation = connection.features.test_collations['non_default'] new_field = CharField(max_length=255, db_collation=collation) new_field.set_attributes_from_name('alias') with connection.schema_editor() as editor: @@ -3292,10 +3299,13 @@ class SchemaTests(TransactionTestCase): @skipUnlessDBFeature('supports_collation_on_charfield') def test_alter_field_db_collation(self): + collation = connection.features.test_collations.get('non_default') + if not collation: + self.skipTest('Language collations are not supported.') + with connection.schema_editor() as editor: editor.create_model(Author) - collation = connection.features.test_collations['non_default'] old_field = Author._meta.get_field('name') new_field = CharField(max_length=255, db_collation=collation) new_field.set_attributes_from_name('name') @@ -3312,10 +3322,13 @@ class SchemaTests(TransactionTestCase): @skipUnlessDBFeature('supports_collation_on_charfield') def test_alter_field_type_and_db_collation(self): + collation = connection.features.test_collations.get('non_default') + if not collation: + self.skipTest('Language collations are not supported.') + with connection.schema_editor() as editor: editor.create_model(Note) - collation = connection.features.test_collations['non_default'] old_field = Note._meta.get_field('info') new_field = CharField(max_length=255, db_collation=collation) new_field.set_attributes_from_name('info')