Skip to content

Commit

Permalink
#85 Code review: converted simple fixtures to constants
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanKJSchreurs committed Jan 11, 2023
1 parent a9d6bb4 commit f1c029c
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 104 deletions.
58 changes: 25 additions & 33 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,37 +178,29 @@ def catalog(multi_backend_connection, config) -> AggregatorCollectionCatalog:
)


@pytest.fixture
def json_capabilities_no_endpoints():
return {
"api_version": "1.1.0",
"backend_version": "0.6.5a1.dev20221208+820",
"description": "OpenEO API to the Terrascope/VITO Remote Sensing product catalog and processing services (using GeoPySpark driver).",
"endpoints": [],
"id": "vitoremotesensingopeneoapi-1.1.0",
"links": [],
"stac_version": "0.9.0",
"title": "VITO Remote Sensing openEO API",
"version": "1.1.0"
}
JSON_CAPABILITIES_NO_ENDPOINTS = {
"api_version": "1.1.0",
"backend_version": "0.6.5a1.dev20221208+820",
"description": "OpenEO API to the Terrascope/VITO Remote Sensing product catalog and processing services (using GeoPySpark driver).",
"endpoints": [],
"id": "vitoremotesensingopeneoapi-1.1.0",
"links": [],
"stac_version": "0.9.0",
"title": "VITO Remote Sensing openEO API",
"version": "1.1.0",
}

@pytest.fixture
def json_capabilities_with_service_types_supported():
return {
"api_version": "1.1.0",
"backend_version": "0.6.5a1.dev20221208+820",
"description": "OpenEO API to the Terrascope/VITO Remote Sensing product catalog and processing services (using GeoPySpark driver).",
"endpoints": [
{
"methods": [
"GET"
],
"path": "/service_types"
},
],
"id": "vitoremotesensingopeneoapi-1.1.0",
"links": [],
"stac_version": "0.9.0",
"title": "VITO Remote Sensing openEO API",
"version": "1.1.0"
}

JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED = {
"api_version": "1.1.0",
"backend_version": "0.6.5a1.dev20221208+820",
"description": "OpenEO API to the Terrascope/VITO Remote Sensing product catalog and processing services (using GeoPySpark driver).",
"endpoints": [
{"methods": ["GET"], "path": "/service_types"},
],
"id": "vitoremotesensingopeneoapi-1.1.0",
"links": [],
"stac_version": "0.9.0",
"title": "VITO Remote Sensing openEO API",
"version": "1.1.0",
}
180 changes: 136 additions & 44 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
from openeo_driver.users.auth import HttpAuthHandler
from openeo_driver.errors import ProcessGraphMissingException, ProcessGraphInvalidException, ServiceUnsupportedException
from openeo.rest import OpenEoApiError, OpenEoRestError
from .conftest import DEFAULT_MEMOIZER_CONFIG
from .conftest import (
DEFAULT_MEMOIZER_CONFIG,
JSON_CAPABILITIES_NO_ENDPOINTS,
JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED,
)


