def test_QueryExecutionerWorkflow(sample_queries):
        qExec = QueryExecutioner()

        assert (qExec.endpoint == "https://api-next.datengui.de/graphql"
                ), "Default endpoint is wrong"

        res_query1 = qExec.run_query(sample_queries.data_query1)
        assert res_query1 is not None, "query did not return results"

        assert len(
            res_query1.query_results) > 0, "query did not return results"
        assert (type(res_query1.query_results) is
                dict), "query results are not a python json representation"

        assert type(res_query1.meta_data) == dict, "meta data not a dict"
        assert len(res_query1.meta_data) > 0, "meta data absent"
        assert len(res_query1.meta_data) == 1, "too much meta data"

        assert "BEVMK3" in res_query1.meta_data, "statistic absend"
        assert (res_query1.meta_data["BEVMK3"] !=
                "NO DESCRIPTION FOUND"), "descrption was not obtained"

        info = qExec.get_type_info("Region")
        assert info.kind == "OBJECT", "Region should be an object"
        assert info.enum_values is None, "Region doesn't have enum values"
        assert type(info.fields) == dict, "Fields should be a dict"

        stat_args = info.fields["BEVMK3"].get_arguments()
        assert len(stat_args) > 0
        assert "statistics" in stat_args

        enum_vals = qExec.get_type_info("BEVMK3Statistics").enum_values
        assert type(enum_vals) == dict, "Enum values should be dict"
        assert len(enum_vals) > 0, "Enums should have values"
def test_build_execute_transform_integration_all_regions(query_all_regions):
    """
    Smoke test covering all_regions
    """
    q_exec = QueryExecutioner()

    res = q_exec.run_query(query_all_regions)

    output_transf = QueryOutputTransformer(res)
    output_transf.transform()
def test_build_execute_transform_integration_multi_region(query_multi_regions):
    """
    Smoke test covering multiple regions in
    region query.
    """

    q_exec = QueryExecutioner()

    res = q_exec.run_query(query_multi_regions)

    output_transf = QueryOutputTransformer(res)
    output_transf.transform()
示例#4
0
def save_result_files_from_query(query, name, result_path):
    """
        Helper function that for the construction of files
        to be loaded by `construct_execution_results`
    """
    qe = QueryExecutioner()
    execution_results = qe.run_query(query)

    query_json_file = os.path.join(result_path, f"{name}_query.json")
    result_json_file = os.path.join(result_path, f"{name}.json")
    meta_json_file = os.path.join(result_path, f"{name}_meta.json")

    with open(query_json_file, "w") as file:
        json.dump(query.get_graphql_query(), file)

    with open(result_json_file, "w") as file:
        json.dump([res.query_results for res in execution_results], file)

    with open(meta_json_file, "w") as file:
        json.dump(execution_results[0].meta_data, file)
示例#5
0
def download_all_regions() -> pd.DataFrame:
    """[summary]

    :raises RuntimeError: [description]
    :raises RuntimeError: [description]
    :return: [description]
    :rtype: pd.DataFrame
    """
    def nuts_query(nuts_level):
        q = Query.all_regions(nuts=nuts_level)
        return q

    def lau_query(lau_level):
        q = Query.all_regions(lau=lau_level)
        return q

    qb_all = Query.all_regions()

    qe = QueryExecutioner()
    print("start")
    all_regions = qe.run_query(qb_all)
    print("all")
    r_nuts1 = qe.run_query(nuts_query(1))
    print("nuts1")
    r_nuts2 = qe.run_query(nuts_query(2))
    print("nuts2")
    r_nuts3 = qe.run_query(nuts_query(3))
    print("nuts3")
    r_lau1 = qe.run_query(lau_query(1))
    print("lau")
    # currently no distinction between different laus
    # on datehenguide side
    # r_lau2 = qe.run_query(lau_query(2))

    levels = {
        "nuts1": r_nuts1,
        "nuts2": r_nuts2,
        "nuts3": r_nuts3,
        "lau": r_lau1,
        # 'lau2':r_lau2
    }

    def isAnscestor(region_id, candidate):
        """[summary]

        :param region_id: [description]
        :type region_id: [type]
        :param candidate: [description]
        :type candidate: [type]
        :return: [description]
        :rtype: [type]
        """
        return region_id.startswith(candidate) and candidate != region_id

    def parent(region_id, region_details):
        """[summary]

        :param region_id: [description]
        :type region_id: [type]
        :param region_details: [description]
        :type region_details: [type]
        :return: [description]
        :rtype: [type]
        """
        desc = region_details.assign(ansc=lambda df: df.index.map(
            lambda i: isAnscestor(region_id, i))).query("ansc")
        max_lev = desc.level.max()  # noqa: F841
        parent_frame = desc.query("level == @max_lev")
        if not parent_frame.empty:
            return parent_frame.iloc[0, :].name
        else:
            None

    if all_regions is None:
        raise RuntimeError("Was not able to download all regions")

    for k in levels:
        if levels[k] is None:
            raise RuntimeError(f"Was not able to download {k} regions")

    all_regions_df = pd.concat([
        pd.DataFrame(page["data"]["allRegions"]["regions"])
        for page in cast(List[ExecutionResults], all_regions)[0].query_results
    ]).set_index("id")

    level_df = pd.concat(
        pd.concat([
            pd.DataFrame(page["data"]["allRegions"]["regions"]) for page in
            cast(List[ExecutionResults], levels[k])[0].query_results
        ]).assign(level=k) for k in levels)

    all_rg_parents = all_regions_df.join(
        level_df.set_index("id").loc[:, "level"]).assign(
            parent=lambda df: df.index.map(
                partial(
                    parent,
                    region_details=all_regions_df.assign(level=lambda df: df.
                                                         index.map(len)),
                )))
    all_rg_parents.loc[all_rg_parents.level == "nuts1", "parent"] = "DG"

    return all_rg_parents