Skip to content

Commit

Permalink
test and feat: tests written to check if each column with a timestamp…
Browse files Browse the repository at this point in the history
… is returning the same timestamp string without the character Z. Also the code was adjusted to remove the character z in the timestamp string
  • Loading branch information
rp280 committed Nov 21, 2023
1 parent de01d70 commit 91846a6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
10 changes: 10 additions & 0 deletions ohsome/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def _as_geodataframe(self, multi_index=True):
)

if "@validFrom" in features.columns:
features["@validFrom"] = features["@validFrom"].str.replace("Z", "")
features["@validTo"] = features["@validTo"].str.replace("Z", "")
features["@validFrom"] = pd.to_datetime(
features["@validFrom"], format="ISO8601"
)
Expand All @@ -94,6 +96,9 @@ def _as_geodataframe(self, multi_index=True):
if multi_index:
features = features.set_index(["@osmId", "@validFrom", "@validTo"])
elif "@snapshotTimestamp" in features.columns:
features["@snapshotTimestamp"] = features["@snapshotTimestamp"].str.replace(
"Z", ""
)
features["@snapshotTimestamp"] = pd.to_datetime(
features["@snapshotTimestamp"], format="ISO8601"
)
Expand All @@ -102,12 +107,14 @@ def _as_geodataframe(self, multi_index=True):
elif (
"timestamp" in features.columns and "groupByBoundaryId" in features.columns
):
features["timestamp"] = features["timestamp"].str.replace("Z", "")
features["timestamp"] = pd.to_datetime(
features["timestamp"], format="ISO8601"
)
if multi_index:
features = features.set_index(["groupByBoundaryId", "timestamp"])
elif "@timestamp" in features.columns:
features["@timestamp"] = features["@timestamp"].str.replace("Z", "")
features["@timestamp"] = pd.to_datetime(
features["@timestamp"], format="ISO8601"
)
Expand Down Expand Up @@ -173,13 +180,16 @@ def _format_timestamp(self, result_df: DataFrame) -> None:
:return:
"""
if "timestamp" in result_df.columns:
result_df["timestamp"] = result_df["timestamp"].str.replace("Z", "")
result_df["timestamp"] = pd.to_datetime(
result_df["timestamp"], format="ISO8601"
)
else:
result_df["fromTimestamp"] = result_df["fromTimestamp"].str.replace("Z", "")
result_df["fromTimestamp"] = pd.to_datetime(
result_df["fromTimestamp"], format="ISO8601"
)
result_df["toTimestamp"] = result_df["toTimestamp"].str.replace("Z", "")
result_df["toTimestamp"] = pd.to_datetime(
result_df["toTimestamp"], format="ISO8601"
)
66 changes: 62 additions & 4 deletions ohsome/test/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def test_empty_geodataframe(base_client):


def test_check_timestamp_groupBy_boundary_and_geometry_for_user(base_client):
"""Tests whether the format of count.groupBy.Boundary is a timestamp format with timezone"""
"""Tests whether the format of count.groupBy.Boundary and elements.geometry is a timestamp format without timezone"""

bbox = "8.67,49.39,8.71,49.42"
time = "2008-01-01/2023-01-01/P1Y"
Expand All @@ -376,11 +376,69 @@ def test_check_timestamp_groupBy_boundary_and_geometry_for_user(base_client):
bboxes=bbox, time=time, filter=fltr
)
result_groupBy = response_groupBy.as_dataframe().index.levels[1][0]

response_geometry = client.elements.geometry.post(
bboxes=bbox, time=time, filter=fltr
)
result_geometry = response_geometry.as_dataframe().index.levels[1][0]

assert result_groupBy.tz == dt.timezone.utc
assert result_geometry.tz == dt.timezone.utc
assert result_groupBy.tz is None
assert result_geometry.tz is None


def test_all_columns_with_timestamps_to_be_without_timezone(base_client):
"""Test whether all the columns with timestamp like 'timestamp', '@timestamp','@validFrom', '@validTo',
'fromTimestamp', 'toTimestamp' and '@snapshotTimestamp' are without timezone
"""
bbox = "8.67,49.39,8.71,49.42"
time = "2008-01-01/2023-01-01/P1Y"
time2iso = "2020-02-01,2020-06-29"
fltr = "amenity=cafe and type:node"
client = base_client

fromTimestamp = (
client.contributions.count.density.post(time=time, bboxes=bbox, filter=fltr)
.as_dataframe()
.index.levels[0][0]
)
toTimestamp = (
client.contributions.count.density.post(time=time, bboxes=bbox, filter=fltr)
.as_dataframe()
.index.levels[0][1]
)
at_validFrom = (
client.elementsFullHistory.geometry.post(
time=time2iso, bboxes=bbox, filter=fltr
)
.as_dataframe()
.index.levels[1][0]
)
at_validTo = (
client.elementsFullHistory.geometry.post(
time=time2iso, bboxes=bbox, filter=fltr
)
.as_dataframe()
.index.levels[2][0]
)
at_timestamp = (
client.contributions.geometry.post(time=time2iso, bboxes=bbox, filter=fltr)
.as_dataframe()
.index[0]
)
timestamp = (
client.elements.count.groupByBoundary.post(bboxes=bbox, time=time, filter=fltr)
.as_dataframe()
.index.levels[1][0]
)
at_snapshotTimestamp = (
client.elements.geometry.post(bboxes=bbox, time=time, filter=fltr)
.as_dataframe()
.index.levels[1][0]
)

assert fromTimestamp.tz is None
assert toTimestamp.tz is None
assert at_validFrom.tz is None
assert at_validTo.tz is None
assert at_timestamp.tz is None
assert timestamp.tz is None
assert at_snapshotTimestamp.tz is None

0 comments on commit 91846a6

Please sign in to comment.