diff --git a/django/db/backends/postgresql/creation.py b/django/db/backends/postgresql/creation.py index a609f33fd6..eb8ac3bcf5 100644 --- a/django/db/backends/postgresql/creation.py +++ b/django/db/backends/postgresql/creation.py @@ -2,6 +2,7 @@ import sys from psycopg2 import errorcodes +from django.core.exceptions import ImproperlyConfigured from django.db.backends.base.creation import BaseDatabaseCreation from django.db.backends.utils import strip_quotes @@ -21,9 +22,11 @@ class DatabaseCreation(BaseDatabaseCreation): def sql_table_creation_suffix(self): test_settings = self.connection.settings_dict['TEST'] - assert test_settings['COLLATION'] is None, ( - "PostgreSQL does not support collation setting at database creation time." - ) + if test_settings.get('COLLATION') is not None: + raise ImproperlyConfigured( + 'PostgreSQL does not support collation setting at database ' + 'creation time.' + ) return self._get_database_create_suffix( encoding=test_settings['CHARSET'], template=test_settings.get('TEMPLATE'), diff --git a/django/test/testcases.py b/django/test/testcases.py index b2720c50f3..b947d70a45 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -1253,7 +1253,8 @@ class TestCase(TransactionTestCase): self.setUpTestData() return super()._fixture_setup() - assert not self.reset_sequences, 'reset_sequences cannot be used on TestCase instances' + if self.reset_sequences: + raise TypeError('reset_sequences cannot be used on TestCase instances') self.atomics = self._enter_atomics() def _fixture_teardown(self): diff --git a/tests/backends/postgresql/test_creation.py b/tests/backends/postgresql/test_creation.py index 20f5f7ede9..eb68c6e471 100644 --- a/tests/backends/postgresql/test_creation.py +++ b/tests/backends/postgresql/test_creation.py @@ -3,6 +3,7 @@ from contextlib import contextmanager from io import StringIO from unittest import mock +from django.core.exceptions import ImproperlyConfigured from django.db import DatabaseError, connection from django.db.backends.base.creation import BaseDatabaseCreation from django.test import SimpleTestCase @@ -61,6 +62,15 @@ class DatabaseCreationTests(SimpleTestCase): settings = {'CHARSET': 'UTF8', 'TEMPLATE': 'template0'} self.check_sql_table_creation_suffix(settings, '''WITH ENCODING 'UTF8' TEMPLATE "template0"''') + def test_sql_table_creation_raises_with_collation(self): + settings = {'COLLATION': 'test'} + msg = ( + 'PostgreSQL does not support collation setting at database ' + 'creation time.' + ) + with self.assertRaisesMessage(ImproperlyConfigured, msg): + self.check_sql_table_creation_suffix(settings, None) + def _execute_raise_database_already_exists(self, cursor, parameters, keepdb=False): error = DatabaseError('database %s already exists' % parameters['dbname']) error.pgcode = errorcodes.DUPLICATE_DATABASE diff --git a/tests/test_utils/test_testcase.py b/tests/test_utils/test_testcase.py index 5ec71f84d0..9b11168b13 100644 --- a/tests/test_utils/test_testcase.py +++ b/tests/test_utils/test_testcase.py @@ -43,6 +43,16 @@ class TestTestCase(TestCase): with self.assertRaisesMessage(AssertionError, message): Car.objects.using('other').get() + def test_reset_sequences(self): + old_reset_sequences = self.reset_sequences + self.reset_sequences = True + msg = 'reset_sequences cannot be used on TestCase instances' + try: + with self.assertRaisesMessage(TypeError, msg): + self._fixture_setup() + finally: + self.reset_sequences = old_reset_sequences + class NonDeepCopyAble: def __deepcopy__(self, memo):