Skip to content

Commit e579953

Browse files
committed
added rest_session test cases
1 parent 3ad0f93 commit e579953

File tree

4 files changed

+148
-13
lines changed

4 files changed

+148
-13
lines changed

examples/notebooks/PoC_GEE_UsingProcess.ipynb

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,42 @@
186186
},
187187
{
188188
"cell_type": "code",
189-
"execution_count": 8,
189+
"execution_count": 18,
190190
"metadata": {},
191191
"outputs": [
192192
{
193193
"data": {
194194
"text/plain": [
195-
"<openeo.rest.imagery.RestImagery at 0x7f3b681a0e10>"
195+
"[{'process_id': 'zonal_statistics',\n",
196+
" 'description': 'Calculates statistics for each zone specified in a file.'},\n",
197+
" {'process_id': 'NDVI',\n",
198+
" 'description': 'Finds the minimum value of time series for all bands of the input dataset.'},\n",
199+
" {'process_id': 'filter_bands',\n",
200+
" 'description': 'Selects certain bands from a collection.'},\n",
201+
" {'process_id': 'filter_bbox',\n",
202+
" 'description': 'Drops observations from a collection that are located outside of a given bounding box.'},\n",
203+
" {'process_id': 'filter_daterange',\n",
204+
" 'description': 'Drops observations from a collection that have been captured before a start or after a given end date.'},\n",
205+
" {'process_id': 'count_time',\n",
206+
" 'description': 'Counts the number of images with a valid mask in a time series for all bands of the input dataset.'},\n",
207+
" {'process_id': 'max_time',\n",
208+
" 'description': 'Finds the maximum value of a time series for all bands of the input dataset.'},\n",
209+
" {'process_id': 'min_time',\n",
210+
" 'description': 'Finds the minimum value of a time series for all bands of the input dataset.'},\n",
211+
" {'process_id': 'mean_time',\n",
212+
" 'description': 'Calculates the mean value of a time series for all bands of the input dataset.'},\n",
213+
" {'process_id': 'median_time',\n",
214+
" 'description': 'Calculates the median value of a time series for all bands of the input dataset.'},\n",
215+
" {'process_id': 'sum_time',\n",
216+
" 'description': 'Calculates the sum of a time series for all bands of the input dataset.'},\n",
217+
" {'process_id': 'last_time',\n",
218+
" 'description': 'Returns the last element of a time series for all bands of the input dataset.'},\n",
219+
" {'process_id': 'first_time',\n",
220+
" 'description': 'Returns the first element of a time series for all bands of the input dataset.'},\n",
221+
" {'process_id': 'stretch_colors', 'description': 'Color stretching'}]"
196222
]
197223
},
198-
"execution_count": 8,
224+
"execution_count": 18,
199225
"metadata": {},
200226
"output_type": "execute_result"
201227
}
@@ -204,7 +230,7 @@
204230
"# Select collection product\n",
205231
"coperincus_s2_image = session.image(PRODUCT_ID)\n",
206232
"\n",
207-
"coperincus_s2_image"
233+
"session.get_all_processes()"
208234
]
209235
},
210236
{
@@ -234,16 +260,39 @@
234260
},
235261
{
236262
"cell_type": "code",
237-
"execution_count": 10,
263+
"execution_count": 14,
238264
"metadata": {},
239265
"outputs": [
240266
{
241267
"data": {
242268
"text/plain": [
243-
"<openeo.rest.imagery.RestImagery at 0x7f3b60e92ac8>"
269+
"{'process_id': 'stretch_colors',\n",
270+
" 'args': {'imagery': {'process_id': 'min_time',\n",
271+
" 'args': {'imagery': {'process_id': 'NDVI',\n",
272+
" 'args': {'imagery': {'process_id': 'stretch_colors',\n",
273+
" 'args': {'imagery': {'process_id': 'min_time',\n",
274+
" 'args': {'imagery': {'process_id': 'NDVI',\n",
275+
" 'args': {'imagery': {'process_id': 'filter_daterange',\n",
276+
" 'args': {'imagery': {'process_id': 'filter_bbox',\n",
277+
" 'args': {'imagery': {'product_id': 'COPERNICUS/S2'},\n",
278+
" 'left': 9.0,\n",
279+
" 'right': 9.1,\n",
280+
" 'top': 12.1,\n",
281+
" 'bottom': 12.0,\n",
282+
" 'srs': 'EPSG:4326'}},\n",
283+
" 'from': '2017-01-01',\n",
284+
" 'to': '2017-01-31'}},\n",
285+
" 'red': 'B4',\n",
286+
" 'nir': 'B8'}}}},\n",
287+
" 'min': -1,\n",
288+
" 'max': 1}},\n",
289+
" 'red': 'B4',\n",
290+
" 'nir': 'B8'}}}},\n",
291+
" 'min': -1,\n",
292+
" 'max': 1}}"
244293
]
245294
},
246-
"execution_count": 10,
295+
"execution_count": 14,
247296
"metadata": {},
248297
"output_type": "execute_result"
249298
}
@@ -254,7 +303,7 @@
254303
"timeseries = openeo.min_time(timeseries)\n",
255304
"timeseries = openeo.stretch_colors(timeseries, STRECH_COLORS_MIN, STRECH_COLORS_MAX)\n",
256305
"\n",
257-
"timeseries"
306+
"timeseries.graph"
258307
]
259308
},
260309
{

openeo/rest/rest_session.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ def user_upload_file(self, file_path, remote_path=None):
243243

244244
remote_path = os.path.basename(file_path)
245245

246-
input_file = open(file_path, 'rb').read()
246+
with open(file_path, 'rb') as f:
247+
input_file = f.read()
247248

248249
path = "/users/{}/files/{}".format(self.userid, remote_path)
249250

tests/test_rest_session.py

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@
88

99
import requests_mock
1010

11+
# MockUp Testdata
12+
13+
CAPABILITIES = ['/capabilities', '/capabilities/services', '/capabilities/output_formats', '/data',
14+
'/data/{product_id}', '/processes']
15+
COLLECTIONS = [{'product_id': 'ASTER/AST_L1T_003',
16+
'description': 'ASTER L1T Radiance',
17+
'source': 'NASA LP DAAC at the USGS EROS Center, https://lpdaac.usgs.gov/dataset_discovery/aster/aster_products_table/ast_l1t'},
18+
{'product_id': 'AU/GA/AUSTRALIA_5M_DEM',
19+
'description': 'Australian 5M DEM',
20+
'source': 'Geoscience Australia, https://ecat.ga.gov.au/geonetwork/srv/eng/search#!22be4b55-2465-4320-e053-10a3070a5236'}]
21+
22+
PROCESSES = [{'process_id': 'zonal_statistics',
23+
'description': 'Calculates statistics for each zone specified in a file.'},
24+
{'process_id': 'NDVI',
25+
'description': 'Finds the minimum value of time series for all bands of the input dataset.'},
26+
{'process_id': 'filter_bands',
27+
'description': 'Selects certain bands from a collection.'}]
28+
1129

1230
@requests_mock.mock()
1331
class TestUserFiles(TestCase):
@@ -30,7 +48,7 @@ def match_uploaded_file(self, request):
3048
return True
3149

3250
def test_user_upload_file(self, m):
33-
upload_url ="http://localhost:8000/api/users/{}/files/{}".format(self.user_id,
51+
upload_url = "{}/users/{}/files/{}".format(self.endpoint, self.user_id,
3452
self.upload_remote_fname)
3553
m.register_uri('PUT', upload_url,
3654
additional_matcher=self.match_uploaded_file)
@@ -41,7 +59,7 @@ def test_user_upload_file(self, m):
4159
assert status
4260

4361
def test_user_download_file(self, m):
44-
download_url ="http://localhost:8000/api/users/{}/files/{}".format(self.user_id,
62+
download_url = "{}/users/{}/files/{}".format(self.endpoint, self.user_id,
4563
self.upload_remote_fname)
4664
with open(self.upload_local_fname, 'rb') as response_file:
4765
content = response_file.read()
@@ -57,10 +75,75 @@ def test_user_download_file(self, m):
5775
assert content == downloaded_content
5876

5977
def test_user_delete_file(self, m):
60-
delete_url ="http://localhost:8000/api/users/{}/files/{}".format(self.user_id,
78+
delete_url = "{}/users/{}/files/{}".format(self.endpoint, self.user_id,
6179
self.upload_remote_fname)
6280
m.register_uri('DELETE', delete_url)
6381
session = openeo.session(self.user_id, endpoint=self.endpoint)
6482
session.auth(self.auth_id, self.auth_pwd)
6583
status = session.user_delete_file(self.upload_remote_fname)
6684
assert status
85+
86+
def test_list_capabilities(self, m):
87+
capabilties_url = "{}/capabilities".format(self.endpoint)
88+
m.register_uri('GET', capabilties_url, json=CAPABILITIES)
89+
session = openeo.session(self.user_id, endpoint=self.endpoint)
90+
91+
capabilities = session.list_capabilities()
92+
assert capabilities == CAPABILITIES
93+
94+
def test_list_collections(self, m):
95+
collection_url = "{}/data".format(self.endpoint)
96+
m.register_uri('GET', collection_url, json=COLLECTIONS)
97+
session = openeo.session(self.user_id, endpoint=self.endpoint)
98+
99+
collections = session.list_collections()
100+
assert collections == COLLECTIONS
101+
102+
def test_get_collection(self, m):
103+
collection_org = COLLECTIONS[0]
104+
collection_id = collection_org["product_id"]
105+
collection_url = "{}/data/{}".format(self.endpoint, collection_id)
106+
m.register_uri('GET', collection_url, json=collection_org)
107+
session = openeo.session(self.user_id, endpoint=self.endpoint)
108+
109+
collection = session.get_collection(collection_id)
110+
assert collection == collection_org
111+
112+
def test_get_all_processes(self, m):
113+
processes_url = "{}/processes".format(self.endpoint)
114+
m.register_uri('GET', processes_url, json=PROCESSES)
115+
session = openeo.session(self.user_id, endpoint=self.endpoint)
116+
117+
processes = session.get_all_processes()
118+
assert processes == PROCESSES
119+
120+
def test_get_process(self, m):
121+
process_org = PROCESSES[0]
122+
process_id = process_org['process_id']
123+
process_url = "{}/processes/{}".format(self.endpoint, process_id)
124+
m.register_uri('GET', process_url, json=process_org)
125+
session = openeo.session(self.user_id, endpoint=self.endpoint)
126+
127+
process = session.get_process(process_id)
128+
assert process == process_org
129+
130+
def test_create_job(self, m):
131+
132+
# TODO: Add Test to check if post_data is sent properly
133+
post_data = {}
134+
job_id = "MyId"
135+
result = {"job_id": job_id}
136+
137+
m.register_uri('POST', "{}/jobs?evaluate={}".format(self.endpoint, "lazy"), status_code=200, json=result)
138+
m.register_uri('POST', "{}/jobs?evaluate={}".format(self.endpoint, "wrong"), status_code=400)
139+
140+
session = openeo.session(self.user_id, endpoint=self.endpoint)
141+
142+
resp = session.create_job(post_data)
143+
144+
assert resp == job_id
145+
146+
resp = session.create_job(post_data, evaluation="wrong")
147+
148+
assert resp is None
149+

tests/test_usecase1.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import openeo
77
import requests_mock
88

9-
POST_DATA = '{"process_id": "filter_daterange", "args": { "imagery": { "product_id": "landsat7_ndvi"}, "from": "2014-01-01", "to": "2014-06-01"}}'
9+
POST_DATA = '{"process_id": "filter_daterange", "args": { "imagery": { "product_id": "landsat7_ndvi"}, ' \
10+
'"from": "2014-01-01", "to": "2014-06-01"}}'
11+
1012

1113

1214
@requests_mock.mock()

0 commit comments

Comments
 (0)