Example #1
0
def test_select_list():

    default_sel_filt = opendata.get_info('82070ENG')['DefaultSelection']
    filters_and_selections = default_sel_filt.split("&")

    for fs in filters_and_selections:
        if fs.startswith('$select='):
            select = fs[8:]

    opendata.get_data('82070ENG', select=select.split(', '))
Example #2
0
def test_filters():

    default_sel_filt = opendata.get_info('82070ENG')['DefaultSelection']
    filters_and_selections = default_sel_filt.split("&")

    for fs in filters_and_selections:
        if fs.startswith('$filter='):
            filt = fs[8:]

    opendata.get_data('82070ENG', filters=filt)
Example #3
0
def test_info_values(table_id):

    info = opendata.get_info(table_id)

    # Check response is dict (not a list)
    assert isinstance(info, dict)

    # Check required keys are available
    assert 'Description' in info.keys()
    assert 'ID' in info.keys()
    assert 'Identifier' in info.keys()
Example #4
0
def test_select_n_cols():

    default_sel_filt = opendata.get_info('82070ENG')['DefaultSelection']
    filters_and_selections = default_sel_filt.split("&")

    for fs in filters_and_selections:
        if fs.startswith('$select='):
            select = fs[8:]

    select_list = select.split(', ')
    data = opendata.get_data('82070ENG', select=select_list[0:2])

    assert len(data[0].keys()) == 2
    assert len(data[5].keys()) == 2
    assert len(data[10].keys()) == 2
Example #5
0
def main():

    if len(sys.argv) > 1 and sys.argv[1] == "data":
        parser = argparse.ArgumentParser(prog="cbsodata",
                                         description="""
                CBS Open Data: Command Line Interface

                Get data by table identifier.
            """)
        parse_argument_table_id(parser)
        parse_argument_catalog(parser)
        parse_argument_output_format(parser)
        parse_argument_max_rows(parser)
        parse_argument_output(parser)
        args = parser.parse_args(sys.argv[2:])

        result = get_data(args.table_id, catalog_url=args.catalog_url)

        if args.output_file:
            save_list_to_json(result, args.output_file)

        if args.output_format == "text":
            text_outputter(result, max_rows=args.max_rows)
        else:
            json_outputter(result, max_rows=args.max_rows)

    elif len(sys.argv) > 1 and sys.argv[1] == "info":
        parser = argparse.ArgumentParser(prog="cbsodata",
                                         description="""
                CBS Open Data: Command Line Interface

                Get data infomation by table identifier.
            """)
        parse_argument_table_id(parser)
        parse_argument_catalog(parser)
        parse_argument_output_format(parser)
        parse_argument_output(parser)
        args = parser.parse_args(sys.argv[2:])

        result = get_info(args.table_id, catalog_url=args.catalog_url)

        if args.output_file:
            with open(args.output_file, 'w') as f:
                json.dump(result, f, indent=4)

        if args.output_format == "text":
            text_outputter([{
                "Label": k,
                "Value": v
            } for k, v in result.items()])
        else:
            print(json.dumps(result, indent=4))

    elif len(sys.argv) > 1 and sys.argv[1] == "list":
        parser = argparse.ArgumentParser(prog="cbsodata",
                                         description="""
                CBS Open Data: Command Line Interface

                Get list of available tables.
            """)
        parse_argument_catalog(parser)
        parse_argument_output_format(parser)
        parse_argument_max_rows(parser)
        parse_argument_output(parser)
        args = parser.parse_args(sys.argv[2:])

        result = get_table_list(catalog_url=args.catalog_url)

        if args.output_file:
            save_list_to_json(result, args.output_file)

        if args.output_format == "text":
            text_outputter(result, max_rows=args.max_rows)
        else:
            json_outputter(result, max_rows=args.max_rows)

    # no valid sub command
    else:
        parser = argparse.ArgumentParser(prog="cbsodata",
                                         description="""
                CBS Open Data: Command Line Interface
            """)
        parser.add_argument(
            "subcommand",
            nargs="?",
            type=lambda x: isinstance(x, str) and x in AVAILABLE_CMDS,
            help="the subcommand (one of '{}')".format(
                "', '".join(AVAILABLE_CMDS)))
        parser.add_argument("--version",
                            action='version',
                            version=f"CBSOData version: {__version__}",
                            help="show the package version")

        args = parser.parse_args()

        if args.subcommand is None:
            parser.print_help()
Example #6
0
def test_info(table_id):

    # testing
    info = opendata.get_info(table_id)

    assert isinstance(info, dict)