Skip to content

Commit 97ee437

Browse files
authored
fix(response): assert tags are exploded even if not present in the response (#159)
by None-filling them closes #149
1 parent 435066d commit 97ee437

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

ohsome/response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def _as_geodataframe(
9494
for feature in self.data["features"]:
9595
properties = feature["properties"]
9696
tags = {}
97-
new_properties = {}
97+
new_properties = {k: None for k in explode_tags}
9898
for k in properties.keys():
9999
if (
100100
(k.startswith("@"))

ohsome/test/conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
import logging
55
from unittest.mock import patch, PropertyMock
66

7+
import geopandas as gpd
78
import pytest
9+
from requests import Response
10+
from shapely import Point
811
from urllib3 import Retry
912

1013
import ohsome
14+
from ohsome import OhsomeResponse
1115

1216
logger = logging.getLogger(__name__)
1317

@@ -95,3 +99,21 @@ def vcr_config():
9599
"headers",
96100
]
97101
}
102+
103+
104+
@pytest.fixture()
105+
def dummy_ohsome_response() -> OhsomeResponse:
106+
"""Mocked ohsome response with a single point geometry."""
107+
test_gdf = gpd.GeoDataFrame(
108+
data={
109+
"highway": ["primary"],
110+
"width": ["10"],
111+
"@snapshotTimestamp": ["2024-01-01"],
112+
"@osmId": ["node/1234"],
113+
},
114+
geometry=[Point(0, 0)],
115+
crs="EPSG:4326",
116+
)
117+
response = Response()
118+
response._content = test_gdf.to_json().encode()
119+
return OhsomeResponse(response=response)

ohsome/test/test_response.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
# -*- coding: utf-8 -*-
33
"""Tests for ohsome API response"""
44
import warnings
5+
from datetime import datetime
56

67
import geopandas as gpd
78
import pandas as pd
89
import pytest
10+
from geopandas.testing import assert_geodataframe_equal
11+
from shapely import Point
912

1013

1114
@pytest.mark.vcr
@@ -463,3 +466,80 @@ def test_all_columns_with_timestamps_to_be_without_timezone(base_client):
463466
assert at_timestamp.tz is None
464467
assert timestamp.tz is None
465468
assert at_snapshotTimestamp.tz is None
469+
470+
471+
def test_explode_tags(dummy_ohsome_response):
472+
"""Test if the explode_tags parameter explodes tags."""
473+
expected_df = gpd.GeoDataFrame(
474+
data={
475+
"highway": ["primary"],
476+
"@other_tags": [{"width": "10"}],
477+
"@snapshotTimestamp": [datetime(2024, 1, 1)],
478+
"@osmId": ["node/1234"],
479+
},
480+
geometry=[Point(0, 0)],
481+
crs="EPSG:4326",
482+
)
483+
484+
computed_df = dummy_ohsome_response.as_dataframe(
485+
explode_tags=("highway",), multi_index=False
486+
)
487+
488+
assert_geodataframe_equal(computed_df, expected_df, check_like=True)
489+
490+
491+
def test_explode_tags_none(dummy_ohsome_response):
492+
"""Test if the explode_tags parameter can be set to explode all (to get previous behaviour)."""
493+
expected_df = gpd.GeoDataFrame(
494+
data={
495+
"highway": ["primary"],
496+
"width": ["10"],
497+
"@snapshotTimestamp": [datetime(2024, 1, 1)],
498+
"@osmId": ["node/1234"],
499+
},
500+
geometry=[Point(0, 0)],
501+
crs="EPSG:4326",
502+
)
503+
504+
computed_df = dummy_ohsome_response.as_dataframe(
505+
explode_tags=None, multi_index=False
506+
)
507+
508+
assert_geodataframe_equal(computed_df, expected_df, check_like=True)
509+
510+
511+
def test_explode_tags_empy(dummy_ohsome_response):
512+
"""Test if explode_tags parameter can be disabled."""
513+
expected_df = gpd.GeoDataFrame(
514+
data={
515+
"@other_tags": [{"width": "10", "highway": "primary"}],
516+
"@snapshotTimestamp": [datetime(2024, 1, 1)],
517+
"@osmId": ["node/1234"],
518+
},
519+
geometry=[Point(0, 0)],
520+
crs="EPSG:4326",
521+
)
522+
523+
computed_df = dummy_ohsome_response.as_dataframe(explode_tags=(), multi_index=False)
524+
525+
assert_geodataframe_equal(computed_df, expected_df, check_like=True)
526+
527+
528+
def test_explode_tags_missing_in_response(dummy_ohsome_response):
529+
"""Test if the explode_tags keys are always present in the result, even if they are not part of the response."""
530+
expected_df = gpd.GeoDataFrame(
531+
data={
532+
"this_key_does_not_exist": [None],
533+
"@other_tags": [{"width": "10", "highway": "primary"}],
534+
"@snapshotTimestamp": [datetime(2024, 1, 1)],
535+
"@osmId": ["node/1234"],
536+
},
537+
geometry=[Point(0, 0)],
538+
crs="EPSG:4326",
539+
)
540+
541+
computed_df = dummy_ohsome_response.as_dataframe(
542+
explode_tags=("this_key_does_not_exist",), multi_index=False
543+
)
544+
545+
assert_geodataframe_equal(computed_df, expected_df, check_like=True)

0 commit comments

Comments
 (0)