Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add extension to curie route #319

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion application/routers/curie.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import logging

from typing import Optional
from fastapi import APIRouter, HTTPException, Depends
from starlette.requests import Request
from starlette.responses import HTMLResponse, RedirectResponse
from sqlalchemy.orm import Session

from application.core.utils import (
DigitalLandJSONResponse,
)
from application.search.enum import SuffixEntity
from application.db.models import LookupOrm, EntityOrm
from application.db.session import get_session

Expand All @@ -17,6 +22,7 @@ def get_entity_redirect_by_curie(
prefix: str,
reference: str,
session: Session = Depends(get_session),
extension: Optional[SuffixEntity] = None,
):
lookup = (
session.query(LookupOrm.entity.label("entity"))
Expand All @@ -32,17 +38,39 @@ def get_entity_redirect_by_curie(
if lookup is None:
raise HTTPException(status_code=404)
else:
url = request.url_for("get_entity", entity=lookup.entity)
if extension:
url = request.url_for(
"get_entity", entity=lookup.entity, extension=extension.value
)
logging.error(extension.value)
else:
url = request.url_for("get_entity", entity=lookup.entity)

return RedirectResponse(url, status_code=303)


router.add_api_route(
"/{prefix}/reference/{reference}.{extension}",
endpoint=get_entity_redirect_by_curie,
response_class=HTMLResponse,
include_in_schema=False,
)

router.add_api_route(
"/{prefix}/reference/{reference}",
endpoint=get_entity_redirect_by_curie,
response_class=HTMLResponse,
include_in_schema=False,
)


router.add_api_route(
"/{prefix}:{reference}.{extension}",
endpoint=get_entity_redirect_by_curie,
response_class=DigitalLandJSONResponse,
include_in_schema=False,
)

router.add_api_route(
"/{prefix}:{reference}",
endpoint=get_entity_redirect_by_curie,
Expand Down
6 changes: 6 additions & 0 deletions tests/acceptance/api_contract/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
Tests in this folder represent the contract we hold
with users of this api, it is primarily split by
route or by specific user groups where a full user journey
is reuired
"""
74 changes: 74 additions & 0 deletions tests/acceptance/api_contract/test_curie_route.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""
A python module which holds tests that have to pass for the curie route of
our api. essentially testing the contract with consumers
of the api functionality. Does not focus on looking at html responses
"""
import pytest

from application.db.models import EntityOrm

mock_entities = [
{
"entity": "106",
"name": "A space",
"entry_date": "2019-01-07",
"start_date": "2019-01-05",
"end_date": "2020-01-07",
"dataset": "greenspace",
"json": None,
"organisation_entity": None,
"prefix": "greenspace",
"reference": "Q1234567",
"typology": "geography",
"geometry": "MultiPolygon (((-0.3386878967285156 53.74426323597749, -0.337904691696167 53.743857158459996, -0.33673524856567383 53.744003093019586, -0.33637046813964844 53.74463124033804, -0.3365743160247803 53.74525937826645, -0.33737897872924805 53.74541799747043, -0.33875226974487305 53.74505000000031, -0.3386878967285156 53.74426323597749)))", # noqa: E501
"point": "POINT (-0.33737897872924805 53.74541799747043)",
},
{
"entity": "107",
"name": "A space",
"entry_date": "2019-01-07",
"start_date": "2019-01-05",
"end_date": "2020-01-07",
"dataset": "greenspace",
"json": None,
"organisation_entity": None,
"prefix": "greenspace",
"reference": "Q1234568",
"typology": "geography",
"geometry": "MultiPolygon (((-0.3386878967285156 53.74426323597749, -0.337904691696167 53.743857158459996, -0.33673524856567383 53.744003093019586, -0.33637046813964844 53.74463124033804, -0.3365743160247803 53.74525937826645, -0.33737897872924805 53.74541799747043, -0.33875226974487305 53.74505000000031, -0.3386878967285156 53.74426323597749)))", # noqa: E501
"point": "POINT (-0.33737897872924805 53.74541799747043)",
},
]

# Get Curie Route (/curie/{prefix}:{reference}.{extension}) Testing
# =================================================================
# Path Parameter Testing
# ----------------------


@pytest.mark.parametrize(
"extension,expected_headers",
[
("json", "application/json"),
("geojson", "application/json"),
],
)
def test_curie_extension_returns_correct_headers(
client, db_session, extension, expected_headers
):
"""
Test the extension path parameter will return the correct type of response
"""
# add an entity with the correct_curie
for entity in mock_entities:
db_session.add(EntityOrm(**entity))
db_session.commit()
if extension:
endpoint = f"/curie/greenspace:Q1234568.{extension}"
else:
endpoint = "/curie/greenspace:Q1234568"

response = client.get(endpoint)
headers = response.headers["Content-Type"]

assert expected_headers in headers, f"Incorrect headers returned: '{headers}'"
71 changes: 71 additions & 0 deletions tests/acceptance/api_contract/test_entity_route.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
Module to test the routes that follow /entity. this includes both the get entity and the entity search
"""
import pytest
from application.db.models import EntityOrm

mock_entities = [
{
"entity": "106",
"name": "A space",
"entry_date": "2019-01-07",
"start_date": "2019-01-05",
"end_date": "2020-01-07",
"dataset": "greenspace",
"json": None,
"organisation_entity": None,
"prefix": "greenspace",
"reference": "Q1234567",
"typology": "geography",
"geometry": "MultiPolygon (((-0.3386878967285156 53.74426323597749, -0.337904691696167 53.743857158459996, -0.33673524856567383 53.744003093019586, -0.33637046813964844 53.74463124033804, -0.3365743160247803 53.74525937826645, -0.33737897872924805 53.74541799747043, -0.33875226974487305 53.74505000000031, -0.3386878967285156 53.74426323597749)))", # noqa: E501
"point": "POINT (-0.33737897872924805 53.74541799747043)",
},
{
"entity": "107",
"name": "A space",
"entry_date": "2019-01-07",
"start_date": "2019-01-05",
"end_date": "2020-01-07",
"dataset": "greenspace",
"json": {"local-planning-authority": "E01000001"},
"organisation_entity": None,
"prefix": "greenspace",
"reference": "Q1234568",
"typology": "geography",
"geometry": "MultiPolygon (((-0.3386878967285156 53.74426323597749, -0.337904691696167 53.743857158459996, -0.33673524856567383 53.744003093019586, -0.33637046813964844 53.74463124033804, -0.3365743160247803 53.74525937826645, -0.33737897872924805 53.74541799747043, -0.33875226974487305 53.74505000000031, -0.3386878967285156 53.74426323597749)))", # noqa: E501
"point": "POINT (-0.33737897872924805 53.74541799747043)",
},
]

# Get Entity Route (/entity/{entity}.{extension}) Testing
# =======================================================
# Path Parameter Testing
# ----------------------


@pytest.mark.parametrize(
"extension,expected_headers",
[
("json", "application/json"),
("geojson", "application/json"),
],
)
def test_entity_extension_json(client, db_session, extension, expected_headers):
"""
Test the extension path parameter works and givens
the correct resposne when asking for a json
"""
# add an entity with the correct_curie
for entity in mock_entities:
db_session.add(EntityOrm(**entity))
db_session.commit()

if extension:
endpoint = f"/curie/greenspace:Q1234568.{extension}"
else:
endpoint = "/curie/greenspace:Q1234568"

response = client.get(endpoint)
headers = response.headers["Content-Type"]

assert expected_headers in headers, f"Incorrect headers returned: '{headers}'"
Loading