From b579bac69b63f9d2b8dcdf513b7276f39dd0ffea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Lalinsk=C3=BD?= Date: Sun, 28 Jun 2015 17:38:32 +0200 Subject: [PATCH] Add a new test for the Cursor.next extension --- dbapi20.py | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/dbapi20.py b/dbapi20.py index e3c4112..b1305d4 100644 --- a/dbapi20.py +++ b/dbapi20.py @@ -495,7 +495,7 @@ def test_fetchone(self): cur.execute('select name from %sbooze' % self.table_prefix) r = cur.fetchone() self.assertEqual(len(r),1, - 'cursor.fetchone should have retrieved a single row' + 'cursor.fetchone should have retrieved a row with one columns' ) self.assertEqual(r[0],'Victoria Bitter', 'cursor.fetchone retrieved incorrect data' @@ -507,6 +507,50 @@ def test_fetchone(self): finally: con.close() + def test_next(self): + # https://www.python.org/dev/peps/pep-0249/#next + con = self._connect() + try: + cur = con.cursor() + if not hasattr(cur,'next'): + return + + # cursor.next should raise an Error if called before + # executing a select-type query + self.assertRaises(self.driver.Error,cur.next) + + # cursor.next should raise an Error if called after + # executing a query that cannnot return rows + self.executeDDL1(cur) + self.assertRaises(self.driver.Error,cur.next) + + # cursor.next should return None if a query retrieves ' + # no rows + cur.execute('select name from %sbooze' % self.table_prefix) + self.assertRaises(StopIteration,cur.next) + _failUnless(self,cur.rowcount in (-1,0)) + + # cursor.next should raise an Error if called after + # executing a query that cannnot return rows + cur.execute("%s into %sbooze values ('Victoria Bitter')" % ( + self.insert, self.table_prefix + )) + self.assertRaises(self.driver.Error,cur.next) + + cur.execute('select name from %sbooze' % self.table_prefix) + r = cur.next() + self.assertEqual(len(r),1, + 'cursor.next should have retrieved a row with one column' + ) + self.assertEqual(r[0],'Victoria Bitter', + 'cursor.next retrieved incorrect data' + ) + # cursor.next should raise StopIteration if no more rows available + self.assertRaises(StopIteration,cur.next) + _failUnless(self,cur.rowcount in (-1,1)) + finally: + con.close() + samples = [ 'Carlton Cold', 'Carlton Draft',