def test_to_table_eurostat_one_dim(): # convert collection to table json_pathname = os.path.join(fixture_dir, "www.ec.europa.eu_eurostat", "eurostat-name_gpd_c-geo_IT.json") collection = jsonstat.JsonStatCollection() collection.from_file(json_pathname) gdp_c = collection.dataset('nama_gdp_c') table = gdp_c.to_table() # read generate table by jsonstat js module to_table_pathname = os.path.join( fixture_dir, "output_from_nodejs", "eurostat-name_gpd_c-geo_IT-to_table.csv") import csv with open(to_table_pathname, 'r') as csvfile: cvs_reader = csv.reader(csvfile) for i, row in enumerate(cvs_reader): msg = "table and cvs don't match at line number {}".format(i) def transform_row(v): if v is None: return '' return "{}".format(v) t = list(map(transform_row, table[i])) assert t == row, msg
def test(uri, filename): json_string = jsonstat.download(uri, filename) collection = jsonstat.JsonStatCollection() collection.from_string(json_string) print("*** multiple datasets info") print(collection) oecd = collection.dataset('oecd') print("\n*** dataset {} info".format(oecd.name)) print(oecd) for d in oecd.dimensions(): print("\n*** info for dimensions '{}'".format(d.did)) print(d) print("\n*** value oecd(area:IT,year:2012): {}".format(oecd.data(area='IT', year='2012'))) print("\ngenerate all vec") oecd.generate_all_vec(area='CA') df = oecd.to_data_frame('year', content='id', blocked_dims={'area': 'CA'}) print(df) table = oecd.to_table()
def test_to_table_ssb_no(): """ test convert dataset to table """ json_pathname = os.path.join(fixture_dir, "www.ssb.no", "29843.json") collection = jsonstat.JsonStatCollection() collection.from_file(json_pathname) ds = collection.dataset(0) table = ds.to_table() # read generate table by jsonstat js module to_table_pathname = os.path.join(fixture_dir, "output_from_nodejs", "29843-to_table.csv") import csv with open(to_table_pathname, 'r') as csv_file: cvs_reader = csv.reader(csv_file) for i, row in enumerate(cvs_reader): msg = "table and cvs don't match at line number {}".format(i) def transform_row(v): if v is None: return '' return "{}".format(v) t = list(map(transform_row, table[i])) assert t == row, msg
def test_idx_as_lint(): json_pathname = os.path.join(jsonstat._examples_dir, "www.json-stat.org", "oecd-canada.json") collection = jsonstat.JsonStatCollection() collection.from_file(json_pathname) oecd = collection.dataset('oecd') lint = oecd.idx_as_lint(10) assert lint == [0, 0, 10]
def test_one_dataset_to_str(json_string_v1_one_dataset): collection = jsonstat.JsonStatCollection() collection.from_string(json_string_v1_one_dataset) expected = [ "JsonstatCollection contains the following JsonStatDataSet:\n", "+-----+---------+\n", "| pos | dataset |\n", "+-----+---------+\n", "| 0 | 'oecd' |\n", "+-----+---------+" ] expected = ''.join(expected) assert expected == str(collection)
def test_two_datasets_to_str(json_string_v1_two_datasets): collection = jsonstat.JsonStatCollection() collection.from_string(json_string_v1_two_datasets) expected = ("JsonstatCollection contains the following JsonStatDataSet:\n" "+-----+----------+\n" "| pos | dataset |\n" "+-----+----------+\n" "| 0 | 'oecd' |\n" "| 1 | 'canada' |\n" "+-----+----------+") assert expected == str(collection)
def test_parse_v2_from_file(): filename = os.path.join(jsonstat._examples_dir, "www.json-stat.org", "oecd-canada-col.json") collection = jsonstat.JsonStatCollection() collection.from_file(filename) oecd = collection.dataset(0) assert oecd is not None canada = collection.dataset(1) assert canada is not None
def test_dcat_to_lint(): json_pathname = os.path.join(jsonstat._examples_dir, "www.json-stat.org", "oecd-canada.json") collection = jsonstat.JsonStatCollection() collection.from_file(json_pathname) oecd = collection.dataset('oecd') dcat = {'concept': 'UNR', 'area': 'AU', 'year': '2013'} lint = oecd.dcat_to_lint(dcat) assert lint == [0, 0, 10] idx = oecd.lint_as_idx(lint) assert idx == 10
def test_parsing_NQQ25(): base_uri = 'http://www.cso.ie/StatbankServices/StatbankServices.svc/jsonservice/responseinstance/' uri = base_uri + "NQQ25" filename = "cso_ie-NQQ25.json" json_string = jsonstat.download(uri, os.path.join(fixture_dir, filename)) collection = jsonstat.JsonStatCollection() collection.from_string(json_string) # extract dataset contained into collection ds = collection.dataset(0) assert ds.dimension('Sector') is not None data = ds.data(0) assert 19960 == data.value
def test_parsing_CIA01(): base_uri = 'http://www.cso.ie/StatbankServices/StatbankServices.svc/jsonservice/responseinstance/' uri = base_uri + "CIA01" filename = "cso_ie-CIA01.json" json_string = jsonstat.download(uri, os.path.join(fixture_dir, filename)) collection = jsonstat.JsonStatCollection() collection.from_string(json_string) # extract dataset contained into collection ds = collection.dataset(0) # print(ds) assert ds.dimension('County and Region') is not None data = ds.data(0) assert 41692 == data.value
def test_parse_v1_from_file(): filename = os.path.join(jsonstat._examples_dir, "www.json-stat.org", "oecd-canada.json") collection = jsonstat.JsonStatCollection() collection.from_file(filename) assert collection.dataset('oecd') is not None assert collection.dataset('canada') is not None oecd = collection.dataset("oecd") dim = oecd.dimension("concept") expected = ("+-----+-------+---------------------+\n" "| pos | idx | label |\n" "+-----+-------+---------------------+\n" "| 0 | 'UNR' | 'unemployment rate' |\n" "+-----+-------+---------------------+") assert expected == dim.__str__()
def test_one_dimension(): uri = 'http://data.ssb.no/api/v0/dataset/29843.json?lang=en' filename = "29843.json" json_string = jsonstat.download(uri, os.path.join(fixture_dir, "www.ssb.no", filename)) collection = jsonstat.JsonStatCollection() collection.from_string(json_string) # extract dataset contained into collection ds = collection.dataset(0) # ds.info() # ds.dimension('PKoder').info() # v = ds.value(PKoder="Basic chemicals", Tid="2013M07") # self.assertEqual(112.2, v) data = ds.data(PKoder="P1111", Tid="2013M07") assert 112.2 == data.value
def test_data_with_oecd_canada(): json_pathname = os.path.join(jsonstat._examples_dir, "www.json-stat.org", "oecd-canada.json") collection = jsonstat.JsonStatCollection() collection.from_file(json_pathname) oecd = collection.dataset('oecd') data = oecd.data(concept='UNR', area='AU', year='2004') assert 5.39663128 == data.value assert data.status is None # first position with status at idx 10 dcat = {'concept': 'UNR', 'area': 'AU', 'year': '2013'} data = oecd.data(dcat) assert data.status == "e" data = oecd.data(10) assert data.status == "e" data = oecd.data([0, 0, 10]) assert data.status == "e"
def test_one_dimension(): uri = base_uri + 'nama_gdp_c?precision=1&geo=IT&unit=EUR_HAB&indic_na=B1GM' filename = "eurostat-name_gpd_c-geo_IT.json" json_string = jsonstat.download(uri, os.path.join(fixture_dir, filename)) # extract collection collection = jsonstat.JsonStatCollection() collection.from_string(json_string) # extract dataset contained into collection ds = collection.dataset('nama_gdp_c') """:type: jsonstat.JsonStatDataset""" assert 69 == len(ds) time = ds.dimension('time') assert 69 == len(time) # show some values data = ds.data(geo="IT", time="2011") assert 26000 == data.value
def test_parse_v1_from_string(json_string_v1_two_datasets): collection = jsonstat.JsonStatCollection() collection.from_string(json_string_v1_two_datasets) assert collection.dataset('oecd') is not None assert collection.dataset('canada') is not None