def test_http_error(table_id): try: opendata.get_data(table_id) except requests.HTTPError: assert True else: assert False
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(', '))
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)
def test_get_data_derden(table_id): # option 1 print("global") opendata.options.catalog_url = 'dataderden.cbs.nl' data_option1 = opendata.get_data(table_id) opendata.options.catalog_url = 'opendata.cbs.nl' # option 2 print("context") with opendata.catalog('dataderden.cbs.nl'): data_option2 = opendata.get_data(table_id) # option 3 print("argument") data_option3 = opendata.get_data(table_id, catalog_url='dataderden.cbs.nl') assert len(data_option1[0].keys()) > 0 for key in data_option1[0].keys(): assert data_option1[0][key] == \ data_option2[0][key] == data_option3[0][key]
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
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()
def test_get_data(table_id): opendata.get_data(table_id)