From dc4ca0ae320fd3cd9181502df9c08c901ce48238 Mon Sep 17 00:00:00 2001 From: tdruez Date: Fri, 2 Aug 2024 17:24:43 +0400 Subject: [PATCH] Improve unit tests #138 Signed-off-by: tdruez --- Makefile | 4 +- product_portfolio/importers.py | 8 +-- product_portfolio/tests/test_importers.py | 64 ++++++++++++++++------- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 0a09f0e7..a668e43e 100644 --- a/Makefile +++ b/Makefile @@ -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)" diff --git a/product_portfolio/importers.py b/product_portfolio/importers.py index 7492e3c2..9538980c 100644 --- a/product_portfolio/importers.py +++ b/product_portfolio/importers.py @@ -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 @@ -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)) @@ -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, @@ -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) diff --git a/product_portfolio/tests/test_importers.py b/product_portfolio/tests/test_importers.py index ce90a5d1..93e40906 100644 --- a/product_portfolio/tests/test_importers.py +++ b/product_portfolio/tests/test_importers.py @@ -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/activemq-camel@5.11.0" - 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", @@ -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": [], } ] @@ -971,14 +983,25 @@ 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, @@ -986,6 +1009,7 @@ def test_product_portfolio_import_packages_from_scancodeio_importer(self, mock_f 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)