Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kharude, Sachin <sachin.kharude@here.com>
  • Loading branch information
Kharude, Sachin committed Jul 28, 2021
1 parent 9d2057a commit f37b32c
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/iml/data/dummy_credentials.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
here.user.id = dummy_user_id
here.client.id = dummy_client_id
here.access.key.id = dummy_access_key_id
here.access.key.secret = dummy_access_key_secret
here.token.endpoint.url = dummy_token_endpoint
51 changes: 51 additions & 0 deletions tests/iml/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright (C) 2019-2021 HERE Europe B.V.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# License-Filename: LICENSE
"""Test api module."""
import pytest

from tests.iml.conftest import get_mock_response
from xyzspaces.iml.apis.api import Api
from xyzspaces.iml.exceptions import (
AuthenticationException,
PayloadTooLargeException,
RequestEntityTooLargeException,
TooManyRequestsException,
)


def test_raise_response_exception():
reason = "This is mock reason"
text = "This is mock text"
mock_response = get_mock_response(513, reason, text)
with pytest.raises(PayloadTooLargeException):
Api.raise_response_exception(mock_response)

mock_response = get_mock_response(401, reason, text)
with pytest.raises(AuthenticationException):
Api.raise_response_exception(mock_response)

mock_response = get_mock_response(413, reason, text)
with pytest.raises(RequestEntityTooLargeException):
Api.raise_response_exception(mock_response)

mock_response = get_mock_response(400, reason, text)
with pytest.raises(Exception):
Api.raise_response_exception(mock_response)

mock_response = get_mock_response(429, reason, text)
with pytest.raises(TooManyRequestsException):
Api.raise_response_exception(mock_response)
40 changes: 40 additions & 0 deletions tests/iml/test_credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (C) 2019-2021 HERE Europe B.V.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# License-Filename: LICENSE
"""Test credentials module."""
import tempfile
from pathlib import Path

import pytest

from xyzspaces.iml.credentials import Credentials
from xyzspaces.iml.exceptions import ConfigException


def test_from_credentials_file():
"""Test credentials from file."""
file_path = (
Path(__file__).parent / Path("data") / Path("dummy_credentials.properties")
)
cred = Credentials.from_credentials_file(file_path)
assert cred.cred_properties["user"] == "dummy_user_id"
assert cred.cred_properties["client"] == "dummy_client_id"
assert cred.cred_properties["key"] == "dummy_access_key_id"
assert cred.cred_properties["secret"] == "dummy_access_key_secret"
assert cred.cred_properties["endpoint"] == "dummy_token_endpoint"
with pytest.raises(ConfigException):
with tempfile.NamedTemporaryFile() as tmp:
_ = Credentials.from_credentials_file(tmp.name)
84 changes: 84 additions & 0 deletions tests/iml/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Copyright (C) 2019-2021 HERE Europe B.V.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# License-Filename: LICENSE
"""Test all exceptions."""


import pytest

from tests.iml.conftest import get_mock_response
from xyzspaces.iml.exceptions import (
AuthenticationException,
PayloadTooLargeException,
RequestEntityTooLargeException,
TooManyRequestsException,
)


def test_authentication_exception():
"""Test :class:`AuthenticationException`"""
status_code = 401
reason = "This is mock reason"
text = "This is mock text"
mock_response = get_mock_response(status_code, reason, text)
with pytest.raises(AuthenticationException) as execinfo:
raise AuthenticationException(mock_response)
resp = execinfo.value.args[0]
assert resp.status_code == status_code
assert resp.reason == reason
assert resp.text == text


def test_payload_too_large_exception():
"""Test :class:`PayloadTooLargeException`"""
status_code = 513
reason = "This is mock reason"
text = "This is mock text"
mock_response = get_mock_response(status_code, reason, text)
with pytest.raises(PayloadTooLargeException) as execinfo:
raise PayloadTooLargeException(mock_response)
resp = execinfo.value.args[0]
assert resp.status_code == status_code
assert resp.reason == reason
assert resp.text == text


def test_too_many_requests_exception():
"""Test :class:`TooManyRequestsException`"""
status_code = 429
reason = "This is mock reason"
text = "This is mock text"
mock_response = get_mock_response(status_code, reason, text)
with pytest.raises(TooManyRequestsException) as execinfo:
raise TooManyRequestsException(mock_response)
resp = execinfo.value.args[0]
assert resp.status_code == status_code
assert resp.reason == reason
assert resp.text == text


def test_request_entity_too_large_exception():
"""Test :class:`RequestEntityTooLargeException`"""
status_code = 413
reason = "This is mock reason"
text = "This is mock text"
mock_response = get_mock_response(status_code, reason, text)
with pytest.raises(RequestEntityTooLargeException) as execinfo:
raise RequestEntityTooLargeException(mock_response)
resp = execinfo.value.args[0]
assert resp.status_code == status_code
assert resp.reason == reason
assert resp.text == text
7 changes: 7 additions & 0 deletions tests/iml/test_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# SPDX-License-Identifier: Apache-2.0
# License-Filename: LICENSE
"""Tests for layer module."""
import geopandas
import pytest
from geojson import Feature, FeatureCollection, Point

Expand All @@ -37,6 +38,8 @@ def test_get_feature(read_layer):
feature = int_resp.to_geojson()
assert isinstance(feature, Feature)
assert feature["id"] == "IND"
with pytest.raises(NotImplementedError):
int_resp.to_geopandas()


@pytest.mark.skipif(not env_setup_done(), reason="Credentials are not setup in env.")
Expand All @@ -50,6 +53,10 @@ def test_get_features(read_layer):
assert isinstance(fc, FeatureCollection)
for f in fc["features"]:
assert f["id"] in feature_ids
gdf = int_resp.to_geopandas()
assert isinstance(gdf, geopandas.GeoDataFrame)
with pytest.raises(ValueError):
read_layer.get_features(feature_ids=[])


@pytest.mark.skipif(not env_setup_done(), reason="Credentials are not setup in env.")
Expand Down

0 comments on commit f37b32c

Please sign in to comment.