1. import unittest
    
  2. from unittest import mock
    
  3. 
    
  4. from django.db import DatabaseError, NotSupportedError, connection
    
  5. from django.db.models import BooleanField
    
  6. from django.test import TestCase, TransactionTestCase
    
  7. 
    
  8. from ..models import Square, VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    
  9. 
    
  10. 
    
  11. @unittest.skipUnless(connection.vendor == "oracle", "Oracle tests")
    
  12. class Tests(TestCase):
    
  13.     def test_quote_name(self):
    
  14.         """'%' chars are escaped for query execution."""
    
  15.         name = '"SOME%NAME"'
    
  16.         quoted_name = connection.ops.quote_name(name)
    
  17.         self.assertEqual(quoted_name % (), name)
    
  18. 
    
  19.     def test_quote_name_db_table(self):
    
  20.         model = VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    
  21.         db_table = model._meta.db_table.upper()
    
  22.         self.assertEqual(
    
  23.             f'"{db_table}"',
    
  24.             connection.ops.quote_name(
    
  25.                 "backends_verylongmodelnamezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",
    
  26.             ),
    
  27.         )
    
  28. 
    
  29.     def test_dbms_session(self):
    
  30.         """A stored procedure can be called through a cursor wrapper."""
    
  31.         with connection.cursor() as cursor:
    
  32.             cursor.callproc("DBMS_SESSION.SET_IDENTIFIER", ["_django_testing!"])
    
  33. 
    
  34.     def test_cursor_var(self):
    
  35.         """Cursor variables can be passed as query parameters."""
    
  36.         with connection.cursor() as cursor:
    
  37.             var = cursor.var(str)
    
  38.             cursor.execute("BEGIN %s := 'X'; END; ", [var])
    
  39.             self.assertEqual(var.getvalue(), "X")
    
  40. 
    
  41.     def test_order_of_nls_parameters(self):
    
  42.         """
    
  43.         An 'almost right' datetime works with configured NLS parameters
    
  44.         (#18465).
    
  45.         """
    
  46.         with connection.cursor() as cursor:
    
  47.             query = "select 1 from dual where '1936-12-29 00:00' < sysdate"
    
  48.             # The query succeeds without errors - pre #18465 this
    
  49.             # wasn't the case.
    
  50.             cursor.execute(query)
    
  51.             self.assertEqual(cursor.fetchone()[0], 1)
    
  52. 
    
  53.     def test_boolean_constraints(self):
    
  54.         """Boolean fields have check constraints on their values."""
    
  55.         for field in (BooleanField(), BooleanField(null=True)):
    
  56.             with self.subTest(field=field):
    
  57.                 field.set_attributes_from_name("is_nice")
    
  58.                 self.assertIn('"IS_NICE" IN (0,1)', field.db_check(connection))
    
  59. 
    
  60.     @mock.patch.object(
    
  61.         connection,
    
  62.         "get_database_version",
    
  63.         return_value=(18, 1),
    
  64.     )
    
  65.     def test_check_database_version_supported(self, mocked_get_database_version):
    
  66.         msg = "Oracle 19 or later is required (found 18.1)."
    
  67.         with self.assertRaisesMessage(NotSupportedError, msg):
    
  68.             connection.check_database_version_supported()
    
  69.         self.assertTrue(mocked_get_database_version.called)
    
  70. 
    
  71. 
    
  72. @unittest.skipUnless(connection.vendor == "oracle", "Oracle tests")
    
  73. class TransactionalTests(TransactionTestCase):
    
  74.     available_apps = ["backends"]
    
  75. 
    
  76.     def test_hidden_no_data_found_exception(self):
    
  77.         # "ORA-1403: no data found" exception is hidden by Oracle OCI library
    
  78.         # when an INSERT statement is used with a RETURNING clause (see #28859).
    
  79.         with connection.cursor() as cursor:
    
  80.             # Create trigger that raises "ORA-1403: no data found".
    
  81.             cursor.execute(
    
  82.                 """
    
  83.                 CREATE OR REPLACE TRIGGER "TRG_NO_DATA_FOUND"
    
  84.                 AFTER INSERT ON "BACKENDS_SQUARE"
    
  85.                 FOR EACH ROW
    
  86.                 BEGIN
    
  87.                     RAISE NO_DATA_FOUND;
    
  88.                 END;
    
  89.             """
    
  90.             )
    
  91.         try:
    
  92.             with self.assertRaisesMessage(
    
  93.                 DatabaseError,
    
  94.                 (
    
  95.                     'The database did not return a new row id. Probably "ORA-1403: no '
    
  96.                     'data found" was raised internally but was hidden by the Oracle '
    
  97.                     "OCI library (see https://code.djangoproject.com/ticket/28859)."
    
  98.                 ),
    
  99.             ):
    
  100.                 Square.objects.create(root=2, square=4)
    
  101.         finally:
    
  102.             with connection.cursor() as cursor:
    
  103.                 cursor.execute('DROP TRIGGER "TRG_NO_DATA_FOUND"')
    
  104. 
    
  105.     def test_password_with_at_sign(self):
    
  106.         old_password = connection.settings_dict["PASSWORD"]
    
  107.         connection.settings_dict["PASSWORD"] = "p@ssword"
    
  108.         try:
    
  109.             self.assertIn(
    
  110.                 '/"p@ssword"@',
    
  111.                 connection.client.connect_string(connection.settings_dict),
    
  112.             )
    
  113.             with self.assertRaises(DatabaseError) as context:
    
  114.                 connection.cursor()
    
  115.             # Database exception: "ORA-01017: invalid username/password" is
    
  116.             # expected.
    
  117.             self.assertIn("ORA-01017", context.exception.args[0].message)
    
  118.         finally:
    
  119.             connection.settings_dict["PASSWORD"] = old_password