|
| 1 | +import json |
| 2 | +from typing import Iterator |
| 3 | + |
| 4 | +import pytest |
| 5 | +from starlette.testclient import TestClient |
| 6 | + |
| 7 | +from stac_fastapi.api.app import StacApi |
| 8 | +from stac_fastapi.extensions.core import TransactionExtension |
| 9 | +from stac_fastapi.types.config import ApiSettings |
| 10 | +from stac_fastapi.types.core import BaseCoreClient, BaseTransactionsClient |
| 11 | +from stac_fastapi.types.stac import Item, ItemCollection |
| 12 | + |
| 13 | + |
| 14 | +class DummyCoreClient(BaseCoreClient): |
| 15 | + def all_collections(self, *args, **kwargs): |
| 16 | + raise NotImplementedError |
| 17 | + |
| 18 | + def get_collection(self, *args, **kwargs): |
| 19 | + raise NotImplementedError |
| 20 | + |
| 21 | + def get_item(self, *args, **kwargs): |
| 22 | + raise NotImplementedError |
| 23 | + |
| 24 | + def get_search(self, *args, **kwargs): |
| 25 | + raise NotImplementedError |
| 26 | + |
| 27 | + def post_search(self, *args, **kwargs): |
| 28 | + raise NotImplementedError |
| 29 | + |
| 30 | + def item_collection(self, *args, **kwargs): |
| 31 | + raise NotImplementedError |
| 32 | + |
| 33 | + |
| 34 | +class DummyTransactionsClient(BaseTransactionsClient): |
| 35 | + """Defines a pattern for implementing the STAC transaction extension.""" |
| 36 | + |
| 37 | + def create_item(self, item: Item | ItemCollection, *args, **kwargs): |
| 38 | + return {"created": True, "type": item["type"]} |
| 39 | + |
| 40 | + def update_item(self, *args, **kwargs): |
| 41 | + raise NotImplementedError |
| 42 | + |
| 43 | + def delete_item(self, *args, **kwargs): |
| 44 | + raise NotImplementedError |
| 45 | + |
| 46 | + def create_collection(self, *args, **kwargs): |
| 47 | + raise NotImplementedError |
| 48 | + |
| 49 | + def update_collection(self, *args, **kwargs): |
| 50 | + raise NotImplementedError |
| 51 | + |
| 52 | + def delete_collection(self, *args, **kwargs): |
| 53 | + raise NotImplementedError |
| 54 | + |
| 55 | + |
| 56 | +def test_create_item(client: TestClient, item: Item) -> None: |
| 57 | + response = client.post("/collections/a-collection/items", content=json.dumps(item)) |
| 58 | + assert response.is_success, response.text |
| 59 | + assert response.json()["type"] == "Feature" |
| 60 | + |
| 61 | + |
| 62 | +def test_create_item_collection( |
| 63 | + client: TestClient, item_collection: ItemCollection |
| 64 | +) -> None: |
| 65 | + response = client.post( |
| 66 | + "/collections/a-collection/items", content=json.dumps(item_collection) |
| 67 | + ) |
| 68 | + assert response.is_success, response.text |
| 69 | + assert response.json()["type"] == "FeatureCollection" |
| 70 | + |
| 71 | + |
| 72 | +@pytest.fixture |
| 73 | +def client( |
| 74 | + core_client: DummyCoreClient, transactions_client: DummyTransactionsClient |
| 75 | +) -> Iterator[TestClient]: |
| 76 | + settings = ApiSettings() |
| 77 | + api = StacApi( |
| 78 | + settings=settings, |
| 79 | + client=core_client, |
| 80 | + extensions=[ |
| 81 | + TransactionExtension(client=transactions_client, settings=settings), |
| 82 | + ], |
| 83 | + ) |
| 84 | + with TestClient(api.app) as client: |
| 85 | + yield client |
| 86 | + |
| 87 | + |
| 88 | +@pytest.fixture |
| 89 | +def core_client() -> DummyCoreClient: |
| 90 | + return DummyCoreClient() |
| 91 | + |
| 92 | + |
| 93 | +@pytest.fixture |
| 94 | +def transactions_client() -> DummyTransactionsClient: |
| 95 | + return DummyTransactionsClient() |
| 96 | + |
| 97 | + |
| 98 | +@pytest.fixture |
| 99 | +def item_collection(item: Item) -> ItemCollection: |
| 100 | + return { |
| 101 | + "type": "FeatureCollection", |
| 102 | + "features": [item], |
| 103 | + "links": [], |
| 104 | + "context": None, |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | +@pytest.fixture |
| 109 | +def item() -> Item: |
| 110 | + return { |
| 111 | + "type": "Feature", |
| 112 | + "stac_version": "1.0.0", |
| 113 | + "stac_extensions": [], |
| 114 | + "id": "test_item", |
| 115 | + "geometry": {"type": "Point", "coordinates": [-105, 40]}, |
| 116 | + "bbox": [-105, 40, -105, 40], |
| 117 | + "properties": {}, |
| 118 | + "links": [], |
| 119 | + "assets": {}, |
| 120 | + "collection": "test_collection", |
| 121 | + } |
0 commit comments