From 94dbb502a9ca4b534172510fc7d1b1311ae661ad Mon Sep 17 00:00:00 2001 From: EddInSverige Date: Mon, 28 Aug 2023 20:48:15 +0200 Subject: [PATCH 1/5] Add test_sqlite_row_keys poc --- Lib/test/test_sqlite3/test_factory.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Lib/test/test_sqlite3/test_factory.py b/Lib/test/test_sqlite3/test_factory.py index a7c4417862aff7..2c3352c6edcc21 100644 --- a/Lib/test/test_sqlite3/test_factory.py +++ b/Lib/test/test_sqlite3/test_factory.py @@ -250,6 +250,13 @@ def test_sqlite_row_as_sequence(self): 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 its keys """ + self.con.row_factory = sqlite.Row + 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. From 1057e8517fb3a21acdcee494d36ff6843c33f0da Mon Sep 17 00:00:00 2001 From: EddInSverige Date: Tue, 29 Aug 2023 07:49:48 +0200 Subject: [PATCH 2/5] Replace docstrings with regular comments in row tests --- Lib/test/test_sqlite3/test_factory.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_sqlite3/test_factory.py b/Lib/test/test_sqlite3/test_factory.py index 2c3352c6edcc21..e5ed5b255891f3 100644 --- a/Lib/test/test_sqlite3/test_factory.py +++ b/Lib/test/test_sqlite3/test_factory.py @@ -193,14 +193,14 @@ def test_sqlite_row_iter(self): self.assertEqual(items, [1, 2]) def test_sqlite_row_as_tuple(self): - """Checks if the row object can be converted to a tuple""" + # Checks if the row object can be converted to a tuple self.con.row_factory = sqlite.Row 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""" + # Checks if the row object can be correctly converted to a dictionary self.con.row_factory = sqlite.Row row = self.con.execute("select 1 as a, 2 as b").fetchone() d = dict(row) @@ -208,7 +208,7 @@ def test_sqlite_row_as_dict(self): self.assertEqual(d["b"], row["b"]) def test_sqlite_row_hash_cmp(self): - """Checks if the row object compares and hashes correctly""" + # Checks if the row object compares and hashes correctly self.con.row_factory = sqlite.Row 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() @@ -242,7 +242,7 @@ def test_sqlite_row_hash_cmp(self): 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 """ + # 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() @@ -251,7 +251,7 @@ def test_sqlite_row_as_sequence(self): self.assertIsInstance(row, Sequence) def test_sqlite_row_keys(self): - """ Checks if the row object can return its keys """ + # Checks if the row object can return its keys self.con.row_factory = sqlite.Row row = self.con.execute("select 1 as a, 2 as b").fetchone() From 746ef10ca9a33570ff785b31461ddb96c178cf28 Mon Sep 17 00:00:00 2001 From: EddInSverige Date: Tue, 29 Aug 2023 08:05:37 +0200 Subject: [PATCH 3/5] Improve test comment --- Lib/test/test_sqlite3/test_factory.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_sqlite3/test_factory.py b/Lib/test/test_sqlite3/test_factory.py index e5ed5b255891f3..3013591c8c20e4 100644 --- a/Lib/test/test_sqlite3/test_factory.py +++ b/Lib/test/test_sqlite3/test_factory.py @@ -251,7 +251,7 @@ def test_sqlite_row_as_sequence(self): self.assertIsInstance(row, Sequence) def test_sqlite_row_keys(self): - # Checks if the row object can return its keys + # Checks if the row object can return a list of columns as strings self.con.row_factory = sqlite.Row row = self.con.execute("select 1 as a, 2 as b").fetchone() From ed4953d27d0ae86c5c51d0889d7d9fd238f9d2f3 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 29 Aug 2023 10:03:23 +0200 Subject: [PATCH 4/5] Update Lib/test/test_sqlite3/test_factory.py --- Lib/test/test_sqlite3/test_factory.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_sqlite3/test_factory.py b/Lib/test/test_sqlite3/test_factory.py index 3013591c8c20e4..ec463852a7855c 100644 --- a/Lib/test/test_sqlite3/test_factory.py +++ b/Lib/test/test_sqlite3/test_factory.py @@ -254,7 +254,6 @@ def test_sqlite_row_keys(self): # Checks if the row object can return a list of columns as strings self.con.row_factory = sqlite.Row row = self.con.execute("select 1 as a, 2 as b").fetchone() - self.assertEqual(row.keys(), ['a', 'b']) def test_fake_cursor_class(self): From a37159cf00360a59f57a5de122ba60efda2e37e6 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 29 Aug 2023 10:07:05 +0200 Subject: [PATCH 5/5] Reduce boilerplate code and fixup a docstring --- Lib/test/test_sqlite3/test_factory.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/Lib/test/test_sqlite3/test_factory.py b/Lib/test/test_sqlite3/test_factory.py index ec463852a7855c..48d35b54a2e239 100644 --- a/Lib/test/test_sqlite3/test_factory.py +++ b/Lib/test/test_sqlite3/test_factory.py @@ -116,13 +116,16 @@ def test_is_produced_by_factory(self): 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 @@ def test_sqlite_row_index(self): 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 @@ def test_sqlite_row_index_unicode(self): 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 @@ def test_sqlite_row_slice(self): 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 @@ def test_sqlite_row_iter(self): 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,8 +239,7 @@ def test_sqlite_row_hash_cmp(self): 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) @@ -251,8 +247,7 @@ def test_sqlite_row_as_sequence(self): self.assertIsInstance(row, Sequence) def test_sqlite_row_keys(self): - # Checks if the row object can return a list of columns as strings - self.con.row_factory = sqlite.Row + # 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']) @@ -262,7 +257,6 @@ def test_fake_cursor_class(self): # 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(), ())