Skip to content

Commit c1006d1

Browse files
author
prakash-kalwaniya
committed
tests(config): add subTest per model and cover list branch in handler tests
- Wrap each model_name iteration in self.subTest(model_name=model_name) so failures in TestHandlersTransaction clearly report which model caused the issue - Add test_devicegroup_templates_change_handler_list_branch to cover the list branch of devicegroup_templates_change_handler (type(instance) is list), verifying change_devices_templates.delay fires after commit and not on rollback
1 parent 8bdbd1a commit c1006d1

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

openwisp_controller/config/tests/test_handlers_transaction.py

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,65 @@ def __init__(self, id, model_name):
4141
]
4242

4343
for model_name in model_names:
44-
mock_instance = MockInstance(id="test-id", model_name=model_name)
45-
kwargs = {}
46-
if model_name == Config._meta.model_name:
47-
kwargs["backend"] = "test-backend"
48-
kwargs["old_backend"] = "old-backend"
49-
elif model_name == DeviceGroup._meta.model_name:
50-
kwargs["templates"] = []
51-
kwargs["old_templates"] = []
52-
elif model_name == Device._meta.model_name:
53-
kwargs["group_id"] = "test-group"
54-
kwargs["old_group_id"] = "old-group"
55-
56-
# Case 1: Transaction commits
44+
with self.subTest(model_name=model_name):
45+
mock_instance = MockInstance(id="test-id", model_name=model_name)
46+
kwargs = {}
47+
if model_name == Config._meta.model_name:
48+
kwargs["backend"] = "test-backend"
49+
kwargs["old_backend"] = "old-backend"
50+
elif model_name == DeviceGroup._meta.model_name:
51+
kwargs["templates"] = []
52+
kwargs["old_templates"] = []
53+
elif model_name == Device._meta.model_name:
54+
kwargs["group_id"] = "test-group"
55+
kwargs["old_group_id"] = "old-group"
56+
57+
# Case 1: Transaction commits
58+
mock_delay.reset_mock()
59+
with transaction.atomic():
60+
devicegroup_templates_change_handler(mock_instance, **kwargs)
61+
# Should not be called inside the transaction
62+
mock_delay.assert_not_called()
63+
64+
mock_delay.assert_called_once() # Should be called after commit
65+
66+
# Case 2: Transaction rolls back
67+
mock_delay.reset_mock()
68+
try:
69+
with transaction.atomic():
70+
devicegroup_templates_change_handler(mock_instance, **kwargs)
71+
mock_delay.assert_not_called()
72+
raise IntegrityError("Force rollback")
73+
except IntegrityError:
74+
pass
75+
76+
# Should not be called because it rolled back
77+
mock_delay.assert_not_called()
78+
79+
@patch("openwisp_controller.config.tasks.change_devices_templates.delay")
80+
def test_devicegroup_templates_change_handler_list_branch(self, mock_delay):
81+
"""
82+
Test that the list branch of devicegroup_templates_change_handler
83+
(type(instance) is list) correctly dispatches change_devices_templates.delay
84+
after commit and not on rollback.
85+
"""
86+
instance_list = ["device-id-1", "device-id-2"]
87+
kwargs = {"group_id": "test-group", "old_group_id": "old-group"}
88+
89+
with self.subTest("list branch: delay called after commit"):
5790
mock_delay.reset_mock()
5891
with transaction.atomic():
59-
devicegroup_templates_change_handler(mock_instance, **kwargs)
92+
devicegroup_templates_change_handler(instance_list, **kwargs)
6093
# Should not be called inside the transaction
6194
mock_delay.assert_not_called()
6295

6396
mock_delay.assert_called_once() # Should be called after commit
6497

65-
# Case 2: Transaction rolls back
98+
with self.subTest("list branch: delay not called on rollback"):
6699
mock_delay.reset_mock()
67100
try:
68101
with transaction.atomic():
69-
devicegroup_templates_change_handler(mock_instance, **kwargs)
102+
devicegroup_templates_change_handler(instance_list, **kwargs)
70103
mock_delay.assert_not_called()
71104
raise IntegrityError("Force rollback")
72105
except IntegrityError:

0 commit comments

Comments
 (0)