Added test cases for prefetch.

This commit is contained in:
Anthony Tuininga 2020-11-02 15:20:45 -07:00
parent 04fd1a7ad5
commit 2194d81965
2 changed files with 72 additions and 0 deletions

View File

@ -750,5 +750,48 @@ class TestCase(TestEnv.BaseTestCase):
where rowid = :1""", [rowid])
self.assertEqual("Row %s" % rows[-3], self.cursor.fetchone()[0])
def testPrefetchRows(self):
"test prefetch rows"
self.setUpRoundTripChecker()
# perform simple query and verify only one round trip is needed
with self.connection.cursor() as cursor:
cursor.execute("select sysdate from dual").fetchall()
self.assertRoundTrips(1)
# set prefetchrows to 1 and verify that two round trips are now needed
with self.connection.cursor() as cursor:
cursor.prefetchrows = 1
cursor.execute("select sysdate from dual").fetchall()
self.assertRoundTrips(2)
# simple DDL only requires a single round trip
with self.connection.cursor() as cursor:
cursor.execute("truncate table TestTempTable")
self.assertRoundTrips(1)
# array execution only requires a single round trip
numRows = 590
with self.connection.cursor() as cursor:
sql = "insert into TestTempTable (IntCol) values (:1)"
data = [(n + 1,) for n in range(numRows)]
cursor.executemany(sql, data)
self.assertRoundTrips(1)
# setting prefetch and array size to 1 requires a round-trip for each
# row
with self.connection.cursor() as cursor:
cursor.prefetchrows = 1
cursor.arraysize = 1
cursor.execute("select IntCol from TestTempTable").fetchall()
self.assertRoundTrips(numRows + 1)
# setting prefetch and array size to 300 requires 2 round-trips
with self.connection.cursor() as cursor:
cursor.prefetchrows = 300
cursor.arraysize = 300
cursor.execute("select IntCol from TestTempTable").fetchall()
self.assertRoundTrips(2)
if __name__ == "__main__":
TestEnv.RunTestCases()

View File

@ -163,8 +163,34 @@ def GetPool(user=None, password=None, **kwargs):
def GetClientVersion():
return cx_Oracle.clientversion()
class RoundTripInfo:
def __init__(self, connection):
self.prevRoundTrips = 0
self.adminConn = cx_Oracle.connect(GetAdminConnectString())
with connection.cursor() as cursor:
cursor.execute("select sys_context('userenv', 'sid') from dual")
self.sid, = cursor.fetchone()
self.getRoundTrips()
def getRoundTrips(self):
with self.adminConn.cursor() as cursor:
cursor.execute("""
select ss.value
from v$sesstat ss, v$statname sn
where ss.sid = :sid
and ss.statistic# = sn.statistic#
and sn.name like '%roundtrip%client%'""", sid=self.sid)
currentRoundTrips, = cursor.fetchone()
diffRoundTrips = currentRoundTrips - self.prevRoundTrips
self.prevRoundTrips = currentRoundTrips
return diffRoundTrips
class BaseTestCase(unittest.TestCase):
def assertRoundTrips(self, n):
self.assertEqual(self.roundTripInfo.getRoundTrips(), n)
def getSodaDatabase(self, minclient=(18, 3), minserver=(18, 0),
message="not supported with this client/server combination"):
client = cx_Oracle.clientversion()[:2]
@ -191,6 +217,9 @@ class BaseTestCase(unittest.TestCase):
self.connection = GetConnection()
self.cursor = self.connection.cursor()
def setUpRoundTripChecker(self):
self.roundTripInfo = RoundTripInfo(self.connection)
def tearDown(self):
self.connection.close()
del self.cursor