Skip to content

Commit

Permalink
black
Browse files Browse the repository at this point in the history
  • Loading branch information
robe2037 committed Nov 11, 2024
1 parent bf68ce4 commit 7ef31cd
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 55 deletions.
12 changes: 9 additions & 3 deletions src/ipumspy/api/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ def __init__(
self.geographic_extents = geographic_extents
self.breakdown_and_data_type_layout = breakdown_and_data_type_layout
self.tst_layout = tst_layout

self.api_version = (
self.extract_api_version(kwargs)
if len(kwargs.keys()) > 0
Expand Down Expand Up @@ -753,7 +753,7 @@ def build(self) -> Dict[str, Any]:
built["shapefiles"] = [shapefile.name for shapefile in self.shapefiles]

return built


def extract_from_dict(dct: Dict[str, Any]) -> Union[BaseExtract, List[BaseExtract]]:
"""
Expand All @@ -772,7 +772,13 @@ def extract_from_dict(dct: Dict[str, Any]) -> Union[BaseExtract, List[BaseExtrac
return [extract_from_dict(extract) for extract in dct["extracts"]]

def _make_snake_ext(ext_dict):
obj_keys = ["variables", "samples", "timeUseVariables", "datasets", "timeSeriesTables"]
obj_keys = [
"variables",
"samples",
"timeUseVariables",
"datasets",
"timeSeriesTables",
]

for key in ext_dict.keys():
if isinstance(ext_dict[key], dict):
Expand Down
124 changes: 72 additions & 52 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,33 @@ def live_api_client(environment_variables) -> IpumsApiClient:
def simple_nhgis_extract() -> AggregateDataExtract:
extract = AggregateDataExtract(
"nhgis",
description = "Simple extract for ipumspy unit testing",
datasets = [Dataset("1990_STF1", ["NP1"], ["state"])]
description="Simple extract for ipumspy unit testing",
datasets=[Dataset("1990_STF1", ["NP1"], ["state"])],
)

return extract


@pytest.fixture()
def complex_nhgis_extract() -> AggregateDataExtract:
extract = AggregateDataExtract(
"nhgis",
description = "Complex extract for ipumspy unit testing",
datasets = [
description="Complex extract for ipumspy unit testing",
datasets=[
Dataset("2010_SF1a", ["P1", "P2"], ["block"]),
Dataset("2010_SF2a", ["PCT1"], ["state"], breakdown_values = ["bs32.ge89", "bs33.ch002"])
],
time_series_tables = [
TimeSeriesTable("CW3", ["nation", "state"], ["2000"])
Dataset(
"2010_SF2a",
["PCT1"],
["state"],
breakdown_values=["bs32.ge89", "bs33.ch002"],
),
],
shapefiles = ["us_state_1970_tl2000"],
data_format = "csv_header",
geographic_extents = ["010", "020"],
tst_layout = "time_by_row_layout",
breakdown_and_data_type_layout = "separate_files"
time_series_tables=[TimeSeriesTable("CW3", ["nation", "state"], ["2000"])],
shapefiles=["us_state_1970_tl2000"],
data_format="csv_header",
geographic_extents=["010", "020"],
tst_layout="time_by_row_layout",
breakdown_and_data_type_layout="separate_files",
)

return extract
Expand Down Expand Up @@ -385,17 +389,29 @@ def test_nhgis_feature_errors(live_api_client: IpumsApiClient):
"""
extract = AggregateDataExtract(
"nhgis",
datasets = [Dataset("a", "b" ,"c", "d")],
time_series_tables = [TimeSeriesTable("a", "b")]
datasets=[Dataset("a", "b", "c", "d")],
time_series_tables=[TimeSeriesTable("a", "b")],
)

with pytest.raises(BadIpumsApiRequest) as exc_info:
live_api_client.submit_extract(extract)

assert "The property '#/datasets/a/dataTables' of type string did not match the following type: array" in str(exc_info.value)
assert "The property '#/datasets/a/geogLevels' of type string did not match the following type: array" in str(exc_info.value)
assert "The property '#/datasets/a/years' of type string did not match the following type: array" in str(exc_info.value)
assert "The property '#/timeSeriesTables/A/geogLevels' of type string did not match the following type: array" in str(exc_info.value)
assert (
"The property '#/datasets/a/dataTables' of type string did not match the following type: array"
in str(exc_info.value)
)
assert (
"The property '#/datasets/a/geogLevels' of type string did not match the following type: array"
in str(exc_info.value)
)
assert (
"The property '#/datasets/a/years' of type string did not match the following type: array"
in str(exc_info.value)
)
assert (
"The property '#/timeSeriesTables/A/geogLevels' of type string did not match the following type: array"
in str(exc_info.value)
)


def test_cps_build_extract():
Expand Down Expand Up @@ -593,7 +609,10 @@ def test_atus_build_extract():
)


def test_nhgis_build_extract(simple_nhgis_extract: AggregateDataExtract, complex_nhgis_extract: AggregateDataExtract):
def test_nhgis_build_extract(
simple_nhgis_extract: AggregateDataExtract,
complex_nhgis_extract: AggregateDataExtract,
):
"""
Test NHGIS extract build structure
"""
Expand All @@ -607,25 +626,22 @@ def test_nhgis_build_extract(simple_nhgis_extract: AggregateDataExtract, complex
"dataTables": ["P1", "P2"],
"geogLevels": ["block"],
"years": [],
"breakdownValues": []
"breakdownValues": [],
},
"2010_SF2a": {
"dataTables": ["PCT1"],
"geogLevels": ["state"],
"years": [],
"breakdownValues": ["bs32.ge89", "bs33.ch002"]
}
"breakdownValues": ["bs32.ge89", "bs33.ch002"],
},
},
"breakdownAndDataTypeLayout": "separate_files",
"geographicExtents": ["010", "020"],
"timeSeriesTables": {
"CW3": {
"geogLevels": ["nation", "state"],
"years": ["2000"]
}
"CW3": {"geogLevels": ["nation", "state"], "years": ["2000"]}
},
"timeSeriesTableLayout": "time_by_row_layout",
"shapefiles": ["us_state_1970_tl2000"]
"shapefiles": ["us_state_1970_tl2000"],
}

