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 test_create_hr(self): """Creating new hr reporting requirements When none currently existing """ AppliedIndicatorFactory(is_high_frequency=True, lower_result=self.lower_result) requirement_qs = ReportingRequirement.objects.filter( intervention=self.intervention, report_type=ReportingRequirement.TYPE_HR, ) init_count = requirement_qs.count() data = { "report_type": ReportingRequirement.TYPE_HR, "reporting_requirements": [{ "due_date": datetime.date(2001, 4, 15) }, { "due_date": datetime.date(2001, 5, 15) }] } serializer = InterventionReportingRequirementCreateSerializer( data=data, context=self.context) self.assertTrue(serializer.is_valid()) serializer.create(serializer.validated_data) self.assertEqual(requirement_qs.count(), init_count + 2)
def test_update_create_hr(self): """Updating existing hr reporting requirements and create new""" AppliedIndicatorFactory(is_high_frequency=True, lower_result=self.lower_result) report_type = ReportingRequirement.TYPE_HR requirement = ReportingRequirementFactory( intervention=self.intervention, report_type=report_type, due_date=datetime.date(2001, 4, 15), ) requirement_qs = ReportingRequirement.objects.filter( intervention=self.intervention, report_type=report_type, ) init_count = requirement_qs.count() data = { "report_type": report_type, "reporting_requirements": [{ "due_date": datetime.date(2001, 4, 15), }, { "due_date": datetime.date(2001, 6, 15), }] } serializer = InterventionReportingRequirementCreateSerializer( data=data, context=self.context) self.assertTrue(serializer.is_valid()) serializer.create(serializer.validated_data) self.assertEqual(requirement_qs.count(), init_count + 1) req_updated = ReportingRequirement.objects.get(pk=requirement.pk) self.assertEqual(req_updated.start_date, None) self.assertEqual(req_updated.end_date, req_updated.due_date) self.assertEqual(req_updated.due_date, datetime.date(2001, 4, 15))
def test_update_create_hr(self): """Updating existing hr reporting requirements and create new""" AppliedIndicatorFactory( is_high_frequency=True, lower_result=self.lower_result ) report_type = ReportingRequirement.TYPE_HR data = { "report_type": report_type, "reporting_requirements": [{ "start_date": datetime.date(2001, 3, 15), "due_date": datetime.date(2001, 4, 15), "end_date": datetime.date(2001, 4, 15), }, { "start_date": datetime.date(2001, 4, 16), "due_date": datetime.date(2001, 6, 15), "end_date": datetime.date(2001, 6, 15), }] } serializer = InterventionReportingRequirementCreateSerializer( data=data, context=self.context ) self.assertTrue(serializer.is_valid()) serializer.create(serializer.validated_data) requirement_qs_count = ReportingRequirement.objects.filter( intervention=self.intervention, report_type=report_type, ).count() self.assertEqual(requirement_qs_count, 2)
def test_validation_hr_with_gaps(self): AppliedIndicatorFactory( is_high_frequency=True, lower_result=self.lower_result ) data = { "report_type": ReportingRequirement.TYPE_HR, "reporting_requirements": [{ "start_date": datetime.date(2001, 1, 1), "due_date": datetime.date(2001, 3, 10), "end_date": datetime.date(2001, 3, 10), }, { "start_date": datetime.date(2001, 4, 10), "due_date": datetime.date(2001, 5, 15), "end_date": datetime.date(2001, 5, 15), }] } serializer = InterventionReportingRequirementCreateSerializer( data=data, context=self.context ) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors['reporting_requirements'], {"start_date": 'Next start date needs to be one day after previous end date.'} )
def setUpTestData(cls): cls.intervention = InterventionFactory( start=datetime.date(2001, 1, 1), in_amendment=True, ) cls.result_link = InterventionResultLinkFactory( intervention=cls.intervention) cls.lower_result = LowerResultFactory(result_link=cls.result_link) cls.indicator = AppliedIndicatorFactory(lower_result=cls.lower_result) cls.context = {"intervention": cls.intervention}
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 test_validation_pd_has_no_start(self): intervention = InterventionFactory(in_amendment=True) result_link = InterventionResultLinkFactory(intervention=intervention) lower_result = LowerResultFactory(result_link=result_link) AppliedIndicatorFactory(lower_result=lower_result) data = { "report_type": ReportingRequirement.TYPE_QPR, "reporting_requirements": [] } serializer = InterventionReportingRequirementCreateSerializer( data=data, context={"intervention": intervention}) self.assertFalse(serializer.is_valid()) self.assertEqual(serializer.errors['non_field_errors'], ['PD needs to have a start date.'])
def test_create_applied_indicators(self): indicator = AppliedIndicatorFactory(lower_result=self.lower_result) serializer = LowerResultCUSerializer( data=self.data, context={"applied_indicators": [ {"id": indicator.pk, "name": "Title", "unit": IndicatorBlueprint.NUMBER} ]} ) self.assertTrue(serializer.is_valid()) lower_result = serializer.create(serializer.validated_data) self.assertIsInstance(lower_result, LowerResult) self.assertTrue(AppliedIndicator.objects.filter( lower_result__pk=lower_result.pk ).exists())
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_validation_hr(self): AppliedIndicatorFactory(is_high_frequency=True, lower_result=self.lower_result) data = { "report_type": ReportingRequirement.TYPE_HR, "reporting_requirements": [{ "due_date": datetime.date(2001, 4, 15) }, { "due_date": datetime.date(2001, 5, 15) }] } serializer = InterventionReportingRequirementCreateSerializer( data=data, context=self.context) self.assertTrue(serializer.is_valid())
def test_validation_pd_status(self): intervention = InterventionFactory(start=datetime.date(2001, 1, 1), status=Intervention.CLOSED) result_link = InterventionResultLinkFactory(intervention=intervention) lower_result = LowerResultFactory(result_link=result_link) AppliedIndicatorFactory(lower_result=lower_result) data = { "report_type": ReportingRequirement.TYPE_QPR, "reporting_requirements": [] } serializer = InterventionReportingRequirementCreateSerializer( data=data, context={"intervention": intervention}) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors['non_field_errors'], ['Changes not allowed when PD not in amendment state.'])
def test_validation_hr_early_start(self): AppliedIndicatorFactory(is_high_frequency=True, lower_result=self.lower_result) data = { "report_type": ReportingRequirement.TYPE_HR, "reporting_requirements": [{ "start_date": datetime.date(2000, 1, 1), "due_date": datetime.date(2001, 4, 15), }] } serializer = InterventionReportingRequirementCreateSerializer( data=data, context=self.context) self.assertFalse(serializer.is_valid()) self.assertEqual(serializer.errors['reporting_requirements'], { "start_date": 'Start date needs to be on or after PD start date.' })
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' ] })