Skip to content

Commit 7368d8d

Browse files
committed
patch bugs.
1 parent b412576 commit 7368d8d

File tree

4 files changed

+48
-34
lines changed

4 files changed

+48
-34
lines changed

stac_fastapi/core/stac_fastapi/core/utilities.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
from typing import Any, Dict, List, Optional, Set, Union
99

1010
from stac_fastapi.core.models.patch import ElasticPath
11-
from stac_fastapi.types.stac import Item, PatchOperation
11+
from stac_fastapi.types.stac import (
12+
Item,
13+
PatchAddReplaceTest,
14+
PatchOperation,
15+
PatchRemove,
16+
)
1217

1318
MAX_LIMIT = 10000
1419

@@ -152,7 +157,7 @@ def merge_to_operations(data: Dict) -> List:
152157
for key, value in data.copy().items():
153158

154159
if value is None:
155-
operations.append(PatchOperation(op="remove", path=key))
160+
operations.append(PatchRemove(op="remove", path=key))
156161

157162
elif isinstance(value, dict):
158163
nested_operations = merge_to_operations(value)
@@ -162,7 +167,7 @@ def merge_to_operations(data: Dict) -> List:
162167
operations.append(nested_operation)
163168

164169
else:
165-
operations.append(PatchOperation(op="add", path=key, value=value))
170+
operations.append(PatchAddReplaceTest(op="add", path=key, value=value))
166171

167172
return operations
168173

