-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathtest_orders_api.py
853 lines (671 loc) · 26.4 KB
/
test_orders_api.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
# Copyright 2020 Planet Labs, Inc.
# Copyright 2022 Planet Labs PBC.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
from contextlib import nullcontext as does_not_raise
import copy
import json
import hashlib
from http import HTTPStatus
import logging
import math
from pathlib import Path
from unittest.mock import call, create_autospec
import httpx
import pytest
import respx
from planet import OrdersClient, exceptions, reporting
from planet.clients.orders import OrderStates
TEST_URL = 'http://www.MockNotRealURL.com/api/path'
TEST_BULK_CANCEL_URL = f'{TEST_URL}/bulk/orders/v2/cancel'
TEST_DOWNLOAD_URL = f'{TEST_URL}/download'
TEST_DOWNLOAD_ACTUAL_URL = f'{TEST_URL}/download_actual'
TEST_ORDERS_URL = f'{TEST_URL}/orders/v2'
TEST_STATS_URL = f'{TEST_URL}/stats/orders/v2'
LOGGER = logging.getLogger(__name__)
@pytest.fixture
def downloaded_content():
return {'key': 'downloaded_file'}
@pytest.fixture
def original_content():
return {'key': 'original_file'}
@pytest.fixture
def create_download_mock(downloaded_content, order_description, oid):
"""
Mock an HTTP response for download.
"""
def f():
# Create mock HTTP response
order_description['state'] = 'success'
dl_url = TEST_DOWNLOAD_URL + '/1?token=IAmAToken'
order_description['_links']['results'] = [
{
'location': dl_url, 'name': 'file.json'
},
]
get_url = f'{TEST_ORDERS_URL}/{oid}'
mock_resp = httpx.Response(HTTPStatus.OK, json=order_description)
respx.get(get_url).return_value = mock_resp
mock_resp = httpx.Response(HTTPStatus.OK,
json=downloaded_content,
headers={
'Content-Type':
'application/json',
'Content-Disposition':
'attachment; filename="file.json"'
})
respx.get(dl_url).return_value = mock_resp
return f
def test_OrderStates_reached():
assert not OrderStates.reached('running', 'queued')
assert OrderStates.reached('running', 'running')
assert OrderStates.reached('running', 'failed')
def test_OrderStates_passed():
assert not OrderStates.reached('running', 'queued')
assert not OrderStates.passed('running', 'running')
assert OrderStates.passed('running', 'success')
@respx.mock
@pytest.mark.anyio
async def test_list_orders_basic(order_descriptions, session):
next_page_url = TEST_ORDERS_URL + 'blob/?page_marker=IAmATest'
order1, order2, order3 = order_descriptions
page1_response = {
"_links": {
"_self": "string", "next": next_page_url
},
"orders": [order1, order2]
}
mock_resp = httpx.Response(HTTPStatus.OK, json=page1_response)
respx.get(TEST_ORDERS_URL).return_value = mock_resp
page2_response = {"_links": {"_self": next_page_url}, "orders": [order3]}
mock_resp2 = httpx.Response(HTTPStatus.OK, json=page2_response)
respx.get(next_page_url).return_value = mock_resp2
cl = OrdersClient(session, base_url=TEST_URL)
assert order_descriptions == [o async for o in cl.list_orders()]
@respx.mock
@pytest.mark.anyio
async def test_list_orders_state_success(order_descriptions, session):
list_url = TEST_ORDERS_URL + '?source_type=all&state=failed'
order1, order2, _ = order_descriptions
page1_response = {
"_links": {
"_self": "string"
}, "orders": [order1, order2]
}
mock_resp = httpx.Response(HTTPStatus.OK, json=page1_response)
respx.get(list_url).return_value = mock_resp
cl = OrdersClient(session, base_url=TEST_URL)
# if the value of state doesn't get sent as a url parameter,
# the mock will fail and this test will fail
assert [order1,
order2] == [o async for o in cl.list_orders(state='failed')]
@pytest.mark.anyio
async def test_list_orders_state_invalid(session):
cl = OrdersClient(session, base_url=TEST_URL)
with pytest.raises(exceptions.ClientError):
[o async for o in cl.list_orders(state='invalidstate')]
@respx.mock
@pytest.mark.anyio
@pytest.mark.parametrize("limit,limited_list_length", [(None, 100), (0, 102),
(1, 1)])
async def test_list_orders_limit(order_descriptions,
session,
limit,
limited_list_length):
nono_page_url = None
# Creating 102 (3x34) order descriptions
long_order_descriptions = order_descriptions * 34
all_orders = {}
for x in range(1, len(long_order_descriptions) + 1):
all_orders["order{0}".format(x)] = long_order_descriptions[x - 1]
page1_response = {
"_links": {
"_self": "string", "next": nono_page_url
},
"orders": [
all_orders['order%s' % num]
for num in range(1, limited_list_length + 1)
]
}
mock_resp = httpx.Response(HTTPStatus.OK, json=page1_response)
respx.get(TEST_ORDERS_URL).return_value = mock_resp
cl = OrdersClient(session, base_url=TEST_URL)
assert len([o async for o in cl.list_orders(limit=limit)
]) == limited_list_length
@respx.mock
@pytest.mark.anyio
async def test_create_order_basic(oid,
order_description,
order_request,
session):
route = respx.post(TEST_ORDERS_URL)
route.return_value = httpx.Response(HTTPStatus.OK, json=order_description)
cl = OrdersClient(session, base_url=TEST_URL)
order = await cl.create_order(order_request)
assert order == order_description
assert json.loads(route.calls.last.request.content) == order_request
@respx.mock
@pytest.mark.anyio
async def test_create_order_bad_item_type(order_request, session):
resp = {
"field": {
"Products": [{
"message":
"Bad item type 'invalid' for bundle type 'analytic'"
}]
},
"general": [{
"message": "Unable to accept order"
}]
}
mock_resp = httpx.Response(400, json=resp)
respx.post(TEST_ORDERS_URL).return_value = mock_resp
order_request['products'][0]['item_type'] = 'invalid'
cl = OrdersClient(session, base_url=TEST_URL)
with pytest.raises(exceptions.BadQuery):
await cl.create_order(order_request)
@respx.mock
@pytest.mark.anyio
async def test_create_order_item_id_does_not_exist(order_request, session):
resp = {
"field": {
"Details": [{
"message": ("Item ID 4500474_2133707_2021-05-20_2419 / " +
"Item Type PSScene3Band doesn't exist")
}]
},
"general": [{
"message": "Unable to accept order"
}]
}
mock_resp = httpx.Response(400, json=resp)
respx.post(TEST_ORDERS_URL).return_value = mock_resp
order_request['products'][0]['item_ids'] = \
'4500474_2133707_2021-05-20_2419'
cl = OrdersClient(session, base_url=TEST_URL)
with pytest.raises(exceptions.BadQuery):
await cl.create_order(order_request)
@respx.mock
@pytest.mark.anyio
async def test_get_order(oid, order_description, session):
get_url = f'{TEST_ORDERS_URL}/{oid}'
mock_resp = httpx.Response(HTTPStatus.OK, json=order_description)
respx.get(get_url).return_value = mock_resp
cl = OrdersClient(session, base_url=TEST_URL)
order = await cl.get_order(oid)
assert order_description == order
@pytest.mark.anyio
async def test_get_order_invalid_id(session):
cl = OrdersClient(session, base_url=TEST_URL)
with pytest.raises(exceptions.ClientError):
await cl.get_order('-')
@respx.mock
@pytest.mark.anyio
async def test_get_order_id_doesnt_exist(oid, session):
get_url = f'{TEST_ORDERS_URL}/{oid}'
resp = {"message": f'Could not load order ID: {oid}'}
mock_resp = httpx.Response(404, json=resp)
respx.get(get_url).return_value = mock_resp
cl = OrdersClient(session, base_url=TEST_URL)
with pytest.raises(exceptions.MissingResource):
await cl.get_order(oid)
@respx.mock
@pytest.mark.anyio
async def test_cancel_order(oid, order_description, session):
cancel_url = f'{TEST_ORDERS_URL}/{oid}'
order_description['state'] = 'cancelled'
mock_resp = httpx.Response(HTTPStatus.OK, json=order_description)
example_resp = mock_resp.json()
respx.put(cancel_url).return_value = mock_resp
cl = OrdersClient(session, base_url=TEST_URL)
json_resp = await cl.cancel_order(oid)
assert json_resp == example_resp
@pytest.mark.anyio
async def test_cancel_order_invalid_id(session):
cl = OrdersClient(session, base_url=TEST_URL)
with pytest.raises(exceptions.ClientError):
await cl.cancel_order('invalid_order_id')
@respx.mock
@pytest.mark.anyio
async def test_cancel_order_id_doesnt_exist(oid, session):
cancel_url = f'{TEST_ORDERS_URL}/{oid}'
resp = {"message": f'No such order ID: {oid}.'}
mock_resp = httpx.Response(404, json=resp)
respx.put(cancel_url).return_value = mock_resp
cl = OrdersClient(session, base_url=TEST_URL)
with pytest.raises(exceptions.MissingResource):
await cl.cancel_order(oid)
@respx.mock
@pytest.mark.anyio
async def test_cancel_order_id_cannot_be_cancelled(oid, session):
cancel_url = f'{TEST_ORDERS_URL}/{oid}'
resp = {"message": 'Order not in a cancellable state'}
mock_resp = httpx.Response(409, json=resp)
respx.put(cancel_url).return_value = mock_resp
cl = OrdersClient(session, base_url=TEST_URL)
with pytest.raises(exceptions.Conflict):
await cl.cancel_order(oid)
@respx.mock
@pytest.mark.anyio
async def test_cancel_orders_by_ids(session, oid):
oid2 = '5ece1dc0-ea81-11eb-837c-acde48001122'
test_ids = [oid, oid2]
example_result = {
"result": {
"succeeded": {
"count": 1
},
"failed": {
"count":
1,
"failures": [{
"order_id": oid2,
"message": "Order not in a cancellable state",
}]
}
}
}
mock_resp = httpx.Response(HTTPStatus.OK, json=example_result)
respx.post(TEST_BULK_CANCEL_URL).return_value = mock_resp
cl = OrdersClient(session, base_url=TEST_URL)
res = await cl.cancel_orders(test_ids)
assert res == example_result
expected_body = {"order_ids": test_ids}
actual_body = json.loads(respx.calls.last.request.content)
assert actual_body == expected_body
@pytest.mark.anyio
async def test_cancel_orders_by_ids_invalid_id(session, oid):
cl = OrdersClient(session, base_url=TEST_URL)
with pytest.raises(exceptions.ClientError):
_ = await cl.cancel_orders([oid, "invalid_oid"])
@respx.mock
@pytest.mark.anyio
async def test_cancel_orders_all(session):
example_result = {
"result": {
"succeeded": {
"count": 2
}, "failed": {
"count": 0, "failures": []
}
}
}
mock_resp = httpx.Response(HTTPStatus.OK, json=example_result)
respx.post(TEST_BULK_CANCEL_URL).return_value = mock_resp
cl = OrdersClient(session, base_url=TEST_URL)
res = await cl.cancel_orders()
assert res == example_result
actual_body = json.loads(respx.calls.last.request.content)
assert actual_body == {}
@respx.mock
@pytest.mark.anyio
async def test_wait_default(oid, order_description, session):
get_url = f'{TEST_ORDERS_URL}/{oid}'
order_description2 = copy.deepcopy(order_description)
order_description2['state'] = 'running'
order_description3 = copy.deepcopy(order_description)
order_description3['state'] = 'success'
route = respx.get(get_url)
route.side_effect = [
httpx.Response(HTTPStatus.OK, json=order_description),
httpx.Response(HTTPStatus.OK, json=order_description2),
httpx.Response(HTTPStatus.OK, json=order_description3)
]
cl = OrdersClient(session, base_url=TEST_URL)
state = await cl.wait(oid, delay=0)
assert state == 'success'
@respx.mock
@pytest.mark.anyio
async def test_wait_callback(oid, order_description, session):
get_url = f'{TEST_ORDERS_URL}/{oid}'
order_description2 = copy.deepcopy(order_description)
order_description2['state'] = 'running'
order_description3 = copy.deepcopy(order_description)
order_description3['state'] = 'success'
route = respx.get(get_url)
route.side_effect = [
httpx.Response(HTTPStatus.OK, json=order_description),
httpx.Response(HTTPStatus.OK, json=order_description2),
httpx.Response(HTTPStatus.OK, json=order_description3)
]
mock_bar = create_autospec(reporting.StateBar)
mock_callback = mock_bar.update_state
cl = OrdersClient(session, base_url=TEST_URL)
await cl.wait(oid, delay=0, callback=mock_callback)
# check state was sent to callback as expected
expected = [call(s) for s in ['queued', 'running', 'success']]
mock_callback.assert_has_calls(expected)
@respx.mock
@pytest.mark.anyio
async def test_wait_state(oid, order_description, session):
get_url = f'{TEST_ORDERS_URL}/{oid}'
order_description2 = copy.deepcopy(order_description)
order_description2['state'] = 'running'
order_description3 = copy.deepcopy(order_description)
order_description3['state'] = 'success'
route = respx.get(get_url)
route.side_effect = [
httpx.Response(HTTPStatus.OK, json=order_description),
httpx.Response(HTTPStatus.OK, json=order_description2),
httpx.Response(HTTPStatus.OK, json=order_description3)
]
cl = OrdersClient(session, base_url=TEST_URL)
state = await cl.wait(oid, state='running', delay=0)
assert state == 'running'
@respx.mock
@pytest.mark.anyio
async def test_wait_max_attempts_enabled(oid, order_description, session):
get_url = f'{TEST_ORDERS_URL}/{oid}'
route = respx.get(get_url)
route.side_effect = [
httpx.Response(HTTPStatus.OK, json=order_description),
]
cl = OrdersClient(session, base_url=TEST_URL)
with pytest.raises(exceptions.ClientError):
await cl.wait(oid, max_attempts=1, delay=0)
@respx.mock
@pytest.mark.anyio
async def test_wait_max_attempts_disabled(oid, order_description, session):
get_url = f'{TEST_ORDERS_URL}/{oid}'
route = respx.get(get_url)
route.side_effect = [
httpx.Response(HTTPStatus.OK, json=order_description),
] * 300
cl = OrdersClient(session, base_url=TEST_URL)
with pytest.raises(RuntimeError):
# falls off the end of the mocked responses
await cl.wait(oid, max_attempts=0, delay=0)
@pytest.mark.anyio
async def test_wait_invalid_oid(session):
cl = OrdersClient(session, base_url=TEST_URL)
with pytest.raises(exceptions.ClientError):
await cl.wait("invalid_oid", delay=0)
@pytest.mark.anyio
async def test_wait_invalid_state(oid, session):
cl = OrdersClient(session, base_url=TEST_URL)
with pytest.raises(exceptions.ClientError):
await cl.wait(oid, state="invalid_state", delay=0)
@respx.mock
@pytest.mark.anyio
async def test_aggegated_order_stats(session):
example_stats = {
"organization": {
"queued_orders": 0, "running_orders": 6
},
"user": {
"queued_orders": 0, "running_orders": 0
}
}
mock_resp = httpx.Response(HTTPStatus.OK, json=example_stats)
respx.get(TEST_STATS_URL).return_value = mock_resp
cl = OrdersClient(session, base_url=TEST_URL)
res = await cl.aggregated_order_stats()
assert res == example_stats
@respx.mock
@pytest.mark.anyio
async def test_download_asset_md(tmpdir, session):
dl_url = TEST_DOWNLOAD_URL + '/1?token=IAmAToken'
md_json = {'key': 'value'}
md_headers = {
'Content-Type': 'application/json',
'Content-Disposition': 'attachment; filename="metadata.json"'
}
mock_redirect = httpx.Response(HTTPStatus.FOUND,
headers={
'Location': TEST_DOWNLOAD_ACTUAL_URL,
'Content-Length': '0'
})
mock_resp = httpx.Response(HTTPStatus.OK, json=md_json, headers=md_headers)
respx.get(dl_url).return_value = mock_redirect
respx.get(TEST_DOWNLOAD_ACTUAL_URL).return_value = mock_resp
cl = OrdersClient(session, base_url=TEST_URL)
filename = await cl.download_asset(dl_url, directory=str(tmpdir))
with open(filename) as f:
assert json.load(f) == {'key': 'value'}
assert Path(filename).name == 'metadata.json'
@respx.mock
@pytest.mark.anyio
async def test_download_asset_img_with_retry(tmpdir, open_test_img, session):
dl_url = TEST_DOWNLOAD_URL + '/1?token=IAmAToken'
img_headers = {
'Content-Type': 'image/tiff',
'Content-Length': '527',
'Content-Disposition': 'attachment; filename="img.tif"'
}
async def _stream_img():
data = open_test_img.read()
v = memoryview(data)
chunksize = 100
for i in range(math.ceil(len(v) / (chunksize))):
yield v[i * chunksize:min((i + 1) * chunksize, len(v))]
# populate request parameter to avoid respx cloning, which throws
# an error caused by respx and not this code
# https://github.com/lundberg/respx/issues/130
respx.get(dl_url).side_effect = [
httpx.ReadError("no can do!"),
httpx.Response(HTTPStatus.OK,
stream=_stream_img(),
headers=img_headers,
request='donotcloneme')
]
cl = OrdersClient(session, base_url=TEST_URL)
filename = await cl.download_asset(dl_url, directory=str(tmpdir))
assert filename.name == 'img.tif'
assert filename.is_file()
assert len(filename.read_bytes()) == 527
@respx.mock
@pytest.mark.anyio
@pytest.mark.parametrize("checksum", [("MD5"), ("SHA256")])
@pytest.mark.parametrize(
"asset1_bytes, expectation",
[(b"1", does_not_raise()), (b"1", does_not_raise()),
(b"does not match", pytest.raises(exceptions.ClientError))])
async def test_validate_checksum_checksum(tmpdir,
asset1_bytes,
expectation,
checksum):
itemtype1_dir = Path(tmpdir, 'itemtype1')
itemtype1_dir.mkdir()
asset1 = itemtype1_dir / 'asset1.tif'
asset1.write_bytes(b"1")
asset2 = itemtype1_dir / 'asset2.json'
asset2.write_bytes(b'{"foo": "bar"}')
asset2_bytes = asset2.read_bytes()
manifest_data = {
"name": "",
"files": [
{
"path": "itemtype1/asset1.tif",
"digests": {
"md5": hashlib.md5(asset1_bytes).hexdigest(),
"sha256": hashlib.sha256(asset1_bytes).hexdigest()}
}, {
"path": "itemtype1/asset2.json",
"digests": {
"md5": hashlib.md5(asset2_bytes).hexdigest(),
"sha256": hashlib.sha256(asset2_bytes).hexdigest()}
}]
} # yapf: disable
Path(tmpdir, 'manifest.json').write_text(json.dumps(manifest_data))
with expectation:
OrdersClient.validate_checksum(Path(tmpdir), checksum)
@respx.mock
@pytest.mark.anyio
@pytest.mark.parametrize(
"create, corrupt, expectation",
[(True, False, does_not_raise()),
(True, True, pytest.raises(exceptions.ClientError)),
(False, False, pytest.raises(exceptions.ClientError))])
async def test_validate_checksum_manifest(
tmpdir,
create,
corrupt,
expectation,
):
itemtype1_dir = Path(tmpdir, 'itemtype1')
itemtype1_dir.mkdir()
asset1 = itemtype1_dir / 'asset1.tif'
asset1.write_bytes(b"1")
Path(tmpdir, 'asset1.tif').write_bytes(b"1")
manifest_data = {
"name":
"",
"files": [{
"path": "itemtype1/asset1.tif",
"digests": {
"md5": hashlib.md5(b"1").hexdigest(),
"sha256": hashlib.sha256(b"1").hexdigest()
},
}]
}
if create:
if corrupt:
Path(tmpdir, 'manifest.json').write_text('not json')
else:
Path(tmpdir, 'manifest.json').write_text(json.dumps(manifest_data))
with expectation:
OrdersClient.validate_checksum(Path(tmpdir), 'md5')
@respx.mock
@pytest.mark.anyio
@pytest.mark.parametrize(
"results, paths",
[(None, []),
([], []),
([{"location": f'{TEST_DOWNLOAD_URL}/1',
"name": "oid/itemtype1/asset.json"},
{"location": f'{TEST_DOWNLOAD_URL}/2',
"name": "oid/itemtype2/asset.json"},
],
[Path('oid', 'itemtype1', 'asset.json'),
Path('oid', 'itemtype2', 'asset.json'),
])
]) # yapf: disable
async def test_download_order_success(results,
paths,
tmpdir,
order_description,
oid,
session):
# Mock an HTTP response for download
order_description['state'] = 'success'
order_description['_links']['results'] = results
get_url = f'{TEST_ORDERS_URL}/{oid}'
mock_resp = httpx.Response(HTTPStatus.OK, json=order_description)
respx.get(get_url).return_value = mock_resp
mock_resp1 = httpx.Response(HTTPStatus.OK,
json={'key': 'value'},
headers={
'Content-Type':
'application/json',
'Content-Disposition':
'attachment; filename="asset.json"'
})
respx.get(f'{TEST_DOWNLOAD_URL}/1').return_value = mock_resp1
mock_resp2 = httpx.Response(HTTPStatus.OK,
json={'key2': 'value2'},
headers={
'Content-Type':
'application/json',
'Content-Disposition':
'attachment; filename="asset.json"'
})
respx.get(f'{TEST_DOWNLOAD_URL}/2').return_value = mock_resp2
cl = OrdersClient(session, base_url=TEST_URL)
filenames = await cl.download_order(oid, directory=str(tmpdir))
assert filenames == [Path(tmpdir, p) for p in paths]
if filenames:
with open(filenames[0]) as f:
assert json.load(f) == {'key': 'value'}
with open(filenames[1]) as f:
assert json.load(f) == {'key2': 'value2'}
@respx.mock
@pytest.mark.anyio
async def test_download_order_state(tmpdir, order_description, oid, session):
dl_url1 = TEST_DOWNLOAD_URL + '/1?token=IAmAToken'
order_description['_links']['results'] = [
{
'location': dl_url1
},
]
get_url = f'{TEST_ORDERS_URL}/{oid}'
mock_resp = httpx.Response(HTTPStatus.OK, json=order_description)
respx.get(get_url).return_value = mock_resp
cl = OrdersClient(session, base_url=TEST_URL)
with pytest.raises(exceptions.ClientError):
await cl.download_order(oid, directory=str(tmpdir))
@respx.mock
@pytest.mark.anyio
async def test_download_order_overwrite_true_preexisting_data(
tmpdir,
oid,
session,
create_download_mock,
original_content,
downloaded_content):
"""
Test if download_order() overwrites pre-existing data with
overwrite flag set to True.
"""
# Save JSON to a temporary file
with open(Path(tmpdir, 'file.json'), "a") as out_file:
json.dump(original_content, out_file)
create_download_mock()
cl = OrdersClient(session, base_url=TEST_URL)
await cl.download_order(oid, directory=str(tmpdir), overwrite=True)
# Check that the data downloaded has overwritten the original data
with open(Path(tmpdir, 'file.json')) as f:
assert json.load(f) == downloaded_content
@respx.mock
@pytest.mark.anyio
async def test_download_order_overwrite_false_preexisting_data(
tmpdir, oid, session, create_download_mock, original_content):
"""
Test if download_order() does not overwrite pre-existing
data with overwrite flag set to False.
"""
# Save JSON to a temporary file
with open(Path(tmpdir, 'file.json'), "a") as out_file:
json.dump(original_content, out_file)
create_download_mock()
cl = OrdersClient(session, base_url=TEST_URL)
await cl.download_order(oid, directory=str(tmpdir), overwrite=False)
# Check that the original data has not been overwritten
with open(Path(tmpdir, 'file.json')) as f:
assert json.load(f) == original_content
@respx.mock
@pytest.mark.anyio
async def test_download_order_overwrite_true_nonexisting_data(
tmpdir, oid, session, create_download_mock, downloaded_content):
"""
Test if download_order() downloads data with overwrite flag
set to true without pre-existing data.
"""
create_download_mock()
cl = OrdersClient(session, base_url=TEST_URL)
await cl.download_order(oid, directory=str(tmpdir), overwrite=True)
with open(Path(tmpdir, 'file.json')) as f:
assert json.load(f) == downloaded_content
@respx.mock
@pytest.mark.anyio
async def test_download_order_overwrite_false_nonexisting_data(
tmpdir, oid, session, create_download_mock, downloaded_content):
"""
Test if download_order() downloads data with overwrite flag
set to false without pre-existing data.
"""
create_download_mock()
cl = OrdersClient(session, base_url=TEST_URL)
await cl.download_order(oid, directory=str(tmpdir), overwrite=False)
# Check that the was data downloaded and has the correct contents
with open(Path(tmpdir, 'file.json')) as f:
assert json.load(f) == downloaded_content