Skip to content

Commit

Permalink
fixup! Issue #114/#211/#197 tests for load_geojson and its `propert…
Browse files Browse the repository at this point in the history
…ies` arg
  • Loading branch information
soxofaan committed Aug 4, 2023
1 parent 38d98f5 commit 93aebdd
Showing 1 changed file with 112 additions and 42 deletions.
154 changes: 112 additions & 42 deletions tests/test_vectorcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,53 +331,123 @@ def test_from_geodataframe_default(self, gdf):
assert {k: list(v.values) for k, v in cube.coords.items()} == {"geometries": [0, 1], "properties": ["pop"]}

@pytest.mark.parametrize(
["columns_for_cube", "expected"],
["columns_for_cube", "expected_cube"],
[
("numerical", {"shape": (2, 1), "coords": {"geometries": [0, 1], "properties": ["pop"]}}),
("all", {"shape": (2, 2), "coords": {"geometries": [0, 1], "properties": ["id", "pop"]}}),
(
"numerical",
{
"name": None,
"dims": ("geometries", "properties"),
"coords": {
"geometries": {"attrs": {}, "data": [0, 1], "dims": ("geometries",)},
"properties": {"attrs": {}, "data": ["pop"], "dims": ("properties",)},
},
"data": [[1234], [5678]],
"attrs": {},
},
),
(
"all",
{
"name": None,
"dims": ("geometries", "properties"),
"coords": {
"geometries": {"attrs": {}, "data": [0, 1], "dims": ("geometries",)},
"properties": {"attrs": {}, "data": ["id", "pop"], "dims": ("properties",)},
},
"data": [["first", 1234], ["second", 5678]],
"attrs": {},
},
),
([], None),
(["id"], {"shape": (2, 1), "coords": {"geometries": [0, 1], "properties": ["id"]}}),
(["pop", "id"], {"shape": (2, 2), "coords": {"geometries": [0, 1], "properties": ["pop", "id"]}}),
# TODO: test specifying non-existent column (to be filled with no-data):
# (["pop", "nopenope"], {"shape": (2, 2), "coords": {"geometries": [0, 1], "properties": ["pop", "nopenope"]}}),
(
["id"],
{
"name": None,
"dims": ("geometries", "properties"),
"coords": {
"geometries": {"attrs": {}, "data": [0, 1], "dims": ("geometries",)},
"properties": {"attrs": {}, "data": ["id"], "dims": ("properties",)},
},
"data": [["first"], ["second"]],
"attrs": {},
},
),
(
["pop", "id"],
{
"name": None,
"dims": ("geometries", "properties"),
"coords": {
"geometries": {"attrs": {}, "data": [0, 1], "dims": ("geometries",)},
"properties": {"attrs": {}, "data": ["pop", "id"], "dims": ("properties",)},
},
"data": [[1234, "first"], [5678, "second"]],
"attrs": {},
},
),
(
["color"],
{
"name": None,
"dims": ("geometries", "properties"),
"coords": {
"geometries": {"attrs": {}, "data": [0, 1], "dims": ("geometries",)},
"properties": {"attrs": {}, "data": ["color"], "dims": ("properties",)},
},
"data": [[IsNan()], [IsNan()]],
"attrs": {},
},
),
(
["pop", "color"],
{
"name": None,
"dims": ("geometries", "properties"),
"coords": {
"geometries": {"attrs": {}, "data": [0, 1], "dims": ("geometries",)},
"properties": {"attrs": {}, "data": ["pop", "color"], "dims": ("properties",)},
},
"data": [[1234, IsNan()], [5678, IsNan()]],
"attrs": {},
},
),
],
)
def test_from_geodataframe_columns_for_cube(self, gdf, columns_for_cube, expected):
def test_from_geodataframe_columns_for_cube(self, gdf, columns_for_cube, expected_cube):
vc = DriverVectorCube.from_geodataframe(gdf, columns_for_cube=columns_for_cube)
assert vc.to_geojson() == DictSubSet(
{
"type": "FeatureCollection",
"features": [
DictSubSet(
{
"type": "Feature",
"properties": {"id": "first", "pop": 1234},
"geometry": {
"coordinates": (((1.0, 1.0), (3.0, 1.0), (2.0, 3.0), (1.0, 1.0)),),
"type": "Polygon",
},
}
),
DictSubSet(
{
"type": "Feature",
"properties": {"id": "second", "pop": 5678},
"geometry": {
"coordinates": (((4.0, 2.0), (5.0, 4.0), (3.0, 4.0), (4.0, 2.0)),),
"type": "Polygon",
},
}
),
],
}
)
cube = vc.get_cube()
if expected is None:
assert cube is None
else:
assert cube.dims == ("geometries", "properties")
assert cube.shape == expected["shape"]
assert {k: list(v.values) for k, v in cube.coords.items()} == expected["coords"]

assert vc.to_internal_json() == {
"geometries": DictSubSet(
{
"type": "FeatureCollection",
"features": [
DictSubSet(
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": (((1.0, 1.0), (3.0, 1.0), (2.0, 3.0), (1.0, 1.0)),),
},
"properties": {"id": "first", "pop": 1234},
}
),
DictSubSet(
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": (((4.0, 2.0), (5.0, 4.0), (3.0, 4.0), (4.0, 2.0)),),
},
"properties": {"id": "second", "pop": 5678},
}
),
],
}
),
"cube": expected_cube,
}


@pytest.mark.parametrize(["geojson", "expected"], [
(
Expand Down

0 comments on commit 93aebdd

Please sign in to comment.