Added test to verify withhold transaction behaviour

A withhold cursor can read its data when the transaction is closed, so
it shouldn't start a new one upon movement/close.
This commit is contained in:
Daniele Varrazzo 2014-08-21 05:01:34 +01:00
parent 0e44198a8f
commit 18d67a73d5

View File

@ -176,10 +176,7 @@ class CursorTests(ConnectingTestCase):
curs.execute("select data from invname order by data") curs.execute("select data from invname order by data")
self.assertEqual(curs.fetchall(), [(10,), (20,), (30,)]) self.assertEqual(curs.fetchall(), [(10,), (20,), (30,)])
def test_withhold(self): def _create_withhold_table(self):
self.assertRaises(psycopg2.ProgrammingError, self.conn.cursor,
withhold=True)
curs = self.conn.cursor() curs = self.conn.cursor()
try: try:
curs.execute("drop table withhold") curs.execute("drop table withhold")
@ -190,6 +187,11 @@ class CursorTests(ConnectingTestCase):
curs.execute("insert into withhold values (%s)", (i,)) curs.execute("insert into withhold values (%s)", (i,))
curs.close() curs.close()
def test_withhold(self):
self.assertRaises(psycopg2.ProgrammingError, self.conn.cursor,
withhold=True)
self._create_withhold_table()
curs = self.conn.cursor("W") curs = self.conn.cursor("W")
self.assertEqual(curs.withhold, False); self.assertEqual(curs.withhold, False);
curs.withhold = True curs.withhold = True
@ -209,6 +211,30 @@ class CursorTests(ConnectingTestCase):
curs.execute("drop table withhold") curs.execute("drop table withhold")
self.conn.commit() self.conn.commit()
def test_withhold_no_begin(self):
self._create_withhold_table()
curs = self.conn.cursor("w", withhold=True)
curs.execute("select data from withhold order by data")
self.assertEqual(curs.fetchone(), (10,))
self.assertEqual(self.conn.status, psycopg2.extensions.STATUS_BEGIN)
self.assertEqual(self.conn.get_transaction_status(),
psycopg2.extensions.TRANSACTION_STATUS_INTRANS)
self.conn.commit()
self.assertEqual(self.conn.status, psycopg2.extensions.STATUS_READY)
self.assertEqual(self.conn.get_transaction_status(),
psycopg2.extensions.TRANSACTION_STATUS_IDLE)
self.assertEqual(curs.fetchone(), (20,))
self.assertEqual(self.conn.status, psycopg2.extensions.STATUS_READY)
self.assertEqual(self.conn.get_transaction_status(),
psycopg2.extensions.TRANSACTION_STATUS_IDLE)
curs.close()
self.assertEqual(self.conn.status, psycopg2.extensions.STATUS_READY)
self.assertEqual(self.conn.get_transaction_status(),
psycopg2.extensions.TRANSACTION_STATUS_IDLE)
def test_scrollable(self): def test_scrollable(self):
self.assertRaises(psycopg2.ProgrammingError, self.conn.cursor, self.assertRaises(psycopg2.ProgrammingError, self.conn.cursor,
scrollable=True) scrollable=True)