Skip to content

Commit

Permalink
Add 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 a6ee9f6 commit 4256988
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
33 changes: 33 additions & 0 deletions product_portfolio/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
54 changes: 50 additions & 4 deletions product_portfolio/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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)

Expand All @@ -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"])
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 4256988

Please sign in to comment.