@@ -111,14 +111,14 @@ def tearDown(self):
111
111
class RowFactoryTests (unittest .TestCase ):
112
112
def setUp (self ):
113
113
self .con = sqlite .connect (":memory:" )
114
+ self .con .row_factory = sqlite .Row
114
115
115
116
def test_custom_factory (self ):
116
117
self .con .row_factory = lambda cur , row : list (row )
117
118
row = self .con .execute ("select 1, 2" ).fetchone ()
118
119
self .assertIsInstance (row , list )
119
120
120
121
def test_sqlite_row_index (self ):
121
- self .con .row_factory = sqlite .Row
122
122
row = self .con .execute ("select 1 as a_1, 2 as b" ).fetchone ()
123
123
self .assertIsInstance (row , sqlite .Row )
124
124
@@ -149,7 +149,6 @@ def test_sqlite_row_index(self):
149
149
row [complex ()] # index must be int or string
150
150
151
151
def test_sqlite_row_index_unicode (self ):
152
- self .con .row_factory = sqlite .Row
153
152
row = self .con .execute ("select 1 as \xff " ).fetchone ()
154
153
self .assertEqual (row ["\xff " ], 1 )
155
154
with self .assertRaises (IndexError ):
@@ -159,7 +158,6 @@ def test_sqlite_row_index_unicode(self):
159
158
160
159
def test_sqlite_row_slice (self ):
161
160
# A sqlite.Row can be sliced like a list.
162
- self .con .row_factory = sqlite .Row
163
161
row = self .con .execute ("select 1, 2, 3, 4" ).fetchone ()
164
162
self .assertEqual (row [0 :0 ], ())
165
163
self .assertEqual (row [0 :1 ], (1 ,))
@@ -176,8 +174,7 @@ def test_sqlite_row_slice(self):
176
174
self .assertEqual (row [3 :0 :- 2 ], (4 , 2 ))
177
175
178
176
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.
181
178
row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
182
179
183
180
# Is iterable in correct order and produces valid results:
@@ -189,23 +186,20 @@ def test_sqlite_row_iter(self):
189
186
self .assertEqual (items , [1 , 2 ])
190
187
191
188
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.
194
190
row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
195
191
t = tuple (row )
196
192
self .assertEqual (t , (row ['a' ], row ['b' ]))
197
193
198
194
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.
201
196
row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
202
197
d = dict (row )
203
198
self .assertEqual (d ["a" ], row ["a" ])
204
199
self .assertEqual (d ["b" ], row ["b" ])
205
200
206
201
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.
209
203
row_1 = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
210
204
row_2 = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
211
205
row_3 = self .con .execute ("select 1 as a, 3 as b" ).fetchone ()
@@ -238,21 +232,24 @@ def test_sqlite_row_hash_cmp(self):
238
232
self .assertEqual (hash (row_1 ), hash (row_2 ))
239
233
240
234
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.
243
236
row = self .con .execute ("select 1 as a, 2 as b" ).fetchone ()
244
237
245
238
as_tuple = tuple (row )
246
239
self .assertEqual (list (reversed (row )), list (reversed (as_tuple )))
247
240
self .assertIsInstance (row , Sequence )
248
241
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
+
249
247
def test_fake_cursor_class (self ):
250
248
# Issue #24257: Incorrect use of PyObject_IsInstance() caused
251
249
# segmentation fault.
252
250
# Issue #27861: Also applies for cursor factory.
253
251
class FakeCursor (str ):
254
252
__class__ = sqlite .Cursor
255
- self .con .row_factory = sqlite .Row
256
253
self .assertRaises (TypeError , self .con .cursor , FakeCursor )
257
254
self .assertRaises (TypeError , sqlite .Row , FakeCursor (), ())
258
255
0 commit comments