Skip to content

Commit

Permalink
no need to modify it if it already equals that
Browse files Browse the repository at this point in the history
  • Loading branch information
SalmonMode committed Jan 23, 2020
1 parent a5b8acb commit 5e53f0a
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
@@ -1,5 +1,6 @@
language: python
dist: xenial
cache: false

jobs:
fast_finish: true
Expand Down Expand Up @@ -97,7 +98,7 @@ install:
- pip install tox==3.9.0

script:
- tox
- tox --force-dep "pytest@git+https://github.com/blueyed/pytest@fixture-stack#egg=pytest"

after_success:
- |
Expand Down
29 changes: 16 additions & 13 deletions pytest_django/fixtures.py
Expand Up @@ -36,28 +36,34 @@
def django_db_modify_db_settings_tox_suffix():
skip_if_no_django()

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)
return os.getenv("TOX_PARALLEL_ENV")


@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
_set_suffix_to_test_databases(suffix=xdist_suffix)
return getattr(request.config, "slaveinput", {}).get("slaveid")


@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,
django_db_modify_db_settings_xdist_suffix
):
skip_if_no_django()
xdist_worker = django_db_modify_db_settings_xdist_suffix
tox_environment = django_db_modify_db_settings_tox_suffix
suffix_parts = []
if tox_environment:
# Put a suffix like _py27-django21 on tox workers
suffix_parts.append(tox_environment)
if xdist_worker:
# Put a suffix like _gw0, _gw1 etc on xdist processes
suffix_parts.append(xdist_worker)
suffix = "_".join(suffix_parts)
if suffix:
_set_suffix_to_test_databases(suffix=suffix)


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -182,10 +188,7 @@ def _set_suffix_to_test_databases(suffix):
continue

db_settings.setdefault("TEST", {})
test_db_name = test_name
if not test_name.endswith("_{}".format(suffix)):
test_db_name = "{}_{}".format(test_name, suffix)
db_settings["TEST"]["NAME"] = test_db_name
db_settings["TEST"]["NAME"] = "{}_{}".format(test_name, suffix)


# ############### User visible fixtures ################
Expand Down
6 changes: 0 additions & 6 deletions pytest_django_test/settings_base.py
@@ -1,5 +1,3 @@
import os

import django

ROOT_URLCONF = "pytest_django_test.urls"
Expand All @@ -14,10 +12,6 @@
STATIC_URL = "/static/"
SECRET_KEY = "foobar"

# Used to construct unique test database names to allow detox to run multiple
# versions at the same time
db_suffix = "_%s" % os.getuid()

MIDDLEWARE = [
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
Expand Down
4 changes: 2 additions & 2 deletions pytest_django_test/settings_mysql_innodb.py
@@ -1,9 +1,9 @@
from .settings_base import * # noqa: F403
from .settings_base import * # noqa: F401 F403

DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": "pytest_django" + db_suffix, # noqa: F405
"NAME": "pytest_django_should_never_get_accessed",
"HOST": "localhost",
"USER": "root",
"OPTIONS": {"init_command": "SET default_storage_engine=InnoDB"},
Expand Down
4 changes: 2 additions & 2 deletions pytest_django_test/settings_mysql_myisam.py
@@ -1,9 +1,9 @@
from pytest_django_test.settings_base import * # noqa: F403
from .settings_base import * # noqa: F401 F403

DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": "pytest_django" + db_suffix, # noqa: F405
"NAME": "pytest_django_should_never_get_accessed",
"HOST": "localhost",
"USER": "root",
"OPTIONS": {"init_command": "SET default_storage_engine=MyISAM"},
Expand Down
4 changes: 2 additions & 2 deletions pytest_django_test/settings_postgres.py
@@ -1,4 +1,4 @@
from pytest_django_test.settings_base import * # noqa
from .settings_base import * # noqa: F401 F403

# PyPy compatibility
try:
Expand All @@ -12,7 +12,7 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "pytest_django" + db_suffix, # noqa
"NAME": "pytest_django_should_never_get_accessed",
"HOST": "localhost",
"USER": "",
}
Expand Down
2 changes: 1 addition & 1 deletion pytest_django_test/settings_sqlite.py
@@ -1,4 +1,4 @@
from .settings_base import * # noqa
from .settings_base import * # noqa: F401 F403

DATABASES = {
"default": {
Expand Down
4 changes: 2 additions & 2 deletions pytest_django_test/settings_sqlite_file.py
@@ -1,6 +1,6 @@
import tempfile

from pytest_django_test.settings_base import * # noqa
from .settings_base import * # noqa: F401 F403

# This is a SQLite configuration, which uses a file based database for
# tests (via setting TEST_NAME / TEST['NAME']).
Expand All @@ -11,7 +11,7 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "/should_never_be_accessed",
"NAME": "/pytest_django_should_never_get_accessed",
"TEST": {"NAME": _filename},
}
}
34 changes: 19 additions & 15 deletions tests/test_db_setup.py
Expand Up @@ -159,32 +159,34 @@ def test_xdist_with_reuse(django_testdir):
from .app.models import Item
def _check(settings):
def _check(settings, worker_id):
# Make sure that the database name looks correct
db_name = settings.DATABASES['default']['NAME']
assert db_name.endswith('_gw0') or db_name.endswith('_gw1')
assert db_name == (
'test_pytest_django_should_never_get_accessed_inner_inner_{}'
.format(worker_id)
)
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)
def test_a(settings, worker_id):
_check(settings, worker_id)
@pytest.mark.django_db
def test_b(settings):
_check(settings)
def test_b(settings, worker_id):
_check(settings, worker_id)
@pytest.mark.django_db
def test_c(settings):
_check(settings)
def test_c(settings, worker_id):
_check(settings, worker_id)
@pytest.mark.django_db
def test_d(settings):
_check(settings)
def test_d(settings, worker_id):
_check(settings, worker_id)
"""
)

Expand Down Expand Up @@ -270,7 +272,7 @@ def test_sqlite_database_renamed(self, django_testdir):
from django.db import connections
@pytest.mark.django_db
def test_a():
def test_a(worker_id):
(conn_db2, conn_default) = sorted(
connections.all(),
key=lambda conn: conn.alias,
Expand All @@ -288,7 +290,7 @@ def test_a():
assert conn_db2.vendor == 'sqlite'
db_name = conn_db2.creation._get_test_db_name()
assert db_name.startswith('test_custom_db_name_gw')
assert db_name == 'test_custom_db_name_{}'.format(worker_id)
"""
)

Expand Down Expand Up @@ -377,13 +379,15 @@ def test_db_with_tox_suffix(self, django_testdir, monkeypatch):
from django.db import connections
@pytest.mark.django_db
def test_inner():
def test_inner(worker_id):
(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')
assert db_name == 'test_custom_db_name_py37-django22_{}'.format(
worker_id,
)
"""
)

Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Expand Up @@ -27,6 +27,8 @@ deps =
postgres: psycopg2-binary
coverage: coverage-enable-subprocess

pytest

pytest41: pytest>=4.1,<4.2
pytest41: attrs==17.4.0
xdist: pytest-xdist>=1.15
Expand Down

0 comments on commit 5e53f0a

Please sign in to comment.