5858D1_SKIP_LIST = 'skip_passed/list'
5959D1_SKIP_COUNT = 'skip_passed/count'
6060
61+ # Dict lookup keys matching the keys in settings_test.DATABASE
6162TEMPLATE_DB_KEY = 'template'
6263TEST_DB_KEY = 'default'
6364
64- GMN_DB_FIXTURE_LABEL = 'db_fixture'
65-
6665# Allow redefinition of functions. Pytest allows multiple hooks with the same
6766# name.
6867# flake8: noqa: F811
6968
70- # template_db_lock = threading.Lock()
71- template_db_lock = multiprocessing .Lock ()
72-
7369# Hack to get access to print and logging output when running under pytest-xdist
7470# and pytest-catchlog. Without this, only output from failed tests is displayed.
7571sys .stdout = sys .stderr
@@ -112,7 +108,7 @@ def pytest_addoption(parser):
112108
113109 parser .addoption (
114110 '--fixture-refresh' , action = 'store_true' ,
115- help = 'Drop and create GMN template database from JSON fixture file'
111+ help = 'Drop and recreate GMN template database from JSON fixture file'
116112 )
117113
118114 # Skip passed tests
@@ -417,7 +413,8 @@ def enable_db_access(db):
417413
418414
419415@pytest .yield_fixture (scope = 'session' , autouse = True )
420- def django_db_setup (request ):
416+ @pytest .mark .django_db
417+ def django_db_setup (request , django_db_blocker ):
421418 """Set up DB fixture
422419 When running in parallel with xdist, this is called once for each worker,
423420 causing a separate database to be set up for each worker.
@@ -426,46 +423,47 @@ def django_db_setup(request):
426423
427424 db_set_unique_db_name (request )
428425
429- # Regular multiprocessing.Lock() context manager did not work here. Also
430- # tried creating the lock at module scope, and also directly calling
431- # acquire() and release(). It's probably related to how the worker processes
432- # relate to each other when launched by pytest-xdist as compared to what the
433- # multiprocessing module expects.
434- with posix_ipc .Semaphore (
435- '/{}' .format (__name__ ), flags = posix_ipc .O_CREAT , initial_value = 1
436- ):
437- logging .warning (
438- 'LOCK BEGIN {} {}' .format (
439- db_get_name_by_key (TEMPLATE_DB_KEY ), d1_common .date_time .utc_now ()
426+ with django_db_blocker .unblock ():
427+ # Regular multiprocessing.Lock() context manager did not work here. Also
428+ # tried creating the lock at module scope, and also directly calling
429+ # acquire() and release(). It's probably related to how the worker processes
430+ # relate to each other when launched by pytest-xdist as compared to what the
431+ # multiprocessing module expects.
432+ with posix_ipc .Semaphore (
433+ '/{}' .format (__name__ ), flags = posix_ipc .O_CREAT , initial_value = 1
434+ ):
435+ logging .warning (
436+ 'LOCK BEGIN {} {}' .format (
437+ db_get_name_by_key (TEMPLATE_DB_KEY ), d1_common .date_time .utc_now ()
438+ )
440439 )
441- )
442440
443- if not db_exists (TEMPLATE_DB_KEY ):
444- db_create_blank (TEMPLATE_DB_KEY )
445- db_migrate (TEMPLATE_DB_KEY )
446- db_populate_by_json (TEMPLATE_DB_KEY )
447- db_migrate (TEMPLATE_DB_KEY )
441+ if not db_exists (TEMPLATE_DB_KEY ):
442+ db_create_blank (TEMPLATE_DB_KEY )
443+ db_migrate (TEMPLATE_DB_KEY )
444+ db_populate_by_json (TEMPLATE_DB_KEY )
445+ db_migrate (TEMPLATE_DB_KEY )
448446
449- logging .warning (
450- 'LOCK END {} {}' .format (
451- db_get_name_by_key (TEMPLATE_DB_KEY ), d1_common .date_time .utc_now ()
447+ logging .warning (
448+ 'LOCK END {} {}' .format (
449+ db_get_name_by_key (TEMPLATE_DB_KEY ), d1_common .date_time .utc_now ()
450+ )
452451 )
453- )
454452
455- db_drop (TEST_DB_KEY )
456- db_create_from_template ()
457- # db_migrate(TEST_DB_KEY)
453+ db_drop (TEST_DB_KEY )
454+ db_create_from_template ()
455+ # db_migrate(TEST_DB_KEY)
458456
459- # Haven't found out how to prevent transactions from being started. so
460- # closing the implicit transaction here so that template fixture remains
461- # available.
462- # django.db.connections[test_db_key ].commit()
457+ # Haven't found out how to prevent transactions from being started. so
458+ # closing the implicit transaction here so that template fixture remains
459+ # available.
460+ # django.db.connections[TEST_DB_KEY ].commit()
463461
464- logging . debug (django .conf .settings )
462+ # print (django.conf.settings)
465463
466- yield
464+ yield
467465
468- db_drop (TEST_DB_KEY )
466+ db_drop (TEST_DB_KEY )
469467
470468
471469def db_get_name_by_key (db_key ):
@@ -501,8 +499,10 @@ def db_populate_by_json(db_key):
501499 logging .debug ('db_populate_by_json() {}' .format (db_key ))
502500 django .core .management .call_command (
503501 'loaddata' ,
504- GMN_DB_FIXTURE_LABEL ,
505- database = db_key #, commit=True
502+ d1_test .sample .get_path ('db_fixture.json.bz2' ),
503+ database = db_key ,
504+ # verbosity=0,
505+ # commit=False,
506506 )
507507 db_commit_and_close (db_key )
508508
0 commit comments