-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathtest_transaction.py
277 lines (222 loc) · 8.69 KB
/
test_transaction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import json
from typing import Dict, Iterator, List, Union
import pytest
from stac_pydantic import Collection
from stac_pydantic.item import Item
from stac_pydantic.item_collection import ItemCollection
from starlette.testclient import TestClient
from stac_fastapi.api.app import StacApi
from stac_fastapi.extensions.core import TransactionExtension
from stac_fastapi.types.config import ApiSettings
from stac_fastapi.types.core import BaseCoreClient, BaseTransactionsClient
from stac_fastapi.types.transaction import PatchOperation
class DummyCoreClient(BaseCoreClient):
def all_collections(self, *args, **kwargs):
raise NotImplementedError
def get_collection(self, *args, **kwargs):
raise NotImplementedError
def get_item(self, *args, **kwargs):
raise NotImplementedError
def get_search(self, *args, **kwargs):
raise NotImplementedError
def post_search(self, *args, **kwargs):
raise NotImplementedError
def item_collection(self, *args, **kwargs):
raise NotImplementedError
class DummyTransactionsClient(BaseTransactionsClient):
"""Dummy client returning parts of the request, rather than proper STAC items."""
def create_item(self, item: Union[Item, ItemCollection], *args, **kwargs):
return {"created": True, "type": item.type}
def update_item(self, collection_id: str, item_id: str, item: Item, **kwargs):
return {
"path_collection_id": collection_id,
"path_item_id": item_id,
"type": item.type,
}
def merge_patch_item(
self,
collection_id: str,
item_id: str,
item: Dict,
**kwargs,
):
return {
"path_collection_id": collection_id,
"path_item_id": item_id,
"type": item["type"],
}
def json_patch_item(
self,
collection_id: str,
item_id: str,
operations: List[PatchOperation],
**kwargs,
):
return {
"path_collection_id": collection_id,
"path_item_id": item_id,
"first_op_type": operations[0].op,
}
def delete_item(self, item_id: str, collection_id: str, **kwargs):
return {
"path_collection_id": collection_id,
"path_item_id": item_id,
}
def create_collection(self, collection: Collection, **kwargs):
return {"type": collection.type}
def update_collection(self, collection_id: str, collection: Collection, **kwargs):
return {"path_collection_id": collection_id, "type": collection.type}
def merge_patch_collection(
self,
collection_id: str,
collection: Dict,
**kwargs,
):
return {"path_collection_id": collection_id, "type": collection["type"]}
def json_patch_collection(
self,
collection_id: str,
operations: List[PatchOperation],
**kwargs,
):
return {
"path_collection_id": collection_id,
"first_op_type": operations[0].op,
}
def delete_collection(self, collection_id: str, **kwargs):
return {"path_collection_id": collection_id}
def test_create_item(client: TestClient, item: Item) -> None:
response = client.post("/collections/a-collection/items", content=json.dumps(item))
assert response.is_success, response.text
assert response.json()["type"] == "Feature"
def test_create_item_collection(
client: TestClient, item_collection: ItemCollection
) -> None:
response = client.post(
"/collections/a-collection/items", content=json.dumps(item_collection)
)
assert response.is_success, response.text
assert response.json()["type"] == "FeatureCollection"
def test_update_item(client: TestClient, item: Item) -> None:
response = client.put(
"/collections/a-collection/items/an-item", content=json.dumps(item)
)
assert response.is_success, response.text
assert response.json()["path_collection_id"] == "a-collection"
assert response.json()["path_item_id"] == "an-item"
assert response.json()["type"] == "Feature"
def test_merge_patch_item(client: TestClient, item: Item) -> None:
response = client.patch(
"/collections/a-collection/items/an-item", content=json.dumps(item)
)
assert response.is_success, response.text
assert response.json()["path_collection_id"] == "a-collection"
assert response.json()["path_item_id"] == "an-item"
assert response.json()["type"] == "Feature"
def test_json_patch_item(client: TestClient) -> None:
operations = [{"op": "add", "path": "properties.new_prop", "value": "new_prop_value"}]
headers = {"Content-Type": "application/json-patch+json"}
response = client.patch(
"/collections/a-collection/items/an-item",
headers=headers,
content=json.dumps(operations),
)
assert response.is_success, response.text
assert response.json()["path_collection_id"] == "a-collection"
assert response.json()["path_item_id"] == "an-item"
assert response.json()["first_op_type"] == "add"
def test_delete_item(client: TestClient) -> None:
response = client.delete("/collections/a-collection/items/an-item")
assert response.is_success, response.text
assert response.json()["path_collection_id"] == "a-collection"
assert response.json()["path_item_id"] == "an-item"
def test_create_collection(client: TestClient, collection: Collection) -> None:
response = client.post("/collections", content=json.dumps(collection))
assert response.is_success, response.text
assert response.json()["type"] == "Collection"
def test_update_collection(client: TestClient, collection: Collection) -> None:
response = client.put("/collections/a-collection", content=json.dumps(collection))
assert response.is_success, response.text
assert response.json()["path_collection_id"] == "a-collection"
assert response.json()["type"] == "Collection"
def test_merge_patch_collection(client: TestClient, collection: Collection) -> None:
response = client.patch(
"/collections/a-collection",
content=json.dumps(collection),
)
assert response.is_success, response.text
assert response.json()["path_collection_id"] == "a-collection"
assert response.json()["type"] == "Collection"
def test_json_patch_collection(client: TestClient) -> None:
operations = [{"op": "add", "path": "summaries.new_prop", "value": "new_prop_value"}]
headers = {"Content-Type": "application/json-patch+json"}
response = client.patch(
"/collections/a-collection/items/an-item",
headers=headers,
content=json.dumps(operations),
)
assert response.is_success, response.text
assert response.json()["path_collection_id"] == "a-collection"
assert response.json()["first_op_type"] == "add"
def test_delete_collection(client: TestClient, collection: Collection) -> None:
response = client.delete("/collections/a-collection")
assert response.is_success, response.text
assert response.json()["path_collection_id"] == "a-collection"
@pytest.fixture
def client(
core_client: DummyCoreClient, transactions_client: DummyTransactionsClient
) -> Iterator[TestClient]:
settings = ApiSettings()
api = StacApi(
settings=settings,
client=core_client,
extensions=[
TransactionExtension(client=transactions_client, settings=settings),
],
)
with TestClient(api.app) as client:
yield client
@pytest.fixture
def core_client() -> DummyCoreClient:
return DummyCoreClient()
@pytest.fixture
def transactions_client() -> DummyTransactionsClient:
return DummyTransactionsClient()
@pytest.fixture
def item_collection(item: Item) -> ItemCollection:
return {
"type": "FeatureCollection",
"features": [item],
"links": [],
"context": None,
}
@pytest.fixture
def item() -> Item:
return {
"type": "Feature",
"stac_version": "1.0.0",
"stac_extensions": [],
"id": "test_item",
"geometry": {"type": "Point", "coordinates": [-105, 40]},
"bbox": [-105, 40, -105, 40],
"properties": {"datetime": "2020-06-13T13:00:00Z"},
"links": [],
"assets": {},
"collection": "test_collection",
}
@pytest.fixture
def collection() -> Collection:
return {
"type": "Collection",
"stac_version": "1.0.0",
"stac_extensions": [],
"id": "test_collection",
"description": "A test collection",
"extent": {
"spatial": {"bbox": [[-180, -90, 180, 90]]},
"temporal": {"interval": [["2000-01-01T00:00:00Z", "2024-01-01T00:00:00Z"]]},
},
"links": [],
"assets": {},
"license": "proprietary",
}