From 58465616326dbd41fc959b4f079145039d4676bf Mon Sep 17 00:00:00 2001 From: tdruez Date: Fri, 27 Dec 2024 10:54:01 +0100 Subject: [PATCH] Add unit tests #102 Signed-off-by: tdruez --- product_portfolio/models.py | 13 +++-- product_portfolio/tests/test_models.py | 68 ++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/product_portfolio/models.py b/product_portfolio/models.py index b4ff7d5..2809e63 100644 --- a/product_portfolio/models.py +++ b/product_portfolio/models.py @@ -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): diff --git a/product_portfolio/tests/test_models.py b/product_portfolio/tests/test_models.py index 0ff5c2e..2b14d9e 100644 --- a/product_portfolio/tests/test_models.py +++ b/product_portfolio/tests/test_models.py @@ -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(