@@ -44,7 +44,7 @@ def advisory_data():
4444
4545
4646@pytest .mark .django_db
47- def test_recompute_content_ids_basic (advisory_data ):
47+ def test_recompute_content_ids_basic_async (advisory_data ):
4848 """
4949 Test that advisories without content IDs get them computed.
5050 """
@@ -68,7 +68,7 @@ def test_recompute_content_ids_basic(advisory_data):
6868
6969
7070@pytest .mark .django_db
71- def test_recompute_content_ids_multiple_batches (advisory_data ):
71+ def test_recompute_content_ids_multiple_batches_async (advisory_data ):
7272 """
7373 Test that content ID computation works across multiple batches.
7474 """
@@ -103,6 +103,66 @@ def test_recompute_content_ids_multiple_batches(advisory_data):
103103 assert Advisory .objects .exclude (unique_content_id__length = 64 ).count () == 0
104104
105105
106+ @pytest .mark .django_db
107+ def test_recompute_content_ids_basic (advisory_data ):
108+ """
109+ Test that advisories without content IDs get them computed.
110+ """
111+ advisory = Advisory .objects .create (
112+ summary = advisory_data .summary ,
113+ affected_packages = [pkg .to_dict () for pkg in advisory_data .affected_packages ],
114+ references = [ref .to_dict () for ref in advisory_data .references ],
115+ unique_content_id = "" ,
116+ date_collected = datetime .datetime (2024 , 1 , 1 , tzinfo = pytz .UTC ),
117+ )
118+
119+ with patch ("vulnerabilities.pipelines.recompute_content_ids.get_max_workers" ) as mock_workers :
120+ mock_workers .return_value = 0
121+
122+ pipeline = RecomputeContentIDPipeline ()
123+ pipeline .recompute_content_ids ()
124+
125+ advisory .refresh_from_db ()
126+ assert advisory .unique_content_id != ""
127+ assert len (advisory .unique_content_id ) == 64 # SHA256 hash length
128+
129+
130+ @pytest .mark .django_db
131+ def test_recompute_content_ids_multiple_batches (advisory_data ):
132+ """
133+ Test that content ID computation works across multiple batches.
134+ """
135+ dates = [
136+ datetime .datetime (
137+ 2024 + (i // (12 * 28 )), # Year
138+ ((i // 28 ) % 12 ) + 1 , # Month (1-12)
139+ (i % 28 ) + 1 , # Day (1-28)
140+ tzinfo = pytz .UTC ,
141+ )
142+ for i in range (2500 ) # Create 2500 advisories
143+ ]
144+
145+ for date in dates :
146+ Advisory .objects .create (
147+ summary = advisory_data .summary ,
148+ affected_packages = [pkg .to_dict () for pkg in advisory_data .affected_packages ],
149+ references = [ref .to_dict () for ref in advisory_data .references ],
150+ unique_content_id = "" ,
151+ date_imported = date ,
152+ date_collected = date ,
153+ )
154+
155+ with patch ("vulnerabilities.pipelines.recompute_content_ids.get_max_workers" ) as mock_workers :
156+ mock_workers .return_value = 0
157+
158+ pipeline = RecomputeContentIDPipeline ()
159+ pipeline .BATCH_SIZE = 1000
160+ pipeline .recompute_content_ids ()
161+
162+ assert not Advisory .objects .filter (unique_content_id = "" ).exists ()
163+ assert Advisory .objects .exclude (unique_content_id__length = 64 ).count () == 0
164+
165+
106166@pytest .mark .django_db
107167def test_recompute_content_ids_preserves_existing (advisory_data ):
108168 """
@@ -137,7 +197,7 @@ def test_recompute_content_ids_error_handling(advisory_data):
137197 summary = advisory_data .summary ,
138198 affected_packages = [pkg .to_dict () for pkg in advisory_data .affected_packages ],
139199 references = [ref .to_dict () for ref in advisory_data .references ],
140- unique_content_id = "" ,
200+ unique_content_id = "Test " ,
141201 date_collected = datetime .datetime (2024 , 1 , 1 , tzinfo = pytz .UTC ),
142202 )
143203
@@ -150,8 +210,6 @@ def test_recompute_content_ids_error_handling(advisory_data):
150210 mock_workers .return_value = 0
151211
152212 pipeline = RecomputeContentIDPipeline ()
153- pipeline .recompute_content_ids ()
154-
155- advisory = Advisory .objects .first ()
156- assert advisory is not None
157- assert advisory .unique_content_id == ""
213+ # expect an error
214+ with pytest .raises (Exception ):
215+ pipeline .recompute_content_ids ()
0 commit comments