@@ -310,11 +310,8 @@ def mn_client_v1_v2(request):
310310@pytest .fixture (scope = 'session' , autouse = True )
311311def set_unique_sciobj_store_path (request ):
312312 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 ))
318315 )
319316 django .conf .settings .OBJECT_STORE_PATH = tmp_store_path
320317 d1_gmn .app .sciobj_store .create_clean_tmp_store ()
@@ -326,46 +323,50 @@ def set_unique_sciobj_store_path(request):
326323@pytest .yield_fixture (scope = 'session' )
327324def django_db_setup (request , django_db_blocker ):
328325 """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.
329328 """
330329 logging .info ('Setting up DB fixture' )
331330
332331 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+ ])
335336 django .conf .settings .DATABASES [test_db_key ]['NAME' ] = test_db_name
336337
337338 template_db_key = 'template'
338339 template_db_name = django .conf .settings .DATABASES [template_db_key ]['NAME' ]
339340
340341 with django_db_blocker .unblock ():
342+
341343 if pytest .config .getoption ('--fixture-regen' ):
342344 drop_database (test_db_name )
343345 create_blank_db (test_db_key , test_db_name )
344346 django .db .connections [test_db_key ].commit ()
345347 pytest .exit ('Database dropped and reinitialized. Now run mk_db_fixture' )
346348
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+
351354 drop_database (test_db_name )
352355 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+
357362 migrate_db (test_db_key )
358- logging . debug ( 'Test DB ready' )
363+
359364 yield
360365
366+ for connection in django .db .connections .all ():
367+ connection .close ()
361368
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 )
369370
370371
371372def create_db_from_template (test_db_name , template_db_name ):
@@ -407,6 +408,10 @@ def migrate_db(test_db_key):
407408
408409def drop_database (db_name ):
409410 logging .debug ('Dropping database: {}' .format (db_name ))
411+
412+ for connection in django .db .connections .all ():
413+ connection .close ()
414+
410415 run_sql ('postgres' , 'drop database if exists {};' .format (db_name ))
411416
412417
@@ -436,3 +441,20 @@ def run_sql(db, sql):
436441 conn .close ()
437442 for connection in django .db .connections .all ():
438443 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