def test_1122_connection_attributes(self):
     "1122 - test connection attribute values"
     connection = oracledb.connect(test_env.get_main_user(),
                                   test_env.get_main_password(),
                                   test_env.get_connect_string(),
                                   encoding="ASCII")
     self.assertEqual(connection.maxBytesPerCharacter, 1)
     connection = oracledb.connect(test_env.get_main_user(),
                                   test_env.get_main_password(),
                                   test_env.get_connect_string(),
                                   encoding="UTF-8")
     self.assertEqual(connection.maxBytesPerCharacter, 4)
     if test_env.get_client_version() >= (12, 1):
         self.assertEqual(connection.ltxid, b'')
     self.assertEqual(connection.current_schema, None)
     connection.current_schema = "test_schema"
     self.assertEqual(connection.current_schema, "test_schema")
     self.assertEqual(connection.edition, None)
     connection.external_name = "test_external"
     self.assertEqual(connection.external_name, "test_external")
     connection.internal_name = "test_internal"
     self.assertEqual(connection.internal_name, "test_internal")
     connection.stmtcachesize = 30
     self.assertEqual(connection.stmtcachesize, 30)
     self.assertRaises(TypeError, connection.stmtcachesize, 20.5)
     self.assertRaises(TypeError, connection.stmtcachesize, "value")
 def test_2407_heterogeneous(self):
     "2407 - test heterogeneous pool with user and password specified"
     pool = test_env.get_pool(min=2,
                              max=8,
                              increment=3,
                              homogeneous=False,
                              getmode=oracledb.SPOOL_ATTRVAL_WAIT)
     self.assertEqual(pool.homogeneous, 0)
     conn = pool.acquire()
     self.__verify_connection(pool.acquire(), test_env.get_main_user())
     conn.close()
     conn = pool.acquire(test_env.get_main_user(),
                         test_env.get_main_password())
     self.__verify_connection(conn, test_env.get_main_user())
     conn.close()
     conn = pool.acquire(test_env.get_proxy_user(),
                         test_env.get_proxy_password())
     self.__verify_connection(conn, test_env.get_proxy_user())
     conn.close()
     user_str = "%s[%s]" % \
             (test_env.get_main_user(), test_env.get_proxy_user())
     conn = pool.acquire(user_str, test_env.get_main_password())
     self.__verify_connection(conn, test_env.get_proxy_user(),
                              test_env.get_main_user())
     conn.close()
 def test_1105_bad_connect_string(self):
     "1105 - connection to database with bad connect string"
     self.assertRaises(oracledb.DatabaseError, oracledb.connect,
                       test_env.get_main_user())
     self.assertRaises(oracledb.DatabaseError, oracledb.connect,
                       test_env.get_main_user() + "@" + \
                       test_env.get_connect_string())
     self.assertRaises(oracledb.DatabaseError, oracledb.connect,
                       test_env.get_main_user() + "@" + \
                       test_env.get_connect_string() + "/" + \
                       test_env.get_main_password())
 def test_1115_single_arg(self):
     "1115 - connection to database with user, password, DSN together"
     arg = "%s/%s@%s" % (test_env.get_main_user(),
                         test_env.get_main_password(),
                         test_env.get_connect_string())
     connection = oracledb.connect(arg)
     self.__verify_args(connection)
 def test_3224_update_with_batch_error(self):
     "3224 - test executing in succession with batch error"
     self.cursor.execute("truncate table TestArrayDML")
     rows = [(1, "First", 100), (2, "Second", 200), (3, "Third", 300),
             (4, "Second", 300), (5, "Fifth", 300), (6, "Sixth", 400),
             (6, "Seventh", 400), (8, "Eighth", 100)]
     sql = "insert into TestArrayDML (IntCol, StringCol, IntCol2) " \
             "values (:1, :2, :3)"
     self.cursor.executemany(sql, rows, batcherrors=True)
     user = test_env.get_main_user()
     expected_errors = [
         ( 6, 1, "ORA-00001: unique constraint " \
                 "(%s.TESTARRAYDML_PK) violated" % user.upper())
     ]
     actual_errors = [(e.offset, e.code, e.message) \
                     for e in self.cursor.getbatcherrors()]
     self.assertEqual(actual_errors, expected_errors)
     rows = [(101, "First"), (201, "Second"), (3000, "Third"),
             (900, "Ninth"), (301, "Third")]
     sql = "update TestArrayDML set IntCol2 = :1 where StringCol = :2"
     self.cursor.executemany(sql,
                             rows,
                             arraydmlrowcounts=True,
                             batcherrors=True)
     expected_errors = [
         (2, 1438, "ORA-01438: value larger than specified " \
                   "precision allowed for this column")
     ]
     actual_errors = [(e.offset, e.code, e.message) \
                     for e in self.cursor.getbatcherrors()]
     self.assertEqual(actual_errors, expected_errors)
     self.assertEqual(self.cursor.getarraydmlrowcounts(), [1, 2, 0, 0, 1])
     self.assertEqual(self.cursor.rowcount, 4)
 def test_1131_change_password_during_connect(self):
     "1131 - test changing password during connect"
     connection = test_env.get_connection()
     if self.is_on_oracle_cloud(connection):
         self.skipTest("passwords on Oracle Cloud are strictly controlled")
     sys_random = random.SystemRandom()
     new_password = "".join(sys_random.choice(string.ascii_letters) \
                    for i in range(20))
     connection = oracledb.connect(dsn=test_env.get_connect_string(),
                                   user=test_env.get_main_user(),
                                   password=test_env.get_main_password(),
                                   newpassword=new_password)
     connection = oracledb.connect(dsn=test_env.get_connect_string(),
                                   user=test_env.get_main_user(),
                                   password=new_password)
     connection.changepassword(new_password, test_env.get_main_password())
 def __verify_args(self, connection):
     self.assertEqual(connection.username, test_env.get_main_user(),
                      "user name differs")
     self.assertEqual(connection.tnsentry, test_env.get_connect_string(),
                      "tnsentry differs")
     self.assertEqual(connection.dsn, test_env.get_connect_string(),
                      "dsn differs")
 def test_1106_bad_password(self):
     "1106 - connection to database with bad password"
     self.assertRaises(oracledb.DatabaseError,
                       oracledb.connect,
                       user=test_env.get_main_user(),
                       password=test_env.get_main_password() + "X",
                       dsn=test_env.get_connect_string())
 def test_1102_app_context_negative(self):
     "1102 - test invalid use of application context"
     self.assertRaises(TypeError,
                       oracledb.connect,
                       test_env.get_main_user(),
                       test_env.get_main_password(),
                       test_env.get_connect_string(),
                       appcontext=[('userenv', 'action')])
