def test_aggregate_quantitative_unit(self): # Given root = ProjectFixtureBuilder()\ .with_results([{ 'title': 'Result #1', 'indicators': [{ 'title': 'Indicator #1', 'periods': [ { 'period_start': date(2010, 1, 1), 'period_end': date(2010, 12, 31), 'label': 'L1', }, { 'period_start': date(2011, 1, 1), 'period_end': date(2011, 12, 31), 'label': 'L2', }, { 'period_start': date(2012, 1, 1), 'period_end': date(2012, 12, 31), 'label': 'L3', }, ] }] }])\ .with_contributors([ {'title': 'Contrib #1', 'contributors': [{'title': 'Subcon #1.1'}]}, {'title': 'Contrib #2'} ])\ .build() l1 = root.get_label('L1') l2 = root.get_label('L2') l3 = root.get_label('L3') contrib2 = root.get_contributor(title='Contrib #2') subcon1_1 = root.get_contributor(title='Subcon #1.1') user = self.create_user('*****@*****.**', 'password', is_admin=True) contrib2.get_period(period_start=date(2010, 1, 1)).set_label(l1).add_update(user, value=1) contrib2.get_period(period_start=date(2011, 1, 1)).set_label(l2).add_update(user, value=2) contrib2.get_period(period_start=date(2012, 1, 1)).set_label(l3).add_update(user, value=3) subcon1_1.get_period(period_start=date(2010, 1, 1)).add_update(user, value=4) subcon1_1.get_period(period_start=date(2011, 1, 1)).set_label(l1).add_update(user, value=5) subcon1_1.get_period(period_start=date(2012, 1, 1)).set_label(l2).add_update(user, value=6) # When indicators = build_view_object(root.object) # Then self.assertEqual(1, len(indicators)) indicator = indicators[0] self.assertEqual(6, indicator.get_labeled_period('L1').actual_value) self.assertEqual(8, indicator.get_labeled_period('L2').actual_value) self.assertEqual(3, indicator.get_labeled_period('L3').actual_value)
def test_reordering_result_at_lead_reflected_to_contributor(self): # Given self.create_user(self.email, self.password, is_superuser=True) self.c.login(username=self.email, password=self.password) lead = ProjectFixtureBuilder()\ .with_title('Lead Project')\ .with_results([ {'title': 'Result #1'}, {'title': 'Result #2'}, ])\ .with_contributors([ {'title': 'Contrib Project'} ])\ .build() contrib = lead.get_contributor(title='Contrib Project') lead_result = lead.results.get(title='Result #2') # When url = '/rest/v1/project/{}/reorder_items/'.format(lead.object.id) data = { 'item_type': 'result', 'item_id': lead_result.id, 'item_direction': 'up', } self.c.post(url, data=data, follow=True) # Then self.assertEqual(lead.results.get(title='Result #1').order, 1) self.assertEqual(lead.results.get(title='Result #2').order, 0) self.assertEqual(contrib.results.get(title='Result #1').order, 1) self.assertEqual(contrib.results.get(title='Result #2').order, 0)
def test_should_not_be_able_to_reorder_indicator(self): # Given self.create_user(self.email, self.password, is_superuser=True) self.c.login(username=self.email, password=self.password) lead = ProjectFixtureBuilder()\ .with_title('Lead Project')\ .with_results([ { 'title': 'Result #1', 'indicators': [ {'title': 'Indicator #1'}, {'title': 'Indicator #2'}, ] }, ])\ .with_contributors([ {'title': 'Contrib Project'} ])\ .build() contrib = lead.get_contributor(title='Contrib Project') indicators = contrib.indicators.all() # When url = '/rest/v1/project/{}/reorder_items/'.format(contrib.object.id) data = { 'item_type': 'indicator', 'item_id': indicators[0].id, 'item_direction': 'down', } response = self.c.post(url, data=data, follow=True) # Then self.assertEqual(response.status_code, 400)
def test_change_parent_to_sibling(self): # Given root = ProjectFixtureBuilder()\ .with_title('Parent project')\ .with_disaggregations({'Foo': ['Bar']})\ .with_results([{ 'title': 'Result #1', 'indicators': [{ 'title': 'Indicator #1', 'periods': [{ 'period_start': date(2020, 1, 1), 'period_end': date(2020, 12, 31), }] }] }])\ .with_contributors([ {'title': 'Child project'}, {'title': 'New project'} ])\ .build() child_project = root.get_contributor(title='Child project') new_project = root.get_contributor(title='New project') # When command.change_parent(new_project.object, child_project.object) # Then self.assertIsNone( new_project.object.parents_all().filter(id=root.object.id).first()) self.assertIsNotNone(new_project.object.parents_all().filter( id=child_project.object.id).first()) self.assertEqual( new_project.results.get(title='Result #1').parent_result, child_project.results.get(title='Result #1')) self.assertEqual( new_project.indicators.get(title='Indicator #1').parent_indicator, child_project.indicators.get(title='Indicator #1')) self.assertEqual( new_project.periods.get( period_start=date(2020, 1, 1)).parent_period, child_project.periods.get(period_start=date(2020, 1, 1))) self.assertEqual( new_project.object.dimension_names.get( name='Foo').parent_dimension_name, child_project.object.dimension_names.get(name='Foo')) self.assertEqual( new_project.get_disaggregation('Foo', 'Bar').parent_dimension_value, child_project.get_disaggregation('Foo', 'Bar'))
class TestHierarchyBase(BaseTestCase): def setUp(self): super().setUp() self.org = self.create_organisation('My Org') self.root_project_facade = ProjectFixtureBuilder() \ .with_results( [{ 'title': 'Result #1', 'indicators': [ {'title': 'Indicator #1', 'periods': [{'period_start': '2020-01-01', 'period_end': '2020-01-31'}]}, {'title': 'Indicator #2', 'periods': [{'period_start': '2020-03-01', 'period_end': '2020-03-31'}]}, ] }] ) \ .with_contributors( [ {'title': 'Contrib #1'}, ] ) \ .build() self.program = self.create_project_hierarchy( self.org, self.root_project_facade.object, 2) self.root_project = self.root_project_facade.object self.contributor_project = self.root_project_facade.get_contributor( title="Contrib #1").object self.parent_period = IndicatorPeriod.objects.filter( indicator__result=self.root_project.results.first()).first() self.child_period = IndicatorPeriod.objects.filter( indicator__result=self.contributor_project.results.first()).first( ) def create_orphan(self): # Create orphan self.child_period.parent_period = None self.child_period.save() self.parent_period.delete()
def test_aggregate_quantitative_percentage(self): # Given root = ProjectFixtureBuilder()\ .with_results([{ 'title': 'Result #1', 'indicators': [{ 'title': 'Indicator #1', 'type': QUANTITATIVE, 'measure': PERCENTAGE_MEASURE, 'periods': [ { 'period_start': date(2010, 1, 1), 'period_end': date(2010, 12, 31), 'label': 'L1', }, { 'period_start': date(2011, 1, 1), 'period_end': date(2011, 12, 31), 'label': 'L2', }, { 'period_start': date(2012, 1, 1), 'period_end': date(2012, 12, 31), 'label': 'L3', }, ] }] }])\ .with_contributors([ {'title': 'Contrib #1', 'contributors': [{'title': 'Subcon #1.1'}]}, {'title': 'Contrib #2'} ])\ .build() l1 = root.get_label('L1') l2 = root.get_label('L2') l3 = root.get_label('L3') contrib2 = root.get_contributor(title='Contrib #2') subcon1_1 = root.get_contributor(title='Subcon #1.1') user = self.create_user('*****@*****.**', 'password', is_admin=True) contrib2.get_period(period_start=date(2010, 1, 1)).set_label(l1).add_update(user, numerator=1, denominator=10) contrib2.get_period(period_start=date(2011, 1, 1)).set_label(l2).add_update(user, numerator=2, denominator=10) contrib2.get_period(period_start=date(2012, 1, 1)).set_label(l3).add_update(user, numerator=3, denominator=10) subcon1_1.get_period(period_start=date(2010, 1, 1)).add_update(user, numerator=4, denominator=10) subcon1_1.get_period(period_start=date(2011, 1, 1)).set_label(l1).add_update(user, numerator=5, denominator=10) subcon1_1.get_period(period_start=date(2012, 1, 1)).set_label(l2).add_update(user, numerator=6, denominator=10) # When indicators = build_view_object(root.object) # Then self.assertEqual(1, len(indicators)) indicator = indicators[0] pl1 = indicator.get_labeled_period('L1') pl2 = indicator.get_labeled_period('L2') pl3 = indicator.get_labeled_period('L3') self.assertEqual((6, 20, 30), (pl1.numerator, pl1.denominator, pl1.actual_value)) self.assertEqual((8, 20, 40), (pl2.numerator, pl2.denominator, pl2.actual_value)) self.assertEqual((3, 10, 30), (pl3.numerator, pl3.denominator, pl3.actual_value))
def test_aggregate_quantitative_percentage_with_disaggregation(self): # Given root = ProjectFixtureBuilder()\ .with_disaggregations({ 'Gender': ['Male', 'Female'], })\ .with_results([{ 'title': 'Result #1', 'indicators': [{ 'title': 'Indicator #1', 'type': QUANTITATIVE, 'measure': PERCENTAGE_MEASURE, 'periods': [ { 'period_start': date(2010, 1, 1), 'period_end': date(2010, 12, 31), 'label': 'L1', }, { 'period_start': date(2011, 1, 1), 'period_end': date(2011, 12, 31), 'label': 'L2', }, { 'period_start': date(2012, 1, 1), 'period_end': date(2012, 12, 31), 'label': 'L3', }, ] }] }])\ .with_contributors([ {'title': 'Contrib #1', 'contributors': [{'title': 'Subcon #1.1'}]}, {'title': 'Contrib #2'} ])\ .build() l1 = root.get_label('L1') l2 = root.get_label('L2') l3 = root.get_label('L3') contrib2 = root.get_contributor(title='Contrib #2') subcon1_1 = root.get_contributor(title='Subcon #1.1') user = self.create_user('*****@*****.**', 'password', is_admin=True) contrib2.get_period(period_start=date(2010, 1, 1)).set_label(l1)\ .add_update(user, numerator=1, denominator=10, disaggregations={ 'Gender': { 'Female': {'numerator': 1, 'denominator': 10} } }) contrib2.get_period(period_start=date(2011, 1, 1)).set_label(l2)\ .add_update(user, numerator=2, denominator=10, disaggregations={ 'Gender': { 'Male': {'numerator': 1, 'denominator': 5}, 'Female': {'numerator': 1, 'denominator': 5} } }) contrib2.get_period(period_start=date(2012, 1, 1)).set_label(l3)\ .add_update(user, numerator=3, denominator=10, disaggregations={ 'Gender': { 'Male': {'numerator': 1, 'denominator': 5}, 'Female': {'numerator': 2, 'denominator': 5} } }) subcon1_1.get_period(period_start=date(2010, 1, 1))\ .add_update(user, numerator=4, denominator=10, disaggregations={ 'Gender': { 'Male': {'numerator': 2, 'denominator': 5}, 'Female': {'numerator': 2, 'denominator': 5} } }) subcon1_1.get_period(period_start=date(2011, 1, 1)).set_label(l1)\ .add_update(user, numerator=5, denominator=10, disaggregations={ 'Gender': { 'Male': {'numerator': 2, 'denominator': 5}, 'Female': {'numerator': 3, 'denominator': 5} } }) subcon1_1.get_period(period_start=date(2012, 1, 1)).set_label(l2)\ .add_update(user, numerator=6, denominator=10, disaggregations={ 'Gender': { 'Male': {'numerator': 3, 'denominator': 5}, 'Female': {'numerator': 3, 'denominator': 5} } }) # When indicators = build_view_object(root.object) # Then self.assertEqual(1, len(indicators)) indicator = indicators[0] pl1 = indicator.get_labeled_period('L1') pl2 = indicator.get_labeled_period('L2') pl3 = indicator.get_labeled_period('L3') self.assertEqual(calculate_percentage(2, 5), pl1.get_disaggregation_of('Gender', 'Male')) self.assertEqual(calculate_percentage(4, 15), pl1.get_disaggregation_of('Gender', 'Female')) self.assertEqual(calculate_percentage(4, 10), pl2.get_disaggregation_of('Gender', 'Male')) self.assertEqual(calculate_percentage(4, 10), pl2.get_disaggregation_of('Gender', 'Female')) self.assertEqual(calculate_percentage(1, 5), pl3.get_disaggregation_of('Gender', 'Male')) self.assertEqual(calculate_percentage(2, 5), pl3.get_disaggregation_of('Gender', 'Female'))
def test_aggregate_quantitative_unit_with_disaggregation(self): # Given root = ProjectFixtureBuilder()\ .with_disaggregations({ 'Gender': ['Male', 'Female'], })\ .with_results([{ 'title': 'Result #1', 'indicators': [{ 'title': 'Indicator #1', 'periods': [ { 'period_start': date(2010, 1, 1), 'period_end': date(2010, 12, 31), 'label': 'L1', }, { 'period_start': date(2011, 1, 1), 'period_end': date(2011, 12, 31), 'label': 'L2', }, { 'period_start': date(2012, 1, 1), 'period_end': date(2012, 12, 31), 'label': 'L3', }, ] }] }])\ .with_contributors([ {'title': 'Contrib #1', 'contributors': [{'title': 'Subcon #1.1'}]}, {'title': 'Contrib #2'} ])\ .build() l1 = root.get_label('L1') l2 = root.get_label('L2') l3 = root.get_label('L3') contrib2 = root.get_contributor(title='Contrib #2') subcon1_1 = root.get_contributor(title='Subcon #1.1') user = self.create_user('*****@*****.**', 'password', is_admin=True) contrib2.get_period(period_start=date(2010, 1, 1)).set_label(l1)\ .add_update(user, value=1, disaggregations={ 'Gender': { 'Male': {'value': 1}, } }) contrib2.get_period(period_start=date(2011, 1, 1)).set_label(l2)\ .add_update(user, value=2, disaggregations={ 'Gender': { 'Male': {'value': 1}, 'Female': {'value': 1}, } }) contrib2.get_period(period_start=date(2012, 1, 1)).set_label(l3)\ .add_update(user, value=3, disaggregations={ 'Gender': { 'Male': {'value': 1}, 'Female': {'value': 2}, } }) subcon1_1.get_period(period_start=date(2010, 1, 1))\ .add_update(user, value=4, disaggregations={ 'Gender': { 'Male': {'value': 2}, 'Female': {'value': 2}, } }) subcon1_1.get_period(period_start=date(2011, 1, 1)).set_label(l1)\ .add_update(user, value=5, disaggregations={ 'Gender': { 'Male': {'value': 2}, 'Female': {'value': 3}, } }) subcon1_1.get_period(period_start=date(2012, 1, 1)).set_label(l2)\ .add_update(user, value=6, disaggregations={ 'Gender': { 'Male': {'value': 2}, 'Female': {'value': 4}, } }) # When indicators = build_view_object(root.object) # Then self.assertEqual(1, len(indicators)) indicator = indicators[0] pl1 = indicator.get_labeled_period('L1') pl2 = indicator.get_labeled_period('L2') pl3 = indicator.get_labeled_period('L3') self.assertEqual(3, pl1.get_disaggregation_of('Gender', 'Male')) self.assertEqual(3, pl1.get_disaggregation_of('Gender', 'Female')) self.assertEqual(3, pl2.get_disaggregation_of('Gender', 'Male')) self.assertEqual(5, pl2.get_disaggregation_of('Gender', 'Female')) self.assertEqual(1, pl3.get_disaggregation_of('Gender', 'Male')) self.assertEqual(2, pl3.get_disaggregation_of('Gender', 'Female'))