|
1 | 1 | import json |
2 | 2 | from unittest import TestCase |
| 3 | +from unittest.mock import MagicMock |
| 4 | +from unittest.mock import patch |
3 | 5 |
|
4 | 6 | from src.superannotate import AppException |
5 | 7 | from src.superannotate import SAClient |
@@ -181,3 +183,39 @@ def test_create_project_invalid_form_structure(self): |
181 | 183 | self.PROJECT_TYPE, |
182 | 184 | form=invalid_form, |
183 | 185 | ) |
| 186 | + |
| 187 | + def test_create_project_form_attach_failure_cleanup(self): |
| 188 | + """Test that project is deleted when form attachment fails""" |
| 189 | + with open(self.FORM_PATH) as f: |
| 190 | + form_data = json.load(f) |
| 191 | + |
| 192 | + # Mock the controller to simulate form attachment failure |
| 193 | + with patch.object( |
| 194 | + sa.controller.projects, "attach_form" |
| 195 | + ) as mock_attach_form, patch.object( |
| 196 | + sa.controller.projects, "delete" |
| 197 | + ) as mock_delete: |
| 198 | + # Create a mock response that raises AppException when raise_for_status is called |
| 199 | + mock_response = MagicMock() |
| 200 | + mock_response.raise_for_status.side_effect = AppException( |
| 201 | + "Form attachment failed" |
| 202 | + ) |
| 203 | + mock_attach_form.return_value = mock_response |
| 204 | + |
| 205 | + # Attempt to create project - should fail and trigger cleanup |
| 206 | + with self.assertRaises(AppException) as context: |
| 207 | + sa.create_project( |
| 208 | + self.PROJECT_NAME, |
| 209 | + "desc", |
| 210 | + self.PROJECT_TYPE, |
| 211 | + form=form_data, |
| 212 | + ) |
| 213 | + |
| 214 | + # Verify form attachment was attempted |
| 215 | + mock_attach_form.assert_called_once() |
| 216 | + |
| 217 | + # Verify project deletion was called for cleanup |
| 218 | + mock_delete.assert_called_once() |
| 219 | + |
| 220 | + # Verify the original exception is re-raised |
| 221 | + assert "Form attachment failed" in str(context.exception) |
0 commit comments