1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
|
#------------------------------------------------------------------------------
# Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
#
# Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved.
#
# Portions Copyright 2001-2007, Computronix (Canada) Ltd., Edmonton, Alberta,
# Canada. All rights reserved.
#------------------------------------------------------------------------------
"""
3200 - Module for testing features introduced in 12.1
"""
import datetime
import unittest
import cx_Oracle as oracledb
import test_env
@unittest.skipUnless(test_env.get_client_version() >= (12, 1),
"unsupported client")
class TestCase(test_env.BaseTestCase):
def test_3200_array_dml_row_counts_off(self):
"3200 - test executing with arraydmlrowcounts mode disabled"
self.cursor.execute("truncate table TestArrayDML")
rows = [(1, "First"), (2, "Second")]
sql = "insert into TestArrayDML (IntCol,StringCol) values (:1,:2)"
self.cursor.executemany(sql, rows, arraydmlrowcounts=False)
self.assertRaises(oracledb.DatabaseError,
self.cursor.getarraydmlrowcounts)
rows = [(3, "Third"), (4, "Fourth")]
self.cursor.executemany(sql, rows)
self.assertRaises(oracledb.DatabaseError,
self.cursor.getarraydmlrowcounts)
def test_3201_array_dml_row_counts_on(self):
"3201 - test executing with arraydmlrowcounts mode enabled"
self.cursor.execute("truncate table TestArrayDML")
rows = [
(1, "First", 100),
(2, "Second", 200),
(3, "Third", 300),
(4, "Fourth", 300),
(5, "Fifth", 300)
]
sql = "insert into TestArrayDML (IntCol,StringCol,IntCol2) " \
"values (:1,:2,:3)"
self.cursor.executemany(sql, rows, arraydmlrowcounts=True)
self.connection.commit()
self.assertEqual(self.cursor.getarraydmlrowcounts(), [1, 1, 1, 1, 1])
self.cursor.execute("select count(*) from TestArrayDML")
count, = self.cursor.fetchone()
self.assertEqual(count, len(rows))
def test_3202_bind_plsql_boolean_collection_in(self):
"3202 - test binding a boolean collection (in)"
type_obj = self.connection.gettype("PKG_TESTBOOLEANS.UDT_BOOLEANLIST")
obj = type_obj.newobject()
obj.setelement(1, True)
obj.extend([True, False, True, True, False, True])
result = self.cursor.callfunc("pkg_TestBooleans.TestInArrays", int,
(obj,))
self.assertEqual(result, 5)
def test_3203_bind_plsql_boolean_collection_out(self):
"3203 - test binding a boolean collection (out)"
type_obj = self.connection.gettype("PKG_TESTBOOLEANS.UDT_BOOLEANLIST")
obj = type_obj.newobject()
self.cursor.callproc("pkg_TestBooleans.TestOutArrays", (6, obj))
self.assertEqual(obj.aslist(), [True, False, True, False, True, False])
def test_3204_bind_plql_date_collection_in(self):
"3204 - test binding a PL/SQL date collection (in)"
type_obj = self.connection.gettype("PKG_TESTDATEARRAYS.UDT_DATELIST")
obj = type_obj.newobject()
obj.setelement(1, datetime.datetime(2016, 2, 5))
obj.append(datetime.datetime(2016, 2, 8, 12, 15, 30))
obj.append(datetime.datetime(2016, 2, 12, 5, 44, 30))
result = self.cursor.callfunc("pkg_TestDateArrays.TestInArrays",
oracledb.NUMBER,
(2, datetime.datetime(2016, 2, 1), obj))
self.assertEqual(result, 24.75)
def test_3205_bind_plqsl_date_collection_in_out(self):
"3205 - test binding a PL/SQL date collection (in/out)"
type_obj = self.connection.gettype("PKG_TESTDATEARRAYS.UDT_DATELIST")
obj = type_obj.newobject()
obj.setelement(1, datetime.datetime(2016, 1, 1))
obj.append(datetime.datetime(2016, 1, 7))
obj.append(datetime.datetime(2016, 1, 13))
obj.append(datetime.datetime(2016, 1, 19))
self.cursor.callproc("pkg_TestDateArrays.TestInOutArrays", (4, obj))
expected_values = [
datetime.datetime(2016, 1, 8),
datetime.datetime(2016, 1, 14),
datetime.datetime(2016, 1, 20),
datetime.datetime(2016, 1, 26)
]
self.assertEqual(obj.aslist(), expected_values)
def test_3206_bind_plsql_date_collection_out(self):
"3206 - test binding a PL/SQL date collection (out)"
type_obj = self.connection.gettype("PKG_TESTDATEARRAYS.UDT_DATELIST")
obj = type_obj.newobject()
self.cursor.callproc("pkg_TestDateArrays.TestOutArrays", (3, obj))
expected_values = [
datetime.datetime(2002, 12, 13, 4, 48),
datetime.datetime(2002, 12, 14, 9, 36),
datetime.datetime(2002, 12, 15, 14, 24)
]
self.assertEqual(obj.aslist(), expected_values)
def test_3207_bind_plsql_number_collection_in(self):
"3207 - test binding a PL/SQL number collection (in)"
type_name = "PKG_TESTNUMBERARRAYS.UDT_NUMBERLIST"
type_obj = self.connection.gettype(type_name)
obj = type_obj.newobject()
obj.setelement(1, 10)
obj.extend([20, 30, 40, 50])
result = self.cursor.callfunc("pkg_TestNumberArrays.TestInArrays", int,
(5, obj))
self.assertEqual(result, 155)
def test_3208_bind_plsql_number_collection_in_out(self):
"3208 - test binding a PL/SQL number collection (in/out)"
type_name = "PKG_TESTNUMBERARRAYS.UDT_NUMBERLIST"
type_obj = self.connection.gettype(type_name)
obj = type_obj.newobject()
obj.setelement(1, 5)
obj.extend([8, 3, 2])
self.cursor.callproc("pkg_TestNumberArrays.TestInOutArrays", (4, obj))
self.assertEqual(obj.aslist(), [50, 80, 30, 20])
def test_3209_bind_plsql_number_collection_out(self):
"3209 - test binding a PL/SQL number collection (out)"
type_name = "PKG_TESTNUMBERARRAYS.UDT_NUMBERLIST"
type_obj = self.connection.gettype(type_name)
obj = type_obj.newobject()
self.cursor.callproc("pkg_TestNumberArrays.TestOutArrays", (3, obj))
self.assertEqual(obj.aslist(), [100, 200, 300])
def test_3210_bind_plsql_record_array(self):
"3210 - test binding an array of PL/SQL records (in)"
rec_type = self.connection.gettype("PKG_TESTRECORDS.UDT_RECORD")
array_type = self.connection.gettype("PKG_TESTRECORDS.UDT_RECORDARRAY")
array_obj = array_type.newobject()
for i in range(3):
obj = rec_type.newobject()
obj.NUMBERVALUE = i + 1
obj.STRINGVALUE = "String in record #%d" % (i + 1)
obj.DATEVALUE = datetime.datetime(2017, i + 1, 1)
obj.TIMESTAMPVALUE = datetime.datetime(2017, 1, i + 1)
obj.BOOLEANVALUE = (i % 2) == 1
obj.PLSINTEGERVALUE = i * 5
obj.BINARYINTEGERVALUE = i * 2
array_obj.append(obj)
result = self.cursor.callfunc("pkg_TestRecords.TestInArrays", str,
(array_obj,))
self.assertEqual(result,
"udt_Record(1, 'String in record #1', " \
"to_date('2017-01-01', 'YYYY-MM-DD'), " \
"to_timestamp('2017-01-01 00:00:00', " \
"'YYYY-MM-DD HH24:MI:SS'), false, 0, 0); " \
"udt_Record(2, 'String in record #2', " \
"to_date('2017-02-01', 'YYYY-MM-DD'), " \
"to_timestamp('2017-01-02 00:00:00', " \
"'YYYY-MM-DD HH24:MI:SS'), true, 5, 2); " \
"udt_Record(3, 'String in record #3', " \
"to_date('2017-03-01', 'YYYY-MM-DD'), " \
"to_timestamp('2017-01-03 00:00:00', " \
"'YYYY-MM-DD HH24:MI:SS'), false, 10, 4)")
def test_3211_bind_plsql_record_in(self):
"3211 - test binding a PL/SQL record (in)"
type_obj = self.connection.gettype("PKG_TESTRECORDS.UDT_RECORD")
obj = type_obj.newobject()
obj.NUMBERVALUE = 18
obj.STRINGVALUE = "A string in a record"
obj.DATEVALUE = datetime.datetime(2016, 2, 15)
obj.TIMESTAMPVALUE = datetime.datetime(2016, 2, 12, 14, 25, 36)
obj.BOOLEANVALUE = False
obj.PLSINTEGERVALUE = 21
obj.BINARYINTEGERVALUE = 5
result = self.cursor.callfunc("pkg_TestRecords.GetStringRep", str,
(obj,))
self.assertEqual(result,
"udt_Record(18, 'A string in a record', " \
"to_date('2016-02-15', 'YYYY-MM-DD'), " \
"to_timestamp('2016-02-12 14:25:36', " \
"'YYYY-MM-DD HH24:MI:SS'), false, 21, 5)")
def test_3212_bind_plsql_record_out(self):
"3212 - test binding a PL/SQL record (out)"
type_obj = self.connection.gettype("PKG_TESTRECORDS.UDT_RECORD")
obj = type_obj.newobject()
obj.NUMBERVALUE = 5
obj.STRINGVALUE = "Test value"
obj.DATEVALUE = datetime.datetime.today()
obj.TIMESTAMPVALUE = datetime.datetime.today()
obj.BOOLEANVALUE = False
obj.PLSINTEGERVALUE = 23
obj.BINARYINTEGERVALUE = 9
self.cursor.callproc("pkg_TestRecords.TestOut", (obj,))
self.assertEqual(obj.NUMBERVALUE, 25)
self.assertEqual(obj.STRINGVALUE, "String in record")
self.assertEqual(obj.DATEVALUE, datetime.datetime(2016, 2, 16))
self.assertEqual(obj.TIMESTAMPVALUE,
datetime.datetime(2016, 2, 16, 18, 23, 55))
self.assertEqual(obj.BOOLEANVALUE, True)
self.assertEqual(obj.PLSINTEGERVALUE, 45)
self.assertEqual(obj.BINARYINTEGERVALUE, 10)
def test_3213_bind_plsql_string_collection_in(self):
"3213 - test binding a PL/SQL string collection (in)"
type_name = "PKG_TESTSTRINGARRAYS.UDT_STRINGLIST"
type_obj = self.connection.gettype(type_name)
obj = type_obj.newobject()
obj.setelement(1, "First element")
obj.setelement(2, "Second element")
obj.setelement(3, "Third element")
result = self.cursor.callfunc("pkg_TestStringArrays.TestInArrays", int,
(5, obj))
self.assertEqual(result, 45)
def test_3214_bind_plsql_string_collection_in_out(self):
"3214 - test binding a PL/SQL string collection (in/out)"
type_name = "PKG_TESTSTRINGARRAYS.UDT_STRINGLIST"
type_obj = self.connection.gettype(type_name)
obj = type_obj.newobject()
obj.setelement(1, "The first element")
obj.append("The second element")
obj.append("The third and final element")
self.cursor.callproc("pkg_TestStringArrays.TestInOutArrays", (3, obj))
expected_values = [
'Converted element # 1 originally had length 17',
'Converted element # 2 originally had length 18',
'Converted element # 3 originally had length 27'
]
self.assertEqual(obj.aslist(), expected_values)
def test_3215_bind_plsql_string_collection_out(self):
"3215 - test binding a PL/SQL string collection (out)"
type_name = "PKG_TESTSTRINGARRAYS.UDT_STRINGLIST"
type_obj = self.connection.gettype(type_name)
obj = type_obj.newobject()
self.cursor.callproc("pkg_TestStringArrays.TestOutArrays", (4, obj))
expected_values = [
'Test out element # 1',
'Test out element # 2',
'Test out element # 3',
'Test out element # 4'
]
self.assertEqual(obj.aslist(), expected_values)
def test_3216_bind_plsql_string_collection_out_with_holes(self):
"3216 - test binding a PL/SQL string collection (out with holes)"
type_name = "PKG_TESTSTRINGARRAYS.UDT_STRINGLIST"
type_obj = self.connection.gettype(type_name)
obj = type_obj.newobject()
self.cursor.callproc("pkg_TestStringArrays.TestIndexBy", (obj,))
self.assertEqual(obj.first(), -1048576)
self.assertEqual(obj.last(), 8388608)
self.assertEqual(obj.next(-576), 284)
self.assertEqual(obj.prev(284), -576)
self.assertEqual(obj.size(), 4)
self.assertEqual(obj.exists(-576), True)
self.assertEqual(obj.exists(-577), False)
self.assertEqual(obj.getelement(284), 'Third element')
expected_list = [
"First element",
"Second element",
"Third element",
"Fourth element"
]
self.assertEqual(obj.aslist(), expected_list)
expected_dict = {
-1048576: 'First element',
-576: 'Second element',
284: 'Third element',
8388608: 'Fourth element'
}
self.assertEqual(obj.asdict(), expected_dict)
obj.delete(-576)
obj.delete(284)
expected_list.pop(2)
expected_list.pop(1)
self.assertEqual(obj.aslist(), expected_list)
expected_dict.pop(-576)
expected_dict.pop(284)
self.assertEqual(obj.asdict(), expected_dict)
def test_3217_exception_in_iteration(self):
"3217 - test executing with arraydmlrowcounts with exception"
self.cursor.execute("truncate table TestArrayDML")
rows = [
(1, "First"),
(2, "Second"),
(2, "Third"),
(4, "Fourth")
]
sql = "insert into TestArrayDML (IntCol,StringCol) values (:1,:2)"
self.assertRaises(oracledb.DatabaseError, self.cursor.executemany,
sql, rows, arraydmlrowcounts=True)
self.assertEqual(self.cursor.getarraydmlrowcounts(), [1, 1])
def test_3218_executing_delete(self):
"3218 - test executing delete statement with arraydmlrowcount mode"
self.cursor.execute("truncate table TestArrayDML")
rows = [
(1, "First", 100),
(2, "Second", 200),
(3, "Third", 300),
(4, "Fourth", 300),
(5, "Fifth", 300),
(6, "Sixth", 400),
(7, "Seventh", 400),
(8, "Eighth", 500)
]
sql = "insert into TestArrayDML (IntCol,StringCol,IntCol2) " \
"values (:1, :2, :3)"
self.cursor.executemany(sql, rows)
rows = [(200,), (300,), (400,)]
statement = "delete from TestArrayDML where IntCol2 = :1"
self.cursor.executemany(statement, rows, arraydmlrowcounts=True)
self.assertEqual(self.cursor.getarraydmlrowcounts(), [1, 3, 2])
self.assertEqual(self.cursor.rowcount, 6)
def test_3219_executing_update(self):
"3219 - test executing update statement with arraydmlrowcount mode"
self.cursor.execute("truncate table TestArrayDML")
rows = [
(1, "First",100),
(2, "Second",200),
(3, "Third",300),
(4, "Fourth",300),
(5, "Fifth",300),
(6, "Sixth",400),
(7, "Seventh",400),
(8, "Eighth",500)
]
sql = "insert into TestArrayDML (IntCol,StringCol,IntCol2) " \
"values (:1, :2, :3)"
self.cursor.executemany(sql, rows)
rows = [
("One", 100),
("Two", 200),
("Three", 300),
("Four", 400)
]
sql = "update TestArrayDML set StringCol = :1 where IntCol2 = :2"
self.cursor.executemany(sql, rows, arraydmlrowcounts=True)
self.assertEqual(self.cursor.getarraydmlrowcounts(), [1, 1, 3, 2])
self.assertEqual(self.cursor.rowcount, 7)
def test_3220_implicit_results(self):
"3220 - test getimplicitresults() returns the correct data"
self.cursor.execute("""
declare
c1 sys_refcursor;
c2 sys_refcursor;
begin
open c1 for
select NumberCol
from TestNumbers
where IntCol between 3 and 5;
dbms_sql.return_result(c1);
open c2 for
select NumberCol
from TestNumbers
where IntCol between 7 and 10;
dbms_sql.return_result(c2);
end;""")
results = self.cursor.getimplicitresults()
self.assertEqual(len(results), 2)
self.assertEqual([n for n, in results[0]], [3.75, 5, 6.25])
self.assertEqual([n for n, in results[1]], [8.75, 10, 11.25, 12.5])
def test_3221_implicit_results_no_statement(self):
"3221 - test getimplicitresults() without executing a statement"
self.assertRaises(oracledb.InterfaceError,
self.cursor.getimplicitresults)
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_3223_batch_error_false(self):
"3223 - test batcherrors mode set to False"
self.cursor.execute("truncate table TestArrayDML")
rows = [
(1, "First", 100),
(2, "Second", 200),
(2, "Third", 300)
]
sql = "insert into TestArrayDML (IntCol, StringCol, IntCol2) " \
"values (:1, :2, :3)"
self.assertRaises(oracledb.IntegrityError, self.cursor.executemany,
sql, rows, batcherrors=False)
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_3225_implicit_results(self):
"3225 - test using implicit cursors to execute new statements"
cursor = self.connection.cursor()
cursor.execute("""
declare
c1 sys_refcursor;
begin
open c1 for
select NumberCol
from TestNumbers
where IntCol between 3 and 5;
dbms_sql.return_result(c1);
end;""")
results = cursor.getimplicitresults()
self.assertEqual(len(results), 1)
self.assertEqual([n for n, in results[0]], [3.75, 5, 6.25])
results[0].execute("select :1 from dual", (7,))
row, = results[0].fetchone()
self.assertEqual(row, 7)
if __name__ == "__main__":
test_env.run_test_cases()
|