Skip to content

Commit fa6afb3

Browse files
committed
Adding collection patch tests.
1 parent fb6fe82 commit fa6afb3

File tree

6 files changed

+890
-538
lines changed

6 files changed

+890
-538
lines changed

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ async def json_patch_collection(
916916
operations=operations,
917917
base_url=str(kwargs["request"].base_url),
918918
)
919+
919920
return CollectionSerializer.db_to_stac(
920921
collection,
921922
kwargs["request"],

stac_fastapi/core/stac_fastapi/core/models/patch.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""patch helpers."""
22

3-
from typing import Any, Optional
3+
from typing import Any, Optional, Union
44

55
from pydantic import BaseModel, computed_field, model_validator
66

@@ -17,7 +17,7 @@ class ElasticPath(BaseModel):
1717
nest: Optional[str] = None
1818
partition: Optional[str] = None
1919
key: Optional[str] = None
20-
index: Optional[int] = None
20+
_index: Optional[int] = None
2121

2222
@model_validator(mode="before")
2323
@classmethod
@@ -31,13 +31,26 @@ def validate_model(cls, data: Any):
3131

3232
data["nest"], data["partition"], data["key"] = data["path"].rpartition(".")
3333

34-
if data["key"].isdigit():
35-
data["index"] = int(data["key"])
36-
data["path"] = f"{data['nest']}[{data['index']}]"
34+
if data["key"].isdigit() or data["key"] == "-":
35+
data["_index"] = -1 if data["key"] == "-" else int(data["key"])
3736
data["nest"], data["partition"], data["key"] = data["nest"].rpartition(".")
37+
data["path"] = f"{data['nest']}[{data['_index']}]"
3838

3939
return data
4040

41+
@property
42+
def index(self) -> Union[int, str, None]:
43+
"""Compute location of path.
44+
45+
Returns:
46+
str: path location
47+
"""
48+
if self._index and self._index < 0:
49+
50+
return f"ctx._source.{self.location}.size() - {-self._index}"
51+
52+
return self._index
53+
4154
@computed_field # type: ignore[misc]
4255
@property
4356
def location(self) -> str:

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,12 +1216,18 @@ async def json_patch_collection(
12161216

12171217
script = operations_to_script(script_operations)
12181218

1219-
await self.client.update(
1220-
index=COLLECTIONS_INDEX,
1221-
id=collection_id,
1222-
script=script,
1223-
refresh=True,
1224-
)
1219+
try:
1220+
await self.client.update(
1221+
index=COLLECTIONS_INDEX,
1222+
id=collection_id,
1223+
script=script,
1224+
refresh=True,
1225+
)
1226+
1227+
except exceptions.BadRequestError as exc:
1228+
raise HTTPException(
1229+
status_code=400, detail=exc.info["error"]["caused_by"]["to_string"]
1230+
) from exc
12251231

12261232
collection = await self.find_collection(collection_id)
12271233

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ async def json_patch_item(
10131013

10141014
try:
10151015
await self.client.update(
1016-
index=index_by_collection_id(collection_id),
1016+
index=index_alias_by_collection_id(collection_id),
10171017
id=mk_item_id(item_id, collection_id),
10181018
body={"script": script},
10191019
refresh=True,
@@ -1248,14 +1248,19 @@ async def json_patch_collection(
12481248

12491249
script = operations_to_script(script_operations)
12501250

1251-
if not new_collection_id:
1251+
try:
12521252
await self.client.update(
12531253
index=COLLECTIONS_INDEX,
12541254
id=collection_id,
1255-
body={"script": script},
1256-
refresh=refresh,
1255+
script=script,
1256+
refresh=True,
12571257
)
12581258

1259+
except exceptions.BadRequestError as exc:
1260+
raise HTTPException(
1261+
status_code=400, detail=exc.info["error"]["caused_by"]["to_string"]
1262+
) from exc
1263+
12591264
collection = await self.find_collection(collection_id)
12601265

12611266
if new_collection_id:

0 commit comments

Comments
 (0)