@@ -288,7 +288,135 @@ def test_a():
288288
289289 assert conn_db2.vendor == 'sqlite'
290290 db_name = conn_db2.creation._get_test_db_name()
291- assert 'test_custom_db_name_gw' in db_name
291+ assert db_name.startswith('test_custom_db_name_gw')
292+ """
293+ )
294+
295+ result = django_testdir .runpytest_subprocess ("--tb=short" , "-vv" , "-n1" )
296+ assert result .ret == 0
297+ result .stdout .fnmatch_lines (["*PASSED*test_a*" ])
298+
299+
300+ class TestSqliteWithTox :
301+
302+ db_settings = {
303+ "default" : {
304+ "ENGINE" : "django.db.backends.sqlite3" ,
305+ "NAME" : "db_name" ,
306+ "TEST" : {"NAME" : "test_custom_db_name" },
307+ }
308+ }
309+
310+ def test_db_with_tox_suffix (self , django_testdir , monkeypatch ):
311+ "A test to check that Tox DB suffix works when running in parallel."
312+ monkeypatch .setenv ("TOX_PARALLEL_ENV" , "py37-django22" )
313+
314+ django_testdir .create_test_module (
315+ """
316+ import pytest
317+ from django.db import connections
318+
319+ @pytest.mark.django_db
320+ def test_inner():
321+
322+ (conn, ) = connections.all()
323+
324+ assert conn.vendor == 'sqlite'
325+ db_name = conn.creation._get_test_db_name()
326+ assert db_name == 'test_custom_db_name_py37-django22'
327+ """
328+ )
329+
330+ result = django_testdir .runpytest_subprocess ("--tb=short" , "-vv" )
331+ assert result .ret == 0
332+ result .stdout .fnmatch_lines (["*test_inner*PASSED*" ])
333+
334+ def test_db_with_empty_tox_suffix (self , django_testdir , monkeypatch ):
335+ "A test to check that Tox DB suffix is not used when suffix would be empty."
336+ monkeypatch .setenv ("TOX_PARALLEL_ENV" , "" )
337+
338+ django_testdir .create_test_module (
339+ """
340+ import pytest
341+ from django.db import connections
342+
343+ @pytest.mark.django_db
344+ def test_inner():
345+
346+ (conn,) = connections.all()
347+
348+ assert conn.vendor == 'sqlite'
349+ db_name = conn.creation._get_test_db_name()
350+ assert db_name == 'test_custom_db_name'
351+ """
352+ )
353+
354+ result = django_testdir .runpytest_subprocess ("--tb=short" , "-vv" )
355+ assert result .ret == 0
356+ result .stdout .fnmatch_lines (["*test_inner*PASSED*" ])
357+
358+
359+ class TestSqliteWithToxAndXdist :
360+
361+ db_settings = {
362+ "default" : {
363+ "ENGINE" : "django.db.backends.sqlite3" ,
364+ "NAME" : "db_name" ,
365+ "TEST" : {"NAME" : "test_custom_db_name" },
366+ }
367+ }
368+
369+ def test_db_with_tox_suffix (self , django_testdir , monkeypatch ):
370+ "A test to check that both Tox and xdist suffixes work together."
371+ pytest .importorskip ("xdist" )
372+ monkeypatch .setenv ("TOX_PARALLEL_ENV" , "py37-django22" )
373+
374+ django_testdir .create_test_module (
375+ """
376+ import pytest
377+ from django.db import connections
378+
379+ @pytest.mark.django_db
380+ def test_inner():
381+
382+ (conn, ) = connections.all()
383+
384+ assert conn.vendor == 'sqlite'
385+ db_name = conn.creation._get_test_db_name()
386+ assert db_name.startswith('test_custom_db_name_py37-django22_gw')
387+ """
388+ )
389+
390+ result = django_testdir .runpytest_subprocess ("--tb=short" , "-vv" , "-n1" )
391+ assert result .ret == 0
392+ result .stdout .fnmatch_lines (["*PASSED*test_inner*" ])
393+
394+
395+ class TestSqliteInMemoryWithXdist :
396+
397+ db_settings = {
398+ "default" : {
399+ "ENGINE" : "django.db.backends.sqlite3" ,
400+ "NAME" : ":memory:" ,
401+ "TEST" : {"NAME" : ":memory:" },
402+ }
403+ }
404+
405+ def test_sqlite_in_memory_used (self , django_testdir ):
406+ pytest .importorskip ("xdist" )
407+
408+ django_testdir .create_test_module (
409+ """
410+ import pytest
411+ from django.db import connections
412+
413+ @pytest.mark.django_db
414+ def test_a():
415+ (conn, ) = connections.all()
416+
417+ assert conn.vendor == 'sqlite'
418+ db_name = conn.creation._get_test_db_name()
419+ assert 'file:memorydb' in db_name or db_name == ':memory:'
292420 """
293421 )
294422
0 commit comments