Skip to content

Commit

Permalink
Improve unit tests #138
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Aug 2, 2024
1 parent 6b4d4ca commit dc4ca0a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ doc8:
--ignore-path docs/installation_and_sysadmin/ --quiet docs/

valid:
@echo "-> Run Ruff linter"
@${ACTIVATE} ruff check --fix
@echo "-> Run Ruff format"
@${ACTIVATE} ruff format
@echo "-> Run Ruff linter"
@${ACTIVATE} ruff check --fix

check:
@echo "-> Run Ruff linter validation (pycodestyle, bandit, isort, and more)"
Expand Down
8 changes: 4 additions & 4 deletions product_portfolio/importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ def import_package(self, package_data):
# Check if the Package already exists in the local Dataspace
try:
package = Package.objects.scope(self.user.dataspace).get(**unique_together_lookups)
self.existing["package"].append(str(package))
self.existing["package"].append(package)
except (ObjectDoesNotExist, MultipleObjectsReturned):
package = None

Expand All @@ -677,7 +677,7 @@ def import_package(self, package_data):
reference_object = qs.first()
try:
package = copy_object(reference_object, user_dataspace, self.user, update=False)
self.created["package"].append(str(package))
self.created["package"].append(package)
except IntegrityError as error:
self.errors["package"].append(str(error))

Expand All @@ -694,7 +694,7 @@ def import_package(self, package_data):
except ValidationError as errors:
self.errors["package"].append(str(errors))
return
self.created["package"].append(str(package))
self.created["package"].append(package)

ProductPackage.objects.get_or_create(
product=self.product,
Expand Down Expand Up @@ -739,4 +739,4 @@ def import_dependency(self, dependency_data):
self.errors["dependency"].append(str(errors))
return

self.created["dependency"].append(str(dependency))
self.created["dependency"].append(dependency)
64 changes: 44 additions & 20 deletions product_portfolio/tests/test_importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,19 +939,12 @@ def test_product_portfolio_product_import_from_scan_view_empty_packages(self):
self.assertContains(response, expected2)
self.assertContains(response, expected3)

@mock.patch("dejacode_toolkit.scancodeio.ScanCodeIO.fetch_project_dependencies")
@mock.patch("dejacode_toolkit.scancodeio.ScanCodeIO.fetch_project_packages")
def test_product_portfolio_import_packages_from_scancodeio_importer(self, mock_fetch_packages):
def test_product_portfolio_import_packages_from_scancodeio_importer(
self, mock_fetch_packages, mock_fetch_dependencies
):
purl = "pkg:maven/org.apache.activemq/[email protected]"
dependencies = [
{
"purl": "pkg:maven/org.apache.camel/camel-catalog",
"scope": "compile",
"is_runtime": True,
"is_optional": False,
"is_resolved": False,
"extracted_requirement": "2.18.1",
}
]
mock_fetch_packages.return_value = [
{
"type": "maven",
Expand All @@ -961,7 +954,26 @@ def test_product_portfolio_import_packages_from_scancodeio_importer(self, mock_f
"primary_language": "Java",
"purl": purl,
"declared_license_expression": "bsd-new",
"dependencies": dependencies,
}
]

dependency_uid = "pkg:pypi/aboutcode-toolkit@10?uuid=9d150876-8431-4d85"
mock_fetch_dependencies.return_value = [
{
"purl": "pkg:pypi/aboutcode-toolkit@10",
"extracted_requirement": "aboutcode-toolkit==10",
"scope": "install",
"is_runtime": True,
"is_optional": False,
"is_resolved": True,
"is_direct": True,
"dependency_uid": dependency_uid,
"for_package_uid": None,
"resolved_to_package_uid": None,
"datafile_path": "setup.cfg",
"datasource_id": "pypi_setup_cfg",
"package_type": "pypi",
"affected_by_vulnerabilities": [],
}
]

Expand All @@ -971,21 +983,33 @@ def test_product_portfolio_import_packages_from_scancodeio_importer(self, mock_f
product=self.product1,
)
created, existing, errors = importer.save()
self.assertEqual(1, len(created))
created_package = created[0]
created_package = created.get("package")[0]
self.assertEqual(purl, created_package.package_url)
self.assertEqual("bsd-new", created_package.license_expression)
self.assertEqual(dependencies, created_package.dependencies)
self.assertEqual([], existing)
self.assertEqual({}, existing)
self.assertEqual([purl], [package.package_url for package in self.product1.packages.all()])
self.assertEqual([], errors)
self.assertEqual({}, errors)

created_dependency = created.get("dependency")[0]
self.assertEqual(dependency_uid, created_dependency.dependency_uid)
self.assertEqual("pkg:pypi/aboutcode-toolkit@10", created_dependency.declared_dependency)
self.assertEqual("aboutcode-toolkit==10", created_dependency.extracted_requirement)
self.assertEqual("install", created_dependency.scope)
self.assertEqual("pypi_setup_cfg", created_dependency.datasource_id)
self.assertTrue(created_dependency.is_runtime)
self.assertFalse(created_dependency.is_optional)
self.assertTrue(created_dependency.is_resolved)
self.assertTrue(created_dependency.is_direct)
self.assertIsNone(created_dependency.for_package_id)
self.assertIsNone(created_dependency.resolved_to_package_id)

importer = ImportPackageFromScanCodeIO(
user=self.super_user,
project_uuid=uuid.uuid4(),
product=self.product1,
)
created, existing, errors = importer.save()
self.assertEqual([], created)
self.assertEqual([purl], [package.package_url for package in existing])
self.assertEqual([], errors)
self.assertEqual({}, created)
self.assertEqual([purl], [str(package) for package in existing["package"]])
self.assertEqual([dependency_uid], [str(package) for package in existing["dependency"]])
self.assertEqual({}, errors)

0 comments on commit dc4ca0a

Please sign in to comment.