From 7f4f373e89e01acca12362aebca42ec8715736cc Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 29 Nov 2019 15:26:08 +0100 Subject: [PATCH 01/13] tests: load schema2 fixture only one time (#38067) Use special db fixture (using pytest-django primitives and transaction.atomic()) to initialize bijoe visualization only one time for all tests on the schema2 fixture. It improves tests run time. --- tests/conftest.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index e9ec155..25a7eb6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,7 @@ import os import glob import json -from contextlib import closing +from contextlib import closing, contextmanager import subprocess import tempfile import shutil @@ -13,7 +13,7 @@ import django_webtest import psycopg2 -from django.db import connection +from django.db import transaction from django.contrib.auth.models import User from django.core.management import call_command @@ -49,6 +49,7 @@ def admin(db): SCHEMA_PATHS = os.path.join(os.path.dirname(__file__), 'fixtures/') +@contextmanager def load_schema_db(schema): import random @@ -107,8 +108,8 @@ def load_schema_db(schema): @pytest.fixture(scope='module') def schema1_db(): - for x in load_schema_db('schema1'): - yield x + with load_schema_db('schema1') as d: + yield d @pytest.fixture @@ -121,13 +122,23 @@ def schema1(db, schema1_db, settings): @pytest.fixture(scope='module') def schema2_db(): - for x in load_schema_db('schema2'): - yield x + with load_schema_db('schema2') as d: + yield d + + +@pytest.fixture(scope='module') +def schema2_fixtures(schema2_db, django_db_setup, django_db_blocker): + with django_db_blocker.unblock(): + with transaction.atomic(): + try: + for json_fixture in schema2_db['fixtures']: + call_command('loaddata', json_fixture) + yield + finally: + transaction.set_rollback(True) @pytest.fixture -def schema2(db, schema2_db, settings): +def schema2(schema2_db, schema2_fixtures, settings, django_db_blocker): settings.BIJOE_SCHEMAS = schema2_db['bijoe_schemas'] - for json_fixture in schema2_db['fixtures']: - call_command('loaddata', json_fixture) - return schema2_db + yield schema2_db -- 2.23.0