@@ -116,13 +116,16 @@ def test_is_produced_by_factory(self):
116116
117117class RowFactoryTests (MemoryDatabaseMixin , unittest .TestCase ):
118118
119+ def setUp (self ):
120+ super ().setUp ()
121+ self .con .row_factory = sqlite .Row
122+
119123 def test_custom_factory (self ):
120124 self .con .row_factory = lambda cur , row : list (row )
121125 row = self .con .execute ("select 1, 2" ).fetchone ()
122126 self .assertIsInstance (row , list )
123127
124128 def test_sqlite_row_index (self ):
125- self .con .row_factory = sqlite .Row
126129 row = self .con .execute ("select 1 as a_1, 2 as b" ).fetchone ()
127130 self .assertIsInstance (row , sqlite .Row )
128131
@@ -153,7 +156,6 @@ def test_sqlite_row_index(self):
153156 row [complex ()] # index must be int or string
154157
155158 def test_sqlite_row_index_unicode (self ):
156- self .con .row_factory = sqlite .Row
157159 row = self .con .execute ("select 1 as \xff " ).fetchone ()
158160 self .assertEqual (row ["\xff " ], 1 )
159161 with self .assertRaises (IndexError ):
@@ -163,7 +165,6 @@ def test_sqlite_row_index_unicode(self):
163165
164166 def test_sqlite_row_slice (self ):
165167 # A sqlite.Row can be sliced like a list.
166- self .con .row_factory = sqlite .Row
167168 row = self .con .execute ("select 1, 2, 3, 4" ).fetchone ()
168169 self .assertEqual (row [0 :0 ], ())
169170 self .assertEqual (row [0 :1 ], (1 ,))
@@ -180,8 +181,7 @@ def test_sqlite_row_slice(self):
180181 self .assertEqual (row [3 :0 :- 2 ], (4 , 2 ))
181182
182183 def test_sqlite_row_iter (self ):
183- """Checks if the row object is iterable"""
184- self .con .row_factory = sqlite .Row
184+ # Checks if the row object is iterable.
185185 row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
186186
187187 # Is iterable in correct order and produces valid results:
@@ -193,23 +193,20 @@ def test_sqlite_row_iter(self):
193193 self .assertEqual (items , [1 , 2 ])
194194
195195 def test_sqlite_row_as_tuple (self ):
196- """Checks if the row object can be converted to a tuple"""
197- self .con .row_factory = sqlite .Row
196+ # Checks if the row object can be converted to a tuple.
198197 row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
199198 t = tuple (row )
200199 self .assertEqual (t , (row ['a' ], row ['b' ]))
201200
202201 def test_sqlite_row_as_dict (self ):
203- """Checks if the row object can be correctly converted to a dictionary"""
204- self .con .row_factory = sqlite .Row
202+ # Checks if the row object can be correctly converted to a dictionary.
205203 row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
206204 d = dict (row )
207205 self .assertEqual (d ["a" ], row ["a" ])
208206 self .assertEqual (d ["b" ], row ["b" ])
209207
210208 def test_sqlite_row_hash_cmp (self ):
211- """Checks if the row object compares and hashes correctly"""
212- self .con .row_factory = sqlite .Row
209+ # Checks if the row object compares and hashes correctly.
213210 row_1 = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
214211 row_2 = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
215212 row_3 = self .con .execute ("select 1 as a, 3 as b" ).fetchone ()
@@ -242,21 +239,24 @@ def test_sqlite_row_hash_cmp(self):
242239 self .assertEqual (hash (row_1 ), hash (row_2 ))
243240
244241 def test_sqlite_row_as_sequence (self ):
245- """ Checks if the row object can act like a sequence """
246- self .con .row_factory = sqlite .Row
242+ # Checks if the row object can act like a sequence.
247243 row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
248244
249245 as_tuple = tuple (row )
250246 self .assertEqual (list (reversed (row )), list (reversed (as_tuple )))
251247 self .assertIsInstance (row , Sequence )
252248
249+ def test_sqlite_row_keys (self ):
250+ # Checks if the row object can return a list of columns as strings.
251+ row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
252+ self .assertEqual (row .keys (), ['a' , 'b' ])
253+
253254 def test_fake_cursor_class (self ):
254255 # Issue #24257: Incorrect use of PyObject_IsInstance() caused
255256 # segmentation fault.
256257 # Issue #27861: Also applies for cursor factory.
257258 class FakeCursor (str ):
258259 __class__ = sqlite .Cursor
259- self .con .row_factory = sqlite .Row
260260 self .assertRaises (TypeError , self .con .cursor , FakeCursor )
261261 self .assertRaises (TypeError , sqlite .Row , FakeCursor (), ())
262262
0 commit comments