Skip to content

Commit 1a75381

Browse files
author
Oliver Sauder
committed
Split up reset sequence test to avoid transaction issue on newly created db
1 parent 19540fe commit 1a75381

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

tests/test_database.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_transactions_enabled_via_reset_seq(
8383

8484
assert not connection.in_atomic_block
8585

86-
def test_django_db_reset_sequences_fixture(
86+
def test_django_db_reset_sequences_fixture_requested(
8787
self, db, django_testdir, non_zero_sequences_counter):
8888

8989
if not db_supports_reset_sequences():
@@ -96,10 +96,6 @@ def test_django_db_reset_sequences_fixture(
9696
import pytest
9797
from .app.models import Item
9898
99-
def test_django_db_reset_sequences_not_requested(db):
100-
item = Item.objects.create(name='new_item')
101-
assert item.id > 1
102-
10399
def test_django_db_reset_sequences_requested(
104100
django_db_reset_sequences):
105101
item = Item.objects.create(name='new_item')
@@ -108,10 +104,32 @@ def test_django_db_reset_sequences_requested(
108104

109105
result = django_testdir.runpytest_subprocess('-v', '--reuse-db')
110106
result.stdout.fnmatch_lines([
111-
"*test_django_db_reset_sequences_not_requested PASSED*",
112107
"*test_django_db_reset_sequences_requested PASSED*",
113108
])
114109

110+
def test_django_db_reset_sequences_fixture_not_requested(
111+
self, db, django_testdir, non_zero_sequences_counter):
112+
113+
if not db_supports_reset_sequences():
114+
pytest.skip('transactions and reset_sequences must be supported '
115+
'by the database to run this test')
116+
117+
# The test runs on a database that already contains objects, so its
118+
# id counter is > 1. We check for the ids of newly created objects.
119+
django_testdir.create_test_module('''
120+
import pytest
121+
from .app.models import Item
122+
123+
def test_django_db_reset_sequences_not_requested(db):
124+
item = Item.objects.create(name='new_item')
125+
assert item.id > 1
126+
''')
127+
128+
result = django_testdir.runpytest_subprocess('-v', '--reuse-db')
129+
result.stdout.fnmatch_lines([
130+
"*test_django_db_reset_sequences_not_requested PASSED*",
131+
])
132+
115133
@pytest.fixture
116134
def mydb(self, all_dbs):
117135
# This fixture must be able to access the database

0 commit comments

Comments
 (0)