Skip to content

Commit

Permalink
Return added Item/Collection in Transactions response (#424)
Browse files Browse the repository at this point in the history
* Add collection_id path parameter and check against Item collection property

* Fix unformatted f-strings

* Fix Item PUT endpoint per #385

* Update API tests to use new PUT paths

* Make equivalent changes to sqlalchemy backend

* Add CHANGELOG entry for #425

* Fix failing tests from previous merge

* Return 400 for Item id or collection conflicts

* Add failing tests

* Return added Item/Collection from Transactions endpoints

* Format changes

* Add CHANGES entry for #424

* Get Transactions Item response collection id from path parameter

* remove duplicate test cases

Co-authored-by: Jeff Albrecht <[email protected]>
  • Loading branch information
duckontheweb and geospatial-jeff authored Aug 2, 2022
1 parent d2fcf13 commit e6b353a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* Transactions Extension update Item endpoint validates that the `{collection_id}` path parameter matches the Item `"collection"` property
from the request body, if present, and falls back to using the path parameter if no `"collection"` property is found in the body
([#425](https://github.com/stac-utils/stac-fastapi/pull/425))
* PGStac Backend Transactions endpoints return added Item/Collection instead of Item/Collection from request ([#424](https://github.com/stac-utils/stac-fastapi/pull/424))

## [2.3.0]

Expand Down
26 changes: 22 additions & 4 deletions stac_fastapi/pgstac/stac_fastapi/pgstac/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Items,
)
from stac_fastapi.pgstac.db import dbfunc
from stac_fastapi.pgstac.models.links import CollectionLinks, ItemLinks
from stac_fastapi.types import stac as stac_types
from stac_fastapi.types.core import AsyncBaseTransactionsClient

Expand All @@ -37,7 +38,12 @@ async def create_item(
request = kwargs["request"]
pool = request.app.state.writepool
await dbfunc(pool, "create_item", item)
return item
item["links"] = await ItemLinks(
collection_id=collection_id,
item_id=item["id"],
request=request,
).get_links(extra_links=item.get("links"))
return stac_types.Item(**item)

async def update_item(
self, collection_id: str, item_id: str, item: stac_types.Item, **kwargs
Expand All @@ -59,7 +65,12 @@ async def update_item(
request = kwargs["request"]
pool = request.app.state.writepool
await dbfunc(pool, "update_item", item)
return item
item["links"] = await ItemLinks(
collection_id=collection_id,
item_id=item["id"],
request=request,
).get_links(extra_links=item.get("links"))
return stac_types.Item(**item)

async def create_collection(
self, collection: stac_types.Collection, **kwargs
Expand All @@ -68,7 +79,11 @@ async def create_collection(
request = kwargs["request"]
pool = request.app.state.writepool
await dbfunc(pool, "create_collection", collection)
return collection
collection["links"] = await CollectionLinks(
collection_id=collection["id"], request=request
).get_links(extra_links=collection.get("links"))

return stac_types.Collection(**collection)

async def update_collection(
self, collection: stac_types.Collection, **kwargs
Expand All @@ -77,7 +92,10 @@ async def update_collection(
request = kwargs["request"]
pool = request.app.state.writepool
await dbfunc(pool, "update_collection", collection)
return collection
collection["links"] = await CollectionLinks(
collection_id=collection["id"], request=request
).get_links(extra_links=collection.get("links"))
return stac_types.Collection(**collection)

async def delete_item(
self, item_id: str, **kwargs
Expand Down
13 changes: 13 additions & 0 deletions stac_fastapi/pgstac/tests/resources/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ async def test_create_collection(app_client, load_test_data: Callable):
get_coll = Collection.parse_obj(resp.json())
assert post_coll.dict(exclude={"links"}) == get_coll.dict(exclude={"links"})

post_self_link = next(
(link for link in post_coll.links if link.rel == "self"), None
)
get_self_link = next((link for link in get_coll.links if link.rel == "self"), None)
assert post_self_link is not None and get_self_link is not None
assert post_self_link.href == get_self_link.href


async def test_update_collection(app_client, load_test_data, load_test_collection):
in_coll = load_test_collection
in_coll.keywords.append("newkeyword")

resp = await app_client.put("/collections", json=in_coll.dict())
assert resp.status_code == 200
put_coll = Collection.parse_obj(resp.json())

resp = await app_client.get(f"/collections/{in_coll.id}")
assert resp.status_code == 200
Expand All @@ -35,6 +43,11 @@ async def test_update_collection(app_client, load_test_data, load_test_collectio
assert in_coll.dict(exclude={"links"}) == get_coll.dict(exclude={"links"})
assert "newkeyword" in get_coll.keywords

put_self_link = next((link for link in put_coll.links if link.rel == "self"), None)
get_self_link = next((link for link in get_coll.links if link.rel == "self"), None)
assert put_self_link is not None and get_self_link is not None
assert put_self_link.href == get_self_link.href


async def test_delete_collection(
app_client, load_test_data: Callable, load_test_collection
Expand Down
13 changes: 13 additions & 0 deletions stac_fastapi/pgstac/tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ async def test_create_item(app_client, load_test_data: Callable, load_test_colle
get_item = Item.parse_obj(resp.json())
assert in_item.dict(exclude={"links"}) == get_item.dict(exclude={"links"})

post_self_link = next(
(link for link in post_item.links if link.rel == "self"), None
)
get_self_link = next((link for link in get_item.links if link.rel == "self"), None)
assert post_self_link is not None and get_self_link is not None
assert post_self_link.href == get_self_link.href


async def test_create_item_mismatched_collection_id(
app_client, load_test_data: Callable, load_test_collection
Expand Down Expand Up @@ -141,6 +148,7 @@ async def test_update_item(
f"/collections/{coll.id}/items/{item.id}", content=item.json()
)
assert resp.status_code == 200
put_item = Item.parse_obj(resp.json())

resp = await app_client.get(f"/collections/{coll.id}/items/{item.id}")
assert resp.status_code == 200
Expand All @@ -149,6 +157,11 @@ async def test_update_item(
assert item.dict(exclude={"links"}) == get_item.dict(exclude={"links"})
assert get_item.properties.description == "Update Test"

post_self_link = next((link for link in put_item.links if link.rel == "self"), None)
get_self_link = next((link for link in get_item.links if link.rel == "self"), None)
assert post_self_link is not None and get_self_link is not None
assert post_self_link.href == get_self_link.href


async def test_update_item_mismatched_collection_id(
app_client, load_test_data: Callable, load_test_collection, load_test_item
Expand Down

0 comments on commit e6b353a

Please sign in to comment.