diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-05-28 09:58:34 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-05-28 09:58:34 (GMT) |
commit | 47a981337a8f3898482a1c0c51ad2d91c7a6bc2f (patch) | |
tree | 88aef37e02ac0120825904fa8befdc588e028cf2 /Lib/sqlite3/test | |
parent | 432810f9f35c014afecbd24edda6b26a3cf04fd9 (diff) | |
download | cpython-47a981337a8f3898482a1c0c51ad2d91c7a6bc2f.zip cpython-47a981337a8f3898482a1c0c51ad2d91c7a6bc2f.tar.gz cpython-47a981337a8f3898482a1c0c51ad2d91c7a6bc2f.tar.bz2 |
Issue #10203: sqlite3.Row now truly supports sequence protocol. In particulr
it supports reverse() and negative indices. Original patch by Claudiu Popa.
Diffstat (limited to 'Lib/sqlite3/test')
-rw-r--r-- | Lib/sqlite3/test/factory.py | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/Lib/sqlite3/test/factory.py b/Lib/sqlite3/test/factory.py index 1013755..98dcae5 100644 --- a/Lib/sqlite3/test/factory.py +++ b/Lib/sqlite3/test/factory.py @@ -23,6 +23,7 @@ import unittest import sqlite3 as sqlite +from collections.abc import Sequence class MyConnection(sqlite.Connection): def __init__(self, *args, **kwargs): @@ -96,9 +97,19 @@ class RowFactoryTests(unittest.TestCase): self.assertEqual(col1, 1, "by name: wrong result for column 'A'") self.assertEqual(col2, 2, "by name: wrong result for column 'B'") - col1, col2 = row[0], row[1] - self.assertEqual(col1, 1, "by index: wrong result for column 0") - self.assertEqual(col2, 2, "by index: wrong result for column 1") + self.assertEqual(row[0], 1, "by index: wrong result for column 0") + self.assertEqual(row[1], 2, "by index: wrong result for column 1") + self.assertEqual(row[-1], 2, "by index: wrong result for column -1") + self.assertEqual(row[-2], 1, "by index: wrong result for column -2") + + with self.assertRaises(IndexError): + row['c'] + with self.assertRaises(IndexError): + row[2] + with self.assertRaises(IndexError): + row[-3] + with self.assertRaises(IndexError): + row[2**1000] def CheckSqliteRowIter(self): """Checks if the row object is iterable""" @@ -142,6 +153,15 @@ class RowFactoryTests(unittest.TestCase): self.assertNotEqual(row_1, row_3) self.assertNotEqual(hash(row_1), hash(row_3)) + def CheckSqliteRowAsSequence(self): + """ Checks if the row object can act like a sequence """ + self.con.row_factory = sqlite.Row + row = self.con.execute("select 1 as a, 2 as b").fetchone() + + as_tuple = tuple(row) + self.assertEqual(list(reversed(row)), list(reversed(as_tuple))) + self.assertIsInstance(row, Sequence) + def tearDown(self): self.con.close() |