Skip to content

Commit 9fd93c0

Browse files
author
prakash-kalwaniya
committed
tests(config): use sentinel exception to avoid masking real errors on rollback
Replace bare 'except IntegrityError: pass' with a ForceRollback sentinel exception and self.assertRaises so that only the deliberate rollback is caught; this prevents real IntegrityErrors raised by devicegroup_templates_change_handler or mock_delay assertions from being silently swallowed.
1 parent c1006d1 commit 9fd93c0

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

openwisp_controller/config/tests/test_handlers_transaction.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from unittest.mock import patch
22

3-
from django.db import IntegrityError, transaction
3+
from django.db import transaction
44
from django.test import TransactionTestCase
55
from swapper import load_model
66

@@ -11,6 +11,10 @@
1111
Config = load_model("config", "Config")
1212

1313

14+
class ForceRollback(Exception):
15+
"""Sentinel exception used to deliberately roll back a transaction in tests."""
16+
17+
1418
class TestHandlersTransaction(TransactionTestCase):
1519
@patch("openwisp_controller.config.tasks.change_devices_templates.delay")
1620
def test_devicegroup_templates_change_handler_on_commit(self, mock_delay):
@@ -65,13 +69,11 @@ def __init__(self, id, model_name):
6569

6670
# Case 2: Transaction rolls back
6771
mock_delay.reset_mock()
68-
try:
72+
with self.assertRaises(ForceRollback):
6973
with transaction.atomic():
7074
devicegroup_templates_change_handler(mock_instance, **kwargs)
7175
mock_delay.assert_not_called()
72-
raise IntegrityError("Force rollback")
73-
except IntegrityError:
74-
pass
76+
raise ForceRollback
7577

7678
# Should not be called because it rolled back
7779
mock_delay.assert_not_called()
@@ -97,13 +99,11 @@ def test_devicegroup_templates_change_handler_list_branch(self, mock_delay):
9799

98100
with self.subTest("list branch: delay not called on rollback"):
99101
mock_delay.reset_mock()
100-
try:
102+
with self.assertRaises(ForceRollback):
101103
with transaction.atomic():
102104
devicegroup_templates_change_handler(instance_list, **kwargs)
103105
mock_delay.assert_not_called()
104-
raise IntegrityError("Force rollback")
105-
except IntegrityError:
106-
pass
106+
raise ForceRollback
107107

108108
# Should not be called because it rolled back
109109
mock_delay.assert_not_called()

0 commit comments

Comments
 (0)