10
10
from collections import OrderedDict
11
11
from nose .tools import raises
12
12
13
-
14
- OUTPUT = "response_test .xls"
13
+ FILE_NAME = "response_test"
14
+ OUTPUT = "%s .xls" % FILE_NAME
15
15
16
16
17
17
class TestInput (webio .ExcelInput ):
@@ -31,11 +31,10 @@ def get_file_tuple(self, field_name):
31
31
return field_name
32
32
33
33
34
- def dumpy_response (content , content_type = None , status = 200 ):
34
+ def dumpy_response (content , content_type = None , status = 200 , file_name = None ):
35
35
"""A dummy response"""
36
- f = open (OUTPUT , 'wb' )
37
- f .write (content )
38
- f .close ()
36
+ with open (file_name , 'wb' ) as f :
37
+ f .write (content )
39
38
40
39
41
40
webio .ExcelResponse = dumpy_response
@@ -236,19 +235,19 @@ def setUp(self):
236
235
237
236
def test_make_response_from_sheet (self ):
238
237
sheet = pe .Sheet (self .data )
239
- webio .make_response (sheet , "xls" )
238
+ webio .make_response (sheet , "xls" , file_name = FILE_NAME )
240
239
self .verify ()
241
240
242
241
def test_make_response_from_array (self ):
243
- webio .make_response_from_array (self .data , "xls" )
242
+ webio .make_response_from_array (self .data , "xls" , file_name = FILE_NAME )
244
243
self .verify ()
245
244
246
245
def test_make_response_from_records (self ):
247
246
records = [
248
247
{"X" : 1 , "Y" : 2 , "Z" : 3 },
249
248
{"X" : 4 , "Y" : 5 , "Z" : 6 }
250
249
]
251
- webio .make_response_from_records (records , "xls" )
250
+ webio .make_response_from_records (records , "xls" , file_name = FILE_NAME )
252
251
self .verify ()
253
252
254
253
def test_make_response_from_dict (self ):
@@ -257,7 +256,7 @@ def test_make_response_from_dict(self):
257
256
"Y" : [2 , 5 ],
258
257
"Z" : [3 , 6 ]
259
258
}
260
- webio .make_response_from_dict (adict , "xls" )
259
+ webio .make_response_from_dict (adict , "xls" , file_name = FILE_NAME )
261
260
self .verify ()
262
261
263
262
def test_make_response_from_table (self ):
@@ -269,7 +268,7 @@ def test_make_response_from_table(self):
269
268
session .add (row1 )
270
269
session .add (row2 )
271
270
session .commit ()
272
- webio .make_response_from_a_table (session , Signature , "xls" )
271
+ webio .make_response_from_a_table (session , Signature , "xls" , file_name = FILE_NAME )
273
272
self .verify ()
274
273
session .close ()
275
274
@@ -284,7 +283,7 @@ def test_make_response_from_query_sets(self):
284
283
session .commit ()
285
284
query_sets = session .query (Signature ).filter_by (X = 1 ).all ()
286
285
column_names = ["X" , "Y" , "Z" ]
287
- webio .make_response_from_query_sets (query_sets , column_names , "xls" )
286
+ webio .make_response_from_query_sets (query_sets , column_names , "xls" , file_name = FILE_NAME )
288
287
sheet2 = pe .get_sheet (file_name = OUTPUT )
289
288
assert sheet2 .to_array () == [
290
289
["X" , "Y" , "Z" ],
@@ -310,11 +309,11 @@ def setUp(self):
310
309
311
310
def test_make_response_from_book (self ):
312
311
book = pe .get_book (bookdict = self .content )
313
- webio .make_response (book , "xls" )
312
+ webio .make_response (book , "xls" , file_name = FILE_NAME )
314
313
self .verify ()
315
314
316
315
def test_make_response_from_book_dict (self ):
317
- webio .make_response_from_book_dict (self .content , "xls" )
316
+ webio .make_response_from_book_dict (self .content , "xls" , file_name = FILE_NAME )
318
317
self .verify ()
319
318
320
319
def verify (self ):
@@ -340,7 +339,7 @@ def test_make_response_from_tables(self):
340
339
session .add (row3 )
341
340
session .add (row4 )
342
341
session .commit ()
343
- webio .make_response_from_tables (session , [Signature , Signature2 ], "xls" )
342
+ webio .make_response_from_tables (session , [Signature , Signature2 ], "xls" , file_name = FILE_NAME )
344
343
book = pe .get_book (file_name = OUTPUT )
345
344
expected = OrderedDict ()
346
345
expected .update ({'signature' : [['X' , 'Y' , 'Z' ], [1 , 2 , 3 ], [4 , 5 , 6 ]]})
0 commit comments