Skip to content

Commit

Permalink
fix(test): prevent tests from failing at random timestamps
Browse files Browse the repository at this point in the history
Co-authored-by: Maciej Adamiak <[email protected]>
  • Loading branch information
SlowMo24 and maciej-adamiak authored Feb 21, 2024
1 parent 289f4c5 commit 4104b70
Show file tree
Hide file tree
Showing 6 changed files with 5,647 additions and 41 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## Unreleased

### Changed

- improved and sped up testing (first steps towards [#139](https://github.com/GIScience/ohsome-py/issues/139))
- move metadata property from singleton to `chached_property`

### Fixed

- removed time-dependency of unit tests that would cause them to fail at any time after the cassettes were recorded

## 0.3.0

### Added
Expand Down
37 changes: 11 additions & 26 deletions ohsome/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""OhsomeClient classes to build and handle requests to ohsome API"""
import datetime as dt
import json
from functools import cached_property
from pathlib import Path
from typing import Union, Optional, List
from urllib.parse import urljoin
Expand Down Expand Up @@ -129,8 +130,7 @@ def __init__(
base_api_url, log, log_dir, cache, user_agent, retry
)
self._parameters = None
self._metadata = None
self._url = None
self._metadata_url = f"{self.base_api_url}metadata"

@property
def base_api_url(self):
Expand All @@ -142,12 +142,8 @@ def start_timestamp(self):
Returns the temporal extent of the current ohsome API
:return:
"""
if self._metadata is None:
self._query_metadata()
return dt.datetime.fromisoformat(
self._metadata["extractRegion"]["temporalExtent"]["fromTimestamp"].strip(
"Z"
)
self.metadata["extractRegion"]["temporalExtent"]["fromTimestamp"].strip("Z")
)

@property
Expand All @@ -156,10 +152,8 @@ def end_timestamp(self):
Returns the temporal extent of the current ohsome API
:return:
"""
if self._metadata is None:
self._query_metadata()
return dt.datetime.fromisoformat(
self._metadata["extractRegion"]["temporalExtent"]["toTimestamp"].strip("Z")
self.metadata["extractRegion"]["temporalExtent"]["toTimestamp"].strip("Z")
)

@property
Expand All @@ -168,41 +162,33 @@ def api_version(self):
Returns the version of the ohsome API
:return:
"""
if self._metadata is None:
self._query_metadata()
return self._metadata["apiVersion"]
return self.metadata["apiVersion"]

@property
@cached_property
def metadata(self):
if self._metadata is None:
self._query_metadata()
return self._metadata

def _query_metadata(self):
"""
Send ohsome GET request
:return:
"""
self._url = self._base_api_url + "metadata"
try:
response = self._session().get(self._url)
response = self._session().get(self._metadata_url)
response.raise_for_status()
except requests.exceptions.ConnectionError:
raise OhsomeException(
message="Connection Error: Query could not be sent. Make sure there are no network "
f"problems and that the ohsome API URL {self._url} is valid.",
url=self._url,
f"problems and that the ohsome API URL {self._metadata_url} is valid.",
url=self._metadata_url,
params=self._parameters,
)
except requests.exceptions.HTTPError as e:
raise OhsomeException(
message=e.response.json()["message"],
url=self._url,
url=self._metadata_url,
params=self._parameters,
error_code=e.response.status_code,
)
else:
self._metadata = response.json()
return response.json()


class _OhsomePostClient(_OhsomeBaseClient):
Expand Down Expand Up @@ -232,7 +218,6 @@ def __init__(
base_api_url, log, log_dir, cache, user_agent, retry
)
self._parameters = None
self._metadata = None
self._url = None

def post(
Expand Down
Loading

0 comments on commit 4104b70

Please sign in to comment.