@@ -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