def test_one_program(self): p = RFProgramFactory() RFIndicatorFactory(program=p, target_frequency=Indicator.LOP, targets=500) RFIndicatorFactory(program=p, target_frequency=Indicator.ANNUAL, targets=200) with self.assertNumQueries(2): data = self.get_serialized_data(p.pk)[0] self.assertEqual(data['frequencies'], [1, 3])
def test_rf_program_two_indicators_update_only(self): p = RFProgramFactory( tiers=['Tier1', 'Tier2'], levels=1, ) expected_pks = [] levels = sorted(p.levels.all(), key=operator.attrgetter('level_depth')) for level in levels: expected_pks.append(RFIndicatorFactory(program=p, level=level).pk) indicator = RFIndicatorFactory(program=p, level=levels[1], name='Test Name') expected_pks.append(indicator.pk) full_data = ProgramPageProgramSerializer.get_for_pk(p.pk).data with self.assertNumQueries(5): data = ProgramPageUpdateSerializer.update_indicator_pk(p.pk, indicator.pk).data self.assertEqual(len(data), 5) self.assertEqual(data['pk'], p.pk) self.assertEqual(data['indicator_pks_level_order'], data['indicator_pks_chain_order']) indicator_pks = data['indicator_pks_level_order'] self.assertEqual(indicator_pks, expected_pks) self.assertEqual(data['indicator']['name'], 'Test Name') for indicator_pk in full_data['indicators']: self.assertIn(indicator_pk, data['indicators']) self.assertEqual( full_data['indicators'][indicator_pk]['number'], data['indicators'][indicator_pk]['number'] )
def test_one_duplicated_sector(self): program = RFProgramFactory() sector = SectorFactory(program=program) RFIndicatorFactory(program=program, sector=sector) RFIndicatorFactory(program=program, sector=sector) data = get_serialized_data(program.pk) self.assertEqual(len(data['sectors']), 1)
def test_migrated_has_level_number_lower_tier(self): p = RFProgramFactory(tiers=['tier1', 'tier2', 'tier3'], levels=2) l1 = [l for l in p.levels.all() if l.level_depth == 2][0] level = p.levels.filter(parent=l1, customsort=2).first() RFIndicatorFactory(program=p, level=level, level_order=0) RFIndicatorFactory(program=p, level=level, level_order=1) data = self.get_indicator_data(program=p, level=level, level_order=2) self.assertEqual(data['number'], "tier3 1.2c")
def test_one_duplicated_indicator_type(self): program = RFProgramFactory() i_type = IndicatorTypeFactory() indicator = RFIndicatorFactory(program=program) indicator.indicator_type.add(i_type) indicator2 = RFIndicatorFactory(program=program) indicator2.indicator_type.add(i_type) data = get_serialized_data(program.pk) self.assertEqual(len(data['indicator_types']), 1)
def test_multiple_sectors(self): program = RFProgramFactory() sector = SectorFactory(program=program) sector2 = SectorFactory(program=program) RFIndicatorFactory(program=program, sector=sector) RFIndicatorFactory(program=program, sector=sector2) data = get_serialized_data(program.pk) self.assertEqual(len(data['sectors']), 2) self.assertEqual(set(sd['pk'] for sd in data['sectors']), set([sector.pk, sector2.pk]))
def test_frequencies(self): p1 = RFProgramFactory() RFIndicatorFactory(program=p1, target_frequency=Indicator.LOP, targets=200) p2 = RFProgramFactory() RFIndicatorFactory(program=p2, target_frequency=Indicator.ANNUAL, targets=1000) data = self.get_serialized_data([p1.pk, p2.pk]) self.assertEqual(len(data), 2) data1 = [d for d in data if d['pk'] == p1.pk][0] self.assertEqual(data1['frequencies'], [1]) data2 = [d for d in data if d['pk'] == p2.pk][0] self.assertEqual(data2['frequencies'], [3])
def test_program_three_levels_with_unassigned_indicators(self): program = RFProgramFactory(tiers=['Tier1', 'Tier2]']) goal = LevelFactory(program=program, parent=None) output = LevelFactory(program=program, parent=goal) output_indicator = RFIndicatorFactory(program=program, level=output) impact = LevelFactory(program=program, parent=output) impact_indicator1 = RFIndicatorFactory(program=program, level=impact, number='21', old_level='Activity') impact_indicator2 = RFIndicatorFactory(program=program, level=impact, number='11', old_level='Goal') for old_level, number, pk in [ ('Activity', '2.1', 106), ('Impact', '1.4', 102), ('Impact', '1.2', 101), ('Output', '1.3', 103), ('Goal', '1', 104), ('Activity', '1.1.5', 105), ]: RFIndicatorFactory(program=program, level=None, old_level=old_level, number=number, pk=pk) data = get_serialized_data(program.pk) self.assertEqual(data['level_pks_level_order'], [goal.pk, output.pk, impact.pk]) self.assertEqual(data['level_pks_chain_order'], [goal.pk, output.pk, impact.pk]) self.assertEqual( sorted(data['indicator_pks_for_level'], key=lambda l: l['pk']), sorted([ { 'pk': goal.pk, 'indicator_pks': [] }, { 'pk': output.pk, 'indicator_pks': [output_indicator.pk] }, { 'pk': impact.pk, 'indicator_pks': [impact_indicator1.pk, impact_indicator2.pk] }, ], key=lambda l: l['pk'])) self.assertEqual(data['unassigned_indicator_pks'], [104, 101, 102, 103, 105, 106])
def test_program_migrated_unassigned_indicators(self): p = RFProgramFactory(tiers=["Tier1", "Tier2", "Tier3"]) l1 = LevelFactory(program=p, parent=None, customsort=0) LevelFactory(program=p, parent=l1, customsort=0) i1 = RFIndicatorFactory(program=p, level=None) i2 = RFIndicatorFactory(program=p, level=None) data = ProgramLevelOrderingProgramSerializer( Program.rf_aware_objects.filter(pk=p.pk), many=True ).data[0] self.assertIn(i1.pk, data['indicator_pks_level_order']) self.assertIn(i2.pk, data['indicator_pks_level_order']) self.assertIn(i1.pk, data['indicator_pks_chain_order']) self.assertIn(i2.pk, data['indicator_pks_chain_order'])
def test_multiple_indicator_types(self): program = RFProgramFactory() i_type = IndicatorTypeFactory() i_type1 = IndicatorTypeFactory() i_type2 = IndicatorTypeFactory() i_1 = RFIndicatorFactory(program=program) i_1.indicator_type.add(i_type) i_1.indicator_type.add(i_type1) i_2 = RFIndicatorFactory(program=program) i_2.indicator_type.add(i_type2) data = get_serialized_data(program.pk) self.assertEqual(len(data['indicator_types']), 3) self.assertEqual(set(itd['pk'] for itd in data['indicator_types']), set([i_type.pk, i_type1.pk, i_type2.pk]))
def test_multiple_programs(self): p = RFProgramFactory(migrated=False) RFIndicatorFactory(program=p, target_frequency=Indicator.LOP, targets=500) RFIndicatorFactory(program=p, target_frequency=Indicator.ANNUAL, targets=200) p2 = RFProgramFactory(migrated=True, tiers=True, levels=1) RFIndicatorFactory(program=p2, target_frequency=Indicator.SEMI_ANNUAL, targets=5000) RFIndicatorFactory(program=p2, target_frequency=Indicator.ANNUAL, targets=200) p3 = RFProgramFactory(tiers=['Tier1', 'Tier2'], levels=2) for level in p3.levels.all(): RFIndicatorFactory(program=p3, target_frequency=Indicator.MONTHLY, targets=5000, level=level) with self.assertNumQueries(2): data = {d['pk']: d for d in self.get_serialized_data(p.pk, p2.pk, p3.pk)} self.assertEqual(data[p.pk]['frequencies'], [1, 3]) self.assertEqual(data[p2.pk]['frequencies'], [3, 4]) self.assertEqual(data[p3.pk]['frequencies'], [7])
def test_rf_program_two_indicators(self): p = RFProgramFactory( tiers=['Tier1', 'Tier2'], levels=1, reporting_period_start=datetime.date(2014, 5, 1) ) for level in p.levels.all(): RFIndicatorFactory(program=p, level=level) with self.assertNumQueries(4): data = ProgramPageProgramSerializer.get_for_pk(p.pk).data self.assertEqual(data['pk'], p.pk) self.assertEqual(data['name'], p.name) self.assertTrue(data['results_framework']) self.assertEqual(data['by_result_chain'], 'by Tier2 chain') self.assertEqual(data['reporting_period_start_iso'], '2014-05-01') self.assertEqual(data['site_count'], 0) self.assertTrue(data['has_levels']) self.assertEqual(data['indicator_pks_level_order'], data['indicator_pks_chain_order']) indicator_pks = data['indicator_pks_level_order'] self.assertEqual(len(indicator_pks), 2) indicators_data = data['indicators'] self.assertEqual(len(indicators_data), 2) goal_indicator_data = indicators_data[indicator_pks[0]] self.assertEqual(goal_indicator_data['number'], 'Tier1 a') output_indicator_data = indicators_data[indicator_pks[1]] self.assertEqual(output_indicator_data['number'], 'Tier2 1a')
def test_manual_numbering(self): p = RFProgramFactory(tiers=['Tier1', 'Tier2', 'Tier3'], levels=1, auto_number_indicators=False) levels = list( sorted( sorted( [l for l in p.levels.all()], key=lambda l: l.customsort ), key=lambda l: l.level_depth ) ) numbers = ['482', '28.C', '999'] pks = [] for level, number in zip(list(reversed(levels)), numbers): pks.append( RFIndicatorFactory( program=p, level=level, number=number ).pk ) expected_pks = list(reversed(pks)) expected_numbers = list(reversed(numbers)) with self.assertNumQueries(4): data = ProgramPageProgramSerializer.get_for_pk(p.pk).data self.assertEqual(len(data['indicators']), 3) self.assertEqual(data['indicator_pks_level_order'], expected_pks) self.assertEqual(data['indicator_pks_chain_order'], expected_pks) for pk, number in zip(expected_pks, expected_numbers): self.assertEqual(data['indicators'][pk]['number'], number)
def test_program_unmigrated_indicators_old_level_order(self): p = RFProgramFactory(migrated=False) i1 = RFIndicatorFactory(program=p, old_level='Activity') i2 = RFIndicatorFactory(program=p, old_level='Outcome') i3 = RFIndicatorFactory(program=p, old_level='Output') data = ProgramLevelOrderingProgramSerializer( Program.rf_aware_objects.filter(pk=p.pk), many=True ).data[0] self.assertEqual( data['indicator_pks_level_order'], [i2.pk, i3.pk, i1.pk] ) self.assertEqual( data['indicator_pks_chain_order'], [i2.pk, i3.pk, i1.pk] )
def test_program_unmigrated_indicators_old_level_new_level_and_number_order(self): translation.activate('fr') p = RFProgramFactory(migrated=False) l1 = LevelFactory(program=p, parent=None, customsort=0) l2 = LevelFactory(program=p, parent=l1, customsort=0) indicators = [ RFIndicatorFactory(program=p, old_level=old_level, number=number, level=level) for old_level, number, level in reversed([ ('Goal', '2', None), ('Outcome', '1', None), ('Outcome', '1.2', l1), ('Outcome', '1.3', None), ('Output', '1.1.1', l2), ('Output', '1.1.2', None), ('Activity', '1.1.2.1', l1) ]) ] indicator_pks = [i.pk for i in indicators] data = ProgramLevelOrderingProgramSerializer( Program.rf_aware_objects.filter(pk=p.pk), many=True ).data[0] self.assertEqual( data['indicator_pks_level_order'], list(reversed(indicator_pks)) ) self.assertEqual( data['indicator_pks_chain_order'], list(reversed(indicator_pks)) ) translation.activate('en')
def test_unmigrated_program(self): p = RFProgramFactory(migrated=False) pks = [] old_levels_numbers = [('Activity', '5'), ('Activity', '3'), ('Outcome', '3.4'), ('Outcome', '2.8'), ('Outcome', '2.2')] for old_level, number in old_levels_numbers: pks.append( RFIndicatorFactory(program=p, old_level=old_level, number=number, target_frequency=Indicator.SEMI_ANNUAL, targets=400, results=550).pk) expected_pks = list(reversed(pks)) with self.assertNumQueries(IPTT_QUERY_COUNT): data = get_serialized_data(p.pk) self.assertEqual(expected_pks, data['unassigned_indicator_pks']) self.assertEqual(data['level_pks_level_order'], []) self.assertEqual(data['level_pks_chain_order'], []) self.assertEqual(data['indicator_pks_for_level'], []) self.assertEqual(data['frequencies'], [Indicator.SEMI_ANNUAL]) activity = [ i_data for i_data in data['indicators'] if i_data['number'] == '5' ][0] self.assertEqual(activity['old_level_name'], 'Activity') translation.activate('fr') with self.assertNumQueries(IPTT_QUERY_COUNT): french_data = get_serialized_data(p.pk) activity = [ i_data for i_data in french_data['indicators'] if i_data['number'] == '5' ][0] self.assertEqual(activity['old_level_name'], u'Activité') translation.activate('en')
def indicators(self, create, extracted, **kwargs): if extracted: from factories.indicators_models import RFIndicatorFactory if kwargs and kwargs.get('levels'): if not self.results_framework: levels = itertools.cycle([ ('old_level', level_name) for (pk, level_name ) in RFIndicatorFactory._meta.model.OLD_LEVELS ]) else: levels = itertools.cycle([ ('level', level) for level in self.levels.all().order_by('pk') ]) else: levels = False indicator_data = kwargs.get('all', {}) indicator_data.update({'program': self}) for count in range(extracted): this_indicator_data = {} this_indicator_data.update(indicator_data) this_indicator_data.update(kwargs.get(str(count), {})) if levels: (level_field, level_value) = next(levels) this_indicator_data.update({level_field: level_value}) RFIndicatorFactory(**this_indicator_data)
def test_needs_additional_target_periods_false(self): p = RFProgramFactory() RFIndicatorFactory(program=p, target_frequency=Indicator.TRI_ANNUAL, targets=500) data = ProgramPageProgramSerializer( Program.program_page_objects.filter(pk=p.pk), many=True ).data[0] self.assertFalse(data['needs_additional_target_periods'])
def test_needs_additional_target_periods_no_targets(self): p = RFProgramFactory() RFIndicatorFactory(program=p) data = ProgramPageProgramSerializer( Program.program_page_objects.filter(pk=p.pk), many=True ).data[0] self.assertFalse(data['needs_additional_target_periods'])
def test_has_two_type_pks(self): it1 = IndicatorTypeFactory() it2 = IndicatorTypeFactory() i = RFIndicatorFactory(program=RFProgramFactory()) i.indicator_type.add(it1) i.indicator_type.add(it2) data = get_serialized_data(i.pk) self.assertEqual(data['indicator_type_pks'], sorted([it1.pk, it2.pk]))
def test_period_date_ranges(self): p = RFProgramFactory(closed=False, months=12, age=4) # 4+months ago RFIndicatorFactory(program=p, target_frequency=Indicator.LOP, targets=200) data = self.get_serialized_data([p.pk])[0] self.assertEqual(data['pk'], p.pk) for frequency, count in [(1, 1), (2, 2), (3, 1), (4, 2), (5, 3), (6, 4), (7, 12)]: self.assertEqual(len(data['period_date_ranges'][frequency]), count) self.assertEqual(len([f for f in data['period_date_ranges'][7] if f['past']]), 5)
def test_has_one_site_pk(self): i = RFIndicatorFactory( program=RFProgramFactory(), target_frequency=Indicator.LOP, targets=1000, results=True) site = SiteProfileFactory() i.result_set.first().site.add(site) data = get_serialized_data(i.pk) self.assertEqual(data['site_pks'], [site.pk])
def test_one_sector(self): program = RFProgramFactory() sector = SectorFactory(program=program) RFIndicatorFactory(program=program, sector=sector) data = get_serialized_data(program.pk) self.assertEqual(len(data['sectors']), 1) self.assertEqual(data['sectors'][0]['pk'], sector.pk) self.assertEqual(data['sectors'][0]['name'], sector.sector)
def test_program_migrated_one_indicator_per_level(self): p = RFProgramFactory(tiers=["Tier1", "Tier2", "Tier3"]) l1 = LevelFactory(program=p, parent=None, customsort=0) l11 = LevelFactory(program=p, parent=l1, customsort=0) l111 = LevelFactory(program=p, parent=l11, customsort=0) l112 = LevelFactory(program=p, parent=l11, customsort=1) l12 = LevelFactory(program=p, parent=l1, customsort=1) l121 = LevelFactory(program=p, parent=l12, customsort=0) l122 = LevelFactory(program=p, parent=l12, customsort=1) i1 = RFIndicatorFactory(program=p, level=l1, level_order=0) i11 = RFIndicatorFactory(program=p, level=l11, level_order=0) i111 = RFIndicatorFactory(program=p, level=l111, level_order=0) i112 = RFIndicatorFactory(program=p, level=l112, level_order=0) i12 = RFIndicatorFactory(program=p, level=l12, level_order=0) i121 = RFIndicatorFactory(program=p, level=l121, level_order=0) i122 = RFIndicatorFactory(program=p, level=l122, level_order=0) data = ProgramLevelOrderingProgramSerializer( Program.rf_aware_objects.filter(pk=p.pk), many=True ).data[0] self.assertEqual( data['indicator_pks_level_order'], [i1.pk, i11.pk, i12.pk, i111.pk, i112.pk, i121.pk, i122.pk] ) self.assertEqual( data['indicator_pks_chain_order'], [i1.pk, i11.pk, i111.pk, i112.pk, i12.pk, i121.pk, i122.pk] )
def test_migrated_has_level_number(self): p = RFProgramFactory(tiers=['tier1', 'tier2'], levels=True) l = [l for l in p.levels.all() if l.level_depth == 2][0] data = self.get_indicator_data(program=p, level=l, level_order=0) self.assertEqual(data['number'], "tier2 1a") l2 = [l for l in p.levels.all() if l.level_depth == 1][0] RFIndicatorFactory(program=p, level=l2, level_order=0) data2 = self.get_indicator_data(program=p, level=l2, level_order=1) self.assertEqual(data2['number'], "tier1 b")
def test_program_migrated_unassigned_indicators_old_level_order(self): p = RFProgramFactory(tiers=["Tier1", "Tier2", "Tier3"]) l1 = LevelFactory(program=p, parent=None, customsort=0) l11 = LevelFactory(program=p, parent=l1, customsort=0) i1 = RFIndicatorFactory(program=p, level=None, old_level='Activity') i2 = RFIndicatorFactory(program=p, level=None, old_level='Outcome') i3 = RFIndicatorFactory(program=p, level=None, old_level='Output') i4 = RFIndicatorFactory(program=p, level=l11, old_level='Output') data = ProgramLevelOrderingProgramSerializer( Program.rf_aware_objects.filter(pk=p.pk), many=True ).data[0] self.assertEqual( data['indicator_pks_level_order'], [i4.pk, i2.pk, i3.pk, i1.pk] ) self.assertEqual( data['indicator_pks_chain_order'], [i4.pk, i2.pk, i3.pk, i1.pk] )
def test_incomplete_targets(self): p = RFProgramFactory(months=24) i = RFIndicatorFactory( program=p, target_frequency=RFIndicatorFactory._meta.model.QUARTERLY, targets="incomplete") self.assertEqual(i.periodictargets.count(), 7) self.assertLess( i.periodictargets.order_by('-end_date').first().end_date, p.reporting_period_end)
def test_frequencies_many(self): program = RFProgramFactory() frequencies = [ Indicator.ANNUAL, Indicator.SEMI_ANNUAL, Indicator.TRI_ANNUAL, Indicator.MONTHLY ] for frequency in frequencies: RFIndicatorFactory(program=program, target_frequency=frequency) data = get_serialized_data(program.pk) self.assertEqual(data['frequencies'], frequencies)
def test_one_site_duplicated(self): program = RFProgramFactory() site = SiteProfileFactory() indicator = RFIndicatorFactory(program=program, target_frequency=Indicator.LOP, targets=500, results=200, results__count=2) for result in indicator.result_set.all(): result.site.add(site) indicator = RFIndicatorFactory(program=program, target_frequency=Indicator.LOP, targets=500, results=200, results__count=2) for result in indicator.result_set.all(): result.site.add(site) data = get_serialized_data(program.pk) self.assertEqual(len(data['sites']), 1)
def test_one_indicator_type(self): program = RFProgramFactory() i_type = IndicatorTypeFactory() indicator = RFIndicatorFactory(program=program) indicator.indicator_type.add(i_type) data = get_serialized_data(program.pk) self.assertEqual(len(data['indicator_types']), 1) self.assertEqual(data['indicator_types'][0]['pk'], i_type.pk) self.assertEqual(data['indicator_types'][0]['name'], i_type.indicator_type)