Skip to content

Commit e4d26a2

Browse files
committed
Add tests
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 55e2c2e commit e4d26a2

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# VulnerableCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
from unittest.mock import patch
11+
12+
import pytest
13+
14+
from vulnerabilities.models import AdvisoryV2
15+
from vulnerabilities.pipelines.v2_improvers.compute_advisory_content_hash import (
16+
ComputeAdvisoryContentHash,
17+
)
18+
19+
pytestmark = pytest.mark.django_db
20+
21+
22+
@pytest.fixture
23+
def advisory_factory():
24+
def _create(count, with_hash=False, start=0):
25+
objs = []
26+
for i in range(start, start + count):
27+
objs.append(
28+
AdvisoryV2(
29+
summary=f"summary {i}",
30+
advisory_content_hash="existing_hash" if with_hash else None,
31+
unique_content_id=f"unique_id_{i}",
32+
advisory_id=f"ADV-{i}",
33+
datasource_id="ds",
34+
avid=f"ds/ADV-{i}",
35+
url=f"https://example.com/ADV-{i}",
36+
)
37+
)
38+
return AdvisoryV2.objects.bulk_create(objs)
39+
40+
return _create
41+
42+
43+
def run_pipeline():
44+
pipeline = ComputeAdvisoryContentHash()
45+
pipeline.compute_advisory_content_hash()
46+
47+
48+
@patch(
49+
"vulnerabilities.pipelines.v2_improvers.compute_advisory_content_hash.compute_advisory_content"
50+
)
51+
def test_pipeline_updates_only_missing_hash(mock_compute, advisory_factory):
52+
advisory_factory(3, with_hash=False, start=0)
53+
advisory_factory(2, with_hash=True, start=100)
54+
55+
mock_compute.return_value = "new_hash"
56+
57+
run_pipeline()
58+
59+
updated = AdvisoryV2.objects.filter(advisory_content_hash="new_hash").count()
60+
untouched = AdvisoryV2.objects.filter(advisory_content_hash="existing_hash").count()
61+
62+
assert updated == 3
63+
assert untouched == 2
64+
assert mock_compute.call_count == 3
65+
66+
67+
@patch(
68+
"vulnerabilities.pipelines.v2_improvers.compute_advisory_content_hash.compute_advisory_content"
69+
)
70+
def test_pipeline_bulk_update_batches(mock_compute, advisory_factory):
71+
advisory_factory(6000, with_hash=False)
72+
73+
mock_compute.return_value = "batch_hash"
74+
75+
run_pipeline()
76+
77+
assert AdvisoryV2.objects.filter(advisory_content_hash="batch_hash").count() == 6000
78+
79+
assert mock_compute.call_count == 6000
80+
81+
82+
@patch(
83+
"vulnerabilities.pipelines.v2_improvers.compute_advisory_content_hash.compute_advisory_content"
84+
)
85+
def test_pipeline_no_advisories(mock_compute):
86+
run_pipeline()
87+
88+
assert mock_compute.call_count == 0

vulnerabilities/tests/test_api_v2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ def setUp(self):
859859

860860
def test_list_with_purl_filter(self):
861861
url = reverse("package-v3-list")
862-
with self.assertNumQueries(29):
862+
with self.assertNumQueries(31):
863863
response = self.client.get(url, {"purl": "pkg:pypi/sample@1.0.0"})
864864
assert response.status_code == 200
865865
assert "packages" in response.data["results"]
@@ -868,7 +868,7 @@ def test_list_with_purl_filter(self):
868868

869869
def test_bulk_lookup(self):
870870
url = reverse("package-v3-bulk-lookup")
871-
with self.assertNumQueries(28):
871+
with self.assertNumQueries(30):
872872
response = self.client.post(url, {"purls": ["pkg:pypi/sample@1.0.0"]}, format="json")
873873
assert response.status_code == 200
874874
assert "packages" in response.data
@@ -878,7 +878,7 @@ def test_bulk_lookup(self):
878878
def test_bulk_search_plain(self):
879879
url = reverse("package-v3-bulk-search")
880880
payload = {"purls": ["pkg:pypi/sample@1.0.0"], "plain_purl": True, "purl_only": False}
881-
with self.assertNumQueries(28):
881+
with self.assertNumQueries(30):
882882
response = self.client.post(url, payload, format="json")
883883
assert response.status_code == 200
884884
assert "packages" in response.data
@@ -894,7 +894,7 @@ def test_bulk_search_purl_only(self):
894894

895895
def test_lookup_single_package(self):
896896
url = reverse("package-v3-lookup")
897-
with self.assertNumQueries(21):
897+
with self.assertNumQueries(23):
898898
response = self.client.post(url, {"purl": "pkg:pypi/sample@1.0.0"}, format="json")
899899
assert response.status_code == 200
900900
assert any(pkg["purl"] == "pkg:pypi/sample@1.0.0" for pkg in response.data)

0 commit comments

Comments
 (0)