Skip to content

Rename test databases when running parallel Tox #680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ _build
.env
/.coverage.*
/.coverage
/coverage.xml
/htmlcov/
.cache
.pytest_cache/
Expand Down
28 changes: 25 additions & 3 deletions docs/database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,43 @@ If you need to customize the location of your test database, this is the
fixture you want to override.

The default implementation of this fixture requests the
:fixture:`django_db_modify_db_settings_xdist_suffix` to provide compatibility
:fixture:`django_db_modify_db_settings_parallel_suffix` to provide compatibility
with pytest-xdist.

This fixture is by default requested from :fixture:`django_db_setup`.

django_db_modify_db_settings_parallel_suffix
""""""""""""""""""""""""""""""""""""""""""""

.. fixture:: django_db_modify_db_settings_parallel_suffix

Requesting this fixture will add a suffix to the database name when the tests
are run via `pytest-xdist`, or via `tox` in parallel mode.

This fixture is by default requested from
:fixture:`django_db_modify_db_settings`.

django_db_modify_db_settings_tox_suffix
"""""""""""""""""""""""""""""""""""""""

.. fixture:: django_db_modify_db_settings_tox_suffix

Requesting this fixture will add a suffix to the database name when the tests
are run via `tox` in parallel mode.

This fixture is by default requested from
:fixture:`django_db_modify_db_settings_parallel_suffix`.

django_db_modify_db_settings_xdist_suffix
"""""""""""""""""""""""""""""""""""""""""

.. fixture:: django_db_modify_db_settings_xdist_suffix

Requesting this fixture will add a suffix to the database name when the tests
are run via pytest-xdist.
are run via `pytest-xdist`.

This fixture is by default requested from
:fixture:`django_db_modify_db_settings`.
:fixture:`django_db_modify_db_settings_parallel_suffix`.

django_db_use_migrations
""""""""""""""""""""""""
Expand Down
56 changes: 37 additions & 19 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,35 @@


@pytest.fixture(scope="session")
def django_db_modify_db_settings_xdist_suffix(request):
def django_db_modify_db_settings_tox_suffix(request):
skip_if_no_django()

from django.conf import settings

for db_settings in settings.DATABASES.values():

try:
test_name = db_settings["TEST"]["NAME"]
except KeyError:
test_name = None
tox_environment = os.getenv("TOX_PARALLEL_ENV")
if tox_environment:
# Put a suffix like _py27-django21 on tox workers
_set_suffix_to_test_databases(suffix=tox_environment)

if not test_name:
if db_settings["ENGINE"] == "django.db.backends.sqlite3":
continue

test_name = "test_{}".format(db_settings["NAME"])
@pytest.fixture(scope="session")
def django_db_modify_db_settings_xdist_suffix(request):
skip_if_no_django()

xdist_suffix = getattr(request.config, "slaveinput", {}).get("slaveid")
if xdist_suffix:
# Put a suffix like _gw0, _gw1 etc on xdist processes
xdist_suffix = getattr(request.config, "slaveinput", {}).get("slaveid")
if test_name != ":memory:" and xdist_suffix is not None:
test_name = "{}_{}".format(test_name, xdist_suffix)
_set_suffix_to_test_databases(suffix=xdist_suffix)

db_settings.setdefault("TEST", {})
db_settings["TEST"]["NAME"] = test_name

@pytest.fixture(scope="session")
def django_db_modify_db_settings_parallel_suffix(
django_db_modify_db_settings_tox_suffix,
django_db_modify_db_settings_xdist_suffix,
):
skip_if_no_django()


@pytest.fixture(scope="session")
def django_db_modify_db_settings(django_db_modify_db_settings_xdist_suffix):
def django_db_modify_db_settings(django_db_modify_db_settings_parallel_suffix):
skip_if_no_django()


Expand Down Expand Up @@ -169,6 +169,24 @@ def handle(self, *args, **kwargs):
migrate.Command = MigrateSilentCommand


def _set_suffix_to_test_databases(suffix):
from django.conf import settings

for db_settings in settings.DATABASES.values():
test_name = db_settings.get("TEST", {}).get("NAME")

if not test_name:
if db_settings["ENGINE"] == "django.db.backends.sqlite3":
continue
test_name = "test_{}".format(db_settings["NAME"])

