Reuse db schema instead of recreating it fro mscratch during tests (#67914)

This commit is contained in:
Agate 2022-08-03 15:11:24 +02:00
parent d2b5a32997
commit 274b6eabde
2 changed files with 18 additions and 6 deletions

View File

@ -1,7 +1,9 @@
import json
import os
import django.db.utils
import pytest
from django.core.management import call_command
from django.db import connection, transaction
from django_webtest import DjangoTestApp, WebTestMixin
from tenant_schemas.postgresql_backend.base import FakeTenant
@ -74,18 +76,28 @@ def tenant_factory(transactional_db, tenant_base, settings):
schema_name = name.replace('-', '_').replace('.', '_')
t = Tenant(domain_url=name, schema_name=schema_name)
with transaction.atomic():
t.create_schema()
t.create_schema(check_if_exists=True)
connection.set_schema_to_public()
tenants.append(t)
return t
try:
yield factory
finally:
# cleanup all created tenants
connection.set_schema_to_public()
with tenant_context(FakeTenant('public')):
for tenant in tenants:
tenant.delete(force_drop=True)
for tenant in tenants:
with tenant_context(tenant):
call_command(
"flush",
verbosity=0,
interactive=False,
database='default',
reset_sequences=False,
allow_cascade=True,
inhibit_post_migrate=False,
)
@pytest.fixture

View File

@ -5,8 +5,8 @@
[tox]
toxworkdir = {env:TMPDIR:/tmp}/tox-{env:USER}/hobo/{env:BRANCH_NAME:}
envlist =
py3-django22-drf39-{hobo,multipublik,multitenant,schemas,authentic,passerelle}
py3-django22-drf312-coverage-{hobo,multipublik,multitenant,schemas,authentic,passerelle}
py3-django22-drf39-{hobo,authentic,multipublik,multitenant,schemas,passerelle}
py3-django22-drf312-coverage-{hobo,authentic,multipublik,multitenant,schemas,passerelle}
py3-black
[testenv]