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/oracle | |
| parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'tests/backends/oracle')
| -rw-r--r-- | tests/backends/oracle/test_creation.py | 65 | ||||
| -rw-r--r-- | tests/backends/oracle/test_introspection.py | 22 | ||||
| -rw-r--r-- | tests/backends/oracle/test_operations.py | 24 | ||||
| -rw-r--r-- | tests/backends/oracle/tests.py | 53 |
4 files changed, 97 insertions, 67 deletions
diff --git a/tests/backends/oracle/test_creation.py b/tests/backends/oracle/test_creation.py index 930d2520e2..6f6b9fd233 100644 --- a/tests/backends/oracle/test_creation.py +++ b/tests/backends/oracle/test_creation.py @@ -7,16 +7,19 @@ from django.db.backends.oracle.creation import DatabaseCreation from django.test import TestCase -@unittest.skipUnless(connection.vendor == 'oracle', 'Oracle tests') -@mock.patch.object(DatabaseCreation, '_maindb_connection', return_value=connection) -@mock.patch('sys.stdout', new_callable=StringIO) -@mock.patch('sys.stderr', new_callable=StringIO) +@unittest.skipUnless(connection.vendor == "oracle", "Oracle tests") +@mock.patch.object(DatabaseCreation, "_maindb_connection", return_value=connection) +@mock.patch("sys.stdout", new_callable=StringIO) +@mock.patch("sys.stderr", new_callable=StringIO) class DatabaseCreationTests(TestCase): - - def _execute_raise_user_already_exists(self, cursor, statements, parameters, verbosity, allow_quiet_fail=False): + def _execute_raise_user_already_exists( + self, cursor, statements, parameters, verbosity, allow_quiet_fail=False + ): # Raise "user already exists" only in test user creation - if statements and statements[0].startswith('CREATE USER'): - raise DatabaseError("ORA-01920: user name 'string' conflicts with another user or role name") + if statements and statements[0].startswith("CREATE USER"): + raise DatabaseError( + "ORA-01920: user name 'string' conflicts with another user or role name" + ) def _execute_raise_tablespace_already_exists( self, cursor, statements, parameters, verbosity, allow_quiet_fail=False @@ -30,17 +33,21 @@ class DatabaseCreationTests(TestCase): def _test_database_passwd(self): # Mocked to avoid test user password changed - return connection.settings_dict['SAVED_PASSWORD'] + return connection.settings_dict["SAVED_PASSWORD"] def patch_execute_statements(self, execute_statements): - return mock.patch.object(DatabaseCreation, '_execute_statements', execute_statements) + return mock.patch.object( + DatabaseCreation, "_execute_statements", execute_statements + ) - @mock.patch.object(DatabaseCreation, '_test_user_create', return_value=False) + @mock.patch.object(DatabaseCreation, "_test_user_create", return_value=False) def test_create_test_db(self, *mocked_objects): creation = DatabaseCreation(connection) # Simulate test database creation raising "tablespace already exists" - with self.patch_execute_statements(self._execute_raise_tablespace_already_exists): - with mock.patch('builtins.input', return_value='no'): + with self.patch_execute_statements( + self._execute_raise_tablespace_already_exists + ): + 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 tablespace. @@ -54,13 +61,15 @@ class DatabaseCreationTests(TestCase): with self.assertRaises(SystemExit): creation._create_test_db(verbosity=0, keepdb=True) - @mock.patch.object(DatabaseCreation, '_test_database_create', return_value=False) + @mock.patch.object(DatabaseCreation, "_test_database_create", return_value=False) def test_create_test_user(self, *mocked_objects): creation = DatabaseCreation(connection) - with mock.patch.object(DatabaseCreation, '_test_database_passwd', self._test_database_passwd): + with mock.patch.object( + DatabaseCreation, "_test_database_passwd", self._test_database_passwd + ): # Simulate test user creation raising "user already exists" with self.patch_execute_statements(self._execute_raise_user_already_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 user. @@ -68,27 +77,35 @@ class DatabaseCreationTests(TestCase): # "User already exists" error is ignored when keepdb is on creation._create_test_db(verbosity=0, keepdb=True) # Simulate test user creation raising unexpected error - with self.patch_execute_statements(self._execute_raise_insufficient_privileges): + with self.patch_execute_statements( + self._execute_raise_insufficient_privileges + ): with self.assertRaises(SystemExit): creation._create_test_db(verbosity=0, keepdb=False) with self.assertRaises(SystemExit): creation._create_test_db(verbosity=0, keepdb=True) def test_oracle_managed_files(self, *mocked_objects): - def _execute_capture_statements(self, cursor, statements, parameters, verbosity, allow_quiet_fail=False): + def _execute_capture_statements( + self, cursor, statements, parameters, verbosity, allow_quiet_fail=False + ): self.tblspace_sqls = statements creation = DatabaseCreation(connection) # Simulate test database creation with Oracle Managed File (OMF) # tablespaces. - with mock.patch.object(DatabaseCreation, '_test_database_oracle_managed_files', return_value=True): + with mock.patch.object( + DatabaseCreation, "_test_database_oracle_managed_files", return_value=True + ): with self.patch_execute_statements(_execute_capture_statements): with connection.cursor() as cursor: - creation._execute_test_db_creation(cursor, creation._get_test_db_params(), verbosity=0) + creation._execute_test_db_creation( + cursor, creation._get_test_db_params(), verbosity=0 + ) tblspace_sql, tblspace_tmp_sql = creation.tblspace_sqls # Datafile names shouldn't appear. - self.assertIn('DATAFILE SIZE', tblspace_sql) - self.assertIn('TEMPFILE SIZE', tblspace_tmp_sql) + self.assertIn("DATAFILE SIZE", tblspace_sql) + self.assertIn("TEMPFILE SIZE", tblspace_tmp_sql) # REUSE cannot be used with OMF. - self.assertNotIn('REUSE', tblspace_sql) - self.assertNotIn('REUSE', tblspace_tmp_sql) + self.assertNotIn("REUSE", tblspace_sql) + self.assertNotIn("REUSE", tblspace_tmp_sql) diff --git a/tests/backends/oracle/test_introspection.py b/tests/backends/oracle/test_introspection.py index 6664dda3d3..c6a143eb2d 100644 --- a/tests/backends/oracle/test_introspection.py +++ b/tests/backends/oracle/test_introspection.py @@ -6,24 +6,30 @@ from django.test import TransactionTestCase from ..models import Square -@unittest.skipUnless(connection.vendor == 'oracle', 'Oracle tests') +@unittest.skipUnless(connection.vendor == "oracle", "Oracle tests") class DatabaseSequenceTests(TransactionTestCase): available_apps = [] def test_get_sequences(self): with connection.cursor() as cursor: - seqs = connection.introspection.get_sequences(cursor, Square._meta.db_table, Square._meta.local_fields) + seqs = connection.introspection.get_sequences( + cursor, Square._meta.db_table, Square._meta.local_fields + ) self.assertEqual(len(seqs), 1) - self.assertIsNotNone(seqs[0]['name']) - self.assertEqual(seqs[0]['table'], Square._meta.db_table) - self.assertEqual(seqs[0]['column'], 'id') + self.assertIsNotNone(seqs[0]["name"]) + self.assertEqual(seqs[0]["table"], Square._meta.db_table) + self.assertEqual(seqs[0]["column"], "id") def test_get_sequences_manually_created_index(self): with connection.cursor() as cursor: with connection.schema_editor() as editor: - editor._drop_identity(Square._meta.db_table, 'id') - seqs = connection.introspection.get_sequences(cursor, Square._meta.db_table, Square._meta.local_fields) - self.assertEqual(seqs, [{'table': Square._meta.db_table, 'column': 'id'}]) + editor._drop_identity(Square._meta.db_table, "id") + seqs = connection.introspection.get_sequences( + cursor, Square._meta.db_table, Square._meta.local_fields + ) + self.assertEqual( + seqs, [{"table": Square._meta.db_table, "column": "id"}] + ) # Recreate model, because adding identity is impossible. editor.delete_model(Square) editor.create_model(Square) diff --git a/tests/backends/oracle/test_operations.py b/tests/backends/oracle/test_operations.py index 54fe73139d..523bdcda8a 100644 --- a/tests/backends/oracle/test_operations.py +++ b/tests/backends/oracle/test_operations.py @@ -7,13 +7,15 @@ from django.test import TransactionTestCase from ..models import Person, Tag -@unittest.skipUnless(connection.vendor == 'oracle', 'Oracle tests') +@unittest.skipUnless(connection.vendor == "oracle", "Oracle tests") class OperationsTests(TransactionTestCase): - available_apps = ['backends'] + available_apps = ["backends"] def test_sequence_name_truncation(self): - seq_name = connection.ops._get_no_autofield_sequence_name('schema_authorwithevenlongee869') - self.assertEqual(seq_name, 'SCHEMA_AUTHORWITHEVENLOB0B8_SQ') + seq_name = connection.ops._get_no_autofield_sequence_name( + "schema_authorwithevenlongee869" + ) + self.assertEqual(seq_name, "SCHEMA_AUTHORWITHEVENLOB0B8_SQ") def test_bulk_batch_size(self): # Oracle restricts the number of parameters in a query. @@ -21,11 +23,11 @@ class OperationsTests(TransactionTestCase): self.assertEqual(connection.ops.bulk_batch_size([], objects), len(objects)) # Each field is a parameter for each object. self.assertEqual( - connection.ops.bulk_batch_size(['id'], objects), + connection.ops.bulk_batch_size(["id"], objects), connection.features.max_query_params, ) self.assertEqual( - connection.ops.bulk_batch_size(['id', 'other'], objects), + connection.ops.bulk_batch_size(["id", "other"], objects), connection.features.max_query_params // 2, ) @@ -105,8 +107,8 @@ class OperationsTests(TransactionTestCase): ) # Sequences. self.assertEqual(len(statements[4:]), 2) - self.assertIn('BACKENDS_PERSON_SQ', statements[4]) - self.assertIn('BACKENDS_TAG_SQ', statements[5]) + self.assertIn("BACKENDS_PERSON_SQ", statements[4]) + self.assertIn("BACKENDS_TAG_SQ", statements[5]) def test_sql_flush_sequences_allow_cascade(self): statements = connection.ops.sql_flush( @@ -136,6 +138,6 @@ class OperationsTests(TransactionTestCase): ) # Sequences. self.assertEqual(len(statements[5:]), 3) - self.assertIn('BACKENDS_PERSON_SQ', statements[5]) - self.assertIn('BACKENDS_VERYLONGMODELN7BE2_SQ', statements[6]) - self.assertIn('BACKENDS_TAG_SQ', statements[7]) + self.assertIn("BACKENDS_PERSON_SQ", statements[5]) + self.assertIn("BACKENDS_VERYLONGMODELN7BE2_SQ", statements[6]) + self.assertIn("BACKENDS_TAG_SQ", statements[7]) diff --git a/tests/backends/oracle/tests.py b/tests/backends/oracle/tests.py index 6d31d33d57..a518db2c64 100644 --- a/tests/backends/oracle/tests.py +++ b/tests/backends/oracle/tests.py @@ -4,14 +4,11 @@ from django.db import DatabaseError, connection from django.db.models import BooleanField from django.test import TransactionTestCase -from ..models import ( - Square, VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ, -) +from ..models import Square, VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ -@unittest.skipUnless(connection.vendor == 'oracle', 'Oracle tests') +@unittest.skipUnless(connection.vendor == "oracle", "Oracle tests") class Tests(unittest.TestCase): - def test_quote_name(self): """'%' chars are escaped for query execution.""" name = '"SOME%NAME"' @@ -21,21 +18,24 @@ class Tests(unittest.TestCase): def test_quote_name_db_table(self): model = VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ db_table = model._meta.db_table.upper() - self.assertEqual(f'"{db_table}"', connection.ops.quote_name( - 'backends_verylongmodelnamezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz', - )) + self.assertEqual( + f'"{db_table}"', + connection.ops.quote_name( + "backends_verylongmodelnamezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", + ), + ) def test_dbms_session(self): """A stored procedure can be called through a cursor wrapper.""" with connection.cursor() as cursor: - cursor.callproc('DBMS_SESSION.SET_IDENTIFIER', ['_django_testing!']) + cursor.callproc("DBMS_SESSION.SET_IDENTIFIER", ["_django_testing!"]) def test_cursor_var(self): """Cursor variables can be passed as query parameters.""" with connection.cursor() as cursor: var = cursor.var(str) cursor.execute("BEGIN %s := 'X'; END; ", [var]) - self.assertEqual(var.getvalue(), 'X') + self.assertEqual(var.getvalue(), "X") def test_order_of_nls_parameters(self): """ @@ -53,41 +53,46 @@ class Tests(unittest.TestCase): """Boolean fields have check constraints on their values.""" for field in (BooleanField(), BooleanField(null=True)): with self.subTest(field=field): - field.set_attributes_from_name('is_nice') + field.set_attributes_from_name("is_nice") self.assertIn('"IS_NICE" IN (0,1)', field.db_check(connection)) -@unittest.skipUnless(connection.vendor == 'oracle', 'Oracle tests') +@unittest.skipUnless(connection.vendor == "oracle", "Oracle tests") class TransactionalTests(TransactionTestCase): - available_apps = ['backends'] + available_apps = ["backends"] def test_hidden_no_data_found_exception(self): # "ORA-1403: no data found" exception is hidden by Oracle OCI library # when an INSERT statement is used with a RETURNING clause (see #28859). with connection.cursor() as cursor: # Create trigger that raises "ORA-1403: no data found". - cursor.execute(""" + cursor.execute( + """ CREATE OR REPLACE TRIGGER "TRG_NO_DATA_FOUND" AFTER INSERT ON "BACKENDS_SQUARE" FOR EACH ROW BEGIN RAISE NO_DATA_FOUND; END; - """) + """ + ) try: - with self.assertRaisesMessage(DatabaseError, ( - 'The database did not return a new row id. Probably "ORA-1403: ' - 'no data found" was raised internally but was hidden by the ' - 'Oracle OCI library (see https://code.djangoproject.com/ticket/28859).' - )): + with self.assertRaisesMessage( + DatabaseError, + ( + 'The database did not return a new row id. Probably "ORA-1403: ' + 'no data found" was raised internally but was hidden by the ' + "Oracle OCI library (see https://code.djangoproject.com/ticket/28859)." + ), + ): Square.objects.create(root=2, square=4) finally: with connection.cursor() as cursor: cursor.execute('DROP TRIGGER "TRG_NO_DATA_FOUND"') def test_password_with_at_sign(self): - old_password = connection.settings_dict['PASSWORD'] - connection.settings_dict['PASSWORD'] = 'p@ssword' + old_password = connection.settings_dict["PASSWORD"] + connection.settings_dict["PASSWORD"] = "p@ssword" try: self.assertIn( '/"p@ssword"@', @@ -97,6 +102,6 @@ class TransactionalTests(TransactionTestCase): connection.cursor() # Database exception: "ORA-01017: invalid username/password" is # expected. - self.assertIn('ORA-01017', context.exception.args[0].message) + self.assertIn("ORA-01017", context.exception.args[0].message) finally: - connection.settings_dict['PASSWORD'] = old_password + connection.settings_dict["PASSWORD"] = old_password |
