diff --git a/.gitignore b/.gitignore index 23c4682..10b63f9 100644 --- a/.gitignore +++ b/.gitignore @@ -196,3 +196,6 @@ cython_debug/ .vscode/settings.json .codex + + +env* diff --git a/app/platforms/base.py b/app/platforms/base.py index 5e0d664..a0aa127 100644 --- a/app/platforms/base.py +++ b/app/platforms/base.py @@ -7,7 +7,7 @@ from app.schemas.parameters import Parameter from app.schemas.unit_job import ServiceDetails -from stac_pydantic import Collection +from stac_pydantic.item_collection import ItemCollection class BaseProcessingPlatform(ABC): @@ -75,7 +75,7 @@ async def get_job_status( @abstractmethod async def get_job_results( self, user_token: str, job_id: str, details: ServiceDetails - ) -> Collection: + ) -> ItemCollection: """ Retrieve the job results of a processing job that is running on the platform. diff --git a/app/platforms/implementations/ogc_api_process.py b/app/platforms/implementations/ogc_api_process.py index 6c86aa3..002ab9a 100644 --- a/app/platforms/implementations/ogc_api_process.py +++ b/app/platforms/implementations/ogc_api_process.py @@ -1,21 +1,12 @@ import json import re -from typing import List +from asyncio import to_thread import requests -from app.auth import exchange_token, get_current_user_claims from fastapi import Response +from httpx import Response as HTTPXResponse +from httpx import get as http_get from loguru import logger - -from app.platforms.base import BaseProcessingPlatform -from app.platforms.dispatcher import register_platform -from app.schemas.enum import OutputFormatEnum, ProcessTypeEnum, ProcessingStatusEnum -from app.schemas.parameters import ParamTypeEnum, Parameter -from app.schemas.unit_job import ServiceDetails -from httpx import get as http_get, Response as HTTPXResponse -from stac_pydantic.collection import Collection, Extent, SpatialExtent, TimeInterval -from stac_pydantic.links import Links -from stac_pydantic.version import STAC_VERSION from ogc_api_processes_client.api_client_wrapper import ApiClientWrapper from ogc_api_processes_client.configuration import Configuration from ogc_api_processes_client.models.inline_or_ref_data import InlineOrRefData @@ -24,7 +15,18 @@ from ogc_api_processes_client.models.qualified_input_value import QualifiedInputValue from ogc_api_processes_client.models.status_code import StatusCode from ogc_api_processes_client.models.status_info import StatusInfo -from typing import Dict +from stac_pydantic.collection import Collection +from stac_pydantic.item import Item +from stac_pydantic.item_collection import ItemCollection +from stac_pydantic.links import Links +from stac_pydantic.shared import Asset + +from app.auth import exchange_token, get_current_user_claims +from app.platforms.base import BaseProcessingPlatform +from app.platforms.dispatcher import register_platform +from app.schemas.enum import OutputFormatEnum, ProcessingStatusEnum, ProcessTypeEnum +from app.schemas.parameters import Parameter, ParamTypeEnum +from app.schemas.unit_job import ServiceDetails STAC_COLLECTION_SCHEMA = ( "https://schemas.stacspec.org/v1.0.0/collection-spec/json-schema/collection.json" @@ -300,7 +302,7 @@ def _map_ogcapi_status(self, ogcapi_status: StatusCode) -> ProcessingStatusEnum: logger.warning(f"Mapping of unknown OGC API status: {ogcapi_status}") return ProcessingStatusEnum.UNKNOWN - def _extract_download_link_from_asset(self, asset: dict) -> str | None: + def _extract_download_link_from_asset(self, asset: Asset) -> str | None: """ Extracts the download link from an asset dictionary. Checks if the `href` field is present and contains an HTTPS URL. If this is not @@ -314,8 +316,8 @@ def _extract_download_link_from_asset(self, asset: dict) -> str | None: str | None: The download link if available, otherwise None. """ refs = [ - asset.get("href"), - asset.get("alternate", {}).get("https", {}).get("href"), + asset.href, + asset.to_dict().get("alternate", {}).get("https", {}).get("href"), ] for href in refs: if href and href.startswith("https://"): @@ -377,7 +379,7 @@ def _generate_signed_url(self, href: str, user_token: str) -> str | None: ) return None - def _update_assets_hrefs(self, assets: dict, user_token: str) -> dict: + def _update_assets_hrefs(self, assets: dict[str, Asset], user_token: str) -> dict: """ Update the hrefs of the assets to be HTTPS URLs. If the current href is an S3 URL, the code will look into `alternate` links to @@ -402,29 +404,23 @@ def _update_assets_hrefs(self, assets: dict, user_token: str) -> dict: "original HTTPS href." ) signed_url = href - updated_asset["href"] = signed_url + updated_asset.href = signed_url updated_assets[asset_name] = updated_asset return updated_assets - def _build_collection_from_features( + def _build_item_collection_from_features( self, features: list, - assets: dict, result_name: str, - user_token: str, - details: ServiceDetails, - internal_job_id: str, - ) -> Collection: + ) -> ItemCollection: """ - Build a STAC Collection from a list of GeoJSON features and their - aggregated assets. The spatial extent is derived from the feature - bounding boxes and the temporal extent from the feature datetime - properties. + Build a STAC Collection from a list of GeoJSON features. + The spatial extent is derived from the feature bounding boxes + and the temporal extent from the feature datetime properties. Args: features: GeoJSON feature list. - assets: Aggregated asset dict collected from those features. result_name: Identifier used as the collection ID. user_token: Token used to sign asset hrefs. details: Service details containing namespace and application information. @@ -438,7 +434,7 @@ def _build_collection_from_features( max_x, max_y = float("-inf"), float("-inf") found_bbox = False for feature in features: - bbox = feature.get("bbox") + bbox = feature.bbox if isinstance(bbox, (list, tuple)) and len(bbox) >= 4: min_x = min(min_x, float(bbox[0])) min_y = min(min_y, float(bbox[1])) @@ -452,88 +448,93 @@ def _build_collection_from_features( # Temporal extent — min/max of all datetime-like properties datetimes: list[str] = [] for feature in features: - props = feature.get("properties") or {} - for dt_key in ("datetime", "start_datetime", "end_datetime"): - dt_val = props.get(dt_key) + props = feature.properties + for dt_val in (props.datetime, props.start_datetime, props.end_datetime): if isinstance(dt_val, str): datetimes.append(dt_val) - temporal_interval: list[list] = ( - [[min(datetimes), max(datetimes)]] if datetimes else [[None, None]] - ) - updated_assets = self._update_assets_hrefs(assets, user_token) + logger.debug(f"Features: {features}") logger.debug( f"Building STAC Collection '{result_name}' from {len(features)} feature(s) " - f"with {len(updated_assets)} asset(s)." ) - return Collection( - id=f"{details.namespace}-{internal_job_id}", - stac_version=STAC_VERSION, - title=f"Results for {details.application}", - description=( - f"OGC API process result items for job '{internal_job_id}' " - f"of application '{details.application}'." - ), - type="Collection", - license="proprietary", + logger.debug(f"bbox = {spatial_bbox}") + + return ItemCollection( + bbox=spatial_bbox, + type="FeatureCollection", links=Links([]), - extent=Extent( - spatial=SpatialExtent(bbox=[spatial_bbox]), - temporal=TimeInterval(interval=temporal_interval), - ), - assets=updated_assets, + features=features, ) - def _extract_assets_from_feature_collection( + def _remove_stac_links(self, links: Links, keep: list[str] | None = None): + if not keep: + keep = [] + stac_links = [ + link + for link in links.root + if link.rel in ["self", "item", "items", "collection", "parent", "root"] + and link.rel not in keep + or link.rel.endswith("queryables") + ] + for link in stac_links: + links.root.remove(link) + return links.root + + def _process_feature_collection( self, feature_collection: dict, *, result_name: str, user_token: str, - details: ServiceDetails, - internal_job_id: str, - ) -> Collection: - assets: dict = {} - features = feature_collection.get("features", []) + ) -> ItemCollection: logger.debug(f"Feature collection: {json.dumps(feature_collection, indent=2)}") + + features_dict = feature_collection.get("features", []) + features = [Item.model_validate(f) for f in features_dict] + + collection = None + for feature in features: - feature_assets = feature.get("assets") - if isinstance(feature_assets, dict): - assets.update(feature_assets) - continue + collection_link = next( + (link for link in feature.links.root if link.rel == "collection"), None + ) + feature.links = Links( + self._remove_stac_links(feature.links, keep=["collection"]) + ) + feature.assets = self._update_assets_hrefs(feature.assets, user_token) + feature.collection = None # Some providers expose assets through an item link # instead of inlining them in the feature. - for link in feature.get("links", []): - if "collection" == link.get("rel") and link.get("href"): - collection_link: str = link.get("href") - logger.debug( - f"GeoJSON FeatureCollection results: '{result_name}' " - f"points to a valid collection URL: {collection_link}" - ) + if collection is None and collection_link is not None: + logger.debug( + f"GeoJSON FeatureCollection results: '{result_name}' " + f"points to a valid collection URL: {collection_link.href}" + ) - response: HTTPXResponse = http_get( - collection_link, - follow_redirects=True, - headers={"Authorization": f"Bearer {user_token}"}, - ) - response.raise_for_status() - collection_data = response.json() - collection_data["assets"] = self._update_assets_hrefs( - collection_data.get("assets", {}), user_token - ) - collection = Collection.model_validate(collection_data) - logger.debug( - f"Extracted collection '{collection.id}' " - f"with assets: {list((collection.assets or {}).keys())}" - ) - return collection + response: HTTPXResponse = http_get( + collection_link.href, + follow_redirects=True, + headers={"Authorization": f"Bearer {user_token}"}, + ) + response.raise_for_status() + collection_data = response.json() + collection = Collection.model_validate(collection_data) + collection.assets = self._update_assets_hrefs( + collection.assets or {}, user_token + ) + logger.debug( + f"Extracted collection '{collection.id}' " + f"with assets: {list((collection.assets or {}).keys())}" + ) + collection.links = Links(self._remove_stac_links(collection.links)) - return self._build_collection_from_features( - features, assets, result_name, user_token, details, internal_job_id + item_collection = self._build_item_collection_from_features( + features, result_name ) + return item_collection async def get_job_status( self, user_token: str, job_id: str, details: ServiceDetails @@ -556,7 +557,7 @@ async def get_job_status( async def get_job_results( self, user_token: str, job_id: str, details: ServiceDetails - ) -> Collection: + ) -> ItemCollection: logger.debug(f"Fetching job result for opfenEO job with ID {job_id}") logger.debug("Exchanging user token for OGC API Process execution...") @@ -570,7 +571,7 @@ async def get_job_results( details.endpoint, namespace, exchanged_token ) - result: Dict[str, InlineOrRefData] = api_client.get_result( + result: dict[str, InlineOrRefData] = api_client.get_result( job_id=internal_job_id ) @@ -612,9 +613,28 @@ async def get_job_results( if STAC_COLLECTION_SCHEMA == schema_reference: logger.success(f"STAC Collection found in results: '{result_name}'") - return Collection.model_validate( + collection = Collection.model_validate( qualified_value.value.actual_instance ) + features = [] + for link in collection.links.root: + if link.rel != "item": + continue + response = await to_thread( + http_get, + link.href, + follow_redirects=True, + headers={ + "Authorization": f"Bearer {exchanged_token or user_token}" + }, + ) + response.raise_for_status() + features.append(response.json()) + return self._process_feature_collection( + {"features": features}, + result_name=result_name, + user_token=exchanged_token or user_token, + ) elif GEOJSON_FEATURECOLLECTION_SCHEMA == schema_reference: logger.success( f"GeoJSON FeatureCollection found in results: '{result_name}'" @@ -622,12 +642,10 @@ async def get_job_results( feature_collection = ( qualified_value.value.oneof_schema_2_validator or {} ) - return self._extract_assets_from_feature_collection( + return self._process_feature_collection( feature_collection, result_name=result_name, user_token=exchanged_token or user_token, - details=details, - internal_job_id=internal_job_id, ) else: logger.warning( @@ -636,28 +654,11 @@ async def get_job_results( ) # result not found, send back an empty collection - - return Collection( - id=f"{details.namespace}-{internal_job_id}", - stac_version=STAC_VERSION, - title=f"Results for {details.application}", - description=( - f"OGC API process result items for job '{internal_job_id}' " - f"of application '{details.application}'." - ), - type="Collection", - license="proprietary", - links=Links([]), - extent=Extent( - spatial=SpatialExtent(bbox=[(-180.0, -90.0, 180.0, 90.0)]), - temporal=TimeInterval(interval=[[None, None]]), - ), - assets={}, - ) + return self._build_item_collection_from_features([], "No results found") async def get_service_parameters( self, user_token: str, details: ServiceDetails - ) -> List[Parameter]: + ) -> list[Parameter]: parameters = [] logger.debug( diff --git a/app/platforms/implementations/openeo.py b/app/platforms/implementations/openeo.py index 2d35e8d..ff52d6a 100644 --- a/app/platforms/implementations/openeo.py +++ b/app/platforms/implementations/openeo.py @@ -9,7 +9,7 @@ import requests from dotenv import load_dotenv from loguru import logger -from stac_pydantic import Collection +from stac_pydantic import Collection, Item, ItemCollection from app.auth import exchange_token from app.config.schemas import AuthMethod @@ -187,10 +187,16 @@ async def _get_job_status_once( async def _get_job_results_once( self, user_token: str, job_id: str, details: ServiceDetails - ) -> Collection: + ) -> ItemCollection: connection = await self._setup_connection(user_token, details.endpoint) job = connection.job(job_id) - return Collection(**job.get_results().get_metadata()) + collection = Collection.model_validate(job.get_results().get_metadata()) + features = [ + Item.model_validate(connection.get(link.href, expected_status=200).json()) + for link in collection.links.root + if link.rel == "item" + ] + return ItemCollection(type="FeatureCollection", features=features) def _get_client_credentials(self, url: str) -> tuple[str, str, str]: """ @@ -387,7 +393,7 @@ async def get_job_status( async def get_job_results( self, user_token: str, job_id: str, details: ServiceDetails - ) -> Collection: + ) -> ItemCollection: try: logger.debug(f"Fetching job result for openEO job with ID {job_id}") return await self._get_job_results_once(user_token, job_id, details) diff --git a/app/routers/unit_jobs.py b/app/routers/unit_jobs.py index 5770746..efb6a5b 100644 --- a/app/routers/unit_jobs.py +++ b/app/routers/unit_jobs.py @@ -26,7 +26,7 @@ get_processing_job_results, ) -from stac_pydantic import Collection +from stac_pydantic.item_collection import ItemCollection # from app.auth import get_current_user @@ -212,7 +212,7 @@ async def get_job( ) async def get_job_results( job_id: int, db: Session = Depends(get_db), token: str = Depends(oauth2_scheme) -) -> Collection | None: +) -> ItemCollection | None: try: result = await get_processing_job_results(token, db, job_id) if not result: diff --git a/app/services/processing.py b/app/services/processing.py index 39b4f87..bc13976 100644 --- a/app/services/processing.py +++ b/app/services/processing.py @@ -4,7 +4,7 @@ from fastapi import Response from loguru import logger from sqlalchemy.orm import Session -from stac_pydantic import Collection +from stac_pydantic.item_collection import ItemCollection from app.auth import get_current_user_id from app.database.models.processing_job import ( @@ -114,7 +114,7 @@ async def get_processing_job_results( token: str, database: Session, job_id: int, -) -> Collection | None: +) -> ItemCollection | None: user = get_current_user_id(token) record = get_job_by_user_id(database, job_id, user) if not record: diff --git a/tests/platforms/test_ogc_api_process_platform.py b/tests/platforms/test_ogc_api_process_platform.py index b8e76e8..6099368 100644 --- a/tests/platforms/test_ogc_api_process_platform.py +++ b/tests/platforms/test_ogc_api_process_platform.py @@ -6,6 +6,8 @@ import jwt import pytest import requests +from stac_pydantic.shared import Asset +from stac_pydantic.item_collection import ItemCollection TEST_USER_TOKEN = jwt.encode( @@ -20,9 +22,7 @@ def _install_ogc_api_processes_client_stub(): api_client_wrapper_module = types.ModuleType( "ogc_api_processes_client.api_client_wrapper" ) - configuration_module = types.ModuleType( - "ogc_api_processes_client.configuration" - ) + configuration_module = types.ModuleType("ogc_api_processes_client.configuration") models_module = types.ModuleType("ogc_api_processes_client.models") inline_or_ref_data_module = types.ModuleType( "ogc_api_processes_client.models.inline_or_ref_data" @@ -138,7 +138,12 @@ def build_collection_payload(collection_id="collection-1", title="Test Collectio "description": "Test STAC collection", "type": "Collection", "license": "proprietary", - "links": [], + "links": [ + { + "rel": "collection", + "href": "https://example.com/collections/1", + } + ], "extent": { "spatial": {"bbox": [[-180.0, -90.0, 180.0, 90.0]]}, "temporal": {"interval": [[None, None]]}, @@ -146,6 +151,49 @@ def build_collection_payload(collection_id="collection-1", title="Test Collectio } +def build_feature_collection_payload(item_id="item-1"): + return { + "type": "FeatureCollection", + "features": [ + { + "bbox": [12.145, 41.996, 12.458, 42.176], + "type": "Feature", + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [12.145, 41.996], + [12.145, 42.176], + [12.458, 42.176], + [12.458, 41.996], + [12.145, 41.996], + ] + ], + }, + "properties": { + "datetime": None, + "start_datetime": "2024-11-13T17:06:07Z", + "end_datetime": "2024-11-13T17:06:32Z", + "title": f"Title for {item_id}", + }, + "id": item_id, + "stac_version": "1.1.0", + "assets": {}, + "links": [ + { + "rel": "collection", + "href": "https://example.com/collections/1", + } + ], + "stac_extensions": [], + "collection": "opensartoolkit-v2-2-1-dsb8v-zwdj6w", + } + ], + "links": [], + "bbox": [12.145, 41.996, 12.458, 42.176], + } + + @pytest.mark.parametrize( ("schema", "input_id", "expected_type"), [ @@ -172,32 +220,28 @@ def build_collection_payload(collection_id="collection-1", title="Test Collectio ({"type": "integer"}, "limit", ParamTypeEnum.INTEGER), ({"type": "double"}, "scale", ParamTypeEnum.DOUBLE), ( - {"type": "object", "required": ["coordinates", "type", "bbox"], - "properties": { - "type": { - "actual_instance": { - "actual_instance": { - "enum": ["Polygon"] - } - } - } - } - }, + { + "type": "object", + "required": ["coordinates", "type", "bbox"], + "properties": { + "type": { + "actual_instance": {"actual_instance": {"enum": ["Polygon"]}} + } + }, + }, "aoi", ParamTypeEnum.POLYGON, ), ( - {"type": "object", "required": ["coordinates", "type", "bbox"], - "properties": { - "type": { - "actual_instance": { - "actual_instance": { - "enum": ["Point"] - } - } - } - } - }, + { + "type": "object", + "required": ["coordinates", "type", "bbox"], + "properties": { + "type": { + "actual_instance": {"actual_instance": {"enum": ["Point"]}} + } + }, + }, "poi", ParamTypeEnum.POINT, ), @@ -247,17 +291,13 @@ def test_transform_bbox_parameter(platform, value, expected): "invalid", { "type": "Polygon", - "coordinates": [ - [[4, 50], [5, 50], [5, 51], [4, 51], [4, 50]] - ], + "coordinates": [[[4, 50], [5, 50], [5, 51], [4, 51], [4, 50]]], }, { "type": "Feature", "geometry": { "type": "Polygon", - "coordinates": [ - [[4, 50], [5, 50], [5, 51], [4, 51], [4, 50]] - ], + "coordinates": [[[4, 50], [5, 50], [5, 51], [4, 51], [4, 50]]], }, }, ], @@ -333,18 +373,18 @@ def test_generate_signed_url_returns_none_on_request_exception( def test_update_assets_hrefs_keeps_original_href_when_signing_fails(platform): assets = { - "result": { - "href": "s3://bucket/result.tif", - "alternate": { - "https": {"href": "https://download.example.com/resource"} - }, - } + "result": Asset.model_validate( + { + "href": "s3://bucket/result.tif", + "alternate": {"https": {"href": "https://download.example.com/resource"}}, + } + ) } with patch.object(platform, "_generate_signed_url", return_value=None): updated = platform._update_assets_hrefs(assets, "token") - assert updated["result"]["href"] == "https://download.example.com/resource" + assert updated["result"].href == "https://download.example.com/resource" @pytest.mark.parametrize( @@ -443,7 +483,9 @@ async def test_create_api_client_instance_with_token_and_namespace( ) @patch("app.platforms.implementations.ogc_api_process.get_current_user_claims") @patch.object(OGCAPIProcessPlatform, "_transform_parameters", new_callable=AsyncMock) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_execute_job_returns_namespaced_job_id( mock_create_api_client, mock_transform_parameters, @@ -502,7 +544,9 @@ async def test_execute_job_returns_namespaced_job_id( ) @patch("app.platforms.implementations.ogc_api_process.get_current_user_claims") @patch.object(OGCAPIProcessPlatform, "_transform_parameters", new_callable=AsyncMock) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_execute_job_returns_plain_job_id_without_namespace( mock_create_api_client, mock_transform_parameters, @@ -553,7 +597,9 @@ async def test_execute_job_returns_plain_job_id_without_namespace( ) @patch("app.platforms.implementations.ogc_api_process.get_current_user_claims") @patch.object(OGCAPIProcessPlatform, "_transform_parameters", new_callable=AsyncMock) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_execute_job_omits_missing_optional_user_fields( mock_create_api_client, mock_transform_parameters, @@ -613,7 +659,9 @@ async def test_execute_synchronous_job_not_implemented(platform): "app.platforms.implementations.ogc_api_process.exchange_token", new_callable=AsyncMock, ) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_get_job_status_maps_client_status( mock_create_api_client, mock_exchange_token, platform ): @@ -640,22 +688,51 @@ async def test_get_job_status_maps_client_status( @pytest.mark.asyncio +@pytest.mark.parametrize( + "schema_reference", [GEOJSON_FEATURECOLLECTION_SCHEMA, STAC_COLLECTION_SCHEMA] +) +@patch("app.platforms.implementations.ogc_api_process.http_get") +@patch.object(OGCAPIProcessPlatform, "_get_token_for_api", new_callable=AsyncMock) @patch( "app.platforms.implementations.ogc_api_process.exchange_token", new_callable=AsyncMock, ) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) -async def test_get_job_results_returns_stac_collection( - mock_create_api_client, mock_exchange_token, platform +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) +async def test_get_job_results_returns_item_collection( + mock_create_api_client, + mock_exchange_token, + mock_get_token_for_api, + mock_http_get, + platform, + schema_reference, ): + mock_get_token_for_api.return_value = "exchanged-token" mock_exchange_token.return_value = "exchanged-token" + mock_http_get.return_value.json.return_value = build_collection_payload() + feature_collection = build_feature_collection_payload("item-from-stac") + collection = build_collection_payload() + collection["links"] = [ + {"rel": "item", "href": "https://example.com/items/item-from-stac"} + ] + collection["assets"] = { + "ignored": {"href": "https://example.com/collection-only.tif"} + } + if schema_reference == STAC_COLLECTION_SCHEMA: + item_response = MagicMock() + item_response.json.return_value = feature_collection["features"][0] + mock_http_get.side_effect = [item_response, mock_http_get.return_value] api_client = MagicMock() api_client.get_result.return_value = { - "result": SimpleNamespace( + "output": SimpleNamespace( actual_instance=SimpleNamespace( - var_schema=SimpleNamespace(actual_instance=STAC_COLLECTION_SCHEMA), + var_schema=SimpleNamespace( + actual_instance=schema_reference + ), value=SimpleNamespace( - actual_instance=build_collection_payload("collection-from-stac") + actual_instance=collection, + oneof_schema_2_validator=feature_collection, ), ) ) @@ -672,8 +749,30 @@ async def test_get_job_results_returns_stac_collection( ), ) - assert result.id == "collection-from-stac" - assert result.title == "Test Collection" + assert isinstance(result, ItemCollection) + assert result.type == "FeatureCollection" + assert len(result.features) == 1 + assert result.features[0].id == "item-from-stac" + assert result.features[0].properties.title == "Title for item-from-stac" + assert result.features[0].assets == {} + if schema_reference == STAC_COLLECTION_SCHEMA: + assert mock_http_get.call_count == 2 + assert mock_http_get.call_args_list[0].args == ( + "https://example.com/items/item-from-stac", + ) + assert mock_http_get.call_args_list[0].kwargs == { + "follow_redirects": True, + "headers": {"Authorization": "Bearer exchanged-token"}, + } + item_response.raise_for_status.assert_called_once_with() + else: + mock_http_get.assert_called_once() + mock_http_get.assert_called_with( + "https://example.com/collections/1", + follow_redirects=True, + headers={"Authorization": "Bearer exchanged-token"}, + ) + mock_http_get.return_value.raise_for_status.assert_called_once_with() @pytest.mark.asyncio @@ -682,11 +781,14 @@ async def test_get_job_results_returns_stac_collection( "app.platforms.implementations.ogc_api_process.exchange_token", new_callable=AsyncMock, ) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_get_job_results_follows_geojson_collection_link( mock_create_api_client, mock_exchange_token, mock_http_get, platform ): mock_exchange_token.return_value = "exchanged-token" + feature_collection = build_feature_collection_payload("item-from-geojson") api_client = MagicMock() api_client.get_result.return_value = { "result": SimpleNamespace( @@ -694,20 +796,7 @@ async def test_get_job_results_follows_geojson_collection_link( var_schema=SimpleNamespace( actual_instance=GEOJSON_FEATURECOLLECTION_SCHEMA ), - value=SimpleNamespace( - oneof_schema_2_validator={ - "features": [ - { - "links": [ - { - "rel": "collection", - "href": "https://example.com/collections/1", - } - ] - } - ] - } - ), + value=SimpleNamespace(oneof_schema_2_validator=feature_collection), ) ) } @@ -727,7 +816,11 @@ async def test_get_job_results_follows_geojson_collection_link( ), ) - assert result.id == "collection-from-geojson" + assert result.type == "FeatureCollection" + assert len(result.features) == 1 + assert result.features[0].id == "item-from-geojson" + assert result.features[0].collection is None + assert result.features[0].links.root[0].rel == "collection" mock_http_get.assert_called_once_with( "https://example.com/collections/1", follow_redirects=True, @@ -741,7 +834,9 @@ async def test_get_job_results_follows_geojson_collection_link( "app.platforms.implementations.ogc_api_process.exchange_token", new_callable=AsyncMock, ) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_get_job_results_returns_empty_collection_when_no_supported_result( mock_create_api_client, mock_exchange_token, platform ): @@ -762,9 +857,10 @@ async def test_get_job_results_returns_empty_collection_when_no_supported_result ), ) - assert result.id == "ns-job-123" - assert result.title == "Results for buffer" - assert result.license == "proprietary" + assert result.type == "FeatureCollection" + assert result.features == [] + assert result.links.root == [] + assert result.bbox == (-180.0, -90.0, 180.0, 90.0) @pytest.mark.asyncio @@ -772,7 +868,9 @@ async def test_get_job_results_returns_empty_collection_when_no_supported_result "app.platforms.implementations.ogc_api_process.exchange_token", new_callable=AsyncMock, ) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_get_service_parameters_maps_geojson_and_options( mock_create_api_client, mock_exchange_token, platform ): @@ -840,7 +938,9 @@ async def test_get_service_parameters_maps_geojson_and_options( "app.platforms.implementations.ogc_api_process.exchange_token", new_callable=AsyncMock, ) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_get_service_parameters_maps_all_supported_types( mock_create_api_client, mock_exchange_token, platform ): diff --git a/tests/platforms/test_openeo_platform.py b/tests/platforms/test_openeo_platform.py index a63202a..0b505f4 100644 --- a/tests/platforms/test_openeo_platform.py +++ b/tests/platforms/test_openeo_platform.py @@ -16,7 +16,7 @@ from app.schemas.enum import OutputFormatEnum, ProcessingStatusEnum from app.schemas.parameters import ParamTypeEnum, Parameter from app.schemas.unit_job import ServiceDetails -from stac_pydantic import Collection +from stac_pydantic import Collection, ItemCollection from openeo.rest import OpenEoApiError @@ -336,14 +336,53 @@ async def test_get_job_status_404_with_other_code_returns_unknown( @pytest.mark.asyncio +@pytest.mark.parametrize("result_kind", ["empty", "assets", "items", "items_and_assets"]) @patch.object(OpenEOPlatform, "_setup_connection") -async def test_get_job_results_success(mock_connection, platform, fake_result): - mock_connection.return_value = DummyOpenEOClient(result=fake_result) +async def test_get_job_results_success( + mock_connection, platform, fake_result, result_kind +): + metadata = fake_result.model_dump() + assets = {"result": {"href": "https://example.com/result.tif"}} + item = { + "type": "Feature", + "id": "result-item", + "geometry": None, + "properties": {"datetime": "2025-01-01T00:00:00Z"}, + "assets": assets, + "links": [], + } + connection = MagicMock() + connection.job.return_value.get_results.return_value.get_metadata.return_value = metadata + if result_kind in ("assets", "items_and_assets"): + metadata["assets"] = { + "collection-only": {"href": "https://example.com/collection-only.tif"} + } + if result_kind in ("items", "items_and_assets"): + metadata["links"] = [ + {"rel": "item", "href": "https://example.com/items/result-item"} + ] + connection.get.return_value.json.return_value = item + mock_connection.return_value = connection details = ServiceDetails(endpoint="foo", application="bar") result = await platform.get_job_results("foobar", "job123", details) - assert result == fake_result + assert isinstance(result, ItemCollection) + assert result.type == "FeatureCollection" + if result_kind in ("empty", "assets"): + assert result.features == [] + else: + assert len(result.features) == 1 + assert result.features[0].model_dump(mode="json", exclude_none=True)[ + "assets" + ] == assets + assert result.features[0].id == "result-item" + if result_kind in ("items", "items_and_assets"): + connection.get.assert_called_once_with( + "https://example.com/items/result-item", expected_status=200 + ) + else: + connection.get.assert_not_called() @pytest.mark.asyncio @@ -818,7 +857,8 @@ async def test_get_job_results_retries_after_auth_error( result = await platform.get_job_results("fake_token", "job-1", service_details) - assert result == fake_result + assert isinstance(result, ItemCollection) + assert result.type == "FeatureCollection" mock_refresh_connection.assert_awaited_once_with( "fake_token", service_details.endpoint ) diff --git a/tests/routers/test_unit_jobs.py b/tests/routers/test_unit_jobs.py index ef38fb3..830dfd5 100644 --- a/tests/routers/test_unit_jobs.py +++ b/tests/routers/test_unit_jobs.py @@ -2,6 +2,7 @@ from unittest.mock import patch from fastapi import status +from stac_pydantic import ItemCollection from app.error import InternalException @@ -103,13 +104,16 @@ def test_unit_jobs_get_job_internal_error(mock_get_processing_job, client): def test_unit_jobs_get_job_results_200( mock_get_processing_job_results, client, - fake_result, ): + fake_result = ItemCollection(type="FeatureCollection", features=[], links=[]) mock_get_processing_job_results.return_value = fake_result r = client.get("/unit_jobs/1/results") assert r.status_code == 200 + assert r.json()["type"] == "FeatureCollection" + assert "features" in r.json() + assert isinstance(ItemCollection.model_validate(r.json()), ItemCollection) assert json.dumps(r.json(), indent=1) == fake_result.model_dump_json( indent=1, exclude_unset=False )