Skip to content

Commit 8fda704

Browse files
Adjusted test cases to make use of encoding and nencoding parameters to both
standalone connection and session pool creation; added test cases for binding and fetching supplemental characters as well as fetching temporary LOBs; tidied up a few test cases as well.
1 parent 2b9e803 commit 8fda704

File tree

7 files changed

+116
-62
lines changed

7 files changed

+116
-62
lines changed

test/Cursor.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,13 @@ def testIteratorsInterrupted(self):
214214
value, = testIter.next()
215215
self.cursor.execute("insert into TestTempTable (IntCol) values (1)")
216216
if sys.version_info[0] >= 3:
217-
self.assertRaises(cx_Oracle.InterfaceError, next, testIter)
217+
self.assertRaises(cx_Oracle.InterfaceError, next, testIter)
218218
else:
219-
self.assertRaises(cx_Oracle.InterfaceError, testIter.next)
219+
self.assertRaises(cx_Oracle.InterfaceError, testIter.next)
220220

221221
def testBindNames(self):
222222
"""test that bindnames() works correctly."""
223-
self.assertRaises(cx_Oracle.ProgrammingError,
224-
self.cursor.bindnames)
223+
self.assertRaises(cx_Oracle.ProgrammingError, self.cursor.bindnames)
225224
self.cursor.prepare("begin null; end;")
226225
self.assertEqual(self.cursor.bindnames(), [])
227226
self.cursor.prepare("begin :retval := :inval + 5; end;")

test/LobVar.py

Lines changed: 72 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,29 @@
44

55
class TestLobVar(BaseTestCase):
66

