Skip to content

Commit

Permalink
Add unit tests #102
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Dec 27, 2024
1 parent 59d38c4 commit 5846561
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
13 changes: 8 additions & 5 deletions product_portfolio/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,14 +663,17 @@ def label_with_icon(self):
return self.label

def save(self, *args, **kwargs):
"""
Update the weighted_risk_score of all the product relationship using this
purpose instance.
"""
is_addition = not self.pk
super().save(*args, **kwargs)
# No need to set when the object is created as it is not referenced yet.

# No need to trigger the update when the purpose is created,
# as it is not referenced yet by any product relationship.
if not is_addition:
# TODO: We could trigger only if it was changed. -> Add test
# TODO: Handle the case when the exposure_factor is reset to None as well. -> Add test
product_package_qs = ProductPackage.objects.filter(purpose=self)
product_package_qs.update_weighted_risk_score()
self.productpackage_set.update_weighted_risk_score()


class ProductComponentQuerySet(ProductSecuredQuerySet):
Expand Down
68 changes: 68 additions & 0 deletions product_portfolio/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,74 @@ def test_product_relationship_queryset_update_weighted_risk_score(self):
pp1.refresh_from_db()
self.assertIsNone(pp1.weighted_risk_score)

def test_productrelation_model_compute_weighted_risk_score(self):
purpose1 = make_product_item_purpose(self.dataspace)

# 1. package.risk_score = None, purpose = None
package1 = make_package(self.dataspace)
pp1 = make_product_package(self.product1, package=package1)
self.assertIsNone(pp1.compute_weighted_risk_score())

# 2. package.risk_score = 4.0, purpose.exposure_factor = None
package1.raw_update(risk_score=4.0)
pp1.raw_update(purpose=purpose1)
self.assertEqual(4.0, pp1.compute_weighted_risk_score())

# 3. package.risk_score = 4.0, purpose.exposure_factor = 0.5
purpose1.raw_update(exposure_factor=0.5)
self.assertEqual(2.0, pp1.compute_weighted_risk_score())

# 4. package.risk_score = None, purpose.exposure_factor = 0.5
package1.raw_update(risk_score=None)
self.assertIsNone(pp1.compute_weighted_risk_score())

def test_productrelation_model_set_weighted_risk_score(self):
purpose1 = make_product_item_purpose(self.dataspace)

# 1. package.risk_score = None, purpose = None
package1 = make_package(self.dataspace)
pp1 = make_product_package(self.product1, package=package1)
pp1.set_weighted_risk_score()
self.assertIsNone(pp1.weighted_risk_score)

# 2. package.risk_score = 4.0, purpose.exposure_factor = None
package1.raw_update(risk_score=4.0)
pp1.raw_update(purpose=purpose1)
pp1.set_weighted_risk_score()
self.assertEqual(4.0, pp1.weighted_risk_score)

# 3. package.risk_score = 4.0, purpose.exposure_factor = 0.5
purpose1.raw_update(exposure_factor=0.5)
pp1.set_weighted_risk_score()
self.assertEqual(2.0, pp1.weighted_risk_score)

# 4. package.risk_score = None, purpose.exposure_factor = 0.5
package1.raw_update(risk_score=None)
pp1.set_weighted_risk_score()
self.assertIsNone(pp1.weighted_risk_score)

def test_product_item_purpose_update_weighted_risk_score(self):
purpose1 = make_product_item_purpose(self.dataspace, exposure_factor=0.5)

package1 = make_package(self.dataspace, risk_score=10.0)
pp1 = make_product_package(self.product1, package=package1, purpose=purpose1)
self.assertEqual(5.0, pp1.weighted_risk_score)

purpose1.exposure_factor = 0.2
purpose1.save()
pp1.refresh_from_db()
self.assertEqual(2.0, pp1.weighted_risk_score)

pp1.purpose = None
pp1.save()
pp1.refresh_from_db()
self.assertEqual(10.0, pp1.weighted_risk_score)

purpose1.exposure_factor = None
purpose1.save()
pp1.refresh_from_db()
self.assertEqual(10.0, pp1.weighted_risk_score)

def test_productrelation_model_related_component_or_package_property(self):
component1 = Component.objects.create(name="c1", dataspace=self.dataspace)
pc1 = ProductComponent.objects.create(
Expand Down

0 comments on commit 5846561

Please sign in to comment.