def __verify_args(self, connection): self.assertEqual(connection.username, base.get_main_user(), "user name differs") self.assertEqual(connection.tnsentry, base.get_connect_string(), "tnsentry differs") self.assertEqual(connection.dsn, base.get_connect_string(), "dsn differs")
def test_1122_connection_attributes(self): "1122 - test connection attribute values" connection = oracledb.connect(base.get_main_user(), base.get_main_password(), base.get_connect_string(), encoding="ASCII") self.assertEqual(connection.maxBytesPerCharacter, 1) connection = oracledb.connect(base.get_main_user(), base.get_main_password(), base.get_connect_string(), encoding="UTF-8") self.assertEqual(connection.maxBytesPerCharacter, 4) if base.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_1105_bad_connect_string(self): "1105 - connection to database with bad connect string" self.assertRaises(oracledb.DatabaseError, oracledb.connect, base.get_main_user()) self.assertRaises(oracledb.DatabaseError, oracledb.connect, base.get_main_user() + "@" + \ base.get_connect_string()) self.assertRaises(oracledb.DatabaseError, oracledb.connect, base.get_main_user() + "@" + \ base.get_connect_string() + "/" + \ base.get_main_password())
def test_1110_encodings(self): "1110 - connection with only encoding/nencoding specified should work" connection = oracledb.connect(base.get_main_user(), base.get_main_password(), base.get_connect_string()) encoding = connection.encoding nencoding = connection.nencoding alt_encoding = "ISO-8859-1" connection = oracledb.connect(base.get_main_user(), base.get_main_password(), base.get_connect_string(), encoding=alt_encoding) self.assertEqual(connection.encoding, alt_encoding) self.assertEqual(connection.nencoding, nencoding) connection = oracledb.connect(base.get_main_user(), base.get_main_password(), base.get_connect_string(), nencoding=alt_encoding) self.assertEqual(connection.encoding, encoding) self.assertEqual(connection.nencoding, alt_encoding)
def test_1107_change_password(self): "1107 - test changing password" connection = base.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.changepassword(base.get_main_password(), new_password) cconnection = oracledb.connect(base.get_main_user(), new_password, base.get_connect_string()) connection.changepassword(new_password, base.get_main_password())
def test_2400_pool(self): "2400 - test that the pool is created and has the right attributes" pool = base.get_pool(min=2, max=8, increment=3, getmode=oracledb.SPOOL_ATTRVAL_WAIT) self.assertEqual(pool.username, base.get_main_user(), "user name differs") self.assertEqual(pool.tnsentry, base.get_connect_string(), "tnsentry differs") self.assertEqual(pool.dsn, base.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 base.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 base.get_client_version() >= (12, 1): pool.max_lifetime_session = 10 self.assertEqual(pool.max_lifetime_session, 10)
def test_1111_different_encodings(self): "1111 - different encodings can be specified for encoding/nencoding" connection = oracledb.connect(base.get_main_user(), base.get_main_password(), base.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 = base.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(base.get_main_password(), new_password) try: arg = "%s/%s@%s" % (base.get_main_user(), new_password, base.get_connect_string()) oracledb.connect(arg) finally: connection.changepassword(new_password, base.get_main_password())
def test_1915_nclob_different_encodings(self): "1915 - test binding and fetching NCLOB data (different encodings)" connection = oracledb.connect(base.get_main_user(), base.get_main_password(), base.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_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 = base.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 % (base.get_main_user(), base.get_connect_string()) self.assertEqual(str(sub), expected)
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 % \ (base.get_main_user(), base.get_connect_string()) self.assertEqual(str(self.cursor), expected_value)
def test_1102_app_context_negative(self): "1102 - test invalid use of application context" self.assertRaises(TypeError, oracledb.connect, base.get_main_user(), base.get_main_password(), base.get_connect_string(), appcontext=[('userenv', 'action')])
def test_1120_string_format(self): "1120 - test string format of connection" connection = base.get_connection() expected_value = "<cx_Oracle.Connection to %s@%s>" % \ (base.get_main_user(), base.get_connect_string()) self.assertEqual(str(connection), expected_value)
def test_1115_single_arg(self): "1115 - connection to database with user, password, DSN together" arg = "%s/%s@%s" % (base.get_main_user(), base.get_main_password(), base.get_connect_string()) connection = oracledb.connect(arg) self.__verify_args(connection)
def test_1106_bad_password(self): "1106 - connection to database with bad password" self.assertRaises(oracledb.DatabaseError, oracledb.connect, base.get_main_user(), base.get_main_password() + "X", base.get_connect_string())