Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Commit

Permalink
Fix @pytest.mark.django_db(reset_sequence=True) not being sorted as…
Browse files Browse the repository at this point in the history
… transactional

It implies transactional, so should sort as such.
  • Loading branch information
bluetech committed Nov 28, 2021
1 parent c7e0229 commit eeeb163
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
41 changes: 23 additions & 18 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,28 +376,33 @@ def get_order_number(test: pytest.Item) -> int:
if test_cls:
# Beware, TestCase is a subclass of TransactionTestCase
if issubclass(test_cls, TestCase):
return 0
if issubclass(test_cls, TransactionTestCase):
return 1

marker_db = test.get_closest_marker('django_db')
if not marker_db:
transaction = None
uses_db = True
transactional = False
elif issubclass(test_cls, TransactionTestCase):
uses_db = True
transactional = True
else:
uses_db = False
transactional = False
else:
transaction = validate_django_db(marker_db)[0]
if transaction is True:
return 1
marker_db = test.get_closest_marker('django_db')
if marker_db:
transaction, reset_sequences, databases = validate_django_db(marker_db)
uses_db = True
transactional = transaction or reset_sequences
else:
uses_db = False
transactional = False
fixtures = getattr(test, 'fixturenames', [])
transactional = transactional or "transactional_db" in fixtures
uses_db = uses_db or "db" in fixtures

fixtures = getattr(test, 'fixturenames', [])
if "transactional_db" in fixtures:
if transactional:
return 1

if transaction is False:
elif uses_db:
return 0
if "db" in fixtures:
return 0

return 2
else:
return 2

items.sort(key=get_order_number)

Expand Down
11 changes: 10 additions & 1 deletion tests/test_db_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@ def test_run_second_decorator():
def test_run_second_fixture(transactional_db):
pass
def test_run_second_reset_sequences_fixture(django_db_reset_sequences):
pass
def test_run_first_fixture(db):
pass
@pytest.mark.django_db(reset_sequences=True)
def test_run_second_reset_sequences_decorator():
pass
@pytest.mark.django_db
def test_run_first_decorator():
pass
Expand All @@ -65,15 +72,17 @@ class MyTransactionTestCase(TransactionTestCase):
def test_run_second_transaction_test_case(self):
pass
''')
result = django_testdir.runpytest_subprocess('-v', '-s')
result = django_testdir.runpytest_subprocess('-q', '--collect-only')
assert result.ret == 0
result.stdout.fnmatch_lines([
"*test_run_first_fixture*",
"*test_run_first_decorator*",
"*test_run_first_django_test_case*",
"*test_run_second_decorator*",
"*test_run_second_fixture*",
"*test_run_second_reset_sequences_decorator*",
"*test_run_second_transaction_test_case*",
"*test_run_second_reset_sequences_fixture*",
"*test_run_last_test_case*",
"*test_run_last_simple_test_case*",
])
Expand Down

0 comments on commit eeeb163

Please sign in to comment.