From 91846a66f9987eedbe3ac9cdf7a3386ecb20fcca Mon Sep 17 00:00:00 2001 From: Piero Date: Tue, 21 Nov 2023 14:14:02 +0100 Subject: [PATCH] test and feat: tests written to check if each column with a timestamp is returning the same timestamp string without the character Z. Also the code was adjusted to remove the character z in the timestamp string --- ohsome/response.py | 10 ++++++ ohsome/test/test_response.py | 66 +++++++++++++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/ohsome/response.py b/ohsome/response.py index 98701d7..c308df7 100644 --- a/ohsome/response.py +++ b/ohsome/response.py @@ -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" ) @@ -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" ) @@ -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" ) @@ -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" ) diff --git a/ohsome/test/test_response.py b/ohsome/test/test_response.py index e5c453d..50cacbc 100644 --- a/ohsome/test/test_response.py +++ b/ohsome/test/test_response.py @@ -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" @@ -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