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_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 setUp(self): self.user = UserFactory(first_name="Indicator", last_name="CreateTest", username="******", is_superuser=True) 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.program = Program.objects.get( ) # forces reporting_period_start/end from str to date() 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.level = LevelFactory(program=self.program) self.request_factory = RequestFactory() self.client = Client() self.client.login(username="******", password='******')
def test_non_rf_level_with_level_assigned(self): p = RFProgramFactory(migrated=False) data = self.get_indicator_data(program=p, level=LevelFactory(program=p, pk=5), old_level="Outcome") self.assertEqual(data['old_level_name'], "Outcome") self.assertEqual(data['level_pk'], 3)
def setUp(self): super(CSVIndicatorTestBase, self).setUp() self.sectors = [SectorFactory(),] self.levels = [LevelFactory(),] self.disaggregations = [DisaggregationTypeFactory(),] self.indicatortypes = [IndicatorTypeFactory(),] self.indicators = [] self.response = None
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_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_program_one_level(self): program = RFProgramFactory(tiers=['Only Tier']) level = LevelFactory(program=program, parent=None) data = get_serialized_data(program.pk) self.assertEqual(data['level_pks_level_order'], [level.pk]) self.assertEqual(data['level_pks_chain_order'], [level.pk]) self.assertEqual(data['indicator_pks_for_level'], [{ 'pk': level.pk, 'indicator_pks': [] }]) self.assertEqual(data['unassigned_indicator_pks'], [])
def test_program_mercycorps_tiers(self): program = RFProgramFactory(tiers=True) goal = LevelFactory(program=program, parent=None) output1 = LevelFactory(program=program, parent=goal) output2 = LevelFactory(program=program, parent=goal) impact11 = LevelFactory(program=program, parent=output1) impact12 = LevelFactory(program=program, parent=output1) impact21 = LevelFactory(program=program, parent=output2) data = get_serialized_data(program.pk) self.assertEqual(data['level_pks_level_order'], [ goal.pk, output1.pk, output2.pk, impact11.pk, impact12.pk, impact21.pk ]) self.assertEqual(data['level_pks_chain_order'], [ goal.pk, output1.pk, impact11.pk, impact12.pk, output2.pk, impact21.pk ]) self.assertEqual( sorted(data['indicator_pks_for_level'], key=lambda l: l['pk']), sorted([{ 'pk': level_pk, 'indicator_pks': [] } for level_pk in [ goal.pk, output1.pk, impact11.pk, impact12.pk, output2.pk, impact21.pk ]], key=lambda l: l['pk'])) self.assertEqual(data['unassigned_indicator_pks'], [])
def test_program_one_level_with_unassigned_indicators(self): program = RFProgramFactory(tiers=['Only Tier']) level = LevelFactory(program=program, parent=None) indicator_pks = [ RFIndicatorFactory(program=program, level=None, number=number).pk for number in ['231', '14', '11'] ] data = get_serialized_data(program.pk) self.assertEqual(data['indicator_pks_for_level'], [{ 'pk': level.pk, 'indicator_pks': [] }]) self.assertEqual(data['unassigned_indicator_pks'], indicator_pks[::-1])
def test_sorting_by_level_order_migrated(self): program = Program.objects.get(pk=RFProgramFactory(migrated=True).pk) level = LevelFactory(program=program) these_numbers = self.numbers[0] to_assign = these_numbers[0:] to_assign.reverse() for count, number in enumerate(to_assign): IndicatorFactory(program=program, number=number, level=level, level_order=count) indicators = unassigned_sort_func( Indicator.rf_aware_objects.filter(program=program)) self.assertEqual([i.number for i in indicators], to_assign)
def test_program_one_level(self): program = RFProgramFactory(tiers=['Tier1']) tier = program.level_tiers.first() level = LevelFactory(program=program, parent=None, name="Test name") data = get_serialized_data(program.pk, levels=True) for key, value in { 'pk': level.pk, 'name': "Test name", 'ontology': '', 'tier_name': 'Tier1', 'tier_pk': tier.pk, 'chain_pk': 'all' }.items(): self.assertEqual(data['levels'][0][key], value)
def test_program_one_level_with_indicators(self): program = RFProgramFactory(tiers=['Only Tier']) level = LevelFactory(program=program, parent=None) indicator_pks = [ RFIndicatorFactory(program=program, level=level).pk for _ in range(3) ] data = get_serialized_data(program.pk) self.assertEqual(data['indicator_pks_for_level'], [{ 'pk': level.pk, 'indicator_pks': indicator_pks }]) self.assertEqual(data['unassigned_indicator_pks'], [])
def test_program_migrated_indicators_number_order(self): p = RFProgramFactory(tiers=["Tier1", "Tier2", "Tier3"]) l1 = LevelFactory(program=p, parent=None, customsort=0) l11 = LevelFactory(program=p, parent=l1, customsort=0) numbers = ['8', '9', '10'] pks = [] for number in reversed(numbers): pks.append(RFIndicatorFactory(program=p, number=number).pk) i0 = RFIndicatorFactory(program=p, level=l11, number='1', level_order=0, pk=10) i1 = RFIndicatorFactory(program=p, level=l11, number='1012', level_order=1, pk=20) pks.append(i1.pk) pks.append(i0.pk) pks.append(RFIndicatorFactory(program=p, level=l1, number='1012,asdf', pk=40).pk) data = ProgramLevelOrderingProgramSerializer( Program.rf_aware_objects.filter(pk=p.pk), many=True ).data[0] self.assertEqual( data['indicator_pks_level_order'], list(reversed(pks)) ) self.assertEqual( data['indicator_pks_chain_order'], list(reversed(pks)) )
def levels(self, create, extracted, **kwargs): """post-gen hooks are called in declaration order, so this can depend upon tiers method""" tiers = self.level_tiers.all() from factories.indicators_models import LevelFactory def get_children(parents, count): return tuple([ get_children(parent, count) if isinstance(parent, tuple) else tuple([count] * parent) for parent in parents ]) def convert_to_data(counts_for_tier, parents=None, count=0): this_tier_counts = counts_for_tier.pop(0) lower_tier_levels = [] level_sets = [] for c, level_count in enumerate(this_tier_counts): level_sets.append(([row[c] for row in counts_for_tier], [{ 'count': count + x, 'parent': parents[c]['count'] if parents is not None else None, 'depth': parents[c]['depth'] + 1 if parents is not None else 0 } for x in range(level_count)])) count += level_count for lower_tier_counts, level_parents in level_sets: if lower_tier_counts: new_lower_tiers, count = convert_to_data( lower_tier_counts, level_parents, count) lower_tier_levels += new_lower_tiers return [ level_data for data_set in level_sets for level_data in data_set[1] ] + lower_tier_levels, count if isinstance(extracted, int): levels_per_tier = [] parents = (1, ) for tier in tiers: levels_per_tier.append(parents) parents = get_children(parents, extracted) levels_data, _ = convert_to_data(levels_per_tier) elif isinstance(extracted, list): levels_data, _ = convert_to_data(extracted) else: return levels = [] universal_level_data = kwargs.get('all', {}) universal_level_data.update({'program': self}) level_pks = kwargs.get('pks', []) tier = None for count, level_data in enumerate(levels_data): new_tier = tiers[level_data['depth']] if new_tier != tier: depth_count = 1 tier = new_tier this_level_data = { 'name': u"Tier: {} Order: {}".format(tier.name, depth_count), 'customsort': depth_count, 'parent': levels[level_data['parent']] if level_data['parent'] is not None else None } depth_count += 1 if level_pks and len(level_pks) > count: this_level_data.update({'pk': level_pks[count]}) this_level_data.update(universal_level_data) this_level_data.update(kwargs.get(str(level_data['count']), {})) levels.append(LevelFactory(**this_level_data))
def setUp(self): self.level = LevelFactory() self.indicator = IndicatorFactory(name="testname", level=self.level)
def test_program_multiple_levels(self): program = RFProgramFactory(tiers=['Tier1', SPECIAL_CHARS, LONG_NAME]) levels = {} tiers = sorted(program.level_tiers.all(), key=lambda tier: tier.tier_depth) goal = LevelFactory(program=program, parent=None, name=SPECIAL_CHARS, customsort=1) levels[goal.pk] = { 'name': SPECIAL_CHARS, 'tier_pk': tiers[0].pk, 'tier_name': 'Tier1', 'chain_pk': 'all', 'ontology': '' } impact1 = LevelFactory(program=program, parent=goal, name=LONG_NAME, customsort=1) levels[impact1.pk] = { 'name': LONG_NAME, 'tier_pk': tiers[1].pk, 'tier_name': SPECIAL_CHARS, 'chain_pk': impact1.pk, 'ontology': '1' } output1 = LevelFactory(program=program, parent=impact1, name=SPECIAL_CHARS, customsort=1) levels[output1.pk] = { 'name': SPECIAL_CHARS, 'tier_pk': tiers[2].pk, 'tier_name': LONG_NAME, 'chain_pk': impact1.pk, 'ontology': '1.1' } output2 = LevelFactory(program=program, parent=impact1, customsort=2) levels[output2.pk] = {'ontology': '1.2'} impact2 = LevelFactory(program=program, parent=goal, customsort=2) levels[impact2.pk] = { 'tier_pk': tiers[1].pk, 'chain_pk': impact2.pk, 'ontology': '2' } levels[LevelFactory(program=program, parent=goal, customsort=3).pk] = { 'ontology': '3' } levels[LevelFactory(program=program, parent=impact2, customsort=1).pk] = { 'tier_pk': tiers[2].pk, 'chain_pk': impact2.pk, 'ontology': '2.1' } data = get_serialized_data(program.pk, levels=True) counted = [] for level_data in data['levels']: self.assertIn(level_data['pk'], levels) for key, value in levels[level_data['pk']].items(): self.assertEqual(level_data[key], value) counted.append(level_data['pk']) self.assertEqual(set(counted), set(levels.keys()))