From 2194d81965776b5a0e73b882915c07e104da8791 Mon Sep 17 00:00:00 2001 From: Anthony Tuininga Date: Mon, 2 Nov 2020 15:20:45 -0700 Subject: [PATCH] Added test cases for prefetch. --- test/Cursor.py | 43 +++++++++++++++++++++++++++++++++++++++++++ test/TestEnv.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/test/Cursor.py b/test/Cursor.py index 2b32b04..7e871d3 100644 --- a/test/Cursor.py +++ b/test/Cursor.py @@ -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() diff --git a/test/TestEnv.py b/test/TestEnv.py index d52a59d..8ba4560 100644 --- a/test/TestEnv.py +++ b/test/TestEnv.py @@ -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