@@ -210,7 +215,7 @@ def copy_commands(
210215
from_path (ElasticPath): Path to copy from
211216
212217
"""
213-
check_commands(operation.op, from_path, True)
218+
check_commands(commands=commands, op=operation.op, path=from_path, from_path=True)
214219

215220
if from_path.index:
216221
commands.append(
@@ -329,22 +334,24 @@ def operations_to_script(operations: List) -> Dict:
329334
ElasticPath(path=operation.from_) if hasattr(operation, "from_") else None
330335
)
331336

332-
check_commands(commands, operation.op, path)
337+
check_commands(commands=commands, op=operation.op, path=path)
333338

334339
if operation.op in ["copy", "move"]:
335-
copy_commands(commands, operation, path, from_path)
340+
copy_commands(
341+
commands=commands, operation=operation, path=path, from_path=from_path
342+
)
336343

337344
if operation.op in ["remove", "move"]:
338345
remove_path = from_path if from_path else path
339-
remove_commands(commands, remove_path)
346+
remove_commands(commands=commands, path=remove_path)
340347

341348
if operation.op in ["add", "replace"]:
342-
add_commands(commands, operation, path)
349+
add_commands(commands=commands, operation=operation, path=path)
343350

344351
if operation.op == "test":
345-
test_commands(commands, operation, path)
352+
test_commands(commands=commands, operation=operation, path=path)
346353

347-
source = commands_to_source(commands)
354+
source = commands_to_source(commands=commands)
348355

349356
return {
350357
"source": source,

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,9 @@ async def json_patch_item(
987987
)
988988

989989
except exceptions.BadRequestError as exc:
990-
raise KeyError(exc.info["error"]["caused_by"]["to_string"]) from exc
990+
raise exceptions.BadRequestError(
991+
exc.info["error"]["caused_by"]["to_string"]
992+
) from exc
991993

992994
item = await self.get_one_item(collection_id, item_id)
993995

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,9 @@ async def json_patch_item(
10191019
)
10201020

10211021
except exceptions.BadRequestError as exc:
1022-
raise KeyError(exc.info["error"]["caused_by"]["to_string"]) from exc
1022+
raise exceptions.BadRequestError(
1023+
exc.info["error"]["caused_by"]["to_string"]
1024+
) from exc
10231025

10241026
item = await self.get_one_item(collection_id, item_id)
10251027

stac_fastapi/tests/clients/test_elasticsearch.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import os
12
import uuid
23
from copy import deepcopy
34
from typing import Callable
45

56
import pytest
67
from stac_pydantic import Item, api
78

9+
if os.getenv("BACKEND", "elasticsearch").lower() == "opensearch":
10+
from elasticsearch import exceptions
11+
else:
12+
from opensearchpy import exceptions
13+
814
from stac_fastapi.extensions.third_party.bulk_transactions import Items
915
from stac_fastapi.types.errors import ConflictError, NotFoundError
1016
from stac_fastapi.types.stac import PatchAddReplaceTest, PatchMoveCopy, PatchRemove
@@ -265,15 +271,14 @@ async def test_merge_patch_item_remove(ctx, core_client, txn_client):
265271
await txn_client.merge_patch_item(
266272
collection_id=collection_id,
267273
item_id=item_id,
268-
item={"properties": {"foo": None, "hello": None}},
274+
item={"properties": {"gsd": None}},
269275
request=MockRequest,
270276
)
271277

272278
updated_item = await core_client.get_item(
273279
item_id, collection_id, request=MockRequest
274280
)
275-
assert "foo" not in updated_item["properties"]
276-
assert "hello" not in updated_item["properties"]
281+
assert "gsd" not in updated_item["properties"]
277282

278283

279284
@pytest.mark.asyncio
@@ -298,7 +303,7 @@ async def test_json_patch_item_add(ctx, core_client, txn_client):
298303
item_id, collection_id, request=MockRequest
299304
)
300305

301-
assert updated_item["properties"]["bar"] == "foo"
306+
assert updated_item["properties"]["foo"] == "bar"
302307

303308

304309
@pytest.mark.asyncio
@@ -308,7 +313,7 @@ async def test_json_patch_item_replace(ctx, core_client, txn_client):
308313
item_id = item["id"]
309314
operations = [
310315
PatchAddReplaceTest.model_validate(
311-
{"op": "replace", "path": "properties.foo", "value": 100}
316+
{"op": "replace", "path": "properties.gsd", "value": 100}
312317
),
313318
]
314319

@@ -323,7 +328,7 @@ async def test_json_patch_item_replace(ctx, core_client, txn_client):
323328
item_id, collection_id, request=MockRequest
324329
)
325330

326-
assert updated_item["properties"]["foo"] == 100
331+
assert updated_item["properties"]["gsd"] == 100
327332

328333

329334
@pytest.mark.asyncio
@@ -333,7 +338,7 @@ async def test_json_patch_item_test(ctx, core_client, txn_client):
333338
item_id = item["id"]
334339
operations = [
335340
PatchAddReplaceTest.model_validate(
336-
{"op": "test", "path": "properties.foo", "value": 100}
341+
{"op": "test", "path": "properties.gsd", "value": 15}
337342
),
338343
]
339344

@@ -348,7 +353,7 @@ async def test_json_patch_item_test(ctx, core_client, txn_client):
348353
item_id, collection_id, request=MockRequest
349354
)
350355

351-
assert updated_item["properties"]["foo"] == 100
356+
assert updated_item["properties"]["gsd"] == 15
352357

353358

354359
@pytest.mark.asyncio
@@ -358,7 +363,7 @@ async def test_json_patch_item_move(ctx, core_client, txn_client):
358363
item_id = item["id"]
359364
operations = [
360365
PatchMoveCopy.model_validate(
361-
{"op": "move", "path": "properties.bar", "from": "properties.foo"}
366+
{"op": "move", "path": "properties.bar", "from": "properties.gsd"}
362367
),
363368
]
364369

@@ -373,8 +378,8 @@ async def test_json_patch_item_move(ctx, core_client, txn_client):
373378
item_id, collection_id, request=MockRequest
374379
)
375380

376-
assert updated_item["properties"]["bar"] == 100
377-
assert "foo" not in updated_item["properties"]
381+
assert updated_item["properties"]["bar"] == 15
382+
assert "gsd" not in updated_item["properties"]
378383

379384

380385
@pytest.mark.asyncio
@@ -384,7 +389,7 @@ async def test_json_patch_item_copy(ctx, core_client, txn_client):
384389
item_id = item["id"]
385390
operations = [
386391
PatchMoveCopy.model_validate(
387-
{"op": "copy", "path": "properties.foo", "from": "properties.bar"}
392+
{"op": "copy", "path": "properties.foo", "from": "properties.gsd"}
388393
),
389394
]
390395

@@ -399,7 +404,7 @@ async def test_json_patch_item_copy(ctx, core_client, txn_client):
399404
item_id, collection_id, request=MockRequest
400405
)
401406

402-
assert updated_item["properties"]["foo"] == updated_item["properties"]["bar"]
407+
assert updated_item["properties"]["foo"] == updated_item["properties"]["gsd"]
403408

404409

405410
@pytest.mark.asyncio
@@ -408,8 +413,7 @@ async def test_json_patch_item_remove(ctx, core_client, txn_client):
408413
collection_id = item["collection"]
409414
item_id = item["id"]
410415
operations = [
411-
PatchRemove.model_validate({"op": "remove", "path": "properties.foo"}),
412-
PatchRemove.model_validate({"op": "remove", "path": "properties.bar"}),
416+
PatchRemove.model_validate({"op": "remove", "path": "properties.gsd"}),
413417
]
414418

415419
await txn_client.json_patch_item(
@@ -423,8 +427,7 @@ async def test_json_patch_item_remove(ctx, core_client, txn_client):
423427
item_id, collection_id, request=MockRequest
424428
)
425429

426-
assert "foo" not in updated_item["properties"]
427-
assert "bar" not in updated_item["properties"]
430+
assert "gsd" not in updated_item["properties"]
428431

429432

430433
@pytest.mark.asyncio
@@ -438,7 +441,7 @@ async def test_json_patch_item_test_wrong_value(ctx, core_client, txn_client):
438441
),
439442
]
440443

441-
with pytest.raises(ConflictError):
444+
with pytest.raises(exceptions.BadRequestError):
442445

443446
await txn_client.json_patch_item(
444447
collection_id=collection_id,
@@ -461,7 +464,7 @@ async def test_json_patch_item_replace_property_does_not_exists(
461464
),
462465
]
463466

464-
with pytest.raises(ConflictError):
467+
with pytest.raises(exceptions.BadRequestError):
465468

466469
await txn_client.json_patch_item(
467470
collection_id=collection_id,
@@ -482,7 +485,7 @@ async def test_json_patch_item_remove_property_does_not_exists(
482485
PatchRemove.model_validate({"op": "remove", "path": "properties.foo"}),
483486
]
484487

485-
with pytest.raises(ConflictError):
488+
with pytest.raises(exceptions.BadRequestError):
486489

487490
await txn_client.json_patch_item(
488491
collection_id=collection_id,
@@ -505,7 +508,7 @@ async def test_json_patch_item_move_property_does_not_exists(
505508
),
506509
]
507510

508-
with pytest.raises(ConflictError):
511+
with pytest.raises(exceptions.BadRequestError):
509512

510513
await txn_client.json_patch_item(
511514
collection_id=collection_id,
@@ -528,7 +531,7 @@ async def test_json_patch_item_copy_property_does_not_exists(
528531
),
529532
]
530533

531-
with pytest.raises(ConflictError):
534+
with pytest.raises(exceptions.BadRequestError):
532535

533536
await txn_client.json_patch_item(
534537
collection_id=collection_id,

0 commit comments

Comments
 (0)