/
test_utils.py
360 lines (316 loc) · 11 KB
/
test_utils.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import json
from pathlib import Path
import tempfile
from dateutil.parser import parse
import geopandas as gpd
from geopandas import GeoDataFrame
import pandas as pd
import pytest
from shapely.geometry import Polygon, LinearRing
from .context import (
format_time_period,
any_vector_to_fc,
fc_to_query_geometry,
download_from_gcs_unpack,
filter_jobs_on_mode,
autocomplete_order_parameters,
)
POLY = Polygon([(0, 0), (1, 1), (1, 0)])
@pytest.mark.parametrize(
"start_date,end_date,result_time",
[
("2014-01-01", "2016-12-31", "2014-01-01T00:00:00Z/2016-12-31T23:59:59Z"),
(
"2014-01-01T00:00:00",
"2016-12-31T10:11:12",
"2014-01-01T00:00:00Z/2016-12-31T10:11:12Z",
),
(
"2014-01-01T00:00:00",
"2016-12-31",
"2014-01-01T00:00:00Z/2016-12-31T23:59:59Z",
),
(
parse("2014-01-01"),
parse("2016-12-31T10:11:12"),
"2014-01-01T00:00:00Z/2016-12-31T10:11:12Z",
),
(
parse("2014-01-01"),
"2016-12-31",
"2014-01-01T00:00:00Z/2016-12-31T23:59:59Z",
),
(
parse("2014-01-01"),
"2016-12-31T10:11:12",
"2014-01-01T00:00:00Z/2016-12-31T10:11:12Z",
),
],
)
def test_format_time_period(start_date, end_date, result_time):
time_period = format_time_period(
start_date=start_date,
end_date=end_date,
)
assert isinstance(time_period, str)
assert time_period == result_time
@pytest.mark.parametrize(
"start_date,end_date",
[(None, "2014-01-01"), ("2014-01-01", None)],
)
def test_format_time_period_raises_with_missing_dates(start_date, end_date):
with pytest.raises(ValueError) as e:
format_time_period(
start_date=start_date,
end_date=end_date,
)
assert (
"When using dates, both start_date and end_date need to be provided."
in str(e.value)
)
@pytest.mark.parametrize(
"start_date,end_date",
[(None, "2016-01-01"), ("2014-01-01", None)],
)
def test_format_time_period_raises_with_mixed_up_dates(start_date, end_date):
with pytest.raises(ValueError) as e:
format_time_period(
start_date=start_date,
end_date=end_date,
)
assert "The start_date needs to be earlier than the end_date!" in str(e.value)
@pytest.mark.parametrize(
"len_fc, in_vector",
[
(1, POLY),
(
1,
gpd.GeoDataFrame(
pd.DataFrame([0], columns=["id"]),
crs={"init": "epsg:4326"},
geometry=[POLY],
),
),
(
2,
gpd.GeoDataFrame(
pd.DataFrame([0, 1], columns=["id"]),
crs={"init": "epsg:4326"},
geometry=[POLY, POLY],
),
),
(1, [0.0, 0.0, 1.0, 1.0]),
(
1,
{
"id": "0",
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": (
(
(10.00001, 6.0),
(10.0, 5.99),
(10.1, 6.00),
(10.00001, 6.0),
),
),
},
},
),
(
1,
{
"type": "FeatureCollection",
"features": [
{
"id": "0",
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": (
(
(10.00001, 6.0),
(10.0, 5.999),
(10.00, 6.0),
(10.00001, 6.0),
),
),
},
}
],
},
),
(
1,
{
"type": "Polygon",
"coordinates": [
[
[-97.324448, 37.72246],
[-97.349682, 37.722732],
[-97.349939, 37.708405],
[-97.322989, 37.708202],
[-97.320414, 37.708473],
[-97.324448, 37.72246],
]
],
},
),
],
)
def test_any_vector_to_fc(len_fc, in_vector):
fc = any_vector_to_fc(in_vector)
assert isinstance(fc, dict)
assert fc["type"] == "FeatureCollection"
assert fc.get("bbox") is not None
assert fc["features"][0].get("bbox") is not None
assert len(fc["features"]) != 0
assert len(fc["features"]) == len_fc
assert fc["features"][0]["geometry"].get("coordinates") is not None
df = any_vector_to_fc(in_vector, as_dataframe=True)
assert isinstance(df, GeoDataFrame)
assert df.crs.to_string() == "EPSG:4326"
def test_any_vector_to_fc_raises_with_unaccepted_geometry_type():
ring = LinearRing([(0, 0), (1, 1), (1, 0)])
with pytest.raises(ValueError):
any_vector_to_fc(ring)
def test_fc_to_query_geometry_single_intersects():
fp = Path(__file__).resolve().parent / "mock_data/aoi_berlin.geojson"
with open(fp) as json_file:
fc = json.load(json_file)
query_geometry = fc_to_query_geometry(fc=fc, geometry_operation="intersects")
assert isinstance(query_geometry, dict)
assert query_geometry["type"] == "Polygon"
assert query_geometry["coordinates"] == [
[
[13.375966, 52.515068],
[13.375966, 52.516639],
[13.378314, 52.516639],
[13.378314, 52.515068],
[13.375966, 52.515068],
]
]
def test_fc_to_query_geometry_single_bbox():
fp = Path(__file__).resolve().parent / "mock_data/aoi_berlin.geojson"
with open(fp) as json_file:
fc = json.load(json_file)
query_geometry = fc_to_query_geometry(fc=fc, geometry_operation="bbox")
assert isinstance(query_geometry, list)
assert len(query_geometry) == 4
assert query_geometry == [13.375966, 52.515068, 13.378314, 52.516639]
def test_fc_to_query_geometry_multiple_raises():
fp = (
Path(__file__).resolve().parent
/ "mock_data/search_results_limited_columns.geojson"
)
with open(fp) as json_file:
fc = json.load(json_file)
with pytest.raises(ValueError) as e:
fc_to_query_geometry(fc=fc, geometry_operation="intersects")
assert (
str(e.value)
== "UP42 only accepts single geometries, the provided geometry contains multiple geometries."
)
with pytest.raises(ValueError) as e:
fc_to_query_geometry(fc=fc, geometry_operation="bbox")
assert (
str(e.value)
== "UP42 only accepts single geometries, the provided geometry contains multiple geometries."
)
def test_fc_to_query_geometry_multipolygon_raises():
fp = Path(__file__).resolve().parent / "mock_data/multipolygon.geojson"
with open(fp) as json_file:
fc = json.load(json_file)
with pytest.raises(ValueError) as e:
fc_to_query_geometry(fc=fc, geometry_operation="intersects")
assert (
str(e.value)
== "UP42 only accepts single geometries, the provided geometry is a MultiPolygon."
)
def test_download_gcs_unpack_tgz(requests_mock):
cloud_storage_url = "http://clouddownload.api.com/abcdef"
out_tgz = Path(__file__).resolve().parent / "mock_data/result_tif.tgz"
with open(out_tgz, "rb") as src_tgz:
out_tgz_file = src_tgz.read()
requests_mock.get(
url=cloud_storage_url,
content=out_tgz_file,
)
with tempfile.TemporaryDirectory() as tempdir:
with open(Path(tempdir) / "dummy.txt", "w") as fp:
fp.write("dummy")
out_files = download_from_gcs_unpack(
download_url=cloud_storage_url,
output_directory=tempdir,
)
for file in out_files:
assert Path(file).exists()
assert len(out_files) == 2
assert not (Path(tempdir) / "output").exists()
def test_download_gcs_unpack_zip(requests_mock):
cloud_storage_url = "http://clouddownload.api.com/abcdef"
out_zip = Path(__file__).resolve().parent / "mock_data/result_tif.zip"
with open(out_zip, "rb") as src_zip:
out_zip_file = src_zip.read()
requests_mock.get(
url=cloud_storage_url,
content=out_zip_file,
)
with tempfile.TemporaryDirectory() as tempdir:
with open(Path(tempdir) / "dummy.txt", "w") as fp:
fp.write("dummy")
out_files = download_from_gcs_unpack(
download_url=cloud_storage_url,
output_directory=tempdir,
)
for file in out_files:
assert Path(file).exists()
assert len(out_files) == 2
assert not (Path(tempdir) / "output").exists()
def test_download_gcs_not_unpack(requests_mock):
cloud_storage_url = "http://clouddownload.api.com/abcdef"
out_zip = Path(__file__).resolve().parent / "mock_data/aoi_berlin.geojson"
with open(out_zip, "rb") as src_zip:
out_zip_file = src_zip.read()
requests_mock.get(
url=cloud_storage_url,
content=out_zip_file,
)
with tempfile.TemporaryDirectory() as tempdir:
with open(Path(tempdir) / "dummy.txt", "w") as fp:
fp.write("dummy")
with pytest.raises(ValueError):
download_from_gcs_unpack(
download_url=cloud_storage_url,
output_directory=tempdir,
)
def test_filter_jobs_on_mode():
job_json = [{"mode": "DEFAULT"}, {"mode": "DRY_RUN"}]
r = filter_jobs_on_mode(job_json)
assert len(r) == 2
r = filter_jobs_on_mode(job_json, test_jobs=False, real_jobs=True)
assert len(r) == 1
r = filter_jobs_on_mode(job_json, test_jobs=True, real_jobs=False)
assert len(r) == 1
with pytest.raises(ValueError):
filter_jobs_on_mode(job_json, test_jobs=False, real_jobs=False)
def test_autocomplete_order_parameters():
with open(
Path(__file__).resolve().parent / "mock_data/data_product_schema.json"
) as json_file:
json_data_product_schema = json.load(json_file)
order_parameters = {
"dataProduct": "123",
"params": {"existing_param1": "abc"},
}
order_parameters = autocomplete_order_parameters(
order_parameters=order_parameters, schema=json_data_product_schema
)
assert "dataProduct" in order_parameters
assert order_parameters["params"]["existing_param1"] is not None
assert order_parameters["params"]["aoi"] is None
assert order_parameters["params"]["acquisitionMode"] is None
assert order_parameters["params"]["id"] is None