1
1
import csv
2
- import os
3
- import tempfile
4
2
3
+ from django .core .files .storage import default_storage
5
4
from django .core .management import call_command
6
5
from django .core .management .base import CommandError
7
6
from django .test import TestCase
@@ -137,11 +136,8 @@ def test_create_user_not_exist_bad_username(self):
137
136
138
137
class DeviceNotSetup (TestCase ):
139
138
def test_device_not_setup (self ):
140
- csvfile , csvpath = tempfile .mkstemp (suffix = "csv" )
141
139
with self .assertRaisesRegex (CommandError , "No default facility exists" ):
142
- call_command ("importusers" , csvpath )
143
- os .close (csvfile )
144
- os .remove (csvpath )
140
+ call_command ("importusers" , "you_cannot_import_yet.csv" )
145
141
146
142
147
143
class UserImportCommandTestCase (TestCase ):
@@ -157,34 +153,32 @@ def setUpClass(self):
157
153
self .facility , self .superuser = setup_device ()
158
154
159
155
def setUp (self ):
160
- self .csvfile , self . csvpath = tempfile . mkstemp ( suffix = " csv" )
156
+ self .csvfilename = default_storage . get_available_name ( "test. csv" )
161
157
162
158
def tearDown (self ):
163
159
FacilityUser .objects .exclude (username = self .superuser .username ).delete ()
164
- os .close (self .csvfile )
165
- os .remove (self .csvpath )
160
+ default_storage .delete (self .csvfilename )
166
161
167
162
def importFromRows (self , * args ):
168
- csv_file = open_csv_for_writing (self .csvpath )
169
- with csv_file as f :
163
+ with open_csv_for_writing (self .csvfilename ) as f :
170
164
writer = csv .writer (f )
171
165
writer .writerows ([a for a in args ])
172
166
173
- call_command ("importusers" , self .csvpath )
167
+ call_command ("importusers" , self .csvfilename )
174
168
175
169
def test_setup_headers_no_username (self ):
176
170
with self .assertRaisesRegex (CommandError , "No usernames specified" ):
177
171
self .importFromRows (["class" , "facility" ])
178
- call_command ("importusers" , self .csvpath )
172
+ call_command ("importusers" , self .csvfilename )
179
173
180
174
def test_setup_headers_invalid_header (self ):
181
175
with self .assertRaisesRegex (CommandError , "Mix of valid and invalid header" ):
182
176
self .importFromRows (["class" , "facility" , "dogfood" ])
183
- call_command ("importusers" , self .csvpath )
177
+ call_command ("importusers" , self .csvfilename )
184
178
185
179
def test_setup_headers_make_user (self ):
186
180
self .importFromRows (["username" ], ["testuser" ])
187
- call_command ("importusers" , self .csvpath )
181
+ call_command ("importusers" , self .csvfilename )
188
182
self .assertTrue (FacilityUser .objects .filter (username = "testuser" ).exists ())
189
183
190
184
def test_setup_no_headers_make_user (self ):
@@ -271,10 +265,13 @@ def test_import_from_export_csv(self):
271
265
for user in users :
272
266
FacilityUser .objects .create (facility = self .facility , ** user )
273
267
call_command (
274
- "exportusers" , output_file = self .csvpath , overwrite = True , demographic = True
268
+ "exportusers" ,
269
+ output_file = self .csvfilename ,
270
+ overwrite = True ,
271
+ demographic = True ,
275
272
)
276
273
FacilityUser .objects .all ().delete ()
277
- call_command ("importusers" , self .csvpath )
274
+ call_command ("importusers" , self .csvfilename )
278
275
for user in users :
279
276
user_model = FacilityUser .objects .get (username = user ["username" ])
280
277
self .assertEqual (user_model .gender , user ["gender" ])
@@ -284,16 +281,18 @@ def test_import_from_export_csv(self):
284
281
def test_import_from_export_missing_headers (self ):
285
282
for user in users :
286
283
FacilityUser .objects .create (facility = self .facility , ** user )
284
+
287
285
call_command (
288
- "exportusers" , output_file = self .csvpath , overwrite = True , demographic = True
286
+ "exportusers" ,
287
+ output_file = self .csvfilename ,
288
+ overwrite = True ,
289
+ demographic = True ,
289
290
)
290
291
cols_to_remove = ["Facility id" , "Gender" ]
291
- csv_file = open_csv_for_reading (self .csvpath )
292
- with csv_file as source :
292
+ with open_csv_for_reading (self .csvfilename ) as source :
293
293
reader = csv .DictReader (source )
294
294
rows = [row for row in reader ]
295
- csv_file = open_csv_for_writing (self .csvpath )
296
- with csv_file as result :
295
+ with open_csv_for_writing ("new" + self .csvfilename ) as result :
297
296
writer = csv .DictWriter (
298
297
result ,
299
298
tuple (
@@ -305,8 +304,9 @@ def test_import_from_export_missing_headers(self):
305
304
for col in cols_to_remove :
306
305
del row [col ]
307
306
writer .writerow (row )
307
+
308
308
FacilityUser .objects .all ().delete ()
309
- call_command ("importusers" , self .csvpath )
309
+ call_command ("importusers" , "new" + self .csvfilename )
310
310
for user in users :
311
311
user_model = FacilityUser .objects .get (username = user ["username" ])
312
312
self .assertEqual (user_model .birth_year , user ["birth_year" ])
@@ -316,15 +316,16 @@ def test_import_from_export_mixed_headers(self):
316
316
for user in users :
317
317
FacilityUser .objects .create (facility = self .facility , ** user )
318
318
call_command (
319
- "exportusers" , output_file = self .csvpath , overwrite = True , demographic = True
319
+ "exportusers" ,
320
+ output_file = self .csvfilename ,
321
+ overwrite = True ,
322
+ demographic = True ,
320
323
)
321
324
cols_to_replace = {"Facility id" : "facility" , "Gender" : "gender" }
322
- csv_file = open_csv_for_reading (self .csvpath )
323
- with csv_file as source :
325
+ with open_csv_for_reading (self .csvfilename ) as source :
324
326
reader = csv .DictReader (source )
325
327
rows = [row for row in reader ]
326
- csv_file = open_csv_for_writing (self .csvpath )
327
- with csv_file as result :
328
+ with open_csv_for_writing ("new" + self .csvfilename ) as result :
328
329
writer = csv .DictWriter (
329
330
result ,
330
331
tuple (
@@ -339,7 +340,7 @@ def test_import_from_export_mixed_headers(self):
339
340
del row [col ]
340
341
writer .writerow (row )
341
342
FacilityUser .objects .all ().delete ()
342
- call_command ("importusers" , self .csvpath )
343
+ call_command ("importusers" , "new" + self .csvfilename )
343
344
for user in users :
344
345
user_model = FacilityUser .objects .get (username = user ["username" ])
345
346
self .assertEqual (user_model .birth_year , user ["birth_year" ])
0 commit comments