def setUp(self): super(IndicatorUpdateTests, self).setUp() self.result = ResultFactory(periodic_target=None, indicator=self.indicator, program=self.program, achieved=1024, date_collected='2018-06-01')
def setUp(self): self.program = ProgramFactory( reporting_period_start=datetime.date(2018, 1, 1), reporting_period_end=datetime.date(2019, 1, 1), ) self.indicator = IndicatorFactory( program=self.program, target_frequency=Indicator.MID_END, ) PeriodicTargetFactory( indicator=self.indicator, start_date=datetime.date(2018, 1, 1), end_date=datetime.date(2018, 12, 31), ) self.result = ResultFactory( indicator=self.indicator, date_collected=datetime.date(2017, 1, 1), achieved=42, record_name='My Test Record', evidence_url='http://my_evidence_url', ) self.count = 1 self.count += str(self.indicator.pk).count('42') * 2 self.count += str(self.indicator.name).count('42') self.count += str(self.result.pk).count('42') self.user = UserFactory(first_name="FN", last_name="LN", username="******", is_superuser=True) self.user.set_password('password') self.user.save() self.tola_user = TolaUserFactory(user=self.user) self.client.login(username='******', password='******')
def test_form_populates(self): """The form should populate several fields from the db""" request = RequestFactory().get('/') request.user = UserFactory() SectorFactory.create_batch(3) LevelFactory.create_batch(3) IndicatorTypeFactory.create_batch(3) program = ProgramFactory() indicator = IndicatorFactory(program=program) IndicatorFactory.create_batch(3) result = ResultFactory(indicator=indicator) ResultFactory.create_batch(3) expected = SiteProfileFactory() expected2 = SiteProfileFactory() result.site.add(expected2) result.site.add(expected) SiteProfileFactory.create_batch(3) period_choices_start = ((2018, ( ('2018-02-01', 'Quarter 4 (Feb 01, 2018 - Apr 30, 2018)'), ('2018-05-01', 'Quarter 5 (May 01, 2018 - Jul 31, 2018)'))), ) period_choices_end = ((2018, ( ('2018-04-30', 'Quarter 4 (Feb 01, 2018 - Apr 30, 2018)'), ('2018-07-31', 'Quarter 5 (May 01, 2018 - Jul 31, 2018)'))), ) kwargs = { 'program': program, 'request': request, 'initial': { 'period_choices_start': period_choices_start, 'period_choices_end': period_choices_end } } form = IPTTReportFilterForm(**kwargs) stuff = str(form) self.assertIn(expected.name, stuff) self.assertIn(expected2.name, stuff) self.assertIn(indicator.name, stuff)
class IndicatorUpdateTests(TestIndcatorCreateUpdateBase, TestCase): """ Test the update form API works, PTs are created, and that results are reassigned """ def setUp(self): super(IndicatorUpdateTests, self).setUp() self.result = ResultFactory(periodic_target=None, indicator=self.indicator, program=self.program, achieved=1024, date_collected='2018-06-01') def test_get(self): url = reverse_lazy('indicator_update', args=[self.indicator.id]) response = self.client.get(url) # self.assertContains(response, 'Indicator Performance Tracking Table') self.assertTemplateUsed(response, 'indicators/indicator_form_modal.html') def test_lop_update(self): data = self._base_indicator_post_data(Indicator.LOP, []) self.assertEqual(PeriodicTarget.objects.count(), 0) url = reverse_lazy('indicator_update', args=[self.indicator.id]) response = self.client.post(url, data) self.assertEqual(response.status_code, 200) self.assertEqual(PeriodicTarget.objects.count(), 1) self.result.refresh_from_db() self.assertEqual(self.result.periodic_target, PeriodicTarget.objects.get()) # Does updating a second time update the dummy PT? data['lop_target'] = 1024 url = reverse_lazy('indicator_update', args=[self.indicator.id]) response = self.client.post(url, data) indicator = Indicator.objects.get() pt = PeriodicTarget.objects.get() self.assertEqual(pt.indicator, indicator) self.assertEqual(pt.period_name, PeriodicTarget.LOP_PERIOD) self.assertEqual(pt.target, indicator.lop_target) def test_annual_update(self): periodic_targets = [{ "id": 0, "period": "Year 1", "target": "1", "start_date": "Jan 1, 2018", "end_date": "Dec 31, 2018" }, { "id": 0, "period": "Year 2", "target": "2", "start_date": "Jan 1, 2019", "end_date": "Dec 31, 2019" }, { "id": 0, "period": "Year 3", "target": "3", "start_date": "Jan 1, 2020", "end_date": "Dec 31, 2020" }] data = self._base_indicator_post_data(Indicator.ANNUAL, periodic_targets) self.assertEqual(PeriodicTarget.objects.count(), 0) url = reverse_lazy('indicator_update', args=[self.indicator.id]) response = self.client.post(url, data) self.assertEqual(response.status_code, 200) self.assertEqual(PeriodicTarget.objects.count(), 3) self.result.refresh_from_db() self.assertEqual(self.result.periodic_target, PeriodicTarget.objects.order_by('start_date').first()) def test_events_update(self): # update with 2 events periodic_targets = [{ "id": 0, "period": "a", "target": "1", "start_date": "", "end_date": "" }, { "id": 0, "period": "b", "target": "2" }] data = self._base_indicator_post_data(Indicator.EVENT, periodic_targets) self.assertEqual(PeriodicTarget.objects.count(), 0) url = reverse_lazy('indicator_update', args=[self.indicator.id]) response = self.client.post(url, data) self.assertEqual(response.status_code, 200) self.assertEqual(PeriodicTarget.objects.count(), 2) pt = PeriodicTarget.objects.order_by('customsort').first() pt2 = PeriodicTarget.objects.order_by('customsort').last() self.assertEqual(pt.period_name, 'a') self.assertEqual(pt.target, 1) # update again with only 1 event periodic_targets = [{ "id": pt.id, "period": "aaa", "target": "111", "start_date": "", "end_date": "" }, { "id": pt2.id, "period": "b", "target": "2" }] data = self._base_indicator_post_data(Indicator.EVENT, periodic_targets) self.assertEqual(PeriodicTarget.objects.count(), 2) url = reverse_lazy('indicator_update', args=[self.indicator.id]) response = self.client.post(url, data) self.assertEqual(response.status_code, 200) self.assertEqual(PeriodicTarget.objects.count(), 2) pt = PeriodicTarget.objects.order_by('customsort').first() self.assertEqual(pt.period_name, 'aaa') self.assertEqual(pt.target, 111) def test_annual_update_invalid_json(self): """What if client sends in pad periodic_targets JSON?""" periodic_targets = [ { "id": 0, "period": "Year 1", "target": "1", "start_date": "Jan 1, 2017", "end_date": "Dec 31, 2017" }, # wrong dates { "id": 0, "period": "Year 2", "target": "2", "start_date": "Jan 1, 2019", "end_date": "Dec 31, 2019" }, { "id": 0, "period": "Year 3", "target": "3", "start_date": "Jan 1, 2020", "end_date": "Dec 31, 2020" } ] data = self._base_indicator_post_data(Indicator.ANNUAL, periodic_targets) url = reverse_lazy('indicator_update', args=[self.indicator.id]) with self.assertRaises(PeriodicTargetJsonValidationError): self.client.post(url, data) periodic_targets = [ { "id": 0, "period": "Year 1", "target": "1", "start_date": "Jan 1, 2017", "end_date": "Dec 31, 2017" }, # too few pts ] data = self._base_indicator_post_data(Indicator.ANNUAL, periodic_targets) with self.assertRaises(PeriodicTargetJsonValidationError): self.client.post(url, data) periodic_targets = [ { "id": 0, "period": "Year 1", "target": "-1", "start_date": "Jan 1, 2017", "end_date": "Dec 31, 2017" }, # negative value ] data = self._base_indicator_post_data(Indicator.ANNUAL, periodic_targets) with self.assertRaises(PeriodicTargetJsonValidationError): self.client.post(url, data)