Skip to content

Commit a0416aa

Browse files
fix(test): update TestSaveVulnerabilityIds mock for bulk_create
The test mocked Vulnerability_Id.save (individual saves) but save_vulnerability_ids now uses bulk_create. Django's bulk_create validates FK references before issuing SQL, raising ValueError when the finding has no pk. Mock bulk_create instead and assert on the deduplicated object list passed to it.
1 parent 7900fc3 commit a0416aa

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

unittests/test_finding_helper.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ class TestSaveVulnerabilityIds(DojoTestCase):
220220

221221
@patch("dojo.finding.helper.Vulnerability_Id.objects.filter")
222222
@patch("django.db.models.query.QuerySet.delete")
223-
@patch("dojo.finding.helper.Vulnerability_Id.save")
224-
def test_save_vulnerability_ids(self, save_mock, delete_mock, filter_mock):
223+
@patch("dojo.finding.helper.Vulnerability_Id.objects.bulk_create")
224+
def test_save_vulnerability_ids(self, bulk_create_mock, delete_mock, filter_mock):
225225
finding = Finding()
226226
new_vulnerability_ids = ["REF-1", "REF-2", "REF-2"]
227227
filter_mock.return_value = Vulnerability_Id.objects.none()
@@ -230,7 +230,10 @@ def test_save_vulnerability_ids(self, save_mock, delete_mock, filter_mock):
230230

231231
filter_mock.assert_called_with(finding=finding)
232232
delete_mock.assert_called_once()
233-
self.assertEqual(save_mock.call_count, 2)
233+
bulk_create_mock.assert_called_once()
234+
# Duplicates are removed: REF-1 and REF-2 only
235+
created_objects = bulk_create_mock.call_args[0][0]
236+
self.assertEqual(2, len(created_objects))
234237
self.assertEqual("REF-1", finding.cve)
235238

236239
@patch("dojo.models.Finding_Template.save")

0 commit comments

Comments
 (0)