Skip to content

Commit

Permalink
Add tests for data migration
Browse files Browse the repository at this point in the history
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
  • Loading branch information
TG1999 committed Jan 25, 2023
1 parent 9b335c4 commit a001a87
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
11 changes: 9 additions & 2 deletions vulnerabilities/migrations/0037_auto_20230120_1204.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# VulnerableCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/vulnerablecode for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#

from django.db import migrations
from packageurl import PackageURL

from vulnerabilities.severity_systems import SCORING_SYSTEMS

Expand All @@ -9,7 +17,6 @@ def remove_advisories(apps, schema_editor):
Advisory = apps.get_model("vulnerabilities", "Advisory")
deletables = []
for advisory in Advisory.objects.iterator(chunk_size=1000):
print(advisory.pk)
for ref in advisory.references:
if not ref["url"]:
deletables.append(advisory.pk)
Expand Down
35 changes: 35 additions & 0 deletions vulnerabilities/tests/test_data_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,38 @@ def test_merge_rows(self):
]

assert severities == expected


class RemoveCorrupteAdvisories(TestMigrations):
app_name = "vulnerabilities"
migrate_from = "0036_alter_package_package_url_and_more"
migrate_to = "0037_auto_20230120_1204"

def setUpBeforeMigration(self, apps):
# using get_model to avoid circular import
Advisory = apps.get_model("vulnerabilities", "Advisory")

corrupted_advisory = Advisory.objects.create(
aliases=["CVE-2020-1234"],
summary="Corrupted advisory",
references=[
{
"reference_id": "cpe:2.3:a:f5:nginx:1.16.1:*:*:*:*:*:*:*",
"url": "",
"severity": [
{
"scoring_system": "cvssv3_vector",
"value": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
}
],
}
],
date_collected="2020-01-01",
date_published="2020-01-01",
)
corrupted_advisory.save()

def test_removal_of_corrupted_advisory(self):
# using get_model to avoid circular import
Advisory = self.apps.get_model("vulnerabilities", "Advisory")
Advisory.objects.all().count() == 0

0 comments on commit a001a87

Please sign in to comment.