Fixed #27818 -- Replaced try/except/pass with contextlib.suppress().

This commit is contained in:
Mads Jensen 2017-03-09 16:17:41 +01:00 committed by Tim Graham
parent 43a4835edf
commit 550cb3a365
67 changed files with 223 additions and 368 deletions

View file

@ -1,3 +1,5 @@
from contextlib import suppress
from django.db import connection, transaction
from django.test import TransactionTestCase, skipUnlessDBFeature
@ -48,12 +50,10 @@ class TestConnectionOnCommit(TransactionTestCase):
self.assertDone([1])
def test_does_not_execute_if_transaction_rolled_back(self):
try:
with suppress(ForcedError):
with transaction.atomic():
self.do(1)
raise ForcedError()
except ForcedError:
pass
self.assertDone([])
@ -71,12 +71,10 @@ class TestConnectionOnCommit(TransactionTestCase):
with transaction.atomic():
self.do(1)
# one failed savepoint
try:
with suppress(ForcedError):
with transaction.atomic():
self.do(2)
raise ForcedError()
except ForcedError:
pass
# another successful savepoint
with transaction.atomic():
self.do(3)
@ -86,25 +84,21 @@ class TestConnectionOnCommit(TransactionTestCase):
def test_no_hooks_run_from_failed_transaction(self):
"""If outer transaction fails, no hooks from within it run."""
try:
with suppress(ForcedError):
with transaction.atomic():
with transaction.atomic():
self.do(1)
raise ForcedError()
except ForcedError:
pass
self.assertDone([])
def test_inner_savepoint_rolled_back_with_outer(self):
with transaction.atomic():
try:
with suppress(ForcedError):
with transaction.atomic():
with transaction.atomic():
self.do(1)
raise ForcedError()
except ForcedError:
pass
self.do(2)
self.assertDone([2])
@ -113,11 +107,9 @@ class TestConnectionOnCommit(TransactionTestCase):
with transaction.atomic():
with transaction.atomic():
self.do(1)
try:
with suppress(ForcedError):
with transaction.atomic(savepoint=False):
raise ForcedError()
except ForcedError:
pass
self.assertDone([])
@ -125,11 +117,9 @@ class TestConnectionOnCommit(TransactionTestCase):
with transaction.atomic():
with transaction.atomic():
self.do(1)
try:
with suppress(ForcedError):
with transaction.atomic():
raise ForcedError()
except ForcedError:
pass
self.assertDone([1])
@ -151,12 +141,10 @@ class TestConnectionOnCommit(TransactionTestCase):
self.assertDone([1, 2]) # not [1, 1, 2]
def test_hooks_cleared_after_rollback(self):
try:
with suppress(ForcedError):
with transaction.atomic():
self.do(1)
raise ForcedError()
except ForcedError:
pass
with transaction.atomic():
self.do(2)
@ -177,11 +165,9 @@ class TestConnectionOnCommit(TransactionTestCase):
self.assertDone([2])
def test_error_in_hook_doesnt_prevent_clearing_hooks(self):
try:
with suppress(ForcedError):
with transaction.atomic():
transaction.on_commit(lambda: self.notify('error'))
except ForcedError:
pass
with transaction.atomic():
self.do(1)