-
Notifications
You must be signed in to change notification settings - Fork 47
/
fixtures_asset.py
154 lines (133 loc) · 4.14 KB
/
fixtures_asset.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
import datetime
import os
from pathlib import Path
import pytest
from pystac import Item, ItemCollection
from pystac.collection import Extent, SpatialExtent, TemporalExtent
from pystac_client import CollectionClient
from up42.asset import Asset
from .fixtures_globals import (
API_HOST,
ASSET_ID,
ASSET_ID2,
DOWNLOAD_URL,
DOWNLOAD_URL2,
JSON_ASSET,
JSON_STAC_CATALOG_RESPONSE,
JSON_STORAGE_STAC,
STAC_ASSET_ID,
STAC_ASSET_URL,
STAC_COLLECTION_ID,
URL_STAC_CATALOG,
)
@pytest.fixture()
def asset_mock(auth_mock, requests_mock):
# asset info
url_asset_info = f"{API_HOST}/v2/assets/{ASSET_ID}/metadata"
requests_mock.get(url=url_asset_info, json=JSON_ASSET)
mock_item_collection = ItemCollection(
items=[
Item(
id="test",
geometry=None,
properties={},
bbox=None,
datetime=datetime.datetime.now(),
)
]
)
url_asset_stac_info = f"{API_HOST}/v2/assets/stac/search"
requests_mock.post(
url_asset_stac_info,
[
{"json": JSON_STORAGE_STAC},
{"json": JSON_STORAGE_STAC},
{"json": mock_item_collection.to_dict()},
],
)
# asset stac item
requests_mock.get(url=URL_STAC_CATALOG, json=JSON_STAC_CATALOG_RESPONSE)
# asset update
updated_json_asset = JSON_ASSET.copy()
updated_json_asset["title"] = "some_other_title"
updated_json_asset["tags"] = ["othertag1", "othertag2"]
requests_mock.post(url=url_asset_info, json=updated_json_asset)
# download url
requests_mock.post(
url=f"{API_HOST}/v2/assets/{ASSET_ID}/download-url",
json={"url": DOWNLOAD_URL},
)
# stac_info url
mock_client = CollectionClient(
id="up42-storage",
description="UP42 Storage STAC API",
extra_fields={"up42-system:asset_id": ASSET_ID},
extent=Extent(
spatial=SpatialExtent(
bboxes=[
[
13.3783333333333,
52.4976111111112,
13.3844444444445,
52.5017222222223,
]
]
),
temporal=TemporalExtent(
intervals=[
[
datetime.datetime(2021, 5, 31),
datetime.datetime(2021, 5, 31),
]
]
),
),
)
requests_mock.get(
url=f"{API_HOST}/v2/assets/stac/collections/{STAC_COLLECTION_ID}",
json=mock_client.to_dict(),
)
asset = Asset(auth=auth_mock, asset_id=ASSET_ID)
return asset
@pytest.fixture()
def asset_mock2(auth_mock, requests_mock):
url_asset_info = f"{API_HOST}/v2/assets/{ASSET_ID2}/metadata"
requests_mock.get(url=url_asset_info, json={**JSON_ASSET, "id": ASSET_ID2})
requests_mock.post(
url=f"{API_HOST}/v2/assets/{ASSET_ID2}/download-url",
json={"url": DOWNLOAD_URL2},
)
requests_mock.post(
url=f"{API_HOST}/v2/assets/{STAC_ASSET_ID}/download-url",
json={"url": STAC_ASSET_URL},
)
mock_file = (
Path(__file__).resolve().parents[1] / "mock_data/aoi_berlin.geojson"
)
with open(mock_file, "rb") as src_file:
out_file = src_file.read()
requests_mock.get(
url=STAC_ASSET_URL,
content=out_file,
)
asset = Asset(auth=auth_mock, asset_id=ASSET_ID2)
return asset
@pytest.fixture(params=["asset_mock", "asset_mock2"])
def assets_fixture(request, asset_mock, asset_mock2):
mocks = {
"asset_mock": {
"asset_fixture": asset_mock,
"download_url": DOWNLOAD_URL,
"outfile_name": "output.tgz",
},
"asset_mock2": {
"asset_fixture": asset_mock2,
"download_url": DOWNLOAD_URL2,
"outfile_name": "DS_SPOT6_202206240959075_FR1_FR1_SV1_SV1_E013N52_01709.tgz",
},
}
return mocks[request.param]
@pytest.fixture()
def asset_live(auth_live):
asset = Asset(auth=auth_live, asset_id=os.getenv("TEST_UP42_ASSET_ID"))
return asset