Skip to content

Commit eeeb163

Browse files
committed
Fix @pytest.mark.django_db(reset_sequence=True) not being sorted as transactional
It implies transactional, so should sort as such.
1 parent c7e0229 commit eeeb163

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

pytest_django/plugin.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -376,28 +376,33 @@ def get_order_number(test: pytest.Item) -> int:
376376
if test_cls:
377377
# Beware, TestCase is a subclass of TransactionTestCase
378378
if issubclass(test_cls, TestCase):
379-
return 0
380-
if issubclass(test_cls, TransactionTestCase):
381-
return 1
382-
383-
marker_db = test.get_closest_marker('django_db')
384-
if not marker_db:
385-
transaction = None
379+
uses_db = True
380+
transactional = False
381+
elif issubclass(test_cls, TransactionTestCase):
382+
uses_db = True
383+
transactional = True
384+
else:
385+
uses_db = False
386+
transactional = False
386387
else:
387-
transaction = validate_django_db(marker_db)[0]
388-
if transaction is True:
389-
return 1
388+
marker_db = test.get_closest_marker('django_db')
389+
if marker_db:
390+
transaction, reset_sequences, databases = validate_django_db(marker_db)
391+
uses_db = True
392+
transactional = transaction or reset_sequences
393+
else:
394+
uses_db = False
395+
transactional = False
396+
fixtures = getattr(test, 'fixturenames', [])
397+
transactional = transactional or "transactional_db" in fixtures
398+
uses_db = uses_db or "db" in fixtures
390399

391-
fixtures = getattr(test, 'fixturenames', [])
392-
if "transactional_db" in fixtures:
400+
if transactional:
393401
return 1
394-
395-
if transaction is False:
402+
elif uses_db:
396403
return 0
397-
if "db" in fixtures:
398-
return 0
399-
400-
return 2
404+
else:
405+
return 2
401406

402407
items.sort(key=get_order_number)
403408

tests/test_db_setup.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,16 @@ def test_run_second_decorator():
4242
def test_run_second_fixture(transactional_db):
4343
pass
4444
45+
def test_run_second_reset_sequences_fixture(django_db_reset_sequences):
46+
pass
47+
4548
def test_run_first_fixture(db):
4649
pass
4750
51+
@pytest.mark.django_db(reset_sequences=True)
52+
def test_run_second_reset_sequences_decorator():
53+
pass
54+
4855
@pytest.mark.django_db
4956
def test_run_first_decorator():
5057
pass
@@ -65,15 +72,17 @@ class MyTransactionTestCase(TransactionTestCase):
6572
def test_run_second_transaction_test_case(self):
6673
pass
6774
''')
68-
result = django_testdir.runpytest_subprocess('-v', '-s')
75+
result = django_testdir.runpytest_subprocess('-q', '--collect-only')
6976
assert result.ret == 0
7077
result.stdout.fnmatch_lines([
7178
"*test_run_first_fixture*",
7279
"*test_run_first_decorator*",
7380
"*test_run_first_django_test_case*",
7481
"*test_run_second_decorator*",
7582
"*test_run_second_fixture*",
83+
"*test_run_second_reset_sequences_decorator*",
7684
"*test_run_second_transaction_test_case*",
85+
"*test_run_second_reset_sequences_fixture*",
7786
"*test_run_last_test_case*",
7887
"*test_run_last_simple_test_case*",
7988
])

0 commit comments

Comments
 (0)