From 6d026730213de14ece6688ca3bc9d3a2947788c3 Mon Sep 17 00:00:00 2001 From: Casper Welzel Andersen Date: Tue, 10 Aug 2021 15:29:22 +0200 Subject: [PATCH] Return gateway resource for /gateways/{id} This is instead of redirecting to /gateways/{id}/structures as this is now moving more towards *not* reproducing an actual OPTIMADE implementation. --- optimade_gateway/routers/gateways.py | 24 ++++++++++++++---------- tests/conftest.py | 13 +++++++++++++ tests/routers/test_gateways.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/optimade_gateway/routers/gateways.py b/optimade_gateway/routers/gateways.py index 518c8a1e..fbc8da92 100644 --- a/optimade_gateway/routers/gateways.py +++ b/optimade_gateway/routers/gateways.py @@ -7,7 +7,6 @@ where, `id` may be left out. """ from fastapi import APIRouter, Depends, Request -from fastapi.responses import RedirectResponse from optimade.models import ToplevelLinks from optimade.server.query_params import EntryListingQueryParams from optimade.server.schemas import ERROR_RESPONSES @@ -121,15 +120,20 @@ async def post_gateways( async def get_gateway(request: Request, gateway_id: str) -> GatewaysResponseSingle: """`GET /gateways/{gateway ID}` - Represent an OPTIMADE server. - - !!! note - For now, redirect to the gateway's `/structures` entry listing endpoint. - + Return a single [`GatewayResource`][optimade_gateway.models.gateways.GatewayResource]. """ - from optimade_gateway.routers.utils import validate_resource + from optimade.server.routers.utils import meta_values + from optimade_gateway.routers.utils import get_valid_resource + + result = await get_valid_resource(GATEWAYS_COLLECTION, gateway_id) - await validate_resource(GATEWAYS_COLLECTION, gateway_id) - return RedirectResponse( - request.url.replace(path=f"{request.url.path.rstrip('/')}/structures") + return GatewaysResponseSingle( + links=ToplevelLinks(next=None), + data=result, + meta=meta_values( + url=request.url, + data_returned=1, + data_available=await GATEWAYS_COLLECTION.count(), + more_data_available=False, + ), ) diff --git a/tests/conftest.py b/tests/conftest.py index 6519243d..1df9a105 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -132,6 +132,19 @@ async def _get_gateway(id: str) -> dict: return _get_gateway +@pytest.fixture +async def random_gateway() -> dict: + """Get a random gateway currently in the MongoDB""" + from optimade_gateway.mongo.database import MONGO_DB + + gateway_ids = set() + async for gateway in MONGO_DB["gateways"].find( + filter={}, projection={"id": True, "_id": False} + ): + gateway_ids.add(gateway["id"]) + return gateway_ids.pop() + + @pytest.fixture async def reset_db_after(top_dir: Path) -> None: """Reset MongoDB with original test data after the test has run""" diff --git a/tests/routers/test_gateways.py b/tests/routers/test_gateways.py index 69fb2787..ce25f427 100644 --- a/tests/routers/test_gateways.py +++ b/tests/routers/test_gateways.py @@ -190,6 +190,7 @@ async def test_post_gateways_database_ids( assert db["id"] in data["database_ids"] +@pytest.mark.usefixtures("reset_db_after") async def test_post_gateways_create_with_db_ids( client: Callable[ [str, FastAPI, str, Literal["get", "post", "put", "delete", "patch"]], @@ -252,3 +253,30 @@ async def test_post_gateways_create_with_db_ids( db_datum = await MONGO_DB["gateways"].find_one(mongo_filter) for db in db_datum["databases"]: assert db["id"] in [data["databases"][0]["id"], data["database_ids"][0]] + + +async def test_get_single_gateway( + client: Callable[ + [str, FastAPI, str, Literal["get", "post", "put", "delete", "patch"]], + Awaitable[httpx.Response], + ], + random_gateway: str, + top_dir: Path, +): + """Test GET /gateways/{gateway_id}""" + import json + + from optimade_gateway.models.responses import GatewaysResponseSingle + + response = await client(f"/gateways/{random_gateway}") + + assert response.status_code == 200, f"Request failed: {response.json()}" + response = GatewaysResponseSingle(**response.json()) + assert response + + with open(top_dir / "tests/static/test_gateways.json") as handle: + test_data = json.load(handle) + + assert response.meta.data_returned == 1 + assert response.meta.data_available == len(test_data) + assert not response.meta.more_data_available