assert simple_nhgis_extract.build() == {
Expand All @@ -638,13 +654,13 @@ def test_nhgis_build_extract(simple_nhgis_extract: AggregateDataExtract, complex
"dataTables": ["NP1"],
"geogLevels": ["state"],
"years": [],
"breakdownValues": []
"breakdownValues": [],
}
},
"breakdownAndDataTypeLayout": "single_file",
"timeSeriesTables": {},
"timeSeriesTableLayout": "time_by_column_layout",
"shapefiles": []
"shapefiles": [],
}


Expand Down Expand Up @@ -893,14 +909,16 @@ def test_nhgis_extract_from_dict(fixtures_path: Path):
for item in extract:
assert item.collection == "nhgis"
assert item.datasets == [
Dataset(name = "1990_STF1", data_tables = ["NP1", "NP2"], geog_levels = ["county"]),
Dataset(name = "2010_SF1a", data_tables = ["P1"], geog_levels = ["state"])
Dataset(
name="1990_STF1", data_tables=["NP1", "NP2"], geog_levels=["county"]
),
Dataset(name="2010_SF1a", data_tables=["P1"], geog_levels=["state"]),
]
assert item.time_series_tables == [
TimeSeriesTable(name = "CW3", geog_levels = ["state"], years = ["1990"])
TimeSeriesTable(name="CW3", geog_levels=["state"], years=["1990"])
]
assert item.tst_layout == "time_by_column_layout"
assert item.shapefiles == [Shapefile(name = "us_state_1790_tl2000")]
assert item.shapefiles == [Shapefile(name="us_state_1790_tl2000")]
assert item.data_format == "csv_header"
assert item.api_version == 2

Expand Down Expand Up @@ -940,24 +958,21 @@ def test_nhgis_extract_to_dict(fixtures_path: Path):

assert dct["collection"] == "nhgis"
assert dct["datasets"] == {
'2010_SF1a': {
'dataTables': ['P1'],
'geogLevels': ['state'],
'years': [],
'breakdownValues': ['bs32.ge00']
"2010_SF1a": {
"dataTables": ["P1"],
"geogLevels": ["state"],
"years": [],
"breakdownValues": ["bs32.ge00"],
},
'1990_STF1': {
'dataTables': ['NP1', 'NP2'],
'geogLevels': ['county'],
'years': [],
'breakdownValues': ['bs09.ge00']
"1990_STF1": {
"dataTables": ["NP1", "NP2"],
"geogLevels": ["county"],
"years": [],
"breakdownValues": ["bs09.ge00"],
},
}
assert dct["timeSeriesTables"] == {
"CW3": {
"geogLevels": ["state"],
"years": ["1990"]
}
"CW3": {"geogLevels": ["state"], "years": ["1990"]}
}
assert dct["version"] == 2
assert dct["shapefiles"] == ["us_state_1790_tl2000"]
Expand All @@ -981,7 +996,9 @@ def test_submit_extract_live(live_api_client: IpumsApiClient):


@pytest.mark.vcr
def test_nhgis_submit_extract_live(live_api_client: IpumsApiClient, simple_nhgis_extract: AggregateDataExtract):
def test_nhgis_submit_extract_live(
live_api_client: IpumsApiClient, simple_nhgis_extract: AggregateDataExtract
):
"""
Test that NHGIS extracts can be submitted
"""
Expand All @@ -1003,7 +1020,7 @@ def test_submit_extract_dict(live_api_client: IpumsApiClient):
"version": 2,
"samples": {"us2017a": {}},
"variables": {"AGE": {}},
"data_structure": {"hierarchical": {}}
"data_structure": {"hierarchical": {}},
}

extract = live_api_client.submit_extract(extract_dict)
Expand All @@ -1013,6 +1030,7 @@ def test_submit_extract_dict(live_api_client: IpumsApiClient):
assert Sample("us2017a") in extract.samples
assert extract_dict["data_structure"] == extract.data_structure


@pytest.mark.vcr
def test_submit_hierarchical_extract_live(live_api_client: IpumsApiClient):
"""
Expand Down Expand Up @@ -1096,7 +1114,10 @@ def test_download_extract_r(live_api_client: IpumsApiClient, tmpdir: Path):


@pytest.mark.vcr
def test_nhgis_download_extract(live_api_client: IpumsApiClient, tmpdir: Path,):
def test_nhgis_download_extract(
live_api_client: IpumsApiClient,
tmpdir: Path,
):
"""
Test that NHGIS extract files can be downloaded
"""
Expand Down Expand Up @@ -1703,4 +1724,3 @@ def test_get_pages(live_api_client: IpumsApiClient):
live_api_client._get_pages(collection="usa", endpoint="extracts", page_size=5)
)
assert len(page1["data"]) == 5

0 comments on commit 7ef31cd

Please sign in to comment.