Skip to content

Commit af13807

Browse files
authored
fix(response): assert expected columns are present even on empty output (#165)
1 parent 260ed02 commit af13807

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixed
6+
7+
- Assert that the expected columns are also present if the result is empty
8+
59
## 0.3.2
610

711
### Fixed

ohsome/response.py

+15-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""Class for ohsome API response"""
55

66
import json
7-
from typing import Optional
7+
from typing import Optional, Union
88

99
import geopandas as gpd
1010
import pandas as pd
@@ -25,7 +25,7 @@ def __init__(self, response=None, url=None, params=None):
2525

2626
def as_dataframe(
2727
self, multi_index: Optional[bool] = True, explode_tags: Optional[tuple] = ()
28-
):
28+
) -> Union[pd.DataFrame, gpd.GeoDataFrame]:
2929
"""
3030
Converts the ohsome response to a pandas.DataFrame or a geopandas.GeoDataFrame if the
3131
response contains geometries
@@ -40,13 +40,7 @@ def as_dataframe(
4040
else:
4141
return self._as_geodataframe(multi_index, explode_tags)
4242

43-
def _as_dataframe(self, multi_index=True):
44-
"""
45-
Converts the ohsome response to a pandas.DataFrame
46-
:param multi_index: If true returns the dataframe with a multi index
47-
:return: pandas.DataFrame
48-
"""
49-
43+
def _as_dataframe(self, multi_index=True) -> pd.DataFrame:
5044
groupby_names = []
5145
if "result" in self.data.keys():
5246
result_df = pd.DataFrame().from_records(self.data["result"])
@@ -79,15 +73,17 @@ def _as_dataframe(self, multi_index=True):
7973

8074
def _as_geodataframe(
8175
self, multi_index: Optional[bool] = True, explode_tags: Optional[tuple] = ()
82-
):
83-
"""
84-
Converts the ohsome response to a geopandas.GeoDataFrame
85-
:param multi_index: If true returns the dataframe with a multi index
86-
:return: geopandas.GeoDataFrame
87-
"""
88-
76+
) -> gpd.GeoDataFrame:
8977
if len(self.data["features"]) == 0:
90-
return gpd.GeoDataFrame(crs="epsg:4326", columns=["@osmId", "geometry"])
78+
return gpd.GeoDataFrame(
79+
crs="epsg:4326",
80+
columns=["@osmId", "geometry"]
81+
+ (
82+
list(explode_tags) + ["@other_tags"]
83+
if explode_tags is not None
84+
else []
85+
),
86+
)
9187

9288
try:
9389
if explode_tags is not None:
@@ -128,7 +124,7 @@ def _as_geodataframe(
128124

129125
return features.sort_index()
130126

131-
def to_json(self, outfile):
127+
def to_json(self, outfile) -> None:
132128
"""
133129
Write response to json file
134130
:return:
@@ -137,7 +133,7 @@ def to_json(self, outfile):
137133
with open(outfile, "w", encoding="utf-8") as dst:
138134
json.dump(self.data, dst, indent=2, ensure_ascii=False)
139135

140-
def _set_index(self, result_df, groupby_names):
136+
def _set_index(self, result_df, groupby_names) -> None:
141137
"""
142138
Set multi-index based on groupby names and time
143139
:param result_df:

ohsome/test/test_response.py

+23
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
import pandas as pd
99
import pytest
1010
from geopandas.testing import assert_geodataframe_equal
11+
from requests import Response
1112
from shapely import Point
1213

14+
from ohsome import OhsomeResponse
15+
1316

1417
@pytest.mark.vcr
1518
def test_elements_count(base_client):
@@ -406,6 +409,7 @@ def test_empty_geodataframe(base_client):
406409

407410
assert isinstance(result, gpd.GeoDataFrame)
408411
assert result.empty
412+
assert result.columns.to_list() == ["@osmId", "geometry", "@other_tags"]
409413

410414

411415
@pytest.mark.vcr
@@ -543,3 +547,22 @@ def test_explode_tags_missing_in_response(dummy_ohsome_response):
543547
)
544548

545549
assert_geodataframe_equal(computed_df, expected_df, check_like=True)
550+
551+
552+
def test_explode_tags_present_on_empty_result():
553+
"""Test if exploded tags are present in an empty results."""
554+
expected_df = gpd.GeoDataFrame(
555+
columns=["@osmId", "some_key", "some_other_key", "@other_tags", "geometry"],
556+
crs="EPSG:4326",
557+
)
558+
559+
response = Response()
560+
response._content = (
561+
'{"attribution":{"url":"https://ohsome.org/copyrights","text":"© OpenStreetMap contributors"},'
562+
'"apiVersion":"1.10.1","type":"FeatureCollection","features":[]}'
563+
).encode()
564+
computed_df = OhsomeResponse(response=response).as_dataframe(
565+
explode_tags=("some_key", "some_other_key")
566+
)
567+
568+
assert_geodataframe_equal(computed_df, expected_df, check_like=True)

0 commit comments

Comments
 (0)