Skip to content

Commit

Permalink
build(deps-dev): bump notebook from 7.0.6 to 7.0.7 (#142)
Browse files Browse the repository at this point in the history
* build(deps-dev): bump notebook from 7.0.6 to 7.0.7

Bumps [notebook](https://github.com/jupyter/notebook) from 7.0.6 to 7.0.7.
- [Release notes](https://github.com/jupyter/notebook/releases)
- [Changelog](https://github.com/jupyter/notebook/blob/@jupyter-notebook/[email protected]/CHANGELOG.md)
- [Commits](https://github.com/jupyter/notebook/compare/@jupyter-notebook/[email protected]...@jupyter-notebook/[email protected])

---
updated-dependencies:
- dependency-name: notebook
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>

* build(deps-dev): bump jupyterlab from 4.0.8 to 4.0.11 (#143)

* fix(test): prevent tests from failing at random timestamps


Co-authored-by: Maciej Adamiak <[email protected]>

* dependency(urllib3): relax dependency requirement

* fix(BaseClient): prevent raising an error if None is passed to the log_dir

* build(deps-dev): bump jupyterlab from 4.0.8 to 4.0.11

Bumps [jupyterlab](https://github.com/jupyterlab/jupyterlab) from 4.0.8 to 4.0.11.
- [Release notes](https://github.com/jupyterlab/jupyterlab/releases)
- [Changelog](https://github.com/jupyterlab/jupyterlab/blob/@jupyterlab/[email protected]/CHANGELOG.md)
- [Commits](https://github.com/jupyterlab/jupyterlab/compare/@jupyterlab/[email protected]...@jupyterlab/[email protected])

---
updated-dependencies:
- dependency-name: jupyterlab
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Moritz Schott <[email protected]>
Co-authored-by: Maciej Adamiak <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Moritz Schott <[email protected]>
Co-authored-by: Maciej Adamiak <[email protected]>
  • Loading branch information
3 people authored Feb 21, 2024
1 parent e0f4ed3 commit afaf384
Show file tree
Hide file tree
Showing 8 changed files with 5,673 additions and 51 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## Unreleased

### Fixed

- prevent an exception if the `log_dir` for the `OhsomeClient` was set to `None`
- removed time-dependency of unit tests that would cause them to fail at any time after the cassettes were recorded

### Changed

- relaxed dependency requirement for `urllib3` to >=2.0.2 to prevent ohsome-py from becoming a 'diamond-dependency'
- 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`

## 0.3.0

### Added
Expand Down
39 changes: 12 additions & 27 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 @@ -55,7 +56,7 @@ def __init__(
RetryError by the underlying library.
"""
self.log = log
self.log_dir = Path(log_dir)
self.log_dir = Path(log_dir or DEFAULT_LOG_DIR)
if self.log:
self.log_dir.mkdir(parents=True, exist_ok=True)
if base_api_url is not None:
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

Large diffs are not rendered by default.

72 changes: 59 additions & 13 deletions ohsome/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,82 @@
# -*- coding: utf-8 -*-
"""Conftest for shared pytest fixtures"""
import logging
from unittest.mock import patch, PropertyMock

import pytest
from urllib3 import Retry

import ohsome

logger = logging.getLogger(__name__)


@pytest.fixture(scope="session")
def base_client(tmpdir_factory):
@pytest.fixture
def mocked_metadata():
"""A default metadata dictionary.
@return:
"""
return {
"attribution": {
"url": "https://ohsome.org/copyrights",
"text": "© OpenStreetMap contributors",
},
"apiVersion": "1.10.1",
"timeout": 600.0,
"extractRegion": {
"spatialExtent": {
"type": "Polygon",
"coordinates": [
[
[-180.0, -90.0],
[180.0, -90.0],
[180.0, 90.0],
[-180.0, 90.0],
[-180.0, -90.0],
]
],
},
"temporalExtent": {
"fromTimestamp": "2007-10-08T00:00:00Z",
"toTimestamp": "2023-11-25T13:00:00Z",
},
"replicationSequenceNumber": 99919,
},
}


@pytest.fixture
def base_client(mocked_metadata, tmpdir_factory):
"""Session-wide test client."""
temp_directory = tmpdir_factory.mktemp("base_client").mkdir("logs").strpath
client = ohsome.OhsomeClient(log_dir=temp_directory)
assert client.metadata # call metadata once to ensure it is cached
yield client
with patch(
"ohsome.clients._OhsomeInfoClient.metadata",
new_callable=PropertyMock,
return_value=mocked_metadata,
):
client = ohsome.OhsomeClient(log_dir=temp_directory)
yield client


@pytest.fixture(scope="session")
def base_client_without_log():
@pytest.fixture
def base_client_without_log(mocked_metadata):
"""Session-wide test client."""
client = ohsome.OhsomeClient(log=False)
assert client.metadata # call metadata once to ensure it is cached
yield client
with patch(
"ohsome.clients._OhsomeInfoClient.metadata",
new_callable=PropertyMock,
return_value=mocked_metadata,
):
client = ohsome.OhsomeClient(log=False)
yield client


@pytest.fixture(scope="session")
@pytest.fixture
def custom_client_with_wrong_url(tmpdir_factory):
"""Session-wide test client."""
temp_directory = tmpdir_factory.mktemp("base_client").mkdir("logs").strpath
client = ohsome.OhsomeClient(
base_api_url="https://imwrong",
log_dir=temp_directory,
base_api_url="https://imwrong", log_dir=temp_directory, retry=Retry(total=0)
)
yield client

Expand Down
15 changes: 14 additions & 1 deletion ohsome/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pytest

import ohsome
from ohsome import OhsomeClient
from ohsome.constants import OHSOME_VERSION

script_path = os.path.dirname(os.path.realpath(__file__))
Expand Down Expand Up @@ -39,7 +40,7 @@ def test_user_agent(base_client):
Checks user agent set by ohsome-py
:return:
"""
resp = base_client._session().get(base_client._url)
resp = base_client._session().get(base_client._metadata_url)
used_user_agent = resp.request.headers["user-agent"]
assert used_user_agent == f"ohsome-py/{OHSOME_VERSION}"

Expand Down Expand Up @@ -318,3 +319,15 @@ def test_post_with_endpoint_string(base_client):

assert isinstance(result, gpd.GeoDataFrame)
assert len(result) == 1


def test_none_init():
"""Test if the input parameters can set to None explicitly."""
assert OhsomeClient(
base_api_url=None,
log=None,
log_dir=None,
cache=None,
user_agent=None,
retry=None,
)
2 changes: 1 addition & 1 deletion ohsome/test/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import geopandas as gpd
import pandas as pd
import pytest
import datetime as dt


@pytest.mark.vcr
Expand Down Expand Up @@ -406,6 +405,7 @@ def test_empty_geodataframe(base_client):
assert result.empty


@pytest.mark.vcr
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
Expand Down
17 changes: 9 additions & 8 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ requests = "^2.25.1"
pandas = "^2.1.3"
numpy = "^1.20.0"
geopandas = "^0.14.1"
urllib3 = "^2.1.0"
urllib3 = "^2.0.2"
curlify2 = "^2.0.0"

[tool.poetry.group.test.dependencies]
Expand Down

0 comments on commit afaf384

Please sign in to comment.