Skip to content

Commit

Permalink
wip ati tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjwebb committed Nov 25, 2024
1 parent 7f4f35f commit f2ec01e
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
56 changes: 56 additions & 0 deletions stats/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

import copy
import csv
import glob
import json
import os
import re
from collections import Counter, OrderedDict, defaultdict
from datetime import date, datetime, timedelta
from decimal import Decimal, InvalidOperation

from bdd_tester import BDDTester
import iatirulesets
from dateutil.relativedelta import relativedelta
from helpers.currency_conversion import get_USD_value
Expand All @@ -39,6 +41,38 @@
)


INDEX_INDICATOR_DEFINTIONS_PATH = './2024-Index-indicator-definitions'


def load_ati_tests():
"""Load the index tests."""
base_path = os.path.join(INDEX_INDICATOR_DEFINTIONS_PATH, "test_definitions")
step_definitions = os.path.join(base_path, "step_definitions.py")
feature_filepaths = glob.glob(os.path.join(base_path, "*", "*.feature"))
tester = BDDTester(step_definitions)
all_tests = [t for feature_filepath in feature_filepaths for t in tester.load_feature(feature_filepath).tests]

# Remove the current data condition from tests.
for test in all_tests:
test.steps = [x for x in test.steps if not (x.step_type == "given" and x.text == "the activity is current")]

return all_tests


ati_tests = load_ati_tests()


def load_ati_current_data_test():
"""Load the current data test."""
base_path = os.path.join(INDEX_INDICATOR_DEFINTIONS_PATH, "test_definitions")
step_definitions = os.path.join(base_path, "step_definitions.py")
tester = BDDTester(step_definitions)
return tester.load_feature(os.path.join(base_path, "current_data.feature")).tests[0]


ati_current_data_test = load_ati_current_data_test()


def add_years(d, years):
"""Return a date that's `years` years before/after the date (or datetime)
object `d`. Return the same calendar date (month and day) in the
Expand Down Expand Up @@ -2034,6 +2068,28 @@ def transaction_total(self):
out += 1
return out

@returns_numberdict
@memoize
def ati_tests(self):
out = {}
for test in ati_tests:
result = test(self.element)
result = int(bool(result))
out[f"{test.feature.name}: {test.name}"] = result
return out

@returns_number
@memoize
def ati_current(self):
return int(bool(ati_current_data_test(self.element)))

@returns_numberdict
def ati_tests_current(self):
if self.ati_current():
return self.ati_tests()
else:
return {}


ckan = json.load(open("helpers/ckan.json"))
publisher_re = re.compile(r"(.*)\-[^\-]")
Expand Down
48 changes: 48 additions & 0 deletions stats/tests/test_ati.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# coding=utf-8
import pytest
from lxml import etree

from stats.analytics import ActivityStats


class MockActivityStats(ActivityStats):
def __init__(self, major_version):
self.major_version = major_version
return super(MockActivityStats, self).__init__()

def _major_version(self):
return self.major_version


@pytest.mark.parametrize("major_version", ["1", "2"])
def test_comprehensiveness_is_current(major_version):
activity_stats = MockActivityStats(major_version)

activity_stats.element = etree.fromstring(
"""
<iati-activity>
<title>
<narrative>Title</narrative>
</title>
</iati-activity>
"""
)
ati_dict = activity_stats.ati_tests()
assert type(ati_dict) is dict
ati_dict["Title: Title is present"] == 1
ati_dict["Title: Title has at least 10 characters"] == 0

activity_stats.element = etree.fromstring(
"""
<iati-activity>
<title>
<narrative>Title with at least 10 characters</narrative>
</title>
</iati-activity>
"""
)

ati_dict = activity_stats.ati_tests()
assert type(ati_dict) is dict
ati_dict["Title: Title is present"] == 1
ati_dict["Title: Title has at least 10 characters"] == 0

0 comments on commit f2ec01e

Please sign in to comment.