@@ -112,13 +112,16 @@ class RowFactoryTests(unittest.TestCase):
112112 def setUp (self ):
113113 self .con = sqlite .connect (":memory:" )
114114
115+ def setUp (self ):
116+ super ().setUp ()
117+ self .con .row_factory = sqlite .Row
118+
115119 def test_custom_factory (self ):
116120 self .con .row_factory = lambda cur , row : list (row )
117121 row = self .con .execute ("select 1, 2" ).fetchone ()
118122 self .assertIsInstance (row , list )
119123
120124 def test_sqlite_row_index (self ):
121- self .con .row_factory = sqlite .Row
122125 row = self .con .execute ("select 1 as a_1, 2 as b" ).fetchone ()
123126 self .assertIsInstance (row , sqlite .Row )
124127
@@ -149,7 +152,6 @@ def test_sqlite_row_index(self):
149152 row [complex ()] # index must be int or string
150153
151154 def test_sqlite_row_index_unicode (self ):
152- self .con .row_factory = sqlite .Row
153155 row = self .con .execute ("select 1 as \xff " ).fetchone ()
154156 self .assertEqual (row ["\xff " ], 1 )
155157 with self .assertRaises (IndexError ):
@@ -159,7 +161,6 @@ def test_sqlite_row_index_unicode(self):
159161
160162 def test_sqlite_row_slice (self ):
161163 # A sqlite.Row can be sliced like a list.
162- self .con .row_factory = sqlite .Row
163164 row = self .con .execute ("select 1, 2, 3, 4" ).fetchone ()
164165 self .assertEqual (row [0 :0 ], ())
165166 self .assertEqual (row [0 :1 ], (1 ,))
@@ -176,8 +177,7 @@ def test_sqlite_row_slice(self):
176177 self .assertEqual (row [3 :0 :- 2 ], (4 , 2 ))
177178
178179 def test_sqlite_row_iter (self ):
179- """Checks if the row object is iterable"""
180- self .con .row_factory = sqlite .Row
180+ # Checks if the row object is iterable.
181181 row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
182182
183183 # Is iterable in correct order and produces valid results:
@@ -189,23 +189,20 @@ def test_sqlite_row_iter(self):
189189 self .assertEqual (items , [1 , 2 ])
190190
191191 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
192+ # Checks if the row object can be converted to a tuple.
194193 row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
195194 t = tuple (row )
196195 self .assertEqual (t , (row ['a' ], row ['b' ]))
197196
198197 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
198+ # Checks if the row object can be correctly converted to a dictionary.
201199 row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
202200 d = dict (row )
203201 self .assertEqual (d ["a" ], row ["a" ])
204202 self .assertEqual (d ["b" ], row ["b" ])
205203
206204 def test_sqlite_row_hash_cmp (self ):
207- """Checks if the row object compares and hashes correctly"""
208- self .con .row_factory = sqlite .Row
205+ # Checks if the row object compares and hashes correctly.
209206 row_1 = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
210207 row_2 = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
211208 row_3 = self .con .execute ("select 1 as a, 3 as b" ).fetchone ()
@@ -238,21 +235,24 @@ def test_sqlite_row_hash_cmp(self):
238235 self .assertEqual (hash (row_1 ), hash (row_2 ))
239236
240237 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
238+ # Checks if the row object can act like a sequence.
243239 row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
244240
245241 as_tuple = tuple (row )
246242 self .assertEqual (list (reversed (row )), list (reversed (as_tuple )))
247243 self .assertIsInstance (row , Sequence )
248244
245+ def test_sqlite_row_keys (self ):
246+ # Checks if the row object can return a list of columns as strings.
247+ row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
248+ self .assertEqual (row .keys (), ['a' , 'b' ])
249+
249250 def test_fake_cursor_class (self ):
250251 # Issue #24257: Incorrect use of PyObject_IsInstance() caused
251252 # segmentation fault.
252253 # Issue #27861: Also applies for cursor factory.
253254 class FakeCursor (str ):
254255 __class__ = sqlite .Cursor
255- self .con .row_factory = sqlite .Row
256256 self .assertRaises (TypeError , self .con .cursor , FakeCursor )
257257 self .assertRaises (TypeError , sqlite .Row , FakeCursor (), ())
258258
0 commit comments