Skip to content

Commit 1478a38

Browse files
miss-islingtonEddInSverigeerlend-aasland
authored
[3.12] gh-108558: Improve sqlite3 row factory tests (GH-108578) (#108615)
* gh-108558: Improve sqlite3 row factory tests (GH-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 (cherry picked from commit 6eaddc1) Co-authored-by: Edward Schauman-Haigh <[email protected]> Co-authored-by: Erlend E. Aasland <[email protected]> * Fix backport --------- Co-authored-by: Edward Schauman-Haigh <[email protected]> Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent cdca4af commit 1478a38

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

Lib/test/test_sqlite3/test_factory.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ def tearDown(self):
111111
class RowFactoryTests(unittest.TestCase):
112112
def setUp(self):
113113
self.con = sqlite.connect(":memory:")
114+
self.con.row_factory = sqlite.Row
114115

115116
def test_custom_factory(self):
116117
self.con.row_factory = lambda cur, row: list(row)
117118
row = self.con.execute("select 1, 2").fetchone()
118119
self.assertIsInstance(row, list)
119120

120121
def test_sqlite_row_index(self):
121-
self.con.row_factory = sqlite.Row
122122
row = self.con.execute("select 1 as a_1, 2 as b").fetchone()
123123
self.assertIsInstance(row, sqlite.Row)
124124

@@ -149,7 +149,6 @@ def test_sqlite_row_index(self):
149149
row[complex()] # index must be int or string
150150

151151
def test_sqlite_row_index_unicode(self):
152-
self.con.row_factory = sqlite.Row
153152
row = self.con.execute("select 1 as \xff").fetchone()
154153
self.assertEqual(row["\xff"], 1)
155154
with self.assertRaises(IndexError):
@@ -159,7 +158,6 @@ def test_sqlite_row_index_unicode(self):
159158

160159
def test_sqlite_row_slice(self):
161160
# A sqlite.Row can be sliced like a list.
162-
self.con.row_factory = sqlite.Row
163161
row = self.con.execute("select 1, 2, 3, 4").fetchone()
164162
self.assertEqual(row[0:0], ())
165163
self.assertEqual(row[0:1], (1,))
@@ -176,8 +174,7 @@ def test_sqlite_row_slice(self):
176174
self.assertEqual(row[3:0:-2], (4, 2))
177175

178176
def test_sqlite_row_iter(self):
179-
"""Checks if the row object is iterable"""
180-
self.con.row_factory = sqlite.Row
177+
# Checks if the row object is iterable.
181178
row = self.con.execute("select 1 as a, 2 as b").fetchone()
182179

183180
# Is iterable in correct order and produces valid results:
@@ -189,23 +186,20 @@ def test_sqlite_row_iter(self):
189186
self.assertEqual(items, [1, 2])
190187

191188
def test_sqlite_row_as_tuple(self):
192-
"""Checks if the row object can be converted to a tuple"""
193-
self.con.row_factory = sqlite.Row
189+
# Checks if the row object can be converted to a tuple.
194190
row = self.con.execute("select 1 as a, 2 as b").fetchone()
195191
t = tuple(row)
196192
self.assertEqual(t, (row['a'], row['b']))
197193

198194
def test_sqlite_row_as_dict(self):
199-
"""Checks if the row object can be correctly converted to a dictionary"""
200-
self.con.row_factory = sqlite.Row
195+
# Checks if the row object can be correctly converted to a dictionary.
201196
row = self.con.execute("select 1 as a, 2 as b").fetchone()
202197
d = dict(row)
203198
self.assertEqual(d["a"], row["a"])
204199
self.assertEqual(d["b"], row["b"])
205200

206201
def test_sqlite_row_hash_cmp(self):
207-
"""Checks if the row object compares and hashes correctly"""
208-
self.con.row_factory = sqlite.Row
202+
# Checks if the row object compares and hashes correctly.
209203
row_1 = self.con.execute("select 1 as a, 2 as b").fetchone()
210204
row_2 = self.con.execute("select 1 as a, 2 as b").fetchone()
211205
row_3 = self.con.execute("select 1 as a, 3 as b").fetchone()
@@ -238,21 +232,24 @@ def test_sqlite_row_hash_cmp(self):
238232
self.assertEqual(hash(row_1), hash(row_2))
239233

240234
def test_sqlite_row_as_sequence(self):
241-
""" Checks if the row object can act like a sequence """
242-
self.con.row_factory = sqlite.Row
235+
# Checks if the row object can act like a sequence.
243236
row = self.con.execute("select 1 as a, 2 as b").fetchone()
244237

245238
as_tuple = tuple(row)
246239
self.assertEqual(list(reversed(row)), list(reversed(as_tuple)))
247240
self.assertIsInstance(row, Sequence)
248241

242+
def test_sqlite_row_keys(self):
243+
# Checks if the row object can return a list of columns as strings.
244+
row = self.con.execute("select 1 as a, 2 as b").fetchone()
245+
self.assertEqual(row.keys(), ['a', 'b'])
246+
249247
def test_fake_cursor_class(self):
250248
# Issue #24257: Incorrect use of PyObject_IsInstance() caused
251249
# segmentation fault.
252250
# Issue #27861: Also applies for cursor factory.
253251
class FakeCursor(str):
254252
__class__ = sqlite.Cursor
255-
self.con.row_factory = sqlite.Row
256253
self.assertRaises(TypeError, self.con.cursor, FakeCursor)
257254
self.assertRaises(TypeError, sqlite.Row, FakeCursor(), ())
258255

0 commit comments

Comments
 (0)