Skip to content

Commit 8656a22

Browse files
adamantikeblueyed
authored andcommitted
Fix DB renaming fixture for Multi-DB environment (#679)
For Django configurations using multiple databases, there's a subtle bug that doesn't rename databases, if there's an SQLite one that triggers the `return` statement in the existing feature. As the return value doesn't seem to be used, we can just move to the next iteration in the `for`-loop, and evaluate the following DB. A test was added to consider this scenario.
1 parent 5d6a7ef commit 8656a22

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

pytest_django/fixtures.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ def django_db_modify_db_settings_xdist_suffix(request):
4747

4848
if not test_name:
4949
if db_settings["ENGINE"] == "django.db.backends.sqlite3":
50-
return ":memory:"
51-
else:
52-
test_name = "test_{}".format(db_settings["NAME"])
50+
continue
51+
52+
test_name = "test_{}".format(db_settings["NAME"])
5353

5454
# Put a suffix like _gw0, _gw1 etc on xdist processes
5555
xdist_suffix = getattr(request.config, "slaveinput", {}).get("slaveid")

tests/test_db_setup.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,50 @@ def test_a():
215215
result.stdout.fnmatch_lines(["*PASSED*test_a*"])
216216

217217

218+
class TestSqliteWithMultipleDbsAndXdist:
219+
220+
db_settings = {
221+
"default": {
222+
"ENGINE": "django.db.backends.sqlite3",
223+
"NAME": "/tmp/should-not-be-used",
224+
},
225+
"db2": {
226+
"ENGINE": "django.db.backends.sqlite3",
227+
"NAME": "db_name",
228+
"TEST": {"NAME": "test_custom_db_name"},
229+
}
230+
}
231+
232+
def test_sqlite_database_renamed(self, django_testdir):
233+
pytest.importorskip("xdist")
234+
235+
django_testdir.create_test_module(
236+
"""
237+
import pytest
238+
from django.db import connections
239+
240+
@pytest.mark.django_db
241+
def test_a():
242+
(conn_db2, conn_default) = sorted(
243+
connections.all(),
244+
key=lambda conn: conn.alias,
245+
)
246+
247+
assert conn_default.vendor == 'sqlite'
248+
db_name = conn_default.creation._get_test_db_name()
249+
assert 'file:memorydb' in db_name
250+
251+
assert conn_db2.vendor == 'sqlite'
252+
db_name = conn_db2.creation._get_test_db_name()
253+
assert 'test_custom_db_name_gw' in db_name
254+
"""
255+
)
256+
257+
result = django_testdir.runpytest_subprocess("--tb=short", "-vv", "-n1")
258+
assert result.ret == 0
259+
result.stdout.fnmatch_lines(["*PASSED*test_a*"])
260+
261+
218262
@pytest.mark.skipif(
219263
get_django_version() >= (1, 9),
220264
reason=(

0 commit comments

Comments
 (0)