summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorEdward Schauman-Haigh <142528725+EddInSverige@users.noreply.github.com>2023-08-29 08:51:36 (GMT)
committerGitHub <noreply@github.com>2023-08-29 08:51:36 (GMT)
commit6eaddc10e972273c1aed8b88c538e65e4773496e (patch)
tree5118b92e9132c520128b63413de1d13f104acced /Lib
parent5f85b443f7119e1c68a15fc9a342655e544d2852 (diff)
downloadcpython-6eaddc10e972273c1aed8b88c538e65e4773496e.zip
cpython-6eaddc10e972273c1aed8b88c538e65e4773496e.tar.gz
cpython-6eaddc10e972273c1aed8b88c538e65e4773496e.tar.bz2
gh-108558: Improve sqlite3 row factory tests (#108578)
Add test_sqlite_row_keys() to explicitly test sqlite3.Row.keys(). Cleanups: - Reduce test noise by converting docstrings to regular comments - Reduce boilerplate code by adding a setUp() method to RowFactoryTests Co-authored-by: Erlend E. Aasland <erlend@python.org>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_sqlite3/test_factory.py28
1 files changed, 14 insertions, 14 deletions
diff --git a/Lib/test/test_sqlite3/test_factory.py b/Lib/test/test_sqlite3/test_factory.py
index a7c4417..48d35b5 100644
--- a/Lib/test/test_sqlite3/test_factory.py
+++ b/Lib/test/test_sqlite3/test_factory.py
@@ -116,13 +116,16 @@ class RowFactoryTestsBackwardsCompat(MemoryDatabaseMixin, unittest.TestCase):
class RowFactoryTests(MemoryDatabaseMixin, unittest.TestCase):
+ def setUp(self):
+ super().setUp()
+ self.con.row_factory = sqlite.Row
+
def test_custom_factory(self):
self.con.row_factory = lambda cur, row: list(row)
row = self.con.execute("select 1, 2").fetchone()
self.assertIsInstance(row, list)
def test_sqlite_row_index(self):
- self.con.row_factory = sqlite.Row
row = self.con.execute("select 1 as a_1, 2 as b").fetchone()
self.assertIsInstance(row, sqlite.Row)
@@ -153,7 +156,6 @@ class RowFactoryTests(MemoryDatabaseMixin, unittest.TestCase):
row[complex()] # index must be int or string
def test_sqlite_row_index_unicode(self):
- self.con.row_factory = sqlite.Row
row = self.con.execute("select 1 as \xff").fetchone()
self.assertEqual(row["\xff"], 1)
with self.assertRaises(IndexError):
@@ -163,7 +165,6 @@ class RowFactoryTests(MemoryDatabaseMixin, unittest.TestCase):
def test_sqlite_row_slice(self):
# A sqlite.Row can be sliced like a list.
- self.con.row_factory = sqlite.Row
row = self.con.execute("select 1, 2, 3, 4").fetchone()
self.assertEqual(row[0:0], ())
self.assertEqual(row[0:1], (1,))
@@ -180,8 +181,7 @@ class RowFactoryTests(MemoryDatabaseMixin, unittest.TestCase):
self.assertEqual(row[3:0:-2], (4, 2))
def test_sqlite_row_iter(self):
- """Checks if the row object is iterable"""
- self.con.row_factory = sqlite.Row
+ # Checks if the row object is iterable.
row = self.con.execute("select 1 as a, 2 as b").fetchone()
# Is iterable in correct order and produces valid results:
@@ -193,23 +193,20 @@ class RowFactoryTests(MemoryDatabaseMixin, unittest.TestCase):
self.assertEqual(items, [1, 2])
def test_sqlite_row_as_tuple(self):
- """Checks if the row object can be converted to a tuple"""
- self.con.row_factory = sqlite.Row
+ # Checks if the row object can be converted to a tuple.
row = self.con.execute("select 1 as a, 2 as b").fetchone()
t = tuple(row)
self.assertEqual(t, (row['a'], row['b']))
def test_sqlite_row_as_dict(self):
- """Checks if the row object can be correctly converted to a dictionary"""
- self.con.row_factory = sqlite.Row
+ # Checks if the row object can be correctly converted to a dictionary.
row = self.con.execute("select 1 as a, 2 as b").fetchone()
d = dict(row)
self.assertEqual(d["a"], row["a"])
self.assertEqual(d["b"], row["b"])
def test_sqlite_row_hash_cmp(self):
- """Checks if the row object compares and hashes correctly"""
- self.con.row_factory = sqlite.Row
+ # Checks if the row object compares and hashes correctly.
row_1 = self.con.execute("select 1 as a, 2 as b").fetchone()
row_2 = self.con.execute("select 1 as a, 2 as b").fetchone()
row_3 = self.con.execute("select 1 as a, 3 as b").fetchone()
@@ -242,21 +239,24 @@ class RowFactoryTests(MemoryDatabaseMixin, unittest.TestCase):
self.assertEqual(hash(row_1), hash(row_2))
def test_sqlite_row_as_sequence(self):
- """ Checks if the row object can act like a sequence """
- self.con.row_factory = sqlite.Row
+ # Checks if the row object can act like a sequence.
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 test_sqlite_row_keys(self):
+ # Checks if the row object can return a list of columns as strings.
+ row = self.con.execute("select 1 as a, 2 as b").fetchone()
+ self.assertEqual(row.keys(), ['a', 'b'])
+
def test_fake_cursor_class(self):
# Issue #24257: Incorrect use of PyObject_IsInstance() caused
# segmentation fault.
# Issue #27861: Also applies for cursor factory.
class FakeCursor(str):
__class__ = sqlite.Cursor
- self.con.row_factory = sqlite.Row
self.assertRaises(TypeError, self.con.cursor, FakeCursor)
self.assertRaises(TypeError, sqlite.Row, FakeCursor(), ())