Skip to content

Commit 74d16d8

Browse files
Improved the test suite.
1 parent a1f8e1f commit 74d16d8

File tree

2 files changed

+159
-5
lines changed

2 files changed

+159
-5
lines changed

test/test_1200_cursor.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,8 @@ def test_1253_cursor_fetch_raw(self):
606606
self.assertEqual(cursor.fetchraw(), 10)
607607
self.assertEqual(cursor.fetchvars[0].getvalue(), 38)
608608

609-
def test_1254_parse(self):
610-
"1254 - test parsing statements"
609+
def test_1254_parse_query(self):
610+
"1254 - test parsing query statements"
611611
sql = "select LongIntCol from TestNumbers where IntCol = :val"
612612
self.cursor.parse(sql)
613613
self.assertEqual(self.cursor.statement, sql)
@@ -812,11 +812,11 @@ def test_1266_refcursor_prefetchrows(self):
812812
# create refcursor and execute stored procedure
813813
with self.connection.cursor() as cursor:
814814
refcursor = self.connection.cursor()
815-
refcursor.prefetchrows = 300
816-
refcursor.arraysize = 300
815+
refcursor.prefetchrows = 150
816+
refcursor.arraysize = 50
817817
cursor.callproc("myrefcursorproc", [refcursor])
818818
refcursor.fetchall()
819-
self.assertRoundTrips(2)
819+
self.assertRoundTrips(4)
820820

