def check_against_files(test_str): test = create_tst(test_str) from iatidq.dqparsetests import test_functions as tf test_functions = tf() data_files = [ ("good.xml", True), ("bad.xml", False) ] def check_data_file(data_file, expected_result): parse_tree = lxml.etree.parse(os.path.join(current, data_file)) activity = parse_tree.find('iati-activity') observed = test_functions[test.id](activity) print observed, expected_result assert observed == expected_result [ check_data_file(data_file, expected_result) for data_file, expected_result in data_files ]
def _test_example_tests(publisher, country): package_name = '-'.join([publisher, country]) xml_filename = os.path.join("unittests", "artefacts", package_name + '.xml') # check there's nothing in the db #pgs = get_packagegroups_by_name(publisher) #assert len(pgs) == 0 #pkgs = get_packages_by_name(package_name) #assert len(pkgs) == 0 # do refresh #iatidq.dqregistry.refresh_package_by_name(package_name) iatidq.setup.setup_packages_minimal() iatidq.dqimporttests.hardcodedTests() # load foxpath tests os.chdir(parent) iatidq.dqimporttests.importTestsFromFile( "tests/sample_tests.csv", test_level.ACTIVITY) print "Importing indicators" iatidq.dqindicators.importIndicatorsFromFile( "test_pwyf2013", "tests/sample_tests.csv") iatidq.dqindicators.importIndicatorDescriptionsFromFile("pwyf2013", "tests/indicators.csv") log("Importing codelists") #iatidq.dqcodelists.importCodelists() #log("Refreshing package data from Registry") #iatidq.dqregistry.refresh_packages() log("about to start testing") from iatidq.dqparsetests import test_functions as tf test_functions = tf() from iatidq import dqcodelists codelists = dqcodelists.generateCodelists() # FIXME: THIS IS A TOTAL HACK with db.session.begin(): iatidq.models.Result.query.delete() iatidq.models.AggregateResult.query.delete() iatidq.models.AggregationType.query.delete() all_ag = create_aggregation_types({}) from iatidq.testrun import start_new_testrun runtime = start_new_testrun() results = iatidq.models.Result.query.all() assert len(results) == 0 pkg = get_packages_by_name(package_name)[0] log(pkg.package_name) package_id = pkg.id ## this is a hack on a stick #if publisher == 'unitedstates': # setup_organisations(package_id) iatidq.setup.setup_organisations_minimal() assert iatidq.test_queue.check_file(test_functions, codelists, xml_filename, runtime.id, pkg.id) results = iatidq.models.Result.query.all() assert len(results) > 0 print >>sys.stderr, len(results) aggtest_results = iatidq.models.AggregateResult.query.filter( AggregateResult.aggregateresulttype_id == all_ag.id ).all() for result in aggtest_results: assert result.runtime_id == runtime.id assert result.package_id == pkg.id resultful_tests = [ 'valid_xml', 'description/text() exists?', 'description/text() has more than 40 characters?', 'description/@type exists?', 'title/text() exists?', 'title/text() has more than 10 characters?', """activity-date[@type='start-planned']/@iso-date or transaction-date/@iso-date (for each transaction) is less than 13 months ago?""" ] expected_test_ids = [ i.id for i in iatidq.models.Test.query.filter( Test.name.in_(resultful_tests)).all() ] observed_test_ids = [ i.test_id for i in aggtest_results ] print "expected test ids: ", expected_test_ids print "observed test ids: ", observed_test_ids assert set(expected_test_ids) == set(observed_test_ids)