7-
def __PerformTest(self, type, inputType):
7+
def __GetTempLobs(self, sid):
8+
cursor = self.connection.cursor()
9+
cursor.execute("""
10+
select abstract_lobs
11+
from v$temporary_lobs
12+
where sid = :sid""", sid = sid)
13+
row = cursor.fetchone()
14+
if row is None:
15+
return 0
16+
return int(row[0])
17+
18+
def __PerformTest(self, lobType, inputType):
819
longString = ""
9-
directType = getattr(cx_Oracle, type)
10-
self.cursor.execute("truncate table Test%ss" % type)
20+
directType = getattr(cx_Oracle, lobType)
21+
self.cursor.execute("truncate table Test%ss" % lobType)
1122
for i in range(0, 11):
1223
if i > 0:
1324
char = chr(ord('A') + i - 1)
1425
longString += char * 25000
1526
elif inputType != directType:
1627
continue
1728
self.cursor.setinputsizes(longString = inputType)
18-
if type == "BLOB" and sys.version_info[0] >= 3:
29+
if lobType == "BLOB" and sys.version_info[0] >= 3:
1930
bindValue = longString.encode("ascii")
2031
else:
2132
bindValue = longString
@@ -26,70 +37,73 @@ def __PerformTest(self, type, inputType):
2637
) values (
2738
:integerValue,
2839
:longString
29-
)""" % (type, type),
40+
)""" % (lobType, lobType),
3041
integerValue = i,
3142
longString = bindValue)
3243
self.connection.commit()
3344
self.cursor.execute("""
3445
select *
3546
from Test%ss
36-
order by IntCol""" % type)
47+
order by IntCol""" % lobType)
48+
self.__ValidateQuery(self.cursor, lobType)
49+
50+
def __TestTrim(self, lobType):
51+
self.cursor.execute("truncate table Test%ss" % lobType)
52+
self.cursor.setinputsizes(longString = getattr(cx_Oracle, lobType))
53+
longString = "X" * 75000
54+
if lobType == "BLOB" and sys.version_info[0] >= 3:
55+
longString = longString.encode("ascii")
56+
self.cursor.execute("""
57+
insert into Test%ss (
58+
IntCol,
59+
%sCol
60+
) values (
61+
:integerValue,
62+
:longString
63+
)""" % (lobType, lobType),
64+
integerValue = 1,
65+
longString = longString)
66+
self.cursor.execute("""
67+
select %sCol
68+
from Test%ss
69+
where IntCol = 1""" % (lobType, lobType))
70+
lob, = self.cursor.fetchone()
71+
self.assertEqual(lob.size(), 75000)
72+
lob.trim(25000)
73+
self.assertEqual(lob.size(), 25000)
74+
lob.trim()
75+
self.assertEqual(lob.size(), 0)
76+
77+
def __ValidateQuery(self, rows, lobType):
3778
longString = ""
3879
for row in self.cursor:
3980
integerValue, lob = row
4081
if integerValue == 0:
4182
self.assertEqual(lob.size(), 0)
4283
expectedValue = ""
43-
if type == "BLOB" and sys.version_info[0] >= 3:
84+
if lobType == "BLOB" and sys.version_info[0] >= 3:
4485
expectedValue = expectedValue.encode("ascii")
4586
self.assertEqual(lob.read(), expectedValue)
4687
else:
4788
char = chr(ord('A') + integerValue - 1)
4889
prevChar = chr(ord('A') + integerValue - 2)
4990
longString += char * 25000
50-
if type == "BLOB" and sys.version_info[0] >= 3:
91+
if lobType == "BLOB" and sys.version_info[0] >= 3:
5192
actualValue = longString.encode("ascii")
5293
char = char.encode("ascii")
5394
prevChar = prevChar.encode("ascii")
5495
else:
5596
actualValue = longString
5697
self.assertEqual(lob.size(), len(actualValue))
5798
self.assertEqual(lob.read(), actualValue)
58-
if type == "CLOB":
99+
if lobType == "CLOB":
59100
self.assertEqual(str(lob), actualValue)
60101
self.assertEqual(lob.read(len(actualValue)), char)
61102
if integerValue > 1:
62103
offset = (integerValue - 1) * 25000 - 4
63104
string = prevChar * 5 + char * 5
64105
self.assertEqual(lob.read(offset, 10), string)
65106

66-
def __TestTrim(self, type):
67-
self.cursor.execute("truncate table Test%ss" % type)
68-
self.cursor.setinputsizes(longString = getattr(cx_Oracle, type))
69-
longString = "X" * 75000
70-
if type == "BLOB" and sys.version_info[0] >= 3:
71-
longString = longString.encode("ascii")
72-
self.cursor.execute("""
73-
insert into Test%ss (
74-
IntCol,
75-
%sCol
76-
) values (
77-
:integerValue,
78-
:longString
79-
)""" % (type, type),
80-
integerValue = 1,
81-
longString = longString)
82-
self.cursor.execute("""
83-
select %sCol
84-
from Test%ss
85-
where IntCol = 1""" % (type, type))
86-
lob, = self.cursor.fetchone()
87-
self.assertEqual(lob.size(), 75000)
88-
lob.trim(25000)
89-
self.assertEqual(lob.size(), 25000)
90-
lob.trim()
91-
self.assertEqual(lob.size(), 0)
92-
93107
def testBLOBCursorDescription(self):
94108
"test cursor description is accurate for BLOBs"
95109
self.cursor.execute("select * from TestBLOBs")
@@ -131,9 +145,9 @@ def testCLOBTrim(self):
131145
def testMultipleFetch(self):
132146
"test retrieving data from a CLOB after multiple fetches"
133147
self.cursor.arraysize = 1
134-
self.cursor.execute("select CLOBCol from TestCLOBS")
148+
self.cursor.execute("select * from TestCLOBS")
135149
rows = self.cursor.fetchall()
136-
self.assertRaises(cx_Oracle.ProgrammingError, rows[1][0].read)
150+
self.__ValidateQuery(rows, "CLOB")
137151

138152
def testNCLOBCursorDescription(self):
139153
"test cursor description is accurate for NCLOBs"
@@ -154,3 +168,23 @@ def testNCLOBTrim(self):
154168
"test trimming a CLOB"
155169
self.__TestTrim("CLOB")
156170

171+
def testTemporaryLobs(self):
172+
cursor = self.connection.cursor()
173+
cursor.arraysize = self.cursor.arraysize
174+
cursor.execute("""
175+
select sid
176+
from v$session
177+
where audsid = userenv('sessionid')""")
178+
sid, = cursor.fetchone()
179+
tempLobs = self.__GetTempLobs(sid)
180+
self.assertEqual(tempLobs, 0)
181+
cursor.execute("""
182+
select extract(xmlcol, '/').getclobval()
183+
from TestXML""")
184+
for lob, in cursor:
185+
value = lob.read()
186+
del lob
187+
cursor.close()
188+
tempLobs = self.__GetTempLobs(sid)
189+
self.assertEqual(tempLobs, 0)
190+

test/SessionPool.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def __ConnectAndGenerateError(self):
2121

2222
def testPool(self):
2323
"""test that the pool is created and has the right attributes"""
24-
pool = cx_Oracle.SessionPool(USERNAME, PASSWORD, TNSENTRY, 2, 8, 3)
24+
pool = cx_Oracle.SessionPool(USERNAME, PASSWORD, TNSENTRY, 2, 8, 3,
25+
encoding = ENCODING, nencoding = NENCODING)
2526
self.assertEqual(pool.username, USERNAME, "user name differs")
2627
self.assertEqual(pool.tnsentry, TNSENTRY, "tnsentry differs")
2728
self.assertEqual(pool.max, 8, "max differs")
@@ -45,13 +46,15 @@ def testPool(self):
4546

4647
def testProxyAuth(self):
4748
"""test that proxy authentication is possible"""
48-
pool = cx_Oracle.SessionPool(USERNAME, PASSWORD, TNSENTRY, 2, 8, 3)
49+
pool = cx_Oracle.SessionPool(USERNAME, PASSWORD, TNSENTRY, 2, 8, 3,
50+
encoding = ENCODING, nencoding = NENCODING)
4951
self.assertEqual(pool.homogeneous, 1,
5052
"homogeneous should be 1 by default")
5153
self.assertRaises(cx_Oracle.ProgrammingError, pool.acquire,
5254
user = "proxyuser")
5355
pool = cx_Oracle.SessionPool(USERNAME, PASSWORD, TNSENTRY, 2, 8, 3,
54-
homogeneous = False)
56+
homogeneous = False, encoding = ENCODING,
57+
nencoding = NENCODING)
5558
self.assertEqual(pool.homogeneous, 0,
5659
"homogeneous should be 0 after setting it in the constructor")
5760
user = "%s_proxy" % USERNAME
@@ -63,12 +66,14 @@ def testProxyAuth(self):
6366

6467
def testRollbackOnDel(self):
6568
"connection rolls back before being destroyed"
66-
pool = cx_Oracle.SessionPool(USERNAME, PASSWORD, TNSENTRY, 1, 8, 3)
69+
pool = cx_Oracle.SessionPool(USERNAME, PASSWORD, TNSENTRY, 1, 8, 3,
70+
encoding = ENCODING, nencoding = NENCODING)
6771
connection = pool.acquire()
6872
cursor = connection.cursor()
6973
cursor.execute("truncate table TestTempTable")
7074
cursor.execute("insert into TestTempTable (IntCol) values (1)")
71-
pool = cx_Oracle.SessionPool(USERNAME, PASSWORD, TNSENTRY, 1, 8, 3)
75+
pool = cx_Oracle.SessionPool(USERNAME, PASSWORD, TNSENTRY, 1, 8, 3,
76+
encoding = ENCODING, nencoding = NENCODING)
7277
connection = pool.acquire()
7378
cursor = connection.cursor()
7479
cursor.execute("select count(*) from TestTempTable")
@@ -77,13 +82,15 @@ def testRollbackOnDel(self):
7782

7883
def testRollbackOnRelease(self):
7984
"connection rolls back before released back to the pool"
80-
pool = cx_Oracle.SessionPool(USERNAME, PASSWORD, TNSENTRY, 1, 8, 3)
85+
pool = cx_Oracle.SessionPool(USERNAME, PASSWORD, TNSENTRY, 1, 8, 3,
86+
encoding = ENCODING, nencoding = NENCODING)
8187
connection = pool.acquire()
8288
cursor = connection.cursor()
8389
cursor.execute("truncate table TestTempTable")
8490
cursor.execute("insert into TestTempTable (IntCol) values (1)")
8591
pool.release(connection)
86-
pool = cx_Oracle.SessionPool(USERNAME, PASSWORD, TNSENTRY, 1, 8, 3)
92+
pool = cx_Oracle.SessionPool(USERNAME, PASSWORD, TNSENTRY, 1, 8, 3,
93+
encoding = ENCODING, nencoding = NENCODING)
8794
connection = pool.acquire()
8895
cursor = connection.cursor()
8996
cursor.execute("select count(*) from TestTempTable")
@@ -93,7 +100,7 @@ def testRollbackOnRelease(self):
93100
def testThreading(self):
94101
"""test session pool to database with multiple threads"""
95102
self.pool = cx_Oracle.SessionPool(USERNAME, PASSWORD, TNSENTRY, 5, 20,
96-
2, threaded = True)
103+
2, threaded = True, encoding = ENCODING, nencoding = NENCODING)
97104
threads = []
98105
for i in range(20):
99106
thread = threading.Thread(None, self.__ConnectAndDrop)
@@ -105,7 +112,7 @@ def testThreading(self):
105112
def testThreadingWithErrors(self):
106113
"""test session pool to database with multiple threads (with errors)"""
107114
self.pool = cx_Oracle.SessionPool(USERNAME, PASSWORD, TNSENTRY, 5, 20,
108-
2, threaded = True)
115+
2, threaded = True, encoding = ENCODING, nencoding = NENCODING)
109116
threads = []
110117
for i in range(20):
111118
thread = threading.Thread(None, self.__ConnectAndGenerateError)

test/StringVar.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,17 @@ def testFetchOne(self):
271271
self.assertEqual(self.cursor.fetchone(), self.dataByKey[4])
272272
self.assertEqual(self.cursor.fetchone(), None)
273273

274+
def testSupplementalCharacters(self):
275+
"test that binding and fetching supplemental charcters works correctly"
276+
supplementalChars = "𠜎 𠜱 𠝹 𠱓 𠱸 𠲖 𠳏 𠳕 𠴕 𠵼 𠵿 𠸎 𠸏 𠹷 𠺝 " \
277+
"𠺢 𠻗 𠻹 𠻺 𠼭 𠼮 𠽌 𠾴 𠾼 𠿪 𡁜 𡁯 𡁵 𡁶 𡁻 𡃁 𡃉 𡇙 𢃇 " \
278+
"𢞵 𢫕 𢭃 𢯊 𢱑 𢱕 𢳂 𢴈 𢵌 𢵧 𢺳 𣲷 𤓓 𤶸 𤷪 𥄫 𦉘 𦟌 𦧲 " \
279+
"𦧺 𧨾 𨅝 𨈇 𨋢 𨳊 𨳍 𨳒 𩶘"
280+
self.cursor.execute("truncate table TestTempTable")
281+
self.cursor.execute("insert into TestTempTable values (:1, :2)",
282+
(1, supplementalChars))
283+
self.connection.commit()
284+
self.cursor.execute("select StringCol from TestTempTable")
285+
value, = self.cursor.fetchone()
286+
self.assertEqual(value, supplementalChars)
287+

test/TestEnv.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,19 @@
55
import sys
66
import unittest
77

8-
lang = os.environ.get("NLS_LANG")
9-
if lang is None:
10-
os.environ["NLS_LANG"] = ".UTF8"
118
if sys.version_info[0] < 3:
129
input = raw_input
1310

14-
def GetValue(name, label):
15-
value = os.environ.get("CX_ORACLE_" + name)
11+
def GetValue(name, label, defaultValue = None):
12+
value = os.environ.get("CX_ORACLE_" + name, defaultValue)
1613
if value is None:
1714
value = input(label + ": ")
18-
if hasattr(cx_Oracle, "UNICODE") or sys.version_info[0] >= 3:
19-
return value
20-
return unicode(value)
15+
return value
2116

2217
USERNAME = GetValue("USERNAME", "user name")
2318
PASSWORD = GetValue("PASSWORD", "password")
2419
TNSENTRY = GetValue("TNSENTRY", "TNS entry")
20+
ENCODING = GetValue("ENCODING", "encoding", "UTF-8")
21+
NENCODING = GetValue("NENCODING", "national encoding", "UTF-8")
2522
ARRAY_SIZE = int(GetValue("ARRAY_SIZE", "array size"))
2623

test/test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ def setUp(self):
6262
import cx_Oracle
6363
import TestEnv
6464
self.connection = cx_Oracle.connect(TestEnv.USERNAME,
65-
TestEnv.PASSWORD, TestEnv.TNSENTRY)
65+
TestEnv.PASSWORD, TestEnv.TNSENTRY,
66+
encoding = TestEnv.ENCODING, nencoding = TestEnv.NENCODING)
6667
self.cursor = self.connection.cursor()
6768
self.cursor.arraysize = TestEnv.ARRAY_SIZE
6869

@@ -84,6 +85,8 @@ def tearDown(self):
8485
setattr(module, "USERNAME", TestEnv.USERNAME)
8586
setattr(module, "PASSWORD", TestEnv.PASSWORD)
8687
setattr(module, "TNSENTRY", TestEnv.TNSENTRY)
88+
setattr(module, "ENCODING", TestEnv.ENCODING)
89+
setattr(module, "NENCODING", TestEnv.NENCODING)
8790
setattr(module, "ARRAY_SIZE", TestEnv.ARRAY_SIZE)
8891
setattr(module, "TestCase", unittest.TestCase)
8992
setattr(module, "BaseTestCase", BaseTestCase)

test/uConnection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def testExceptionOnClose(self):
5353

5454
def testMakeDSN(self):
5555
"test making a data source name from host, port and sid"
56-
formatString = u"(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=" \
57-
u"(PROTOCOL=TCP)(HOST=%s)(PORT=%d)))(CONNECT_DATA=(SID=%s)))"
56+
formatString = u"(DESCRIPTION=(ADDRESS=" \
57+
u"(PROTOCOL=TCP)(HOST=%s)(PORT=%d))(CONNECT_DATA=(SID=%s)))"
5858
args = (u"hostname", 1521, u"TEST")
5959
result = cx_Oracle.makedsn(*args)
6060
self.assertEqual(result, formatString % args)

0 commit comments

Comments
 (0)