@@ -116,13 +116,16 @@ def test_is_produced_by_factory(self):
116
116
117
117
class RowFactoryTests (MemoryDatabaseMixin , unittest .TestCase ):
118
118
119
+ def setUp (self ):
120
+ super ().setUp ()
121
+ self .con .row_factory = sqlite .Row
122
+
119
123
def test_custom_factory (self ):
120
124
self .con .row_factory = lambda cur , row : list (row )
121
125
row = self .con .execute ("select 1, 2" ).fetchone ()
122
126
self .assertIsInstance (row , list )
123
127
124
128
def test_sqlite_row_index (self ):
125
- self .con .row_factory = sqlite .Row
126
129
row = self .con .execute ("select 1 as a_1, 2 as b" ).fetchone ()
127
130
self .assertIsInstance (row , sqlite .Row )
128
131
@@ -153,7 +156,6 @@ def test_sqlite_row_index(self):
153
156
row [complex ()] # index must be int or string
154
157
155
158
def test_sqlite_row_index_unicode (self ):
156
- self .con .row_factory = sqlite .Row
157
159
row = self .con .execute ("select 1 as \xff " ).fetchone ()
158
160
self .assertEqual (row ["\xff " ], 1 )
159
161
with self .assertRaises (IndexError ):
@@ -163,7 +165,6 @@ def test_sqlite_row_index_unicode(self):
163
165
164
166
def test_sqlite_row_slice (self ):
165
167
# A sqlite.Row can be sliced like a list.
166
- self .con .row_factory = sqlite .Row
167
168
row = self .con .execute ("select 1, 2, 3, 4" ).fetchone ()
168
169
self .assertEqual (row [0 :0 ], ())
169
170
self .assertEqual (row [0 :1 ], (1 ,))
@@ -180,8 +181,7 @@ def test_sqlite_row_slice(self):
180
181
self .assertEqual (row [3 :0 :- 2 ], (4 , 2 ))
181
182
182
183
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.
185
185
row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
186
186
187
187
# Is iterable in correct order and produces valid results:
@@ -193,23 +193,20 @@ def test_sqlite_row_iter(self):
193
193
self .assertEqual (items , [1 , 2 ])
194
194
195
195
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.
198
197
row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
199
198
t = tuple (row )
200
199
self .assertEqual (t , (row ['a' ], row ['b' ]))
201
200
202
201
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.
205
203
row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
206
204
d = dict (row )
207
205
self .assertEqual (d ["a" ], row ["a" ])
208
206
self .assertEqual (d ["b" ], row ["b" ])
209
207
210
208
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.
213
210
row_1 = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
214
211
row_2 = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
215
212
row_3 = self .con .execute ("select 1 as a, 3 as b" ).fetchone ()
@@ -242,21 +239,24 @@ def test_sqlite_row_hash_cmp(self):
242
239
self .assertEqual (hash (row_1 ), hash (row_2 ))
243
240
244
241
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.
247
243
row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
248
244
249
245
as_tuple = tuple (row )
250
246
self .assertEqual (list (reversed (row )), list (reversed (as_tuple )))
251
247
self .assertIsInstance (row , Sequence )
252
248
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
+
253
254
def test_fake_cursor_class (self ):
254
255
# Issue #24257: Incorrect use of PyObject_IsInstance() caused
255
256
# segmentation fault.
256
257
# Issue #27861: Also applies for cursor factory.
257
258
class FakeCursor (str ):
258
259
__class__ = sqlite .Cursor
259
- self .con .row_factory = sqlite .Row
260
260
self .assertRaises (TypeError , self .con .cursor , FakeCursor )
261
261
self .assertRaises (TypeError , sqlite .Row , FakeCursor (), ())
262
262
0 commit comments