From 4256988e936a25a95e8ccf985cba9c9f57996d27 Mon Sep 17 00:00:00 2001 From: tdruez Date: Fri, 2 Aug 2024 19:43:00 +0400 Subject: [PATCH] Add unit tests #138 Signed-off-by: tdruez --- product_portfolio/tests/test_models.py | 33 ++++++++++++++++ product_portfolio/tests/test_views.py | 54 ++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/product_portfolio/tests/test_models.py b/product_portfolio/tests/test_models.py index bf58e170..c4551678 100644 --- a/product_portfolio/tests/test_models.py +++ b/product_portfolio/tests/test_models.py @@ -30,6 +30,7 @@ from product_portfolio.models import Product from product_portfolio.models import ProductComponent from product_portfolio.models import ProductComponentAssignedLicense +from product_portfolio.models import ProductDependency from product_portfolio.models import ProductInventoryItem from product_portfolio.models import ProductPackage from product_portfolio.models import ProductRelationStatus @@ -902,3 +903,35 @@ def test_product_portfolio_scancode_project_model_can_start_import(self): self.assertFalse(scancode_project.can_start_import) scancode_project.status = ScanCodeProject.Status.FAILURE self.assertFalse(scancode_project.can_start_import) + + def test_package_model_queryset_declared_dependencies_count(self): + product2 = Product.objects.create(name="Product2", dataspace=self.dataspace) + product3 = Product.objects.create(name="Product3", dataspace=self.dataspace) + package1 = Package.objects.create(filename="package1", dataspace=self.dataspace) + package2 = Package.objects.create(filename="package2", dataspace=self.dataspace) + + product1_qs = Package.objects.declared_dependencies_count(self.product1) + self.assertEqual(0, product1_qs.get(pk=package1.pk).declared_dependencies_count) + self.assertEqual(0, product1_qs.get(pk=package2.pk).declared_dependencies_count) + product2_qs = Package.objects.declared_dependencies_count(product2) + self.assertEqual(0, product2_qs.get(pk=package1.pk).declared_dependencies_count) + self.assertEqual(0, product2_qs.get(pk=package2.pk).declared_dependencies_count) + product3_qs = Package.objects.declared_dependencies_count(product3) + self.assertEqual(0, product3_qs.get(pk=package1.pk).declared_dependencies_count) + self.assertEqual(0, product3_qs.get(pk=package2.pk).declared_dependencies_count) + + ProductDependency.objects.create( + product=self.product1, for_package=package1, dataspace=self.dataspace + ) + ProductDependency.objects.create( + product=product2, for_package=package1, dataspace=self.dataspace + ) + product1_qs = Package.objects.declared_dependencies_count(self.product1) + self.assertEqual(1, product1_qs.get(pk=package1.pk).declared_dependencies_count) + self.assertEqual(0, product1_qs.get(pk=package2.pk).declared_dependencies_count) + product2_qs = Package.objects.declared_dependencies_count(product2) + self.assertEqual(1, product2_qs.get(pk=package1.pk).declared_dependencies_count) + self.assertEqual(0, product2_qs.get(pk=package2.pk).declared_dependencies_count) + product3_qs = Package.objects.declared_dependencies_count(product3) + self.assertEqual(0, product3_qs.get(pk=package1.pk).declared_dependencies_count) + self.assertEqual(0, product3_qs.get(pk=package2.pk).declared_dependencies_count) diff --git a/product_portfolio/tests/test_views.py b/product_portfolio/tests/test_views.py index 5a07a0d7..3615e369 100644 --- a/product_portfolio/tests/test_views.py +++ b/product_portfolio/tests/test_views.py @@ -35,6 +35,7 @@ from dje.tasks import logger as tasks_logger from dje.tasks import pull_project_data_from_scancodeio from dje.tasks import scancodeio_submit_project +from dje.tests import MaxQueryMixin from dje.tests import add_perms from dje.tests import create_superuser from dje.tests import create_user @@ -47,6 +48,7 @@ from product_portfolio.models import CodebaseResource from product_portfolio.models import Product from product_portfolio.models import ProductComponent +from product_portfolio.models import ProductDependency from product_portfolio.models import ProductItemPurpose from product_portfolio.models import ProductPackage from product_portfolio.models import ProductRelationStatus @@ -57,7 +59,7 @@ from workflow.models import RequestTemplate -class ProductPortfolioViewsTestCase(TestCase): +class ProductPortfolioViewsTestCase(MaxQueryMixin, TestCase): def setUp(self): self.dataspace = Dataspace.objects.create(name="nexB") self.alternate_dataspace = Dataspace.objects.create(name="Alternate") @@ -127,7 +129,8 @@ def test_product_portfolio_detail_view_tab_inventory_and_hierarchy_availability( ProductComponent.objects.create( product=self.product1, component=self.component1, dataspace=self.dataspace ) - response = self.client.get(url) + with self.assertNumQueries(29): + response = self.client.get(url) self.assertContains(response, expected1) self.assertContains(response, expected2) @@ -152,7 +155,8 @@ def test_product_portfolio_detail_view_tab_inventory_availability(self): ProductPackage.objects.create( product=self.product1, package=self.package1, dataspace=self.dataspace ) - response = self.client.get(url) + with self.assertNumQueries(26): + response = self.client.get(url) self.assertContains(response, expected) self.assertIn("Inventory", response.context["tabsets"]) @@ -221,6 +225,49 @@ def test_product_portfolio_detail_view_tab_imports_view(self): self.assertNotContains(response, "hx-trigger") self.assertNotContains(response, "Imports are currently in progress.") + def test_product_portfolio_detail_view_tab_dependency_view(self): + self.client.login(username="nexb_user", password="secret") + url = self.product1.get_url("tab_dependencies") + + with self.assertNumQueries(7): + response = self.client.get(url) + self.assertContains(response, "0 results") + + package2 = Package.objects.create(filename="package2", dataspace=self.dataspace) + # Unresolved package dependency + ProductDependency.objects.create( + dependency_uid=str(uuid.uuid4()), + product=self.product1, + for_package=self.package1, + dataspace=self.dataspace, + ) + # Resolved package dependency + ProductDependency.objects.create( + dependency_uid=str(uuid.uuid4()), + product=self.product1, + for_package=self.package1, + resolved_to_package=package2, + dataspace=self.dataspace, + ) + # Unresolved Product dependency + ProductDependency.objects.create( + dependency_uid=str(uuid.uuid4()), + product=self.product1, + declared_dependency="pkg:type/name", + dataspace=self.dataspace, + ) + # Unresolved Product dependency + ProductDependency.objects.create( + dependency_uid=str(uuid.uuid4()), + product=self.product1, + resolved_to_package=package2, + dataspace=self.dataspace, + ) + + with self.assertMaxQueries(9): + response = self.client.get(url) + self.assertContains(response, "4 results") + def test_product_portfolio_detail_view_object_type_filter_in_inventory_tab(self): self.client.login(username="nexb_user", password="secret") @@ -2609,7 +2656,6 @@ def test_product_portfolio_product_export_spdx_get_spdx_extracted_licenses(self) self.assertEqual(expected, extracted_licenses_as_dict) def test_product_portfolio_product_export_cyclonedx_view(self): - self.maxDiff = None owner1 = Owner.objects.create(name="Owner1", dataspace=self.dataspace) license1 = License.objects.create( key="l1",