if test_name == ":memory:":
continue

db_settings.setdefault("TEST", {})
db_settings["TEST"]["NAME"] = "{}_{}".format(test_name, suffix)


# ############### User visible fixtures ################


Expand Down
2 changes: 2 additions & 0 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from .fixtures import django_db_keepdb # noqa
from .fixtures import django_db_createdb # noqa
from .fixtures import django_db_modify_db_settings # noqa
from .fixtures import django_db_modify_db_settings_parallel_suffix # noqa
from .fixtures import django_db_modify_db_settings_tox_suffix # noqa
from .fixtures import django_db_modify_db_settings_xdist_suffix # noqa
from .fixtures import _live_server_helper # noqa
from .fixtures import admin_client # noqa
Expand Down
130 changes: 129 additions & 1 deletion tests/test_db_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,135 @@ def test_a():

assert conn_db2.vendor == 'sqlite'
db_name = conn_db2.creation._get_test_db_name()
assert 'test_custom_db_name_gw' in db_name
assert db_name.startswith('test_custom_db_name_gw')
"""
)

result = django_testdir.runpytest_subprocess("--tb=short", "-vv", "-n1")
assert result.ret == 0
result.stdout.fnmatch_lines(["*PASSED*test_a*"])


class TestSqliteWithTox:

db_settings = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "db_name",
"TEST": {"NAME": "test_custom_db_name"},
}
}

def test_db_with_tox_suffix(self, django_testdir, monkeypatch):
"A test to check that Tox DB suffix works when running in parallel."
monkeypatch.setenv("TOX_PARALLEL_ENV", "py37-django22")

django_testdir.create_test_module(
"""
import pytest
from django.db import connections

@pytest.mark.django_db
def test_inner():

(conn, ) = connections.all()

assert conn.vendor == 'sqlite'
db_name = conn.creation._get_test_db_name()
assert db_name == 'test_custom_db_name_py37-django22'
"""
)

result = django_testdir.runpytest_subprocess("--tb=short", "-vv")
assert result.ret == 0
result.stdout.fnmatch_lines(["*test_inner*PASSED*"])

def test_db_with_empty_tox_suffix(self, django_testdir, monkeypatch):
"A test to check that Tox DB suffix is not used when suffix would be empty."
monkeypatch.setenv("TOX_PARALLEL_ENV", "")

django_testdir.create_test_module(
"""
import pytest
from django.db import connections

@pytest.mark.django_db
def test_inner():

(conn,) = connections.all()

assert conn.vendor == 'sqlite'
db_name = conn.creation._get_test_db_name()
assert db_name == 'test_custom_db_name'
"""
)

result = django_testdir.runpytest_subprocess("--tb=short", "-vv")
assert result.ret == 0
result.stdout.fnmatch_lines(["*test_inner*PASSED*"])


class TestSqliteWithToxAndXdist:

db_settings = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "db_name",
"TEST": {"NAME": "test_custom_db_name"},
}
}

def test_db_with_tox_suffix(self, django_testdir, monkeypatch):
"A test to check that both Tox and xdist suffixes work together."
pytest.importorskip("xdist")
monkeypatch.setenv("TOX_PARALLEL_ENV", "py37-django22")

django_testdir.create_test_module(
"""
import pytest
from django.db import connections

@pytest.mark.django_db
def test_inner():

(conn, ) = connections.all()

assert conn.vendor == 'sqlite'
db_name = conn.creation._get_test_db_name()
assert db_name.startswith('test_custom_db_name_py37-django22_gw')
"""
)

result = django_testdir.runpytest_subprocess("--tb=short", "-vv", "-n1")
assert result.ret == 0
result.stdout.fnmatch_lines(["*PASSED*test_inner*"])


class TestSqliteInMemoryWithXdist:

db_settings = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
"TEST": {"NAME": ":memory:"},
}
}

def test_sqlite_in_memory_used(self, django_testdir):
pytest.importorskip("xdist")

django_testdir.create_test_module(
"""
import pytest
from django.db import connections

@pytest.mark.django_db
def test_a():
(conn, ) = connections.all()

assert conn.vendor == 'sqlite'
db_name = conn.creation._get_test_db_name()
assert 'file:memorydb' in db_name or db_name == ':memory:'
"""
)

Expand Down