def setUpTestData(cls): cls.unicef_staff = UserFactory(is_staff=True) cls.intervention = InterventionFactory() cls.result_link = InterventionResultLinkFactory() cls.lower_result = LowerResultFactory( name="LL Name", result_link=cls.result_link, ) cls.indicator = IndicatorBlueprintFactory() AppliedIndicatorFactory( context_code="CC321", indicator=cls.indicator, lower_result=LowerResultFactory(name="LL Name", result_link=cls.result_link), cluster_name='ABC', ) AppliedIndicatorFactory( context_code="CC321", indicator=cls.indicator, lower_result=LowerResultFactory(name="LL Name", result_link=cls.result_link), cluster_name='XYZ', ) AppliedIndicatorFactory( context_code="CC321", indicator=cls.indicator, lower_result=LowerResultFactory(name="LL Name", result_link=cls.result_link), cluster_name='XYZ', ) AppliedIndicatorFactory( context_code="CC321", indicator=cls.indicator, lower_result=LowerResultFactory(name="LL Name", result_link=cls.result_link), ) cls.url = reverse("reports:cluster")
def setUpTestData(cls): cls.unicef_staff = UserFactory(is_staff=True) cls.result_link = InterventionResultLinkFactory() cls.lower_result = LowerResultFactory(result_link=cls.result_link) cls.indicator = IndicatorBlueprintFactory() cls.applied = AppliedIndicatorFactory(indicator=cls.indicator, lower_result=cls.lower_result)
def setUp(self): self.indicator = IndicatorBlueprintFactory(title="Indicator") self.data = { "name": "LL Name", "code": "C123", "indicator": {"title": self.indicator.title}, "result_link": self.result_link.pk }
def setUpTestData(cls): cls.section = SectorFactory() cls.intervention = InterventionFactory() cls.result_link = InterventionResultLinkFactory( intervention=cls.intervention, ) cls.lower_result = LowerResultFactory(result_link=cls.result_link, ) cls.applied_indicator = AppliedIndicatorFactory( lower_result=cls.lower_result, ) cls.location = LocationFactory() cls.indicator = IndicatorBlueprintFactory()
def setUp(self): self.indicator = IndicatorBlueprintFactory() self.intervention = InterventionFactory(status=Intervention.ACTIVE) self.result_link = InterventionResultLinkFactory( intervention=self.intervention, ) self.lower_result = LowerResultFactory(result_link=self.result_link, ) self.applied_indicator = AppliedIndicatorFactory( lower_result=self.lower_result, target={ "d": 3, "v": 4 }, ) self.intervention.flat_locations.add(self.location) self.intervention.sections.add(self.section) self.data = { "indicator": { "title": self.indicator.title }, "lower_result": self.lower_result.pk, "locations": [self.location.pk], "section": self.section.pk, }
def test_indicator_blueprint(self): instance = IndicatorBlueprintFactory.build(title='xyz') self.assertEqual(str(instance), u'xyz') instance = IndicatorBlueprintFactory.build(title=u'\xccsland') self.assertEqual(str(instance), u'\xccsland')
class TestAppliedIndicatorSerializer(BaseTenantTestCase): @classmethod def setUpTestData(cls): cls.section = SectionFactory() cls.location = LocationFactory() def setUp(self): self.indicator = IndicatorBlueprintFactory() self.intervention = InterventionFactory(status=Intervention.ACTIVE) self.result_link = InterventionResultLinkFactory( intervention=self.intervention, ) self.lower_result = LowerResultFactory(result_link=self.result_link, ) self.applied_indicator = AppliedIndicatorFactory( lower_result=self.lower_result, target={ "d": 3, "v": 4 }, ) self.intervention.flat_locations.add(self.location) self.intervention.sections.add(self.section) self.data = { "indicator": { "title": self.indicator.title }, "lower_result": self.lower_result.pk, "locations": [self.location.pk], "section": self.section.pk, } def test_validate_invalid_location(self): """If location is not related to intervention, then fail validation""" self.intervention.flat_locations.remove(self.location) serializer = AppliedIndicatorSerializer(data=self.data) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors, { "non_field_errors": [ 'This indicator can only have locations that were ' 'previously saved on the intervention' ] }) def test_validate_no_section(self): """If no section provided on indicator creation, then fail validation""" del self.data["section"] request = RequestFactory().post( reverse('partners_api:intervention-indicators-update', args=[self.intervention.id])) serializer = AppliedIndicatorSerializer(data=self.data, context={"request": request}) self.assertFalse(serializer.is_valid()) self.assertEqual(serializer.errors, {"non_field_errors": ['Section is required']}) # PATCHing an indicator should not require to have sections in the request request = RequestFactory().patch( reverse('partners_api:intervention-indicators-update', args=[self.intervention.id])) serializer = AppliedIndicatorSerializer(data=self.data, context={"request": request}) self.assertTrue(serializer.is_valid()) def test_validate_invalid_section(self): """If sector already set on applied indicator then fail validation""" self.data["section"] = SectionFactory().pk serializer = AppliedIndicatorSerializer(data=self.data) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors, { "non_field_errors": [ 'This indicator can only have a section that was ' 'previously saved on the intervention' ] }) def test_validate_no_cluster_indicator(self): """Check that validation passes when given no cluster indicator id""" self.intervention.flat_locations.add(self.location) serializer = AppliedIndicatorSerializer(data=self.data) self.assertTrue(serializer.is_valid()) def test_validate_indicator_used(self): """CHeck that is indicator already used we fail validation""" self.applied_indicator.indicator = self.indicator self.applied_indicator.save() serializer = AppliedIndicatorSerializer(data=self.data) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors, { "non_field_errors": ['This indicator is already being monitored for this Result'] }) def test_validate_partial_exception(self): """If partial validation, and indicator is not blueprint indicator instance then fail""" self.data["indicator"] = {"title": "wrong"} serializer = AppliedIndicatorSerializer(data=self.data, partial=True) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors, { "non_field_errors": [ 'Indicator blueprint cannot be updated after first use, ' 'please remove this indicator and add another or contact the eTools Focal Point in ' 'your office for assistance' ] }) def test_validate(self): """If cluster indicator provided, no check is happening that value""" self.data["cluster_indicator_id"] = "404" self.intervention.flat_locations.add(self.location) serializer = AppliedIndicatorSerializer(data=self.data) self.assertTrue(serializer.is_valid()) def test_validate_value_numbers(self): self.data["target"] = {"d": 123, "v": "$321"} self.data["baseline"] = {"d": "wrong", "v": 321} self.intervention.flat_locations.add(self.location) serializer = AppliedIndicatorSerializer(data=self.data) self.assertFalse(serializer.is_valid()) self.assertIn("target", serializer.errors) self.assertIn("baseline", serializer.errors) def test_create(self): applied_qs = AppliedIndicator.objects.filter( lower_result__pk=self.lower_result.pk) count = applied_qs.count() serializer = AppliedIndicatorSerializer(data=self.data) self.assertTrue(serializer.is_valid()) indicator = serializer.create(serializer.validated_data) self.assertIsInstance(indicator, AppliedIndicator) self.assertEqual(applied_qs.count(), count + 1) def test_validate_changed_denominator_status_active(self): """Not allowed to change denominator if draft status active and not in amendment mode""" self.applied_indicator.indicator = self.indicator self.applied_indicator.save() self.data["cluster_indicator_id"] = "404" self.data["target"] = {'d': 1, 'v': 2} self.intervention.flat_locations.add(self.location) self.assertEqual(self.intervention.status, Intervention.ACTIVE) self.assertFalse(self.intervention.in_amendment) serializer = AppliedIndicatorSerializer( data=self.data, instance=self.applied_indicator) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors, { "non_field_errors": [ 'You cannot change the Indicator Target Denominator if PD/SSFA is not in status Draft or Signed' ] }) def test_validate_changed_denominator_status_draft(self): """Allowed to change denominator if in draft status""" self.intervention.status = Intervention.DRAFT self.intervention.save() self.applied_indicator.indicator = self.indicator self.applied_indicator.save() self.data["cluster_indicator_id"] = "404" self.data["target"] = {'d': 1, 'v': 2} self.intervention.flat_locations.add(self.location) self.assertEqual(self.intervention.status, Intervention.DRAFT) serializer = AppliedIndicatorSerializer( data=self.data, instance=self.applied_indicator) self.assertTrue(serializer.is_valid()) def test_validate_changed_denominator_status_signed(self): """Allowed to change denominator if in signed status""" self.intervention.status = Intervention.SIGNED self.intervention.save() self.applied_indicator.indicator = self.indicator self.applied_indicator.save() self.data["cluster_indicator_id"] = "404" self.data["target"] = {'d': 1, 'v': 2} self.intervention.flat_locations.add(self.location) self.assertEqual(self.intervention.status, Intervention.SIGNED) serializer = AppliedIndicatorSerializer( data=self.data, instance=self.applied_indicator) self.assertTrue(serializer.is_valid()) def test_validate_changed_denominator_in_amendment(self): """Allowed to change denominator if in amendment mode""" self.intervention.in_amendment = True self.intervention.save() self.applied_indicator.indicator = self.indicator self.applied_indicator.save() self.data["cluster_indicator_id"] = "404" self.data["target"] = {'d': 1, 'v': 2} self.intervention.flat_locations.add(self.location) self.assertTrue(self.intervention.in_amendment) self.assertEqual(self.intervention.status, Intervention.ACTIVE) serializer = AppliedIndicatorSerializer( data=self.data, instance=self.applied_indicator) self.assertTrue(serializer.is_valid()) def test_validate_changed_denominator_ratio_in_amendment(self): """Not allowed to change denominator if in amendment mode and display_type is ratio """ self.intervention.in_amendment = True self.intervention.save() self.indicator.display_type = IndicatorBlueprint.RATIO self.indicator.save() self.applied_indicator.indicator = self.indicator self.applied_indicator.save() self.data["cluster_indicator_id"] = "404" self.data["target"] = {'d': 1, 'v': 2} self.intervention.flat_locations.add(self.location) self.assertTrue(self.intervention.in_amendment) self.assertEqual(self.intervention.status, Intervention.ACTIVE) self.assertEqual(self.applied_indicator.indicator.display_type, IndicatorBlueprint.RATIO) serializer = AppliedIndicatorSerializer( data=self.data, instance=self.applied_indicator) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors, { "non_field_errors": [ 'You cannot change the Indicator Target Denominator if PD/SSFA is not in status Draft or Signed' ] })