Refs #32508 -- Raised ImproperlyConfigured/TypeError instead of using "assert".

This commit is contained in:
Mateo Radman 2021-07-02 19:33:48 +02:00 committed by Mariusz Felisiak
parent 1ff0ea6e9b
commit 2231429991
4 changed files with 28 additions and 4 deletions

View File

@ -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'),

View File

@ -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):

View File

@ -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

View File

@ -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):