Skip to content

Commit

Permalink
Return gateway resource for /gateways/{id}
Browse files Browse the repository at this point in the history
This is instead of redirecting to /gateways/{id}/structures as this is
now moving more towards *not* reproducing an actual OPTIMADE
implementation.
  • Loading branch information
CasperWA committed Aug 10, 2021
1 parent 793e297 commit 6d02673
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
24 changes: 14 additions & 10 deletions optimade_gateway/routers/gateways.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
),
)
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
28 changes: 28 additions & 0 deletions tests/routers/test_gateways.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]],
Expand Down Expand Up @@ -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

0 comments on commit 6d02673

Please sign in to comment.