@@ -310,11 +310,8 @@ def mn_client_v1_v2(request):
310
310
@pytest .fixture (scope = 'session' , autouse = True )
311
311
def set_unique_sciobj_store_path (request ):
312
312
tmp_store_path = os .path .join (
313
- tempfile .gettempdir (), 'gmn_test_obj_store_{}{}' .format (
314
- d1_test .instance_generator .random_data .random_lower_ascii (
315
- min_len = 12 , max_len = 12
316
- ), get_xdist_suffix (request )
317
- )
313
+ tempfile .gettempdir (),
314
+ 'gmn_test_obj_store_{}' .format (get_xdist_unique_suffix (request ))
318
315
)
319
316
django .conf .settings .OBJECT_STORE_PATH = tmp_store_path
320
317
d1_gmn .app .sciobj_store .create_clean_tmp_store ()
@@ -326,46 +323,50 @@ def set_unique_sciobj_store_path(request):
326
323
@pytest .yield_fixture (scope = 'session' )
327
324
def django_db_setup (request , django_db_blocker ):
328
325
"""Set up DB fixture
326
+ When running in parallel with xdist, this is called once for each worker.
327
+ By default, the number of workers is the same as the number of CPU cores.
329
328
"""
330
329
logging .info ('Setting up DB fixture' )
331
330
332
331
test_db_key = 'default'
333
- test_db_name = django .conf .settings .DATABASES [test_db_key ]['NAME' ]
334
- test_db_name += get_xdist_suffix (request )
332
+ test_db_name = '' .join ([
333
+ django .conf .settings .DATABASES [test_db_key ]['NAME' ],
334
+ get_xdist_unique_suffix (request ),
335
+ ])
335
336
django .conf .settings .DATABASES [test_db_key ]['NAME' ] = test_db_name
336
337
337
338
template_db_key = 'template'
338
339
template_db_name = django .conf .settings .DATABASES [template_db_key ]['NAME' ]
339
340
340
341
with django_db_blocker .unblock ():
342
+
341
343
if pytest .config .getoption ('--fixture-regen' ):
342
344
drop_database (test_db_name )
343
345
create_blank_db (test_db_key , test_db_name )
344
346
django .db .connections [test_db_key ].commit ()
345
347
pytest .exit ('Database dropped and reinitialized. Now run mk_db_fixture' )
346
348
347
- try :
348
- load_template_fixture (template_db_key , template_db_name )
349
- except psycopg2 .DatabaseError as e :
350
- logging .error (str (e ))
349
+ # try:
350
+ # load_template_fixture(template_db_key, template_db_name)
351
+ # except psycopg2.DatabaseError as e:
352
+ # logging.error(str(e))
353
+
351
354
drop_database (test_db_name )
352
355
create_db_from_template (test_db_name , template_db_name )
353
- # Haven't found out how to prevent transactions from being started, so
354
- # closing the implicit transaction here so that template fixture remains
355
- # available.
356
- django .db .connections [test_db_key ].commit ()
356
+
357
+ # # Haven't found out how to prevent transactions from being started, so
358
+ # # closing the implicit transaction here so that template fixture remains
359
+ # # available.
360
+ # django.db.connections[test_db_key].commit()
361
+
357
362
migrate_db (test_db_key )
358
- logging . debug ( 'Test DB ready' )
363
+
359
364
yield
360
365
366
+ for connection in django .db .connections .all ():
367
+ connection .close ()
361
368
362
- def get_xdist_suffix (request ):
363
- """When running in parallel with xdist, each thread gets a different suffix.
364
- - In parallel run, return '_gw1', etc.
365
- - In single run, return ''.
366
- """
367
- s = getattr (request .config , 'slaveinput' , {}).get ('slaveid' )
368
- return '_{}' .format (s ) if s is not None else ''
369
+ drop_database (test_db_name )
369
370
370
371
371
372
def create_db_from_template (test_db_name , template_db_name ):
@@ -407,6 +408,10 @@ def migrate_db(test_db_key):
407
408
408
409
def drop_database (db_name ):
409
410
logging .debug ('Dropping database: {}' .format (db_name ))
411
+
412
+ for connection in django .db .connections .all ():
413
+ connection .close ()
414
+
410
415
run_sql ('postgres' , 'drop database if exists {};' .format (db_name ))
411
416
412
417
@@ -436,3 +441,20 @@ def run_sql(db, sql):
436
441
conn .close ()
437
442
for connection in django .db .connections .all ():
438
443
connection .close ()
444
+
445
+
446
+ def get_xdist_unique_suffix (request ):
447
+ return '' .join ([
448
+ d1_test .instance_generator .random_data .random_lower_ascii (
449
+ min_len = 12 , max_len = 12
450
+ ), get_xdist_suffix (request )
451
+ ])
452
+
453
+
454
+ def get_xdist_suffix (request ):
455
+ """When running in parallel with xdist, each thread gets a different suffix.
456
+ - In parallel run, return '_gw1', etc.
457
+ - In single run, return ''.
458
+ """
459
+ s = getattr (request .config , 'slaveinput' , {}).get ('slaveid' )
460
+ return '_{}' .format (s ) if s is not None else ''
0 commit comments