Skip to content

Commit

Permalink
tests: avoid if *all* connections support transactions
Browse files Browse the repository at this point in the history
Only interested in one connection. This causes some mess with mutli-db
support in MySQL, which ends up querying all connections.
  • Loading branch information
bluetech committed May 8, 2021
1 parent 1ba6b3d commit 648e1b6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
15 changes: 7 additions & 8 deletions tests/test_database.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest
from django.db import connection, transaction
from django.test.testcases import connections_support_transactions

from pytest_django.lazy_django import get_django_version
from pytest_django_test.app.models import Item
Expand Down Expand Up @@ -66,21 +65,21 @@ def test_clean_db(self, all_dbs: None) -> None:
assert Item.objects.count() == 0

def test_transactions_disabled(self, db: None) -> None:
if not connections_support_transactions():
if not connection.features.supports_transactions:
pytest.skip("transactions required for this test")

assert connection.in_atomic_block

def test_transactions_enabled(self, transactional_db: None) -> None:
if not connections_support_transactions():
if not connection.features.supports_transactions:
pytest.skip("transactions required for this test")

assert not connection.in_atomic_block

def test_transactions_enabled_via_reset_seq(
self, django_db_reset_sequences: None,
) -> None:
if not connections_support_transactions():
if not connection.features.supports_transactions:
pytest.skip("transactions required for this test")

assert not connection.in_atomic_block
Expand Down Expand Up @@ -120,7 +119,7 @@ def mydb(self, all_dbs: None) -> None:
Item.objects.create(name="spam")

def test_mydb(self, mydb: None) -> None:
if not connections_support_transactions():
if not connection.features.supports_transactions:
pytest.skip("transactions required for this test")

# Check the fixture had access to the db
Expand Down Expand Up @@ -196,21 +195,21 @@ def test_clean_db(self) -> None:

@pytest.mark.django_db
def test_transactions_disabled(self) -> None:
if not connections_support_transactions():
if not connection.features.supports_transactions:
pytest.skip("transactions required for this test")

assert connection.in_atomic_block

@pytest.mark.django_db(transaction=False)
def test_transactions_disabled_explicit(self) -> None:
if not connections_support_transactions():
if not connection.features.supports_transactions:
pytest.skip("transactions required for this test")

assert connection.in_atomic_block

@pytest.mark.django_db(transaction=True)
def test_transactions_enabled(self) -> None:
if not connections_support_transactions():
if not connection.features.supports_transactions:
pytest.skip("transactions required for this test")

assert not connection.in_atomic_block
Expand Down
3 changes: 1 addition & 2 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from django.core import mail
from django.db import connection, transaction
from django.test.client import Client, RequestFactory
from django.test.testcases import connections_support_transactions
from django.utils.encoding import force_str

from pytest_django_test.app.models import Item
Expand Down Expand Up @@ -374,7 +373,7 @@ def test_settings_restored(self) -> None:
assert settings.ALLOWED_HOSTS == ["testserver"]

def test_transactions(self, live_server) -> None:
if not connections_support_transactions():
if not connection.features.support_transactions:
pytest.skip("transactions required for this test")

assert not connection.in_atomic_block
Expand Down

0 comments on commit 648e1b6

Please sign in to comment.