# TODO: "backend.py" should not really be authentication-aware, can we eliminate these constants
Expand Down Expand Up @@ -167,25 +171,49 @@ def test_get_supporting_backend_ids_none_supported(
assert actual_supported_backends == []

def test_get_supporting_backend_ids_all_supported(
self, multi_backend_connection, config, catalog, backend1, backend2, requests_mock,
json_capabilities_with_service_types_supported
self,
multi_backend_connection,
config,
catalog,
backend1,
backend2,
requests_mock,
):
requests_mock.get(backend1 + "/", json=json_capabilities_with_service_types_supported)
requests_mock.get(backend2 + "/", json=json_capabilities_with_service_types_supported)
processing = AggregatorProcessing(backends=multi_backend_connection, catalog=catalog, config=config)
implementation = AggregatorSecondaryServices(backends=multi_backend_connection, processing=processing, config=config)
requests_mock.get(
backend1 + "/", json=JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED
)
requests_mock.get(
backend2 + "/", json=JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED
)
processing = AggregatorProcessing(
backends=multi_backend_connection, catalog=catalog, config=config
)
implementation = AggregatorSecondaryServices(
backends=multi_backend_connection, processing=processing, config=config
)

actual_supported_backends = implementation.get_supporting_backend_ids()
assert actual_supported_backends == ["b1", "b2"]

def test_get_supporting_backend_ids_only_one_supported(
self, multi_backend_connection, config, catalog, backend1, backend2, requests_mock,
json_capabilities_with_service_types_supported, json_capabilities_no_endpoints
self,
multi_backend_connection,
config,
catalog,
backend1,
backend2,
requests_mock,
):
requests_mock.get(backend1 + "/", json=json_capabilities_no_endpoints)
requests_mock.get(backend2 + "/", json=json_capabilities_with_service_types_supported)
processing = AggregatorProcessing(backends=multi_backend_connection, catalog=catalog, config=config)
implementation = AggregatorSecondaryServices(backends=multi_backend_connection, processing=processing, config=config)
requests_mock.get(backend1 + "/", json=JSON_CAPABILITIES_NO_ENDPOINTS)
requests_mock.get(
backend2 + "/", json=JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED
)
processing = AggregatorProcessing(
backends=multi_backend_connection, catalog=catalog, config=config
)
implementation = AggregatorSecondaryServices(
backends=multi_backend_connection, processing=processing, config=config
)

actual_supported_backends = implementation.get_supporting_backend_ids()
assert actual_supported_backends == ["b2"]
Expand All @@ -197,7 +225,6 @@ def test_service_types_simple(
catalog,
backend1,
backend2,
json_capabilities_with_service_types_supported,
requests_mock,
):
"""Given 2 backends and only 1 backend has a single service type, then the aggregator
Expand All @@ -207,8 +234,12 @@ def test_service_types_simple(
requests_mock.get(backend1 + "/service_types", json=single_service_type)
requests_mock.get(backend2 + "/service_types", json={})
# Aggregator checks if the backend supports GET /service_types, so we have to mock that up too.
requests_mock.get(backend1 + "/", json=json_capabilities_with_service_types_supported)
requests_mock.get(backend2 + "/", json=json_capabilities_with_service_types_supported)
requests_mock.get(
backend1 + "/", json=JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED
)
requests_mock.get(
backend2 + "/", json=JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED
)

processing = AggregatorProcessing(backends=multi_backend_connection, catalog=catalog, config=config)
implementation = AggregatorSecondaryServices(backends=multi_backend_connection, processing=processing, config=config)
Expand All @@ -217,8 +248,13 @@ def test_service_types_simple(
assert service_types == single_service_type

def test_service_types_simple_cached(
self, multi_backend_connection, config, catalog, backend1, backend2,
json_capabilities_with_service_types_supported, requests_mock
self,
multi_backend_connection,
config,
catalog,
backend1,
backend2,
requests_mock,
):
"""Scenario: The service_types call is cached:
When we get the service types several times, the second call that happens before the cache expires,
Expand All @@ -229,8 +265,12 @@ def test_service_types_simple_cached(
single_service_type = self.SERVICE_TYPES_ONLT_WMTS
mock_be1 = requests_mock.get(backend1 + "/service_types", json=single_service_type)
# Aggregator checks if the backend supports GET /service_types, so we have to mock that up too.
requests_mock.get(backend1 + "/", json=json_capabilities_with_service_types_supported)
requests_mock.get(backend2 + "/", json=json_capabilities_with_service_types_supported)
requests_mock.get(
backend1 + "/", json=JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED
)
requests_mock.get(
backend2 + "/", json=JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED
)

processing = AggregatorProcessing(backends=multi_backend_connection, catalog=catalog, config=config)
implementation = AggregatorSecondaryServices(backends=multi_backend_connection, processing=processing, config=config)
Expand All @@ -257,8 +297,6 @@ def test_service_types_skips_unsupported_backend(
catalog,
backend1,
backend2,
json_capabilities_no_endpoints,
json_capabilities_with_service_types_supported,
requests_mock,
):
"""Given 2 backends and only 1 backend support secondary services, as states in its capabilities,
Expand All @@ -267,8 +305,12 @@ def test_service_types_skips_unsupported_backend(
"""
# We are testing that the aggregator checks if the backend supports GET /service_types, so we have to mock that up too.
# We want to verify that the capabilities are actually queried.
mock_b1_capabilities = requests_mock.get(backend1 + "/", json=json_capabilities_no_endpoints)
mock_b2_capabilities = requests_mock.get(backend2 + "/", json=json_capabilities_with_service_types_supported)
mock_b1_capabilities = requests_mock.get(
backend1 + "/", json=JSON_CAPABILITIES_NO_ENDPOINTS
)
mock_b2_capabilities = requests_mock.get(
backend2 + "/", json=JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED
)

single_service_type = self.SERVICE_TYPES_ONLT_WMTS
# Backend 1 does support secondary services
Expand All @@ -290,8 +332,14 @@ def test_service_types_skips_unsupported_backend(
assert mock_b2_service_types.called
assert not mock_b1_service_types.called

def test_service_types_multiple_backends(self, multi_backend_connection, config, catalog,
backend1, backend2, json_capabilities_with_service_types_supported, requests_mock
def test_service_types_multiple_backends(
self,
multi_backend_connection,
config,
catalog,
backend1,
backend2,
requests_mock,
):
"""Given 2 backends with each 1 service type, then the aggregator lists both service types."""
service_type_1 = {
Expand Down Expand Up @@ -326,8 +374,12 @@ def test_service_types_multiple_backends(self, multi_backend_connection, config,
requests_mock.get(backend1 + "/service_types", json=service_type_1)
requests_mock.get(backend2 + "/service_types", json=service_type_2)
# Aggregator checks if the backend supports GET /service_types, so we have to mock that up too.
requests_mock.get(backend1 + "/", json=json_capabilities_with_service_types_supported)
requests_mock.get(backend2 + "/", json=json_capabilities_with_service_types_supported)
requests_mock.get(
backend1 + "/", json=JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED
)
requests_mock.get(
backend2 + "/", json=JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED
)

processing = AggregatorProcessing(backends=multi_backend_connection, catalog=catalog, config=config)
implementation = AggregatorSecondaryServices(backends=multi_backend_connection, processing=processing, config=config)
Expand All @@ -339,8 +391,14 @@ def test_service_types_multiple_backends(self, multi_backend_connection, config,
assert actual_service_types == expected_service_types

def test_service_types_warns_about_duplicate_service(
self, multi_backend_connection, config, catalog, backend1, backend2,
json_capabilities_with_service_types_supported, requests_mock, caplog
self,
multi_backend_connection,
config,
catalog,
backend1,
backend2,
requests_mock,
caplog,
):
"""
Given 2 backends which have conflicting service types,
Expand All @@ -367,8 +425,12 @@ def test_service_types_warns_about_duplicate_service(
requests_mock.get(backend1 + "/service_types", json=service_type_1)
requests_mock.get(backend2 + "/service_types", json=service_type_2)
# Aggregator checks if the backend supports GET /service_types, so we have to mock that up too.
requests_mock.get(backend1 + "/", json=json_capabilities_with_service_types_supported)
requests_mock.get(backend2 + "/", json=json_capabilities_with_service_types_supported)
requests_mock.get(
backend1 + "/", json=JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED
)
requests_mock.get(
backend2 + "/", json=JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED
)

processing = AggregatorProcessing(backends=multi_backend_connection, catalog=catalog, config=config)
implementation = AggregatorSecondaryServices(backends=multi_backend_connection, processing=processing, config=config)
Expand Down Expand Up @@ -483,8 +545,13 @@ def test_service_info_wrong_service_id(
assert requests_mock.called

def test_create_service_succeeds(
self, flask_app, multi_backend_connection, config, catalog, backend1,
json_capabilities_with_service_types_supported, requests_mock
self,
flask_app,
multi_backend_connection,
config,
catalog,
backend1,
requests_mock,
):
"""When it gets a correct params for a new service, it successfully creates it."""

Expand All @@ -505,7 +572,9 @@ def test_create_service_succeeds(
)
requests_mock.get(backend1 + "/service_types", json=self.SERVICE_TYPES_ONLT_WMTS)
# Aggregator checks if the backend supports GET /service_types, so we have to mock that up too.
requests_mock.get(backend1 + "/", json=json_capabilities_with_service_types_supported)
requests_mock.get(
backend1 + "/", json=JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED
)

processing = AggregatorProcessing(backends=multi_backend_connection, catalog=catalog, config=config)
implementation = AggregatorSecondaryServices(backends=multi_backend_connection, processing=processing, config=config)
Expand All @@ -521,16 +590,23 @@ def test_create_service_succeeds(
assert actual_openeo_id == expected_service_id

def test_create_service_raises_serviceunsupportedexception(
self, flask_app, multi_backend_connection, config, catalog, backend1,
json_capabilities_with_service_types_supported, requests_mock
self,
flask_app,
multi_backend_connection,
config,
catalog,
backend1,
requests_mock,
):
"""When it gets a request for a service type but no backend supports this service type, it raises ServiceUnsupportedException."""

# At least 1 service type must be present.
# We don't want test to succeed erroneously simply because there are no services at all.
mock_service_types = requests_mock.get(backend1 + "/service_types", json=self.SERVICE_TYPES_ONLT_WMTS)
# Aggregator checks if the backend supports GET /service_types, so we have to mock that up too.
mock_get_capabilities = requests_mock.get(backend1 + "/", json=json_capabilities_with_service_types_supported)
mock_get_capabilities = requests_mock.get(
backend1 + "/", json=JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED
)

non_existent_service_id = "b1-doesnotexist"
# Check that this requests_mock does not get called.
Expand Down Expand Up @@ -565,8 +641,14 @@ def test_create_service_raises_serviceunsupportedexception(

@pytest.mark.parametrize("exception_class", [OpenEoApiError, OpenEoRestError])
def test_create_service_backend_raises_openeoapiexception(
self, flask_app, multi_backend_connection, config, catalog, backend1,
json_capabilities_with_service_types_supported, requests_mock, exception_class
self,
flask_app,
multi_backend_connection,
config,
catalog,
backend1,
requests_mock,
exception_class,
):
"""When the backend raises a general exception the aggregator raises an OpenEOApiException."""

Expand All @@ -579,7 +661,9 @@ def test_create_service_backend_raises_openeoapiexception(
)
mock_service_types = requests_mock.get(backend1 + "/service_types", json=self.SERVICE_TYPES_ONLT_WMTS)
# Aggregator checks if the backend supports GET /service_types, so we have to mock that up too.
mock_get_capabilities = requests_mock.get(backend1 + "/", json=json_capabilities_with_service_types_supported)
mock_get_capabilities = requests_mock.get(
backend1 + "/", json=JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED
)


processing = AggregatorProcessing(backends=multi_backend_connection, catalog=catalog, config=config)
Expand All @@ -602,8 +686,14 @@ def test_create_service_backend_raises_openeoapiexception(
[ProcessGraphMissingException, ProcessGraphInvalidException, ServiceUnsupportedException]
)
def test_create_service_backend_reraises(
self, flask_app, multi_backend_connection, config, catalog, backend1,
json_capabilities_with_service_types_supported, requests_mock, exception_class
self,
flask_app,
multi_backend_connection,
config,
catalog,
backend1,
requests_mock,
exception_class,
):
"""When the backend raises certain exception types of which we know it indicates client error / bad input data,
then the aggregator re-raises that exception.
Expand All @@ -618,7 +708,9 @@ def test_create_service_backend_reraises(
)
mock_get_service_types = requests_mock.get(backend1 + "/service_types", json=self.SERVICE_TYPES_ONLT_WMTS)
# Aggregator checks if the backend supports GET /service_types, so we have to mock that up too.
mock_get_capabilities = requests_mock.get(backend1 + "/", json=json_capabilities_with_service_types_supported)
mock_get_capabilities = requests_mock.get(
backend1 + "/", json=JSON_CAPABILITIES_WITH_SERVICE_TYPES_SUPPORTED
)

processing = AggregatorProcessing(backends=multi_backend_connection, catalog=catalog, config=config)
implementation = AggregatorSecondaryServices(backends=multi_backend_connection, processing=processing, config=config)
Expand Down
Loading

0 comments on commit f1c029c

Please sign in to comment.