Example #10
0
 def test_2314_string_format(self):
     "2314 - test object string format"
     obj_type = self.connection.gettype("UDT_OBJECT")
     user = test_env.get_main_user()
     self.assertEqual(str(obj_type),
                      "<cx_Oracle.ObjectType %s.UDT_OBJECT>" % user.upper())
     self.assertEqual(str(obj_type.attributes[0]),
                      "<cx_Oracle.ObjectAttribute NUMBERVALUE>")
 def test_2408_heterogenous_without_user(self):
     "2408 - test heterogeneous pool without user and password specified"
     pool = test_env.get_pool(user="", password="", min=2, max=8,
                              increment=3,
                              getmode=oracledb.SPOOL_ATTRVAL_WAIT,
                              homogeneous=False)
     self.__verify_connection(pool.acquire(test_env.get_main_user(),
                              test_env.get_main_password()),
                              test_env.get_main_user())
     self.__verify_connection(pool.acquire(test_env.get_proxy_user(),
                              test_env.get_proxy_password()),
                              test_env.get_proxy_user())
     user_str = "%s[%s]" % \
             (test_env.get_main_user(), test_env.get_proxy_user())
     self.__verify_connection(pool.acquire(user_str,
                              test_env.get_main_password()),
                              test_env.get_proxy_user(),
                              test_env.get_main_user())
 def test_1110_encodings(self):
     "1110 - connection with only encoding/nencoding specified should work"
     connection = oracledb.connect(test_env.get_main_user(),
                                   test_env.get_main_password(),
                                   test_env.get_connect_string())
     encoding = connection.encoding
     nencoding = connection.nencoding
     alt_encoding = "ISO-8859-1"
     connection = oracledb.connect(test_env.get_main_user(),
                                   test_env.get_main_password(),
                                   test_env.get_connect_string(),
                                   encoding=alt_encoding)
     self.assertEqual(connection.encoding, alt_encoding)
     self.assertEqual(connection.nencoding, nencoding)
     connection = oracledb.connect(test_env.get_main_user(),
                                   test_env.get_main_password(),
                                   test_env.get_connect_string(),
                                   nencoding=alt_encoding)
     self.assertEqual(connection.encoding, encoding)
     self.assertEqual(connection.nencoding, alt_encoding)
 def test_1111_different_encodings(self):
     "1111 - different encodings can be specified for encoding/nencoding"
     connection = oracledb.connect(test_env.get_main_user(),
                                   test_env.get_main_password(),
                                   test_env.get_connect_string(),
                                   encoding="UTF-8",
                                   nencoding="UTF-16")
     value = "\u03b4\u4e2a"
     cursor = connection.cursor()
     nchar_var = cursor.var(oracledb.DB_TYPE_NVARCHAR, 100)
     nchar_var.setvalue(0, value)
     cursor.execute("select :value from dual", value=nchar_var)
     result, = cursor.fetchone()
     self.assertEqual(result, value)
 def test_1109_parse_password(self):
     "1109 - test connecting with password containing / and @ symbols"
     connection = test_env.get_connection()
     if self.is_on_oracle_cloud(connection):
         self.skipTest("passwords on Oracle Cloud are strictly controlled")
     sys_random = random.SystemRandom()
     chars = list(
         sys_random.choice(string.ascii_letters) for i in range(20))
     chars[4] = "/"
     chars[8] = "@"
     new_password = "".join(chars)
     connection.changepassword(test_env.get_main_password(), new_password)
     try:
         arg = "%s/%s@%s" % (test_env.get_main_user(), new_password,
                             test_env.get_connect_string())
         oracledb.connect(arg)
     finally:
         connection.changepassword(new_password,
                                   test_env.get_main_password())
    def test_1133_current_schema(self):
        "1133 - test current_schame is set properly"
        conn = test_env.get_connection()
        self.assertEqual(conn.current_schema, None)

        user = test_env.get_main_user().upper()
        proxy_user = test_env.get_proxy_user().upper()
        cursor = conn.cursor()
        cursor.execute(f'alter session set current_schema={proxy_user}')
        self.assertEqual(conn.current_schema, proxy_user)

        conn.current_schema = user
        self.assertEqual(conn.current_schema, user)

        cursor.execute("""
                select sys_context('userenv', 'current_schema')
                from dual""")
        result, = cursor.fetchone()
        self.assertEqual(result, user)
    def test_1130_cancel(self):
        "1130 - test connection cancel"
        conn = test_env.get_connection()

        def perform_cancel():
            time.sleep(0.1)
            conn.cancel()

        thread = threading.Thread(target=perform_cancel)
        thread.start()
        try:
            with conn.cursor() as cursor:
                self.assertRaises(oracledb.OperationalError, cursor.callproc,
                                  test_env.get_sleep_proc_name(), [2])
        finally:
            thread.join()
        with conn.cursor() as cursor:
            cursor.execute("select user from dual")
            user, = cursor.fetchone()
            self.assertEqual(user, test_env.get_main_user().upper())
