diff options
| author | django-bot <ops@djangoproject.com> | 2022-02-03 20:24:19 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
| commit | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch) | |
| tree | f0506b668a013d0063e5fba3dbf4863b466713ba /tests/backends/mysql | |
| parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'tests/backends/mysql')
| -rw-r--r-- | tests/backends/mysql/test_creation.py | 77 | ||||
| -rw-r--r-- | tests/backends/mysql/test_features.py | 13 | ||||
| -rw-r--r-- | tests/backends/mysql/test_introspection.py | 54 | ||||
| -rw-r--r-- | tests/backends/mysql/test_operations.py | 18 | ||||
| -rw-r--r-- | tests/backends/mysql/test_schema.py | 15 | ||||
| -rw-r--r-- | tests/backends/mysql/tests.py | 45 |
6 files changed, 123 insertions, 99 deletions
diff --git a/tests/backends/mysql/test_creation.py b/tests/backends/mysql/test_creation.py index 0d3480adea..151d00ff3f 100644 --- a/tests/backends/mysql/test_creation.py +++ b/tests/backends/mysql/test_creation.py @@ -9,34 +9,39 @@ from django.db.backends.mysql.creation import DatabaseCreation from django.test import SimpleTestCase -@unittest.skipUnless(connection.vendor == 'mysql', 'MySQL tests') +@unittest.skipUnless(connection.vendor == "mysql", "MySQL tests") class DatabaseCreationTests(SimpleTestCase): - def _execute_raise_database_exists(self, cursor, parameters, keepdb=False): - raise DatabaseError(1007, "Can't create database '%s'; database exists" % parameters['dbname']) + raise DatabaseError( + 1007, "Can't create database '%s'; database exists" % parameters["dbname"] + ) def _execute_raise_access_denied(self, cursor, parameters, keepdb=False): raise DatabaseError(1044, "Access denied for user") def patch_test_db_creation(self, execute_create_test_db): - return mock.patch.object(BaseDatabaseCreation, '_execute_create_test_db', execute_create_test_db) + return mock.patch.object( + BaseDatabaseCreation, "_execute_create_test_db", execute_create_test_db + ) - @mock.patch('sys.stdout', new_callable=StringIO) - @mock.patch('sys.stderr', new_callable=StringIO) + @mock.patch("sys.stdout", new_callable=StringIO) + @mock.patch("sys.stderr", new_callable=StringIO) def test_create_test_db_database_exists(self, *mocked_objects): # Simulate test database creation raising "database exists" creation = DatabaseCreation(connection) with self.patch_test_db_creation(self._execute_raise_database_exists): - with mock.patch('builtins.input', return_value='no'): + with mock.patch("builtins.input", return_value="no"): with self.assertRaises(SystemExit): # SystemExit is raised if the user answers "no" to the # prompt asking if it's okay to delete the test database. - creation._create_test_db(verbosity=0, autoclobber=False, keepdb=False) + creation._create_test_db( + verbosity=0, autoclobber=False, keepdb=False + ) # "Database exists" shouldn't appear when keepdb is on creation._create_test_db(verbosity=0, autoclobber=False, keepdb=True) - @mock.patch('sys.stdout', new_callable=StringIO) - @mock.patch('sys.stderr', new_callable=StringIO) + @mock.patch("sys.stdout", new_callable=StringIO) + @mock.patch("sys.stderr", new_callable=StringIO) def test_create_test_db_unexpected_error(self, *mocked_objects): # Simulate test database creation raising unexpected error creation = DatabaseCreation(connection) @@ -47,8 +52,8 @@ class DatabaseCreationTests(SimpleTestCase): def test_clone_test_db_database_exists(self): creation = DatabaseCreation(connection) with self.patch_test_db_creation(self._execute_raise_database_exists): - with mock.patch.object(DatabaseCreation, '_clone_db') as _clone_db: - creation._clone_test_db('suffix', verbosity=0, keepdb=True) + with mock.patch.object(DatabaseCreation, "_clone_db") as _clone_db: + creation._clone_test_db("suffix", verbosity=0, keepdb=True) _clone_db.assert_not_called() def test_clone_test_db_options_ordering(self): @@ -56,30 +61,32 @@ class DatabaseCreationTests(SimpleTestCase): try: saved_settings = connection.settings_dict connection.settings_dict = { - 'NAME': 'source_db', - 'USER': '', - 'PASSWORD': '', - 'PORT': '', - 'HOST': '', - 'ENGINE': 'django.db.backends.mysql', - 'OPTIONS': { - 'read_default_file': 'my.cnf', + "NAME": "source_db", + "USER": "", + "PASSWORD": "", + "PORT": "", + "HOST": "", + "ENGINE": "django.db.backends.mysql", + "OPTIONS": { + "read_default_file": "my.cnf", }, } - with mock.patch.object(subprocess, 'Popen') as mocked_popen: - creation._clone_db('source_db', 'target_db') - mocked_popen.assert_has_calls([ - mock.call( - [ - 'mysqldump', - '--defaults-file=my.cnf', - '--routines', - '--events', - 'source_db', - ], - stdout=subprocess.PIPE, - env=None, - ), - ]) + with mock.patch.object(subprocess, "Popen") as mocked_popen: + creation._clone_db("source_db", "target_db") + mocked_popen.assert_has_calls( + [ + mock.call( + [ + "mysqldump", + "--defaults-file=my.cnf", + "--routines", + "--events", + "source_db", + ], + stdout=subprocess.PIPE, + env=None, + ), + ] + ) finally: connection.settings_dict = saved_settings diff --git a/tests/backends/mysql/test_features.py b/tests/backends/mysql/test_features.py index 5d27890a5d..ec5bd442fb 100644 --- a/tests/backends/mysql/test_features.py +++ b/tests/backends/mysql/test_features.py @@ -5,17 +5,20 @@ from django.db.backends.mysql.features import DatabaseFeatures from django.test import TestCase -@skipUnless(connection.vendor == 'mysql', 'MySQL tests') +@skipUnless(connection.vendor == "mysql", "MySQL tests") class TestFeatures(TestCase): - def test_supports_transactions(self): """ All storage engines except MyISAM support transactions. """ - with mock.patch('django.db.connection.features._mysql_storage_engine', 'InnoDB'): + with mock.patch( + "django.db.connection.features._mysql_storage_engine", "InnoDB" + ): self.assertTrue(connection.features.supports_transactions) del connection.features.supports_transactions - with mock.patch('django.db.connection.features._mysql_storage_engine', 'MyISAM'): + with mock.patch( + "django.db.connection.features._mysql_storage_engine", "MyISAM" + ): self.assertFalse(connection.features.supports_transactions) del connection.features.supports_transactions @@ -35,6 +38,6 @@ class TestFeatures(TestCase): def test_allows_auto_pk_0(self): with mock.MagicMock() as _connection: - _connection.sql_mode = {'NO_AUTO_VALUE_ON_ZERO'} + _connection.sql_mode = {"NO_AUTO_VALUE_ON_ZERO"} database_features = DatabaseFeatures(_connection) self.assertIs(database_features.allows_auto_pk_0, True) diff --git a/tests/backends/mysql/test_introspection.py b/tests/backends/mysql/test_introspection.py index 4f13622eda..c1247de232 100644 --- a/tests/backends/mysql/test_introspection.py +++ b/tests/backends/mysql/test_introspection.py @@ -4,24 +4,24 @@ from django.db import connection, connections from django.test import TestCase -@skipUnless(connection.vendor == 'mysql', 'MySQL tests') +@skipUnless(connection.vendor == "mysql", "MySQL tests") class ParsingTests(TestCase): def test_parse_constraint_columns(self): _parse_constraint_columns = connection.introspection._parse_constraint_columns tests = ( - ('`height` >= 0', ['height'], ['height']), - ('`cost` BETWEEN 1 AND 10', ['cost'], ['cost']), - ('`ref1` > `ref2`', ['id', 'ref1', 'ref2'], ['ref1', 'ref2']), + ("`height` >= 0", ["height"], ["height"]), + ("`cost` BETWEEN 1 AND 10", ["cost"], ["cost"]), + ("`ref1` > `ref2`", ["id", "ref1", "ref2"], ["ref1", "ref2"]), ( - '`start` IS NULL OR `end` IS NULL OR `start` < `end`', - ['id', 'start', 'end'], - ['start', 'end'], + "`start` IS NULL OR `end` IS NULL OR `start` < `end`", + ["id", "start", "end"], + ["start", "end"], ), - ('JSON_VALID(`json_field`)', ['json_field'], ['json_field']), - ('CHAR_LENGTH(`name`) > 2', ['name'], ['name']), - ("lower(`ref1`) != 'test'", ['id', 'owe', 'ref1'], ['ref1']), - ("lower(`ref1`) != 'test'", ['id', 'lower', 'ref1'], ['ref1']), - ("`name` LIKE 'test%'", ['name'], ['name']), + ("JSON_VALID(`json_field`)", ["json_field"], ["json_field"]), + ("CHAR_LENGTH(`name`) > 2", ["name"], ["name"]), + ("lower(`ref1`) != 'test'", ["id", "owe", "ref1"], ["ref1"]), + ("lower(`ref1`) != 'test'", ["id", "lower", "ref1"], ["ref1"]), + ("`name` LIKE 'test%'", ["name"], ["name"]), ) for check_clause, table_columns, expected_columns in tests: with self.subTest(check_clause): @@ -29,28 +29,32 @@ class ParsingTests(TestCase): self.assertEqual(list(check_columns), expected_columns) -@skipUnless(connection.vendor == 'mysql', 'MySQL tests') +@skipUnless(connection.vendor == "mysql", "MySQL tests") class StorageEngineTests(TestCase): - databases = {'default', 'other'} + databases = {"default", "other"} def test_get_storage_engine(self): - table_name = 'test_storage_engine' - create_sql = 'CREATE TABLE %s (id INTEGER) ENGINE = %%s' % table_name - drop_sql = 'DROP TABLE %s' % table_name - default_connection = connections['default'] - other_connection = connections['other'] + table_name = "test_storage_engine" + create_sql = "CREATE TABLE %s (id INTEGER) ENGINE = %%s" % table_name + drop_sql = "DROP TABLE %s" % table_name + default_connection = connections["default"] + other_connection = connections["other"] try: with default_connection.cursor() as cursor: - cursor.execute(create_sql % 'InnoDB') + cursor.execute(create_sql % "InnoDB") self.assertEqual( - default_connection.introspection.get_storage_engine(cursor, table_name), - 'InnoDB', + default_connection.introspection.get_storage_engine( + cursor, table_name + ), + "InnoDB", ) with other_connection.cursor() as cursor: - cursor.execute(create_sql % 'MyISAM') + cursor.execute(create_sql % "MyISAM") self.assertEqual( - other_connection.introspection.get_storage_engine(cursor, table_name), - 'MyISAM', + other_connection.introspection.get_storage_engine( + cursor, table_name + ), + "MyISAM", ) finally: with default_connection.cursor() as cursor: diff --git a/tests/backends/mysql/test_operations.py b/tests/backends/mysql/test_operations.py index a98e8963b7..bd6170f299 100644 --- a/tests/backends/mysql/test_operations.py +++ b/tests/backends/mysql/test_operations.py @@ -7,7 +7,7 @@ from django.test import SimpleTestCase from ..models import Person, Tag -@unittest.skipUnless(connection.vendor == 'mysql', 'MySQL tests.') +@unittest.skipUnless(connection.vendor == "mysql", "MySQL tests.") class MySQLOperationsTests(SimpleTestCase): def test_sql_flush(self): # allow_cascade doesn't change statements on MySQL. @@ -20,10 +20,10 @@ class MySQLOperationsTests(SimpleTestCase): allow_cascade=allow_cascade, ), [ - 'SET FOREIGN_KEY_CHECKS = 0;', - 'DELETE FROM `backends_person`;', - 'DELETE FROM `backends_tag`;', - 'SET FOREIGN_KEY_CHECKS = 1;', + "SET FOREIGN_KEY_CHECKS = 0;", + "DELETE FROM `backends_person`;", + "DELETE FROM `backends_tag`;", + "SET FOREIGN_KEY_CHECKS = 1;", ], ) @@ -39,9 +39,9 @@ class MySQLOperationsTests(SimpleTestCase): allow_cascade=allow_cascade, ), [ - 'SET FOREIGN_KEY_CHECKS = 0;', - 'TRUNCATE `backends_person`;', - 'TRUNCATE `backends_tag`;', - 'SET FOREIGN_KEY_CHECKS = 1;', + "SET FOREIGN_KEY_CHECKS = 0;", + "TRUNCATE `backends_person`;", + "TRUNCATE `backends_tag`;", + "SET FOREIGN_KEY_CHECKS = 1;", ], ) diff --git a/tests/backends/mysql/test_schema.py b/tests/backends/mysql/test_schema.py index 44f4a07b18..2fb7fea9c5 100644 --- a/tests/backends/mysql/test_schema.py +++ b/tests/backends/mysql/test_schema.py @@ -4,18 +4,19 @@ from django.db import connection from django.test import TestCase -@unittest.skipUnless(connection.vendor == 'mysql', 'MySQL tests') +@unittest.skipUnless(connection.vendor == "mysql", "MySQL tests") class SchemaEditorTests(TestCase): def test_quote_value(self): import MySQLdb + editor = connection.schema_editor() tested_values = [ - ('string', "'string'"), - ('¿Tú hablas inglés?', "'¿Tú hablas inglés?'"), - (b'bytes', b"'bytes'"), - (42, '42'), - (1.754, '1.754e0' if MySQLdb.version_info >= (1, 3, 14) else '1.754'), - (False, b'0' if MySQLdb.version_info >= (1, 4, 0) else '0'), + ("string", "'string'"), + ("¿Tú hablas inglés?", "'¿Tú hablas inglés?'"), + (b"bytes", b"'bytes'"), + (42, "42"), + (1.754, "1.754e0" if MySQLdb.version_info >= (1, 3, 14) else "1.754"), + (False, b"0" if MySQLdb.version_info >= (1, 4, 0) else "0"), ] for value, expected in tested_values: with self.subTest(value=value): diff --git a/tests/backends/mysql/tests.py b/tests/backends/mysql/tests.py index 02fc312abc..6ea289e151 100644 --- a/tests/backends/mysql/tests.py +++ b/tests/backends/mysql/tests.py @@ -14,20 +14,21 @@ def get_connection(): @override_settings(DEBUG=True) -@unittest.skipUnless(connection.vendor == 'mysql', 'MySQL tests') +@unittest.skipUnless(connection.vendor == "mysql", "MySQL tests") class IsolationLevelTests(TestCase): - read_committed = 'read committed' - repeatable_read = 'repeatable read' + read_committed = "read committed" + repeatable_read = "repeatable read" isolation_values = { - level: level.upper() - for level in (read_committed, repeatable_read) + level: level.upper() for level in (read_committed, repeatable_read) } @classmethod def setUpClass(cls): super().setUpClass() - configured_isolation_level = connection.isolation_level or cls.isolation_values[cls.repeatable_read] + configured_isolation_level = ( + connection.isolation_level or cls.isolation_values[cls.repeatable_read] + ) cls.configured_isolation_level = configured_isolation_level.upper() cls.other_isolation_level = ( cls.read_committed @@ -38,50 +39,58 @@ class IsolationLevelTests(TestCase): @staticmethod def get_isolation_level(connection): with connection.cursor() as cursor: - cursor.execute("SHOW VARIABLES WHERE variable_name IN ('transaction_isolation', 'tx_isolation')") - return cursor.fetchone()[1].replace('-', ' ') + cursor.execute( + "SHOW VARIABLES WHERE variable_name IN ('transaction_isolation', 'tx_isolation')" + ) + return cursor.fetchone()[1].replace("-", " ") def test_auto_is_null_auto_config(self): - query = 'set sql_auto_is_null = 0' + query = "set sql_auto_is_null = 0" connection.init_connection_state() - last_query = connection.queries[-1]['sql'].lower() + last_query = connection.queries[-1]["sql"].lower() if connection.features.is_sql_auto_is_null_enabled: self.assertIn(query, last_query) else: self.assertNotIn(query, last_query) def test_connect_isolation_level(self): - self.assertEqual(self.get_isolation_level(connection), self.configured_isolation_level) + self.assertEqual( + self.get_isolation_level(connection), self.configured_isolation_level + ) def test_setting_isolation_level(self): with get_connection() as new_connection: - new_connection.settings_dict['OPTIONS']['isolation_level'] = self.other_isolation_level + new_connection.settings_dict["OPTIONS"][ + "isolation_level" + ] = self.other_isolation_level self.assertEqual( self.get_isolation_level(new_connection), - self.isolation_values[self.other_isolation_level] + self.isolation_values[self.other_isolation_level], ) def test_uppercase_isolation_level(self): # Upper case values are also accepted in 'isolation_level'. with get_connection() as new_connection: - new_connection.settings_dict['OPTIONS']['isolation_level'] = self.other_isolation_level.upper() + new_connection.settings_dict["OPTIONS"][ + "isolation_level" + ] = self.other_isolation_level.upper() self.assertEqual( self.get_isolation_level(new_connection), - self.isolation_values[self.other_isolation_level] + self.isolation_values[self.other_isolation_level], ) def test_default_isolation_level(self): # If not specified in settings, the default is read committed. with get_connection() as new_connection: - new_connection.settings_dict['OPTIONS'].pop('isolation_level', None) + new_connection.settings_dict["OPTIONS"].pop("isolation_level", None) self.assertEqual( self.get_isolation_level(new_connection), - self.isolation_values[self.read_committed] + self.isolation_values[self.read_committed], ) def test_isolation_level_validation(self): new_connection = connection.copy() - new_connection.settings_dict['OPTIONS']['isolation_level'] = 'xxx' + new_connection.settings_dict["OPTIONS"]["isolation_level"] = "xxx" msg = ( "Invalid transaction isolation level 'xxx' specified.\n" "Use one of 'read committed', 'read uncommitted', " |
