def setUp(self): self.user = UserFactory(first_name="PeterPeter", last_name="PumpkinEater", username="******") self.user.set_password('orangethumb') self.user.save() self.tola_user = TolaUserFactory(user=self.user) self.country = self.tola_user.country self.program = ProgramFactory( funding_status='Funded', reporting_period_start='2016-03-01', reporting_period_end='2020-05-01') self.program.country.add(self.country) self.program.save() # TolaUser not available on User if not logged in self.client.login(username=self.user.username, password='******')
def test_program_view(self): # Create 3 reports, 1 tied to a different user for i in range(2): PinnedReportFactory( tola_user=self.tola_user, program=self.program, ) other_user = UserFactory(first_name='Other', last_name='User', username='******') other_tola_user = TolaUserFactory(user=other_user) PinnedReportFactory( tola_user=other_tola_user, program=self.program, ) response = self.client.get(self.program.program_page_url) self.assertEqual(response.status_code, 200) pinned_reports = response.context['pinned_reports'] self.assertEquals(len(pinned_reports), 3) # 2 + 1 default # verify ordering - pinned reports should be sorted newest to oldest self.assertTrue( pinned_reports[0].creation_date > pinned_reports[1].creation_date)
def setUp(self): self.program = ProgramFactory() self.url = '/api/programtargetfrequencies/?program_id={pk}'.format( pk=self.program.pk) user = UserFactory() self.client = Client() self.client.force_login(user=user)
def setUp(self): self.user = UserFactory(first_name="PeterPeter", last_name="PumpkinEater", username="******") self.user.set_password('orangethumb') self.user.save() self.tola_user = TolaUserFactory(user=self.user) self.country = self.tola_user.country self.program = ProgramFactory(funding_status='Funded', reporting_period_start='2016-03-01', reporting_period_end='2020-05-01') self.program.country.add(self.country) self.program.save() self.indicator = IndicatorFactory( program=self.program, unit_of_measure_type=Indicator.NUMBER, is_cumulative=False, direction_of_change=Indicator.DIRECTION_OF_CHANGE_NONE, target_frequency=Indicator.ANNUAL) self.request_factory = RequestFactory() self.client = Client() self.client.login(username=self.user.username, password='******')
class TestIPTTReportviewURL(test.TestCase): def setUp(self): self.user = UserFactory(first_name="PeterPeter", last_name="PumpkinEater", username="******") self.user.set_password('orangethumb') self.user.save() self.tola_user = TolaUserFactory(user=self.user) self.country = self.tola_user.country self.program = ProgramFactory(funding_status='Funded', reporting_period_start='2016-03-01', reporting_period_end='2020-05-01') self.program.country.add(self.country) self.program.save() self.indicator = IndicatorFactory( program=self.program, unit_of_measure_type=Indicator.NUMBER, is_cumulative=False, direction_of_change=Indicator.DIRECTION_OF_CHANGE_NONE, target_frequency=Indicator.ANNUAL) self.request_factory = test.RequestFactory() self.client = test.Client() self.client.login(username=self.user.username, password='******') def test_get(self): """Does get return 200 and the right template?""" url_kwargs = { 'program': self.program.id, 'reporttype': 'targetperiods', } filterdata = {'frequency': 3, 'timeframe': 1} path = reverse_lazy('iptt_report', kwargs=url_kwargs) response = self.client.get(path, data=filterdata, follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, template_name=IPTTReport.template_name)
def setUp(self): self.mixin = IPTT_Mixin() self.user = UserFactory(first_name="Indy", last_name="Cater", username="******") self.user.set_password('password') self.user.save() self.tola_user = TolaUserFactory(user=self.user) self.country = self.tola_user.country self.program = ProgramFactory(funding_status='Funded', reporting_period_start='2016-04-01', reporting_period_end='2020-06-01') self.program.country.add(self.country) self.program.save() self.indicator = IndicatorFactory( program=self.program, unit_of_measure_type=Indicator.NUMBER, is_cumulative=False, direction_of_change=Indicator.DIRECTION_OF_CHANGE_NONE, target_frequency=Indicator.LOP) self.request_factory = RequestFactory() self.client = Client() self.client.login(username="******", password='******')
def test_delete_of_pinned_report_not_owned_by_user(self): other_user = UserFactory(first_name='Other', last_name='User', username='******') other_tola_user = TolaUserFactory(user=other_user) pr = PinnedReportFactory( tola_user=other_tola_user, program=self.program, ) self.assertEquals(models.PinnedReport.objects.count(), 1) data = { 'pinned_report_id': pr.id, } response = self.client.post(reverse('delete_pinned_report'), data=data) self.assertEqual(response.status_code, 200) # nothing deleted! self.assertEquals(models.PinnedReport.objects.count(), 1)
class IPTTReportQuickstartViewTests(TestCase): """Unit tests to valid the IPTTReportQuickStartView""" def setUp(self): self.user = UserFactory(first_name="Indicator", last_name="CreateTest", username="******") self.user.set_password('password') self.user.save() self.tola_user = TolaUserFactory(user=self.user) self.country = self.tola_user.country self.program = ProgramFactory(funding_status='Funded', reporting_period_start='2016-03-01', reporting_period_end='2020-05-01') self.program.country.add(self.country) self.program.save() self.indicator = IndicatorFactory( program=self.program, unit_of_measure_type=Indicator.NUMBER, is_cumulative=False, direction_of_change=Indicator.DIRECTION_OF_CHANGE_NONE, target_frequency=Indicator.ANNUAL) lop_indicator = IndicatorFactory(program=self.program, target_frequency=Indicator.LOP) self.request_factory = RequestFactory() self.client = Client() self.client.login(username="******", password='******') def test_page_load_returns_200(self): """Do we return 200?""" response = self.client.get(reverse_lazy('iptt_quickstart')) self.assertEqual(response.status_code, 200) def test_page_load_does_not_redirect(self): """This page should not redirect""" response = self.client.get(reverse_lazy('iptt_quickstart'), follow=True) self.assertEqual(len(response.redirect_chain), 0) def test_page_loads_correct_template(self): """Do we load the right template?""" response = self.client.get(reverse_lazy('iptt_quickstart'), follow=True) self.assertTemplateUsed(response, 'indicators/iptt_quickstart.html') self.assertContains(response, 'Indicator Performance Tracking Table') def test_get_form_kwargs(self): """Do we get the correct form kwargs?""" data = { 'csrfmiddlewaretoken': 'lolwut', 'targetperiods-program': self.program.id, 'targetperiods-formprefix': IPTTReportQuickstartView.FORM_PREFIX_TARGET, 'targetperiods-timeframe': Indicator.LOP, 'targetperiods-targetperiods': 1, 'targetperiods-numrecentperiods': 1, } path = reverse_lazy('iptt_quickstart') response = self.client.post(path, data=data, follow=True) kwargs = response.resolver_match.kwargs self.assertEqual(kwargs['reporttype'], IPTTReportQuickstartView.FORM_PREFIX_TARGET) self.assertEqual(int(kwargs['program_id']), self.program.id) def test_get_context_data(self): """Do we get the correct context data?""" data = { 'csrfmiddlewaretoken': 'lolwut', 'targetperiods-program': self.program.id, 'targetperiods-formprefix': IPTTReportQuickstartView.FORM_PREFIX_TARGET, 'targetperiods-timeframe': Indicator.LOP, 'targetperiods-targetperiods': 1, 'targetperiods-numrecentperiods': 1, } path = reverse_lazy('iptt_quickstart') response = self.client.post(path, data=data, follow=True) context_data = response.context_data self.assertEqual(int(context_data['program_id']), self.program.id) # self.assertEqual(context['report_wide'], ?) # self.assertEqual(context['report_date_ranges'], ?) # self.assertEqual(context['indicators'], ?) self.assertRegex(str(context_data['program']), self.program.name) self.assertEqual(str(context_data['reporttype']), IPTTReportQuickstartView.FORM_PREFIX_TARGET) self.assertEqual(str(context_data['report_end_date']), self.program.reporting_period_end) self.assertEqual(str(context_data['report_end_date_actual']), self.program.reporting_period_end) self.assertEqual(str(context_data['report_start_date']), self.program.reporting_period_start) def test_post_with_valid_form(self): """Does POSTing to iptt_quickstart with valid form data redirect to the correct view (iptt_report)?""" data = { 'csrfmiddlewaretoken': 'lolwut', 'targetperiods-program': self.program.id, 'targetperiods-formprefix': IPTTReportQuickstartView.FORM_PREFIX_TARGET, 'targetperiods-timeframe': Indicator.LOP, 'targetperiods-targetperiods': 1, 'targetperiods-numrecentperiods': 1, } path = reverse_lazy('iptt_quickstart') response = self.client.post(path, data=data, follow=True) self.assertEqual(len(response.redirect_chain), 1) self.assertTemplateUsed(response, 'indicators/iptt_report.html') self.assertEqual(response.status_code, 200) def test_post_with_invalid_form(self): """Does POSTing to iptt_quickstart with crap form data leave us at iptt_quickstart?""" path = reverse_lazy('iptt_quickstart') response = self.client.post(path, data={'foo': 'bar'}) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'indicators/iptt_quickstart.html')
class IPTT_ReportViewTests(TestCase): """Unit tests to validate IPTT_ReportView""" def setUp(self): self.user = UserFactory(first_name="PeterPeter", last_name="PumpkinEater", username="******") self.user.set_password('orangethumb') self.user.save() self.tola_user = TolaUserFactory(user=self.user) self.country = self.tola_user.country self.program = ProgramFactory(funding_status='Funded', reporting_period_start='2016-03-01', reporting_period_end='2020-05-01') self.program.country.add(self.country) self.program.save() self.indicator = IndicatorFactory( program=self.program, unit_of_measure_type=Indicator.NUMBER, is_cumulative=False, direction_of_change=Indicator.DIRECTION_OF_CHANGE_NONE, target_frequency=Indicator.ANNUAL) self.request_factory = RequestFactory() self.client = Client() self.client.login(username=self.user.username, password='******') def test_get(self): """Does get return 200 and the right template?""" url_kwargs = { 'program_id': self.program.id, 'reporttype': 'targetperiods', } filterdata = {'targetperiods': 1, 'timeframe': 1} path = reverse_lazy('iptt_report', kwargs=url_kwargs) response = self.client.get(path, data=filterdata, follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, template_name=IPTT_ReportView.template_name) # Verify that real program and indicator data are present self.assertIn(self.program.name, response.content) self.assertIn(self.indicator.name, response.content) # Dates come out the database as '2016-03-01' # Those dates are formatted as 'Mar 01, 2016' in the content expected_start = datetime.datetime.strptime( self.program.reporting_period_start, '%Y-%m-%d') expected_end = datetime.datetime.strptime( self.program.reporting_period_end, '%Y-%m-%d') # TODO: fails because l10n: self.assertIn(expected_start.strftime('%b %d, %Y'), response.content) # TODO: fails because l10n: self.assertIn(expected_end.strftime('%b %d, %Y'), response.content) def test_post(self): """Does post return 200 show the requested report?""" url_kwargs = { 'program_id': self.program.id, 'reporttype': 'targetperiods', } data = { 'csrfmiddlewaretoken': 'lolwut', 'program': self.program.id, 'targetperiods': 1, 'timeframe': 1, } path = reverse_lazy('iptt_report', kwargs=url_kwargs) response = self.client.post(path, data=data, follow=True) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.redirect_chain), 1) # Verify that real program and indicator data are present self.assertIn(self.program.name, response.content) self.assertIn(self.indicator.name, response.content) # Dates returned as '2016-03-01' # Present in content as 'Mar 01, 2016' exp_start = datetime.datetime.strptime( self.program.reporting_period_start, '%Y-%m-%d') exp_end = datetime.datetime.strptime(self.program.reporting_period_end, '%Y-%m-%d')
class IPTT_MixinTests(TestCase): """Test private methods not specifically tested in other test cases""" freqs = { Indicator.ANNUAL: 12, Indicator.SEMI_ANNUAL: IPTT_Mixin.MONTHS_PER_SEMIANNUAL, Indicator.TRI_ANNUAL: IPTT_Mixin.MONTHS_PER_TRIANNUAL, Indicator.QUARTERLY: IPTT_Mixin.MONTHS_PER_QUARTER, Indicator.MONTHLY: IPTT_Mixin.MONTHS_PER_MONTH, # Indicator.LOP, Indicator.MID_END, Indicator.EVENT } def setUp(self): self.mixin = IPTT_Mixin() self.user = UserFactory(first_name="Indy", last_name="Cater", username="******") self.user.set_password('password') self.user.save() self.tola_user = TolaUserFactory(user=self.user) self.country = self.tola_user.country self.program = ProgramFactory(funding_status='Funded', reporting_period_start='2016-04-01', reporting_period_end='2020-06-01') self.program.country.add(self.country) self.program.save() self.indicator = IndicatorFactory( program=self.program, unit_of_measure_type=Indicator.NUMBER, is_cumulative=False, direction_of_change=Indicator.DIRECTION_OF_CHANGE_NONE, target_frequency=Indicator.LOP) self.request_factory = RequestFactory() self.client = Client() self.client.login(username="******", password='******') def test__get_num_months(self): """Do we return the right number of months per period?""" for freq in IPTT_MixinTests.freqs: num_months_in_period = self.mixin._get_num_months(freq) self.assertEqual(num_months_in_period, IPTT_MixinTests.freqs[freq]) def test__get_num_periods_returns_0_for_reversed_date_range(self): """Do we return if end date is before start date?""" _get_num_periods = IPTT_Mixin._get_num_periods start_date = date(2020, 1, 1) end_date = date(2019, 1, 1) self.assertEqual( _get_num_periods(start_date, end_date, Indicator.ANNUAL), 0) self.assertEqual( _get_num_periods(start_date, end_date, Indicator.SEMI_ANNUAL), 0) self.assertEqual( _get_num_periods(start_date, end_date, Indicator.TRI_ANNUAL), 0) self.assertEqual( _get_num_periods(start_date, end_date, Indicator.QUARTERLY), 0) self.assertEqual( _get_num_periods(start_date, end_date, Indicator.MONTHLY), 0) def test__get_period_name(self): """Do we return the correct period names?""" _get_period_name = IPTT_Mixin._get_period_name self.assertEqual(_get_period_name(Indicator.ANNUAL), "Year") self.assertEqual(_get_period_name(Indicator.SEMI_ANNUAL), "Semi-annual") self.assertEqual(_get_period_name(Indicator.TRI_ANNUAL), "Tri-annual") self.assertEqual(_get_period_name(Indicator.QUARTERLY), "Quarter") self.assertEqual(_get_period_name(Indicator.MONTHLY), "Month") def test__get_first_period(self): """Do we calculate the first period of a date range correctly?""" real_start_date = date(2016, 7, 15) for freq in IPTT_MixinTests.freqs: num_months = self.mixin._get_num_months(freq) _get_first_period = self.mixin._get_first_period( real_start_date, num_months) if freq == Indicator.ANNUAL: self.assertEqual(_get_first_period, date(2016, 1, 1)) elif freq == Indicator.SEMI_ANNUAL: self.assertEqual(_get_first_period, date(2016, 7, 1)) elif freq == Indicator.TRI_ANNUAL: self.assertEqual(_get_first_period, date(2016, 5, 1)) elif freq == Indicator.QUARTERLY: self.assertEqual(_get_first_period, date(2016, 7, 1)) elif freq == Indicator.MONTHLY: self.assertEqual(_get_first_period, date(2016, 7, 1)) else: self.fail('Unexpected target frequency' + freq) def test__generate_annotations(self): """Do we generate queryset annotations correctly?""" reporttype = 'timeperiods' filter_start_date = date(2018, 1, 1) filter_end_date = date(2019, 12, 31) num_recents = 0 show_all = True self.mixin.program = Program() self.mixin.program.reporting_period_start = filter_start_date self.mixin.program.reporting_period_end = filter_end_date freqs = (Indicator.LOP, Indicator.MID_END, Indicator.EVENT, Indicator.ANNUAL, Indicator.SEMI_ANNUAL, Indicator.TRI_ANNUAL, Indicator.QUARTERLY, Indicator.MONTHLY) for freq in freqs: (report_end_date, all_date_ranges, periods_date_ranges) = self.mixin._generate_targetperiods( Indicator.MONTHLY) self.assertEqual(self.mixin.program.reporting_period_end, report_end_date) self.assertEqual(self.mixin.program.reporting_period_end, filter_end_date) annotations = self.mixin._generate_annotations( periods_date_ranges, freq, reporttype) if freq == Indicator.LOP: self.assertEqual(annotations, {}) else: self.assertNotEqual(annotations, {}) def test__get_num_periods(self): """Do we return the correct number of periods""" _get_num_periods = IPTT_Mixin._get_num_periods start_date = date(2016, 1, 15) end_date = date(2017, 12, 16) self.assertEqual( _get_num_periods(start_date, end_date, Indicator.ANNUAL), 2) self.assertEqual( _get_num_periods(start_date, end_date, Indicator.SEMI_ANNUAL), 4) self.assertEqual( _get_num_periods(start_date, end_date, Indicator.TRI_ANNUAL), 6) self.assertEqual( _get_num_periods(start_date, end_date, Indicator.QUARTERLY), 8) self.assertEqual( _get_num_periods(start_date, end_date, Indicator.MONTHLY), 24) @unittest.skip('outdated test') def test__generate_targetperiods(self): """Can we generate target periods correctly""" freqs = (Indicator.LOP, Indicator.MID_END, Indicator.EVENT, Indicator.ANNUAL, Indicator.SEMI_ANNUAL, Indicator.TRI_ANNUAL, Indicator.QUARTERLY, Indicator.MONTHLY) self.mixin.filter_form_initial_data = { 'timeframe': 1, 'numrecentperiods': 0, 'period_start': '2018-01-01', 'period_end': '2019-12-31' } filter_start_date = date(2018, 1, 1) filter_end_date = date(2019, 12, 31) num_recents = 0 show_all = True self.mixin.program = Program() self.mixin.program.reporting_period_start = filter_start_date self.mixin.program.reporting_period_end = filter_end_date for freq in freqs: report_end_date, all_date_ranges, targetperiods = self.mixin._generate_targetperiods( freq) self.assertEqual(filter_end_date, report_end_date) self.assertEqual(len(all_date_ranges), 0) self.assertEqual(len(targetperiods), 0) @unittest.skip('outdated test') def test__generate_timeperiods(self): """Can we generate time periods correctly?""" freqs = (Indicator.LOP, Indicator.MID_END, Indicator.EVENT, Indicator.ANNUAL, Indicator.SEMI_ANNUAL, Indicator.TRI_ANNUAL, Indicator.QUARTERLY, Indicator.MONTHLY) filter_start_date = date(2018, 1, 1) filter_end_date = date(2019, 12, 31) num_recents = 0 self.mixin.filter_form_initial_data = { 'timeframe': 1, 'numrecentperiods': num_recents, 'period_start': '2018-01-01', 'period_end': '2019-12-31' } self.mixin.program = Program() self.mixin.program.reporting_period_start = filter_start_date self.mixin.program.reporting_period_end = filter_end_date for freq in freqs: report_end_date, all_date_ranges, timeperiods = self.mixin._generate_targetperiods( Indicator.MONTHLY) self.assertEqual(report_end_date, filter_end_date, 'End dates don\'t match') if freq == Indicator.LOP or freq == Indicator.MID_END or freq == Indicator.EVENT: self.assertEqual(len(all_date_ranges), 0) elif freq == Indicator.ANNUAL: self.assertEqual( len(all_date_ranges), 2, 'Unexpected number of date ranges for {0}: {1}'.format( freq, len(all_date_ranges))) self.assertEqual( len(timeperiods), 2, 'Unexpected number of timeperiods for {0}: {1}'.format( freq, len(timeperiods))) elif freq == Indicator.SEMI_ANNUAL: self.assertEqual( len(all_date_ranges), 4, 'Unexpected number of date ranges for {0}: {1}'.format( freq, len(all_date_ranges))) self.assertEqual( len(timeperiods), 4, 'Unexpected number of timeperiods for {0}: {1}'.format( freq, len(timeperiods))) elif freq == Indicator.TRI_ANNUAL: self.assertEqual( len(all_date_ranges), 6, 'Unexpected number of date ranges for {0}: {1}'.format( freq, len(all_date_ranges))) self.assertEqual( len(timeperiods), 6, 'Unexpected number of timeperiods for {0}: {1}'.format( freq, len(timeperiods))) elif freq == Indicator.QUARTERLY: self.assertEqual( len(all_date_ranges), 8, 'Unexpected number of date ranges for {0}: {1}'.format( freq, len(all_date_ranges))) self.assertEqual( len(timeperiods), 8, 'Unexpected number of timeperiods for {0}: {1}'.format( freq, len(timeperiods))) elif freq == Indicator.MONTHLY: self.assertEqual( len(all_date_ranges), 24, 'Unexpected number of date ranges for {0}: {1}'.format( freq, len(all_date_ranges))) self.assertEqual( len(timeperiods), 24, 'Unexpected number of timeperiods for {0}: {1}'.format( freq, len(timeperiods))) def test__update_filter_form_initial(self): """Do we populate the initial filter form properly?""" data = { 'csrfmiddlewaretoken': 'lolwut', 'program': self.program.id, 'formprefix': IPTTReportQuickstartView.FORM_PREFIX_TARGET, 'timeframe': Indicator.LOP, 'targetperiods': 1, 'numrecentperiods': 1, } query_string = urllib.urlencode(data) formdata = QueryDict(query_string=query_string, mutable=True) self.mixin._update_filter_form_initial(formdata=formdata) filter_form_initial_data = self.mixin.filter_form_initial_data self.assertEqual(len(filter_form_initial_data), 4) self.assertNotIn('csrfmiddlewaretokeen', filter_form_initial_data) self.assertNotIn('program', filter_form_initial_data) # Dicts should have the same key/value pairs; the method first # strips off program and csrfmiddlewaretoken, so do that, too. del (data['csrfmiddlewaretoken']) del (data['program']) for k in data.keys(): self.assertIn(k, formdata) # Coercing both to str because the data arg is an int # and the formdata arg is a unicode value self.assertEqual(str(data[k]), str(formdata[k])) def test__get_filters_with_no_periods(self): data = { 'level': 3, 'sector': 'Conflict Management', # TODO: Load fixtures for level, indicators 'ind_type': 'Custom', 'site': self.program.country.name, 'indicators': self.indicator.id, } filters = self.mixin._get_filters(data) # Assert things about the returned filters self.assertEqual(len(filters), len(data)) self.assertIn(data['level'], filters['level__in']) self.assertIn(data['sector'], filters['sector__in']) self.assertIn(data['ind_type'], filters['indicator_type__in']) self.assertIn(data['site'], filters['result__site__in']) self.assertIn(data['indicators'], filters['id__in']) self.assertEqual(data['level'], *filters['level__in']) self.assertEqual(data['sector'], *filters['sector__in']) self.assertEqual(data['ind_type'], *filters['indicator_type__in']) self.assertEqual(data['site'], *filters['result__site__in']) self.assertEqual(data['indicators'], *filters['id__in']) # TODO: Is it possible to make assertions about the filtered report # TODO: without a GET or POST? def test_prepare_indicators(self): self.skipTest('TODO: Test not implemented') def test_prepare_iptt_period_dateranges(self): self.skipTest('TODO: Test not implemented') # TODO: Mock the super call that invokes a non-existent get_context_data # call on IPTT_Mixin. def test_get_context_data(self): '''Does get_context_data return existing data untouched and without inserting new data?''' self.skipTest('TODO: Test not implemented')
def setUp(self): self.client = Client() self.program = ProgramFactory() self.user = UserFactory()