diff options
author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2017-02-11 21:37:49 +0100 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2017-06-21 12:00:47 -0400 |
commit | 8cb1b1fd8e529d1896daeb089ea726109e0ba4f7 (patch) | |
tree | 3337abead0afac6a46b0ccfd48bc530aec3717de /tests/backends/oracle | |
parent | 0f91ba1adc037345474749faa64d36a4077b3fb8 (diff) | |
download | django-8cb1b1fd8e529d1896daeb089ea726109e0ba4f7.tar.gz |
Reorganized backends tests.
Diffstat (limited to 'tests/backends/oracle')
-rw-r--r-- | tests/backends/oracle/__init__.py | 0 | ||||
-rw-r--r-- | tests/backends/oracle/test_creation.py | 76 | ||||
-rw-r--r-- | tests/backends/oracle/tests.py | 55 |
3 files changed, 131 insertions, 0 deletions
diff --git a/tests/backends/oracle/__init__.py b/tests/backends/oracle/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/backends/oracle/__init__.py diff --git a/tests/backends/oracle/test_creation.py b/tests/backends/oracle/test_creation.py new file mode 100644 index 0000000000..1688c4efd2 --- /dev/null +++ b/tests/backends/oracle/test_creation.py @@ -0,0 +1,76 @@ +import unittest +from io import StringIO +from unittest import mock + +from django.db import connection +from django.db.backends.oracle.creation import DatabaseCreation +from django.db.utils import DatabaseError +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) +class DatabaseCreationTests(TestCase): + + 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") + + def _execute_raise_tablespace_already_exists( + self, cursor, statements, parameters, verbosity, allow_quiet_fail=False + ): + raise DatabaseError("ORA-01543: tablespace 'string' already exists") + + def _execute_raise_insufficient_privileges( + self, cursor, statements, parameters, verbosity, allow_quiet_fail=False + ): + raise DatabaseError("ORA-01031: insufficient privileges") + + def _test_database_passwd(self): + # Mocked to avoid test user password changed + return connection.settings_dict['SAVED_PASSWORD'] + + def patch_execute_statements(self, execute_statements): + return mock.patch.object(DatabaseCreation, '_execute_statements', execute_statements) + + @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.assertRaises(SystemExit): + # SystemExit is raised if the user answers "no" to the + # prompt asking if it's okay to delete the test tablespace. + creation._create_test_db(verbosity=0, keepdb=False) + # "Tablespace already exists" error is ignored when keepdb is on + creation._create_test_db(verbosity=0, keepdb=True) + # Simulate test database creation raising unexpected error + 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) + + @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): + # 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 self.assertRaises(SystemExit): + # SystemExit is raised if the user answers "no" to the + # prompt asking if it's okay to delete the test user. + creation._create_test_db(verbosity=0, keepdb=False) + # "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.assertRaises(SystemExit): + creation._create_test_db(verbosity=0, keepdb=False) + with self.assertRaises(SystemExit): + creation._create_test_db(verbosity=0, keepdb=True) diff --git a/tests/backends/oracle/tests.py b/tests/backends/oracle/tests.py new file mode 100644 index 0000000000..d57d91e677 --- /dev/null +++ b/tests/backends/oracle/tests.py @@ -0,0 +1,55 @@ +import unittest + +from django.db import connection + + +@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"' + quoted_name = connection.ops.quote_name(name) + self.assertEqual(quoted_name % (), name) + + 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!']) + + def test_cursor_var(self): + """Cursor variables can be passed as query parameters.""" + from django.db.backends.oracle.base import Database + with connection.cursor() as cursor: + var = cursor.var(Database.STRING) + cursor.execute("BEGIN %s := 'X'; END; ", [var]) + self.assertEqual(var.getvalue(), 'X') + + def test_long_string(self): + """Text longer than 4000 chars can be saved and read.""" + with connection.cursor() as cursor: + cursor.execute('CREATE TABLE ltext ("TEXT" NCLOB)') + long_str = ''.join(str(x) for x in range(4000)) + cursor.execute('INSERT INTO ltext VALUES (%s)', [long_str]) + cursor.execute('SELECT text FROM ltext') + row = cursor.fetchone() + self.assertEqual(long_str, row[0].read()) + cursor.execute('DROP TABLE ltext') + + def test_client_encoding(self): + """Client encoding is set correctly.""" + connection.ensure_connection() + self.assertEqual(connection.connection.encoding, 'UTF-8') + self.assertEqual(connection.connection.nencoding, 'UTF-8') + + def test_order_of_nls_parameters(self): + """ + An 'almost right' datetime works with configured NLS parameters + (#18465). + """ + with connection.cursor() as cursor: + query = "select 1 from dual where '1936-12-29 00:00' < sysdate" + # The query succeeds without errors - pre #18465 this + # wasn't the case. + cursor.execute(query) + self.assertEqual(cursor.fetchone()[0], 1) |