tests: rewrite scoped JSON fixture (#54008)

We override django_db_setup as a package scoped fixture to share the
loading of the big JSON fixture between tests.
This commit is contained in:
Benjamin Dauvergne 2021-05-17 21:56:12 +02:00
parent 9ad38144b6
commit 70f785ae62
6 changed files with 40 additions and 40 deletions

0
tests/__init__.py Normal file
View File

0
tests/base/__init__.py Normal file
View File

40
tests/base/conftest.py Normal file
View File

@ -0,0 +1,40 @@
# barbacompta - invoicing for dummies
# Copyright (C) 2019-2020 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
from django.core.management import call_command
from django.contrib.auth.models import User
DATA = ["tests/fixture.json"]
@pytest.fixture(scope="package")
def django_db_setup(django_db_setup, django_db_blocker):
print('django_db_setup start')
with django_db_blocker.unblock():
for data in DATA:
call_command("loaddata", data)
admin, created = User.objects.update_or_create(
username="admin",
defaults=dict(email="admin@example.com", is_superuser=True, is_staff=True),
)
admin.set_password("admin")
admin.save()
yield
print('django_db_setup stop')

View File

@ -14,17 +14,10 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
from eo_gestion.eo_facture.models import Contrat
from taggit.models import Tag
@pytest.fixture
def db(base_db):
yield
def test_references(app):
gru = Tag.objects.create(name='GRU')
gi = Tag.objects.create(name='Gestion d\'identité')

View File

@ -14,13 +14,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
@pytest.fixture
def db(base_db):
yield
def test_homepage(app):
response = app.get("/").follow()

View File

@ -18,32 +18,6 @@ import pytest
import django_webtest
from django.core.management import call_command
from django.db import transaction
from django.contrib.auth.models import User
DATA = ["tests/fixture.json"]
@pytest.fixture(scope="session")
def base_db(django_db_setup, django_db_blocker):
with django_db_blocker.unblock():
with transaction.atomic():
try:
for data in DATA:
call_command("loaddata", data)
admin, created = User.objects.update_or_create(
username="admin",
defaults=dict(email="admin@example.com", is_superuser=True, is_staff=True),
)
admin.set_password("admin")
admin.save()
yield
finally:
transaction.set_rollback(True)
@pytest.fixture
def app(db, freezer):