821821
def test_1267_existing_cursor_prefetchrows(self):
822822
"1267 - test prefetch rows using existing cursor"
@@ -980,5 +980,26 @@ def test_1281_dml_can_use_optimised_path(self):
980980
order by IntCol""")
981981
self.assertEqual(self.cursor.fetchall(), data_to_insert)
982982

983+
def test_1282_parse_plsql(self):
984+
"1282 - test parsing plsql statements"
985+
sql = "begin :value := 5; end;"
986+
self.cursor.parse(sql)
987+
self.assertEqual(self.cursor.statement, sql)
988+
self.assertEqual(self.cursor.description, None)
989+
990+
def test_1283_parse_ddl(self):
991+
"1283 - test parsing ddl statements"
992+
sql = "truncate table TestTempTable"
993+
self.cursor.parse(sql)
994+
self.assertEqual(self.cursor.statement, sql)
995+
self.assertEqual(self.cursor.description, None)
996+
997+
def test_1284_parse_dml(self):
998+
"1284 - test parsing dml statements"
999+
sql = "insert into TestTempTable (IntCol) values (1)"
1000+
self.cursor.parse(sql)
1001+
self.assertEqual(self.cursor.statement, sql)
1002+
self.assertEqual(self.cursor.description, None)
1003+
9831004
if __name__ == "__main__":
9841005
test_env.run_test_cases()

test/test_3800_typehandler.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#------------------------------------------------------------------------------
2+
#Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
#------------------------------------------------------------------------------
4+
5+
"""
6+
3800 - Module for testing the input and output type handlers.
7+
"""
8+
9+
import test_env
10+
11+
import cx_Oracle as oracledb
12+
import json
13+
14+
class Building(object):
15+
16+
def __init__(self, building_id, description, num_floors):
17+
self.building_id = building_id
18+
self.description = description
19+
self.num_floors = num_floors
20+
21+
def __repr__(self):
22+
return "<Building %s: %s>" % (self.building_id, self.description)
23+
24+
def __eq__(self, other):
25+
if isinstance(other, Building):
26+
return other.building_id == self.building_id \
27+
and other.description == self.description \
28+
and other.num_floors == self.num_floors
29+
return NotImplemented
30+
31+
def to_json(self):
32+
return json.dumps(self.__dict__)
33+
34+
@classmethod
35+
def from_json(cls, value):
36+
result = json.loads(value)
37+
return cls(**result)
38+
39+
40+
class TestCase(test_env.BaseTestCase):
41+
42+
def building_in_converter(self, value):
43+
return value.to_json()
44+
45+
def input_type_handler(self, cursor, value, num_elements):
46+
if isinstance(value, Building):
47+
return cursor.var(oracledb.STRING, arraysize=num_elements,
48+
inconverter=self.building_in_converter)
49+
50+
def output_type_handler(self, cursor, name, default_type, size, precision,
51+
scale):
52+
if default_type == oracledb.STRING:
53+
return cursor.var(default_type, arraysize=cursor.arraysize,
54+
outconverter=Building.from_json)
55+
56+
def test_3800(self):
57+
"3800 - binding unsupported python object without input type handler"
58+
self.cursor.execute("truncate table TestTempTable")
59+
sql = "insert into TestTempTable (IntCol, StringCol) values (:1, :2)"
60+
building = Building(1, "The First Building", 5)
61+
self.assertRaises(oracledb.NotSupportedError, self.cursor.execute, sql,
62+
(building.building_id, building))
63+
64+
def test_3801(self):
65+
"3801 - not callable input type handler"
66+
self.cursor.execute("truncate table TestTempTable")
67+
building = Building(1, "The First Building", 5)
68+
sql = "insert into TestTempTable (IntCol, StringCol) values (:1, :2)"
69+
self.cursor.inputtypehandler = 5
70+
self.assertRaises(TypeError, self.cursor.execute, sql,
71+
(building.building_id, building))
72+
73+
def test_3802(self):
74+
"3802 - binding unsupported python object with input type handler"
75+
self.cursor.execute("truncate table TestTempTable")
76+
building = Building(1, "The First Building", 5)
77+
sql = "insert into TestTempTable (IntCol, StringCol) values (:1, :2)"
78+
self.cursor.inputtypehandler = self.input_type_handler
79+
self.cursor.execute(sql, (building.building_id, building))
80+
self.connection.commit()
81+
self.cursor.execute("select IntCol, StringCol from TestTempTable")
82+
self.assertEqual(self.cursor.fetchall(),
83+
[(building.building_id, building.to_json())])
84+
85+
def test_3803(self):
86+
"3803 - input type handler and output type handler on cursor level"
87+
self.cursor.execute("truncate table TestTempTable")
88+
building_one = Building(1, "The First Building", 5)
89+
building_two = Building(2, "The Second Building", 87)
90+
sql = "insert into TestTempTable (IntCol, StringCol) values (:1, :2)"
91+
cursor_one = self.connection.cursor()
92+
cursor_two = self.connection.cursor()
93+
cursor_one.inputtypehandler = self.input_type_handler
94+
cursor_one.execute(sql, (building_one.building_id, building_one))
95+
self.connection.commit()
96+
cursor_one.execute("select IntCol, StringCol from TestTempTable")
97+
self.assertEqual(cursor_one.fetchall(),
98+
[(building_one.building_id, building_one.to_json())])
99+
self.assertRaises(oracledb.NotSupportedError, cursor_two.execute,
100+
sql, (building_two.building_id, building_two))
101+
cursor_two.outputtypehandler = self.output_type_handler
102+
cursor_two.execute("select IntCol, StringCol from TestTempTable")
103+
self.assertEqual(cursor_two.fetchall(),
104+
[(building_one.building_id, building_one)])
105+
106+
def test_3804(self):
107+
"3804 - input type handler and output type handler on connection level"
108+
self.cursor.execute("truncate table TestTempTable")
109+
building_one = Building(1, "The First Building", 5)
110+
building_two = Building(2, "The Second Building", 87)
111+
sql = "insert into TestTempTable (IntCol, StringCol) values (:1, :2)"
112+
connection = test_env.get_connection()
113+
connection.inputtypehandler = self.input_type_handler
114+
cursor_one = connection.cursor()
115+
cursor_two = connection.cursor()
116+
cursor_one.execute(sql, (building_one.building_id, building_one))
117+
cursor_two.execute(sql, (building_two.building_id, building_two))
118+
connection.commit()
119+
expected_data = [
120+
(building_one.building_id, building_one),
121+
(building_two.building_id, building_two)
122+
]
123+
connection.outputtypehandler = self.output_type_handler
124+
cursor_one.execute("select IntCol, StringCol from TestTempTable")
125+
self.assertEqual(cursor_one.fetchall(), expected_data)
126+
cursor_two.execute("select IntCol, StringCol from TestTempTable")
127+
self.assertEqual(cursor_two.fetchall(), expected_data)
128+
other_cursor = self.connection.cursor()
129+
self.assertRaises(oracledb.NotSupportedError, other_cursor.execute,
130+
sql, (building_one.building_id, building_one))
131+
132+
if __name__ == "__main__":
133+
test_env.run_test_cases()

0 commit comments

Comments
 (0)