Skip to content

Option to use one database when xdist plugin is used #336

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
10 changes: 10 additions & 0 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def _django_db_setup(request,
"""Session-wide database setup, internal to pytest-django"""
skip_if_no_django()

if is_xdist_one_db_enabled(request.config):
return

from .compat import setup_databases, teardown_databases

# xdist
Expand Down Expand Up @@ -64,6 +67,13 @@ def teardown_database():
request.addfinalizer(teardown_database)


def is_xdist_one_db_enabled(config):
from django.conf import settings
is_sqlite = (settings.DATABASES['default']['ENGINE'] == 'django.db.backends.sqlite3')
# can't use one sqlite3 db for distributed test run because of lock
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should raise a config error probably?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really familiar with error handling in pytest, could you give some example or exception class name?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ValueError might be appropriate.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blueyed done

return config.getvalue('xdist_one_db') and not is_sqlite


def _django_db_fixture_helper(transactional, request, _django_cursor_wrapper):
if is_django_unittest(request):
return
Expand Down
35 changes: 33 additions & 2 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@

import py
import pytest
from django.test.runner import setup_databases

from .db_reuse import monkey_patch_creation_for_db_reuse
from .django_compat import is_django_unittest
from .fixtures import (_django_db_setup, _live_server_helper, admin_client,
admin_user, client, db, django_user_model,
django_username_field, live_server, rf, settings,
transactional_db)
from .lazy_django import django_settings_is_configured, skip_if_no_django
transactional_db, _handle_south, _disable_native_migrations)
from .lazy_django import django_settings_is_configured, skip_if_no_django, get_django_version

# Silence linters for imported fixtures.
(_django_db_setup, _live_server_helper, admin_client, admin_user, client, db,
Expand All @@ -44,6 +46,10 @@ def pytest_addoption(parser):
action='store_true', dest='create_db', default=False,
help='Re-create the database, even if it exists. This '
'option will be ignored if not --reuse-db is given.')
group._addoption('--xdist-one-db',
dest='xdist_one_db', default=False, action='store_true',
help="Use only one database with xdist plugin. "
"Doesn't work with sqlite3 backend due to db lock")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this implies it is true for sqlite in general - but should have a dot at the end.

group._addoption('--ds',
action='store', type='string', dest='ds', default=None,
help='Set DJANGO_SETTINGS_MODULE.')
Expand Down Expand Up @@ -246,6 +252,31 @@ def pytest_report_header(config):
return [config._dsm_report_header]


def pytest_xdist_setupnodes(config):
"""called once before any remote node is set up. """
if not config.getvalue('xdist_one_db'):
return
_setup_django()

from django.conf import settings
if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.sqlite3':
return

_handle_south()

if config.getvalue('nomigrations'):
_disable_native_migrations()

db_args = {}
if get_django_version() >= (1, 8):
db_args['keepdb'] = True
else:
monkey_patch_creation_for_db_reuse()

# Create the database
setup_databases(verbosity=config.option.verbose, interactive=False, **db_args)


@pytest.mark.trylast
def pytest_configure():
# Allow Django settings to be configured in a user pytest_configure call,
Expand Down
26 changes: 26 additions & 0 deletions tests/test_db_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,32 @@ def test_d(settings):
result.stdout.fnmatch_lines(['*PASSED*test_d*'])


def test_xdist_with_one_db_does_not_work_with_sqlite(django_testdir):
django_testdir.create_test_module('''
import pytest

from .app.models import Item

def _check(settings):
# Make sure that the database name looks correct
db_name = settings.DATABASES['default']['NAME']
assert db_name.endswith('_gw0')

assert Item.objects.count() == 0
Item.objects.create(name='foo')
assert Item.objects.count() == 1


@pytest.mark.django_db
def test_a(settings):
_check(settings)
''')

result = django_testdir.runpytest_subprocess('-vv', '-n1', '-s', '--xdist-one-db')
assert result.ret == 0
result.stdout.fnmatch_lines(['*PASSED*test_a*'])


class TestSqliteWithXdist:

pytestmark = skip_on_python32
Expand Down