def test_hierarchies(): activity_stats = ActivityStats() activity_stats.element = etree.fromstring(''' <iati-activity> </iati-activity> ''') assert activity_stats.hierarchies() == {None: 1} activity_stats = ActivityStats() activity_stats.element = etree.fromstring(''' <iati-activity hierarchy="3"> </iati-activity> ''') assert activity_stats.hierarchies() == {'3': 1}
def test_by_hierarchy(): # Unlike the hierarchies dict, by_hierarchy should treat activities without # a hierarchy attribute as hierarchy 1 activity_stats = ActivityStats() activity_stats.element = etree.fromstring(''' <iati-activity> </iati-activity> ''') assert activity_stats.by_hierarchy().keys() == [ '1' ] activity_stats = ActivityStats() activity_stats.element = etree.fromstring(''' <iati-activity hierarchy="3"> </iati-activity> ''') assert activity_stats.by_hierarchy().keys() == [ '3' ]
def test_by_hierarchy(): # Unlike the hierarchies dict, by_hierarchy should treat activities without # a hierarchy attribute as hierarchy 1 activity_stats = ActivityStats() activity_stats.element = etree.fromstring(''' <iati-activity> </iati-activity> ''') assert activity_stats.by_hierarchy().keys() == ['1'] activity_stats = ActivityStats() activity_stats.element = etree.fromstring(''' <iati-activity hierarchy="3"> </iati-activity> ''') assert activity_stats.by_hierarchy().keys() == ['3']
def test_version_detection_no_parent(): """ Tests that XML with no parent returns default version. """ activity = ActivityStats() activity.element = etree.fromstring(''' <iati-activity> </iati-activity> ''') assert activity._version() == '1.01'
def test_version_detection_valid(version): """ Tests that valid versions of the IATI Standard are detected. """ activity = ActivityStats() tree = etree.fromstring(''' <iati-activities version="{0}"> <iati-activity> </iati-activity> </iati-activities> '''.format(version)) activity.element = tree.getchildren()[0] assert activity._version() == version
def test_version_detection_no_version_attrib(): """ Tests that XML with no version attribute returns default version. """ activity = ActivityStats() tree = etree.fromstring(''' <iati-activities> <iati-activity> </iati-activity> </iati-activities> ''') activity.element = tree.getchildren()[0] assert activity._version() == '1.01'
def test_forwardlooking_is_current(): activity_stats = ActivityStats() # If there are no end dates, the activity is current activity_stats.element = etree.fromstring('''<iati-activity> </iati-activity>''') assert activity_stats._forwardlooking_is_current(9990) activity_stats.element = etree.fromstring('''<iati-activity> <activity-date iso-date="9980-01-01" type="start-planned" /> <activity-date iso-date="9980-01-01" type="start-actual" /> </iati-activity>''') assert activity_stats._forwardlooking_is_current(9990) # If there is an end date before the given year, it's not current activity_stats.element = etree.fromstring('''<iati-activity> <activity-date iso-date="9980-01-01" type="end-planned" /> </iati-activity>''') assert not activity_stats._forwardlooking_is_current(9990) activity_stats.element = etree.fromstring('''<iati-activity> <activity-date iso-date="9980-01-01" type="end-actual" /> </iati-activity>''') assert not activity_stats._forwardlooking_is_current(9990) # If there is an end date on or after the given year, it is current activity_stats.element = etree.fromstring('''<iati-activity> <activity-date iso-date="9990-01-01" type="end-planned" /> </iati-activity>''') assert activity_stats._forwardlooking_is_current(9990) activity_stats.element = etree.fromstring('''<iati-activity> <activity-date iso-date="9990-01-01" type="end-actual" /> </iati-activity>''') assert activity_stats._forwardlooking_is_current(9990) activity_stats.element = etree.fromstring('''<iati-activity> <activity-date iso-date="9980-01-01" type="end-planned" /> <activity-date iso-date="9990-01-01" type="end-actual" /> </iati-activity>''') assert activity_stats._forwardlooking_is_current(9990)
def test_forwardlooking_is_current_2xx(): activity_stats = ActivityStats() # If there are no end dates, the activity is current activity_stats.element = wrap_activity('''<iati-activity> </iati-activity>''') assert activity_stats._forwardlooking_is_current(9990) activity_stats.element = wrap_activity('''<iati-activity> <activity-date iso-date="9980-01-01" type="1" /> <activity-date iso-date="9980-01-01" type="2" /> </iati-activity>''') assert activity_stats._forwardlooking_is_current(9990) # If there is an end date before the given year, it's not current activity_stats.element = wrap_activity('''<iati-activity> <activity-date iso-date="9980-01-01" type="3" /> </iati-activity>''') assert not activity_stats._forwardlooking_is_current(9990) activity_stats.element = wrap_activity('''<iati-activity> <activity-date iso-date="9980-01-01" type="4" /> </iati-activity>''') assert not activity_stats._forwardlooking_is_current(9990) # If there is an end date on or after the given year, it is current activity_stats.element = wrap_activity('''<iati-activity> <activity-date iso-date="9990-01-01" type="3" /> </iati-activity>''') assert activity_stats._forwardlooking_is_current(9990) activity_stats.element = wrap_activity('''<iati-activity> <activity-date iso-date="9990-01-01" type="4" /> </iati-activity>''') assert activity_stats._forwardlooking_is_current(9990) activity_stats.element = wrap_activity('''<iati-activity> <activity-date iso-date="9980-01-01" type="3" /> <activity-date iso-date="9990-01-01" type="4" /> </iati-activity>''') assert activity_stats._forwardlooking_is_current(9990)
import datetime import re import sys from lxml import etree from stats.common import budget_year from stats.dashboard import ActivityStats writer = csv.writer(sys.stdout) writer.writerow( ["iati-identifier", "First year to fail", "End dates", "Budget years"]) for fpath in sys.argv[1:]: for activity in etree.parse(fpath).xpath("/iati-activities/iati-activity"): activity_stats = ActivityStats() activity_stats.element = activity current = activity_stats.forwardlooking_activities_current() w_budgets = activity_stats.forwardlooking_activities_with_budgets() end_dates = activity.xpath( "./activity-date[@type='{}' or @type='{}']".format( activity_stats._planned_end_code(), activity_stats._actual_end_code())) budgets = activity.xpath("./budget") iati_identifier = activity.xpath("./iati-identifier/text()")[0] end_dates_str = "" budget_years_str = "" if end_dates: end_dates_str = ",".join( x.xpath("@iso-date")[0] for x in end_dates) else: