Skip to content

Commit d671ebd

Browse files
committed
test: add test for latest advisory data migration
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent ea463ce commit d671ebd

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

vulnerabilities/tests/test_data_migrations.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from django.db import IntegrityError
1313
from django.db import connection
1414
from django.db.migrations.executor import MigrationExecutor
15+
from django.db.models import Count
1516
from django.test import TestCase
1617
from django.utils import timezone
1718
from packageurl import PackageURL
@@ -1031,3 +1032,59 @@ def test_m2m_relationships_work(self):
10311032

10321033
self.assertIn(commit1, impacted.affecting_commits.all())
10331034
self.assertIn(commit2, impacted.fixed_by_commits.all())
1035+
1036+
1037+
class TestLatestAdvisoryV2Migration(TestMigrations):
1038+
"""Tests is_latest field population on existing v2 advisory."""
1039+
1040+
app_name = "vulnerabilities"
1041+
migrate_from = "0120_impactedpackage_last_range_unfurl_at_and_more"
1042+
migrate_to = "0121_advisoryv2_is_latest_alter_advisoryv2_advisory_id_and_more"
1043+
1044+
def setUpBeforeMigration(self, apps):
1045+
AdvisoryV2 = apps.get_model("vulnerabilities", "AdvisoryV2")
1046+
1047+
AdvisoryV2.objects.create(
1048+
unique_content_id="content_id_old",
1049+
url="https://old.example.com",
1050+
summary="Old advisory",
1051+
advisory_id="test_adv",
1052+
avid="test_pipeline/test_adv",
1053+
datasource_id="test_pipeline",
1054+
)
1055+
1056+
AdvisoryV2.objects.create(
1057+
unique_content_id="content_id_old2",
1058+
url="https://old.example.com",
1059+
summary="Old 2 advisory",
1060+
advisory_id="test_adv",
1061+
avid="test_pipeline/test_adv",
1062+
datasource_id="test_pipeline",
1063+
)
1064+
1065+
AdvisoryV2.objects.create(
1066+
unique_content_id="content_id_new",
1067+
url="https://old.example.com",
1068+
summary="New advisory",
1069+
advisory_id="test_adv",
1070+
avid="test_pipeline/test_adv",
1071+
datasource_id="test_pipeline",
1072+
)
1073+
1074+
def test_no_duplicate_is_latest_for_avid(self):
1075+
AdvisoryV2 = apps.get_model("vulnerabilities", "AdvisoryV2")
1076+
1077+
duplicate = (
1078+
AdvisoryV2.objects.filter(is_latest=True)
1079+
.values("avid")
1080+
.annotate(cnt=Count("id"))
1081+
.filter(cnt__gt=1)
1082+
)
1083+
1084+
self.assertFalse(duplicate.exists())
1085+
1086+
def test_latest_is_actually_recent(self):
1087+
AdvisoryV2 = apps.get_model("vulnerabilities", "AdvisoryV2")
1088+
1089+
latest = AdvisoryV2.objects.get(avid="test_pipeline/test_adv", is_latest=True)
1090+
self.assertEqual("New advisory", latest.summary)

0 commit comments

Comments
 (0)