def test_data_with_three_dimensions(): json_pathname = os.path.join(fixture_dir, "dataset", "three_dim_v1.json") dataset = jsonstat.JsonStatDataSet() dataset.from_file(json_pathname) data = dataset.data(one="one_1", two="two_1", three="three_1") assert data.value == 111 assert data.status is None data = dataset.data(one="one_2", two="two_2", three="three_2") assert data.value == 222 # using a bit different file json_pathname = os.path.join(fixture_dir, "dataset", "three_dim_size_as_string_v1.json") dataset = jsonstat.JsonStatDataSet() dataset.from_file(json_pathname) data = dataset.data(one="one_1", two="two_1", three="three_1") assert data.value == 111 assert data.status is None data = dataset.data(one="one_2", two="two_2", three="three_2") assert data.value == 222 # using a bit different file json_pathname = os.path.join(fixture_dir, "dataset", "three_dim_size_as_string_v2.json") dataset = jsonstat.JsonStatDataSet() dataset.from_file(json_pathname) data = dataset.data(one="one_1", two="two_1", three="three_1") assert data.value == 111 assert data.status is None data = dataset.data(one="one_2", two="two_2", three="three_2") assert data.value == 222
def test_data_with_unemployment(): json_pathname = os.path.join(fixture_dir, "dataset", "dataset_unemployment_v1.json") dataset = jsonstat.JsonStatDataSet("canada") dataset.from_file(json_pathname) data = dataset.data(area="AU", year="2012") assert data.value == 11 # using label Australia instead of index AU data = dataset.data(area="Australia", year="2012") assert data.value == 11 # using dictionary data = dataset.data({'area': "Australia", 'year': "2012"}) assert data.value == 11 data = dataset.data({'area': "AU", 'year': "2012"}) assert data.value == 11 data = dataset.data({"OECD countries, EU15 and total": "AU", 'year': '2012'}) assert data.value == 11 data = dataset.data(area="BE", year="2014") assert data.value == 33 assert data.status is None
def test_to_dataframe(): json_pathname = os.path.join(fixture_dir, "dataset", "dataset_unemployment_v1.json") dataset = jsonstat.JsonStatDataSet() dataset.from_file(json_pathname) df = dataset.to_table(content='id', blocked_dims={"area": "IT"}, rtype=pd.DataFrame) # print(df) ar = [['serie', '2012', 'IT', 14], ['serie', '2013', 'IT', 24], ['serie', '2014', 'IT', 34]] expected = pd.DataFrame(ar, columns=['serie', 'year', 'area', 'Value']) pdt.assert_frame_equal(expected, df) df = dataset.to_data_frame('year', content='id', blocked_dims={"area": "IT"}) # TODO: print(df) ar = [['serie', 'IT', 14], ['serie', 'IT', 24], ['serie', 'IT', 34]] expected = pd.DataFrame(ar, columns=['serie', 'area', 'Value'], index=['2012', '2013', '2014']) expected.index.name = 'year' pdt.assert_frame_equal(expected, df)
def test_exception_no_existent_dimension(): json_pathname = os.path.join(fixture_dir, "dataset", "dataset_unemployment_v1.json") dataset = jsonstat.JsonStatDataSet("canada") dataset.from_file(json_pathname) assert dataset.dimension("year").did == "year" with pytest.raises(jsonstat.JsonStatException) as excinfo: dataset.dimension("not existent dim") expected = "dataset 'canada': unknown dimension 'not existent dim' know dimensions ids are: serie, year, area" assert expected == str(excinfo.value)
def test_all_pos(): json_pathname = os.path.join(fixture_dir, "dataset", "dataset_unemployment_v1.json") dataset = jsonstat.JsonStatDataSet("canada") dataset.from_file(json_pathname) result = list(dataset.all_pos()) # fist digit is serie always 0 # second digit is year from 0 to 2 # third digit is area from 0 to 3 # order is ["serie", "year", "area"] expected = [[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3], # first digit 0 [0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 1, 3], # first digit 1 [0, 2, 0], [0, 2, 1], [0, 2, 2], [0, 2, 3]] # first digit 2 assert result == expected
def test_all_pos_with_block(): json_pathname = os.path.join(fixture_dir, "dataset", "dataset_unemployment_v1.json") dataset = jsonstat.JsonStatDataSet("canada") dataset.from_file(json_pathname) result = list(dataset.all_pos({"area": "IT"})) expected = [[0, 0, 3], [0, 1, 3], [0, 2, 3]] assert result == expected dataset.generate_all_vec(area="IT") result = list(dataset.all_pos({"year": "2014"})) expected = [[0, 2, 0], [0, 2, 1], [0, 2, 2], [0, 2, 3]] assert result == expected dataset.generate_all_vec(year='2014')
def test_info(): json_pathname = os.path.join(fixture_dir, "dataset", "dataset_unemployment_v1.json") dataset = jsonstat.JsonStatDataSet("canada") dataset.from_file(json_pathname) expected = ( "name: 'canada'\n" "label: 'Unemployment rate in the OECD countries'\n" "source: 'Unemployment rate in the OECD countries'\n" "size: 12\n" "+-----+-------+--------------------------------+------+------+\n" "| pos | id | label | size | role |\n" "+-----+-------+--------------------------------+------+------+\n" "| 0 | serie | serie | 1 | |\n" "| 1 | year | 2012-2014 | 3 | time |\n" "| 2 | area | OECD countries, EU15 and total | 4 | geo |\n" "+-----+-------+--------------------------------+------+------+" ) assert expected, dataset.__str__()
def test_to_table(): json_pathname = os.path.join(fixture_dir, "dataset", "dataset_unemployment_v1.json") dataset = jsonstat.JsonStatDataSet() dataset.from_file(json_pathname) table = dataset.to_table() # table len is the size of dataset + 1 for headers assert len(dataset) + 1 == len(table) header_expected = [ u'serie', u'2012-2014', u'OECD countries, EU15 and total', u'Value' ] assert header_expected == table[0] first_row_expected = [u'serie', u'2012', u'Australia', 11] assert first_row_expected == table[1] second_row_expected = [u'serie', u'2012', u'Austria', 12] assert second_row_expected == table[2]
def test_all_pos_with_three_dim(): json_pathname = os.path.join(fixture_dir, "dataset", "three_dim_v1.json") dataset = jsonstat.JsonStatDataSet() dataset.from_file(json_pathname) # test 1 result = list(dataset.all_pos({'one': 'one_1'})) expected = [ [0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3], [0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 1, 3], [0, 2, 0], [0, 2, 1], [0, 2, 2], [0, 2, 3]] assert result == expected # test 2 dataset.generate_all_vec(one='one_1') result = list(dataset.all_pos({"two": "two_2"})) expected = [ [0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 1, 3], [1, 1, 0], [1, 1, 1], [1, 1, 2], [1, 1, 3] ] assert result == expected
def test_to_data_frame_year_IT(): json_pathname = os.path.join(fixture_dir, "dataset", "dataset_unemployment_v1.json") dataset = jsonstat.JsonStatDataSet() dataset.from_file(json_pathname) df = dataset.to_data_frame("year", content="id", blocked_dims={'area': "IT"}) # print(df) # serie area Value # year # 2012 serie IT 14 # 2013 serie IT 24 # 2014 serie IT 34 # print(df.columns) # checking taht colums are ['geo','Value] assert (df.columns == pd.Series(['serie', 'area', 'Value'])).all() # pdt.assert_series_equal(df.columns,pd.Series(['area', 'Value'])) assert 34 == df.loc['2014']['Value']
def test_to_table_inverted_order(): json_pathname = os.path.join(fixture_dir, "dataset", "dataset_unemployment_v1.json") dataset = jsonstat.JsonStatDataSet() dataset.from_file(json_pathname) order = [i.did for i in dataset.dimensions()] order = order[::-1] # reverse list order = dataset._from_aidx_to_adim(order) table = dataset.to_table(order=order) # table len is the size of dataset + 1 for headers assert len(dataset) + 1 == len(table) header_expected = [ 'serie', '2012-2014', 'OECD countries, EU15 and total', 'Value' ] assert header_expected == table[0] first_row_expected = ['serie', '2012', 'Australia', 11] assert first_row_expected == table[1] second_row_expected = ['serie', '2013', 'Australia', 21] assert second_row_expected == table[2]
def test_missing_dimension(json_missing_dimension): dataset = jsonstat.JsonStatDataSet("canada") with pytest.raises(jsonstat.JsonStatMalformedJson) as excinfo: dataset.from_string(json_missing_dimension) expected = "dataset 'canada': missing 'dimension' key" assert expected == str(excinfo.value)
def test_empty_value(json_empty_value): dataset = jsonstat.JsonStatDataSet("canada") with pytest.raises(jsonstat.JsonStatMalformedJson) as excinfo: dataset.from_string(json_empty_value) expected = "dataset 'canada': field 'value' is empty" assert expected == str(excinfo.value)
def test_exception_not_valid(): dataset = jsonstat.JsonStatDataSet("canada") with pytest.raises(jsonstat.JsonStatException): dataset.data(year="2003", area="CA")
def test_exception_dataset_size(json_incorrect_data_size): dataset = jsonstat.JsonStatDataSet("canada") with pytest.raises(jsonstat.JsonStatException) as excinfo: dataset.from_string(json_incorrect_data_size) expected = "dataset 'canada': size 4 is different from calculate size 48 by dimension" assert expected == str(excinfo.value)
def test_name(): dataset = jsonstat.JsonStatDataSet("canada") assert dataset.name == "canada"
def test_dimensions(): json_pathname = os.path.join(fixture_dir, "dataset", "dataset_unemployment_v1.json") dataset = jsonstat.JsonStatDataSet("canada") dataset.from_file(json_pathname) assert len(dataset.dimensions()) == 3