Example #17
0
 def test_1915_nclob_different_encodings(self):
     "1915 - test binding and fetching NCLOB data (different encodings)"
     connection = oracledb.connect(test_env.get_main_user(),
                                   test_env.get_main_password(),
                                   test_env.get_connect_string(),
                                   encoding="UTF-8",
                                   nencoding="UTF-16")
     value = "\u03b4\u4e2a"
     cursor = connection.cursor()
     cursor.execute("truncate table TestNCLOBs")
     cursor.setinputsizes(val=oracledb.DB_TYPE_NVARCHAR)
     cursor.execute("insert into TestNCLOBs values (1, :val)", val=value)
     cursor.execute("select NCLOBCol from TestNCLOBs")
     nclob, = cursor.fetchone()
     cursor.setinputsizes(val=oracledb.DB_TYPE_NVARCHAR)
     cursor.execute("update TestNCLOBs set NCLOBCol = :val",
                    val=nclob.read() + value)
     cursor.execute("select NCLOBCol from TestNCLOBs")
     nclob, = cursor.fetchone()
     self.assertEqual(nclob.read(), value + value)
 def test_2400_pool(self):
     "2400 - test that the pool is created and has the right attributes"
     pool = test_env.get_pool(min=2, max=8, increment=3,
                              getmode=oracledb.SPOOL_ATTRVAL_WAIT)
     self.assertEqual(pool.username, test_env.get_main_user(),
                      "user name differs")
     self.assertEqual(pool.tnsentry, test_env.get_connect_string(),
                      "tnsentry differs")
     self.assertEqual(pool.dsn, test_env.get_connect_string(),
                      "dsn differs")
     self.assertEqual(pool.max, 8, "max differs")
     self.assertEqual(pool.min, 2, "min differs")
     self.assertEqual(pool.increment, 3, "increment differs")
     self.assertEqual(pool.opened, 2, "opened differs")
     self.assertEqual(pool.busy, 0, "busy not 0 at start")
     connection_1 = pool.acquire()
     self.assertEqual(pool.busy, 1, "busy not 1 after acquire")
     self.assertEqual(pool.opened, 2, "opened not unchanged (1)")
     connection_2 = pool.acquire()
     self.assertEqual(pool.busy, 2, "busy not 2 after acquire")
     self.assertEqual(pool.opened, 2, "opened not unchanged (2)")
     connection_3 = pool.acquire()
     self.assertEqual(pool.busy, 3, "busy not 3 after acquire")
     self.assertEqual(pool.opened, 5, "opened not changed (3)")
     pool.release(connection_3)
     self.assertEqual(pool.busy, 2, "busy not 2 after release")
     del connection_2
     self.assertEqual(pool.busy, 1, "busy not 1 after del")
     pool.getmode = oracledb.SPOOL_ATTRVAL_NOWAIT
     self.assertEqual(pool.getmode, oracledb.SPOOL_ATTRVAL_NOWAIT)
     if test_env.get_client_version() >= (12, 2):
         pool.getmode = oracledb.SPOOL_ATTRVAL_TIMEDWAIT
         self.assertEqual(pool.getmode, oracledb.SPOOL_ATTRVAL_TIMEDWAIT)
     pool.stmtcachesize = 50
     self.assertEqual(pool.stmtcachesize, 50)
     pool.timeout = 10
     self.assertEqual(pool.timeout, 10)
     if test_env.get_client_version() >= (12, 1):
         pool.max_lifetime_session = 10
         self.assertEqual(pool.max_lifetime_session, 10)
 def test_3222_insert_with_batch_error(self):
     "3222 - test executing insert with multiple distinct batch errors"
     self.cursor.execute("truncate table TestArrayDML")
     rows = [(1, "First", 100), (2, "Second", 200), (2, "Third", 300),
             (4, "Fourth", 400), (5, "Fourth", 1000)]
     sql = "insert into TestArrayDML (IntCol, StringCol, IntCol2) " \
             "values (:1, :2, :3)"
     self.cursor.executemany(sql,
                             rows,
                             batcherrors=True,
                             arraydmlrowcounts=True)
     user = test_env.get_main_user()
     expected_errors = [
         ( 4, 1438, "ORA-01438: value larger than specified " \
                    "precision allowed for this column" ),
         ( 2, 1, "ORA-00001: unique constraint " \
                 "(%s.TESTARRAYDML_PK) violated" % user.upper())
     ]
     actual_errors = [(e.offset, e.code, e.message) \
                     for e in self.cursor.getbatcherrors()]
     self.assertEqual(actual_errors, expected_errors)
     self.assertEqual(self.cursor.getarraydmlrowcounts(), [1, 1, 0, 1, 0])
 def test_1252_string_format(self):
     "1252 - test string format of cursor"
     format_string = "<cx_Oracle.Cursor on <cx_Oracle.Connection to %s@%s>>"
     expected_value = format_string % \
             (test_env.get_main_user(), test_env.get_connect_string())
     self.assertEqual(str(self.cursor), expected_value)
 def test_1120_string_format(self):
     "1120 - test string format of connection"
     connection = test_env.get_connection()
     expected_value = "<cx_Oracle.Connection to %s@%s>" % \
             (test_env.get_main_user(), test_env.get_connect_string())
     self.assertEqual(str(connection), expected_value)
    def test_3000_subscription(self):
        "3000 - test Subscription for insert, update, delete and truncate"

        # skip if running on the Oracle Cloud, which does not support
        # subscriptions currently
        if self.is_on_oracle_cloud():
            message = "Oracle Cloud does not support subscriptions currently"
            self.skipTest(message)

        # truncate table in order to run test in known state
        self.cursor.execute("truncate table TestTempTable")

        # expected values
        table_operations = [
            oracledb.OPCODE_INSERT, oracledb.OPCODE_UPDATE,
            oracledb.OPCODE_INSERT, oracledb.OPCODE_DELETE,
            oracledb.OPCODE_ALTER | oracledb.OPCODE_ALLROWS
        ]
        row_operations = [
            oracledb.OPCODE_INSERT, oracledb.OPCODE_UPDATE,
            oracledb.OPCODE_INSERT, oracledb.OPCODE_DELETE
        ]
        rowids = []

        # set up subscription
        data = SubscriptionData(5)
        connection = test_env.get_connection(threaded=True, events=True)
        sub = connection.subscribe(callback=data.CallbackHandler,
                                   timeout=10,
                                   qos=oracledb.SUBSCR_QOS_ROWIDS)
        sub.registerquery("select * from TestTempTable")
        connection.autocommit = True
        cursor = connection.cursor()

        # insert statement
        cursor.execute("""
                insert into TestTempTable (IntCol, StringCol)
                values (1, 'test')""")
        cursor.execute("select rowid from TestTempTable where IntCol = 1")
        rowids.extend(r for r, in cursor)

        # update statement
        cursor.execute("""
                update TestTempTable set
                    StringCol = 'update'
                where IntCol = 1""")
        cursor.execute("select rowid from TestTempTable where IntCol = 1")
        rowids.extend(r for r, in cursor)

        # second insert statement
        cursor.execute("""
                insert into TestTempTable (IntCol, StringCol)
                values (2, 'test2')""")
        cursor.execute("select rowid from TestTempTable where IntCol = 2")
        rowids.extend(r for r, in cursor)

        # delete statement
        cursor.execute("delete TestTempTable where IntCol = 2")
        rowids.append(rowids[-1])

        # truncate table
        cursor.execute("truncate table TestTempTable")

        # wait for all messages to be sent
        data.condition.acquire()
        data.condition.wait(10)

        # verify the correct messages were sent
        self.assertEqual(data.table_operations, table_operations)
        self.assertEqual(data.row_operations, row_operations)
        self.assertEqual(data.rowids, rowids)

        # test string format of subscription object is as expected
        fmt = "<cx_Oracle.Subscription on <cx_Oracle.Connection to %s@%s>>"
        expected = fmt % \
                (test_env.get_main_user(), test_env.get_connect_string())
        self.assertEqual(str(sub), expected)
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
# SetupTest.py
#
# Creates users and populates their schemas with the tables and packages
# necessary for the cx_Oracle test suite.
#------------------------------------------------------------------------------

import cx_Oracle

import test_env
import DropTest

# connect as administrative user (usually SYSTEM or ADMIN)
conn = cx_Oracle.connect(test_env.get_admin_connect_string())

# drop existing users and editions, if applicable
DropTest.drop_tests(conn)

# create test schemas
print("Creating test schemas...")
test_env.run_sql_script(conn,
                        "SetupTest",
                        main_user=test_env.get_main_user(),
                        main_password=test_env.get_main_password(),
                        proxy_user=test_env.get_proxy_user(),
                        proxy_password=test_env.get_proxy_password())
print("Done.")
Example #24
0
def drop_tests(conn):
    print("Dropping test schemas...")
    test_env.run_sql_script(conn,
                            "drop_test",
                            main_user=test_env.get_main_user(),
                            proxy_user=test_env.get_proxy_user())