Skip to content

Commit 4189df8

Browse files
committed
Add tests for merge to operations.
1 parent be4962f commit 4189df8

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

stac_fastapi/extensions/tests/test_transaction.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ def patch_item(
5454
patch: Union[PartialItem, List[PatchOperation]],
5555
**kwargs,
5656
):
57+
if isinstance(patch, PartialItem):
58+
patch = patch.operations()
59+
5760
return {
5861
"path_collection_id": collection_id,
5962
"path_item_id": item_id,
@@ -78,6 +81,9 @@ def patch_collection(
7881
patch: Union[PartialCollection, List[PatchOperation]],
7982
**kwargs,
8083
):
84+
if isinstance(patch, PartialCollection):
85+
patch = patch.operations()
86+
8187
return {
8288
"path_collection_id": collection_id,
8389
"patch": patch,
@@ -126,15 +132,18 @@ def test_patch_operation_item(client: TestClient) -> None:
126132
]
127133

128134

129-
def test_patch_merge_item(client: TestClient, item: Item) -> None:
135+
def test_patch_merge_item(client: TestClient) -> None:
130136
response = client.patch(
131137
"/collections/a-collection/items/an-item",
132-
content=json.dumps(item),
138+
content=json.dumps({"properties": {"hello": "world", "foo": None}}),
133139
)
134140
assert response.is_success, response.text
135141
assert response.json()["path_collection_id"] == "a-collection"
136142
assert response.json()["path_item_id"] == "an-item"
137-
assert response.json()["patch"]["type"] == "Feature"
143+
assert response.json()["patch"] == [
144+
{"op": "add", "path": "/properties/hello", "value": "world"},
145+
{"op": "remove", "path": "/properties/foo"},
146+
]
138147

139148

140149
def test_delete_item(client: TestClient) -> None:
@@ -169,14 +178,17 @@ def test_patch_operation_collection(client: TestClient) -> None:
169178
]
170179

171180

172-
def test_patch_merge_collection(client: TestClient, collection: Collection) -> None:
181+
def test_patch_merge_collection(client: TestClient) -> None:
173182
response = client.patch(
174183
"/collections/a-collection",
175-
content=json.dumps(collection),
184+
content=json.dumps({"summaries": {"hello": "world", "foo": None}}),
176185
)
177186
assert response.is_success, response.text
178187
assert response.json()["path_collection_id"] == "a-collection"
179-
assert response.json()["patch"]["type"] == "Collection"
188+
assert response.json()["patch"] == [
189+
{"op": "add", "path": "/summaries/hello", "value": "world"},
190+
{"op": "remove", "path": "/summaries/foo"},
191+
]
180192

181193

182194
def test_delete_collection(client: TestClient, collection: Collection) -> None:

stac_fastapi/types/stac_fastapi/types/stac.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,19 @@ def merge_to_operations(data: Dict) -> List[PatchOperation]:
172172

173173
for key, value in data.copy().items():
174174
if value is None:
175-
operations.append(PatchRemove(op="remove", path=key))
175+
operations.append(PatchRemove(op="remove", path=f"/{key}"))
176176

177177
elif isinstance(value, dict):
178178
nested_operations = BasePartial.merge_to_operations(value)
179179

180180
for nested_operation in nested_operations:
181-
nested_operation.path = f"{key}/{nested_operation.path}"
181+
nested_operation.path = f"/{key}{nested_operation.path}"
182182
operations.append(nested_operation)
183183

184184
else:
185-
operations.append(PatchAddReplaceTest(op="add", path=key, value=value))
185+
operations.append(
186+
PatchAddReplaceTest(op="add", path=f"/{key}", value=value)
187+
)
186188

187189
return operations
188190

0 commit comments

Comments
 (0)