|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
""" |
|
4
|
|
|
Created on Thu Jul 12 14:23:09 2018 |
|
5
|
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
|
7
|
|
|
""" |
|
8
|
|
|
|
|
9
|
|
|
import os |
|
10
|
|
|
import json |
|
11
|
|
|
import datetime |
|
12
|
|
|
|
|
13
|
|
|
from unittest.mock import patch, Mock |
|
14
|
|
|
from unittest import TestCase |
|
15
|
|
|
|
|
16
|
|
|
from pyUSIrest.auth import Auth |
|
17
|
|
|
from pyUSIrest.client import ( |
|
18
|
|
|
Root, Team, User, Domain, Submission, Client, Sample, Document) |
|
19
|
|
|
|
|
20
|
|
|
from .test_auth import generate_token |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
# get my path |
|
24
|
|
|
dir_path = os.path.dirname(os.path.realpath(__file__)) |
|
25
|
|
|
|
|
26
|
|
|
# define data path |
|
27
|
|
|
data_path = os.path.join(dir_path, "data") |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
class ClientTest(TestCase): |
|
31
|
|
|
@classmethod |
|
32
|
|
|
def setup_class(cls): |
|
33
|
|
|
cls.mock_get_patcher = patch('pyUSIrest.client.requests.get') |
|
34
|
|
|
cls.mock_get = cls.mock_get_patcher.start() |
|
35
|
|
|
|
|
36
|
|
|
@classmethod |
|
37
|
|
|
def teardown_class(cls): |
|
38
|
|
|
cls.mock_get_patcher.stop() |
|
39
|
|
|
|
|
40
|
|
|
def test_with_tocken_str(self): |
|
41
|
|
|
token = generate_token() |
|
42
|
|
|
client = Client(token) |
|
43
|
|
|
self.assertFalse(client.auth.is_expired()) |
|
44
|
|
|
|
|
45
|
|
|
def test_expired_tocken(self): |
|
46
|
|
|
now = int(datetime.datetime.now().timestamp()) |
|
47
|
|
|
|
|
48
|
|
|
token = generate_token(now=now-10000) |
|
49
|
|
|
client = Client(token) |
|
50
|
|
|
|
|
51
|
|
|
# do a generic request and get error |
|
52
|
|
|
self.assertRaisesRegex( |
|
53
|
|
|
RuntimeError, |
|
54
|
|
|
"Your token is expired", |
|
55
|
|
|
client.follow_url, |
|
56
|
|
|
"https://submission-test.ebi.ac.uk/api/" |
|
57
|
|
|
) |
|
58
|
|
|
|
|
59
|
|
|
def test_server_error(self): |
|
60
|
|
|
"""Deal with the generic 50x states""" |
|
61
|
|
|
|
|
62
|
|
|
token = generate_token() |
|
63
|
|
|
client = Client(token) |
|
64
|
|
|
|
|
65
|
|
|
# create a mock response |
|
66
|
|
|
response = Mock() |
|
67
|
|
|
response.status_code = 500 |
|
68
|
|
|
response.text = ( |
|
69
|
|
|
'<!DOCTYPE html>\n<html>\n<body>\n<meta http-equiv="refresh" ' |
|
70
|
|
|
'content=\'0;URL=http://www.ebi.ac.uk/errors/failure.html\'>\n' |
|
71
|
|
|
'</body>\n</html>\n') |
|
72
|
|
|
|
|
73
|
|
|
self.assertRaisesRegex( |
|
74
|
|
|
ConnectionError, |
|
75
|
|
|
"Problems with API endpoints", |
|
76
|
|
|
client.parse_response, |
|
77
|
|
|
response |
|
78
|
|
|
) |
|
79
|
|
|
|
|
80
|
|
|
def test_follow_url_with_errors(self): |
|
81
|
|
|
"""Deal with problems with following URL (no 200 status code)""" |
|
82
|
|
|
|
|
83
|
|
|
# create a mock response |
|
84
|
|
|
response = Mock() |
|
85
|
|
|
response.status_code = 404 |
|
86
|
|
|
response.text = ( |
|
87
|
|
|
'<h1>Not Found</h1><p>The requested resource was not found on ' |
|
88
|
|
|
'this server.</p>') |
|
89
|
|
|
|
|
90
|
|
|
token = generate_token() |
|
91
|
|
|
client = Client(token) |
|
92
|
|
|
|
|
93
|
|
|
self.mock_get.return_value = response |
|
94
|
|
|
|
|
95
|
|
|
self.assertRaisesRegex( |
|
96
|
|
|
ConnectionError, |
|
97
|
|
|
"Not Found", |
|
98
|
|
|
client.follow_url, |
|
99
|
|
|
Root.api_root + "/meow" |
|
100
|
|
|
) |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
class RootTest(TestCase): |
|
104
|
|
|
@classmethod |
|
105
|
|
|
def setup_class(cls): |
|
106
|
|
|
cls.mock_get_patcher = patch('pyUSIrest.client.requests.get') |
|
107
|
|
|
cls.mock_get = cls.mock_get_patcher.start() |
|
108
|
|
|
|
|
109
|
|
|
@classmethod |
|
110
|
|
|
def teardown_class(cls): |
|
111
|
|
|
cls.mock_get_patcher.stop() |
|
112
|
|
|
|
|
113
|
|
|
def setUp(self): |
|
114
|
|
|
self.auth = Auth(token=generate_token()) |
|
115
|
|
|
|
|
116
|
|
|
with open(os.path.join(data_path, "root.json")) as handle: |
|
117
|
|
|
data = json.load(handle) |
|
118
|
|
|
|
|
119
|
|
|
self.mock_get.return_value = Mock() |
|
120
|
|
|
self.mock_get.return_value.json.return_value = data |
|
121
|
|
|
self.mock_get.return_value.status_code = 200 |
|
122
|
|
|
|
|
123
|
|
|
# get a root object |
|
124
|
|
|
self.root = Root(self.auth) |
|
125
|
|
|
|
|
126
|
|
|
def test_str(self): |
|
127
|
|
|
test = self.root.__str__() |
|
128
|
|
|
reference = "Biosample API root at %s" % (Root.api_root) |
|
129
|
|
|
|
|
130
|
|
|
self.assertIsInstance(test, str) |
|
131
|
|
|
self.assertEqual(reference, test) |
|
132
|
|
|
|
|
133
|
|
|
def read_userTeams(self, filename="userTeams.json"): |
|
134
|
|
|
with open(os.path.join(data_path, filename)) as handle: |
|
135
|
|
|
data = json.load(handle) |
|
136
|
|
|
|
|
137
|
|
|
self.mock_get.return_value = Mock() |
|
138
|
|
|
self.mock_get.return_value.json.return_value = data |
|
139
|
|
|
self.mock_get.return_value.status_code = 200 |
|
140
|
|
|
|
|
141
|
|
|
def test_get_user_teams(self): |
|
142
|
|
|
# initialize |
|
143
|
|
|
self.read_userTeams() |
|
144
|
|
|
|
|
145
|
|
|
# get user teams |
|
146
|
|
|
teams = self.root.get_user_teams() |
|
147
|
|
|
|
|
148
|
|
|
self.assertIsInstance(teams, list) |
|
149
|
|
|
self.assertEqual(len(teams), 1) |
|
150
|
|
|
|
|
151
|
|
|
team = teams[0] |
|
152
|
|
|
self.assertIsInstance(team, Team) |
|
153
|
|
|
|
|
154
|
|
|
def test_get_user_no_teams(self): |
|
155
|
|
|
"""Test for a user having no teams""" |
|
156
|
|
|
|
|
157
|
|
|
# initialize |
|
158
|
|
|
self.read_userTeams(filename="userNoTeams.json") |
|
159
|
|
|
|
|
160
|
|
|
# get user teams |
|
161
|
|
|
teams = self.root.get_user_teams() |
|
162
|
|
|
|
|
163
|
|
|
self.assertIsInstance(teams, list) |
|
164
|
|
|
self.assertEqual(len(teams), 0) |
|
165
|
|
|
|
|
166
|
|
|
def test_get_team_by_name(self): |
|
167
|
|
|
# initialize |
|
168
|
|
|
self.read_userTeams() |
|
169
|
|
|
|
|
170
|
|
|
# get a specific team |
|
171
|
|
|
team = self.root.get_team_by_name("subs.dev-team-1") |
|
172
|
|
|
self.assertIsInstance(team, Team) |
|
173
|
|
|
|
|
174
|
|
|
# get a team I dont't belong to |
|
175
|
|
|
self.assertRaisesRegex( |
|
176
|
|
|
NameError, |
|
177
|
|
|
"team: .* not found", |
|
178
|
|
|
self.root.get_team_by_name, |
|
179
|
|
|
"subs.dev-team-2") |
|
180
|
|
|
|
|
181
|
|
|
def mocked_get_submission(*args, **kwargs): |
|
182
|
|
|
class MockResponse: |
|
183
|
|
|
def __init__(self, json_data, status_code): |
|
184
|
|
|
self.json_data = json_data |
|
185
|
|
|
self.status_code = status_code |
|
186
|
|
|
self.text = "MockResponse not implemented: %s" % (args[0]) |
|
187
|
|
|
|
|
188
|
|
|
def json(self): |
|
189
|
|
|
return self.json_data |
|
190
|
|
|
|
|
191
|
|
|
with open(os.path.join(data_path, "userSubmissions.json")) as handle: |
|
192
|
|
|
submissions = json.load(handle) |
|
193
|
|
|
|
|
194
|
|
|
submission_prefix = "https://submission-test.ebi.ac.uk/api/submissions" |
|
195
|
|
|
status_suffix = "submissionStatus" |
|
196
|
|
|
|
|
197
|
|
|
status_link1 = "/".join([ |
|
198
|
|
|
submission_prefix, |
|
199
|
|
|
"87e7abda-81a8-4b5e-a1c0-323f7f0a4e43", |
|
200
|
|
|
status_suffix]) |
|
201
|
|
|
|
|
202
|
|
|
status_link2 = "/".join([ |
|
203
|
|
|
submission_prefix, |
|
204
|
|
|
"8b05e7f2-92c1-4651-94cb-9101f351f000", |
|
205
|
|
|
status_suffix]) |
|
206
|
|
|
|
|
207
|
|
|
with open(os.path.join(data_path, "submissionStatus1.json")) as handle: |
|
208
|
|
|
status_data1 = json.load(handle) |
|
209
|
|
|
|
|
210
|
|
|
with open(os.path.join(data_path, "submissionStatus2.json")) as handle: |
|
211
|
|
|
status_data2 = json.load(handle) |
|
212
|
|
|
|
|
213
|
|
|
# --- to test get_submission_by_name |
|
214
|
|
|
byname_link = "/".join([ |
|
215
|
|
|
submission_prefix, "c8c86558-8d3a-4ac5-8638-7aa354291d61"]) |
|
216
|
|
|
|
|
217
|
|
|
with open(os.path.join(data_path, "newSubmission.json")) as handle: |
|
218
|
|
|
new_submission = json.load(handle) |
|
219
|
|
|
|
|
220
|
|
|
status_link3 = "/".join([ |
|
221
|
|
|
submission_prefix, |
|
222
|
|
|
"c8c86558-8d3a-4ac5-8638-7aa354291d61", |
|
223
|
|
|
status_suffix]) |
|
224
|
|
|
|
|
225
|
|
|
# --- for each url, return a different response |
|
226
|
|
|
if args[0] == ( |
|
227
|
|
|
'https://submission-test.ebi.ac.uk/api/user/submissions'): |
|
228
|
|
|
return MockResponse(submissions, 200) |
|
229
|
|
|
|
|
230
|
|
|
elif args[0] == status_link1: |
|
231
|
|
|
return MockResponse(status_data1, 200) |
|
232
|
|
|
|
|
233
|
|
|
elif args[0] == status_link2: |
|
234
|
|
|
return MockResponse(status_data2, 200) |
|
235
|
|
|
|
|
236
|
|
|
# --- links for get_submission_by_name |
|
237
|
|
|
elif args[0] == byname_link: |
|
238
|
|
|
return MockResponse(new_submission, 200) |
|
239
|
|
|
|
|
240
|
|
|
elif args[0] == status_link3: |
|
241
|
|
|
return MockResponse(status_data2, 200) |
|
242
|
|
|
|
|
243
|
|
|
return MockResponse(None, 404) |
|
244
|
|
|
|
|
245
|
|
|
@patch('requests.get', side_effect=mocked_get_submission) |
|
246
|
|
|
def test_get_user_submissions(self, mock_get): |
|
247
|
|
|
# get userSubmissions |
|
248
|
|
|
submissions = self.root.get_user_submissions() |
|
249
|
|
|
|
|
250
|
|
|
self.assertIsInstance(submissions, list) |
|
251
|
|
|
self.assertEqual(len(submissions), 2) |
|
252
|
|
|
|
|
253
|
|
|
for submission in submissions: |
|
254
|
|
|
self.assertIsInstance(submission, Submission) |
|
255
|
|
|
|
|
256
|
|
|
# testing filtering |
|
257
|
|
|
draft = self.root.get_user_submissions(status="Draft") |
|
258
|
|
|
|
|
259
|
|
|
self.assertIsInstance(draft, list) |
|
260
|
|
|
self.assertEqual(len(draft), 1) |
|
261
|
|
|
|
|
262
|
|
|
team1 = self.root.get_user_submissions(team="subs.test-team-1") |
|
263
|
|
|
self.assertIsInstance(team1, list) |
|
264
|
|
|
self.assertEqual(len(team1), 1) |
|
265
|
|
|
|
|
266
|
|
|
completed1 = self.root.get_user_submissions( |
|
267
|
|
|
team="subs.dev-team-1", status="Completed") |
|
268
|
|
|
self.assertIsInstance(completed1, list) |
|
269
|
|
|
self.assertEqual(len(completed1), 0) |
|
270
|
|
|
|
|
271
|
|
|
def test_get_user_submission_pagination(self): |
|
272
|
|
|
"""Testing get user submission with pagination""" |
|
273
|
|
|
|
|
274
|
|
|
with open(os.path.join( |
|
275
|
|
|
data_path, "userSubmissionsPage1.json")) as handle: |
|
276
|
|
|
page1 = json.load(handle) |
|
277
|
|
|
|
|
278
|
|
|
with open(os.path.join( |
|
279
|
|
|
data_path, "userSubmissionsPage2.json")) as handle: |
|
280
|
|
|
page2 = json.load(handle) |
|
281
|
|
|
|
|
282
|
|
|
self.mock_get.return_value = Mock() |
|
283
|
|
|
|
|
284
|
|
|
# simulating two distinct replies with side_effect |
|
285
|
|
|
self.mock_get.return_value.json.side_effect = [page1, page2] |
|
286
|
|
|
self.mock_get.return_value.status_code = 200 |
|
287
|
|
|
|
|
288
|
|
|
submissions = self.root.get_user_submissions() |
|
289
|
|
|
|
|
290
|
|
|
self.assertIsInstance(submissions, list) |
|
291
|
|
|
self.assertEqual(len(submissions), 2) |
|
292
|
|
|
|
|
293
|
|
|
@patch('requests.get', side_effect=mocked_get_submission) |
|
294
|
|
|
def test_get_submission_by_name(self, mock_get): |
|
295
|
|
|
submission = self.root.get_submission_by_name( |
|
296
|
|
|
submission_name='c8c86558-8d3a-4ac5-8638-7aa354291d61') |
|
297
|
|
|
|
|
298
|
|
|
self.assertIsInstance(submission, Submission) |
|
299
|
|
|
|
|
300
|
|
|
def test_get_submission_not_found(self): |
|
301
|
|
|
"""Test get a submission with a wrong name""" |
|
302
|
|
|
|
|
303
|
|
|
self.mock_get.return_value = Mock() |
|
304
|
|
|
self.mock_get.return_value.json.return_value = '' |
|
305
|
|
|
self.mock_get.return_value.status_code = 404 |
|
306
|
|
|
|
|
307
|
|
|
self.assertRaisesRegex( |
|
308
|
|
|
NameError, |
|
309
|
|
|
"submission: .* not found", |
|
310
|
|
|
self.root.get_submission_by_name, |
|
311
|
|
|
submission_name='c8c86558-8d3a-4ac5-8638-7aa354291d61') |
|
312
|
|
|
|
|
313
|
|
|
self.mock_get.return_value = Mock() |
|
314
|
|
|
self.mock_get.return_value.status_code = 500 |
|
315
|
|
|
|
|
316
|
|
|
self.assertRaises( |
|
317
|
|
|
ConnectionError, |
|
318
|
|
|
self.root.get_submission_by_name, |
|
319
|
|
|
submission_name='c8c86558-8d3a-4ac5-8638-7aa354291d61') |
|
320
|
|
|
|
|
321
|
|
|
|
|
322
|
|
|
class UserTest(TestCase): |
|
323
|
|
|
@classmethod |
|
324
|
|
|
def setup_class(cls): |
|
325
|
|
|
cls.mock_get_patcher = patch('pyUSIrest.client.requests.get') |
|
326
|
|
|
cls.mock_get = cls.mock_get_patcher.start() |
|
327
|
|
|
|
|
328
|
|
|
cls.mock_post_patcher = patch('pyUSIrest.client.requests.post') |
|
329
|
|
|
cls.mock_post = cls.mock_post_patcher.start() |
|
330
|
|
|
|
|
331
|
|
|
cls.mock_put_patcher = patch('pyUSIrest.client.requests.put') |
|
332
|
|
|
cls.mock_put = cls.mock_put_patcher.start() |
|
333
|
|
|
|
|
334
|
|
|
@classmethod |
|
335
|
|
|
def teardown_class(cls): |
|
336
|
|
|
cls.mock_get_patcher.stop() |
|
337
|
|
|
cls.mock_post_patcher.stop() |
|
338
|
|
|
cls.mock_put_patcher.stop() |
|
339
|
|
|
|
|
340
|
|
|
def setUp(self): |
|
341
|
|
|
self.auth = Auth(token=generate_token()) |
|
342
|
|
|
self.user = User(self.auth) |
|
343
|
|
|
|
|
344
|
|
|
self.data = { |
|
345
|
|
|
"userName": "foo", |
|
346
|
|
|
"email": "[email protected]", |
|
347
|
|
|
"userReference": "usr-f1801430-51e1-4718-8fca-778887087bad", |
|
348
|
|
|
"_links": { |
|
349
|
|
|
"self": { |
|
350
|
|
|
"href": "https://explore.api.aai.ebi.ac.uk/users/usr-" |
|
351
|
|
|
"f1801430-51e1-4718-8fca-778887087bad" |
|
352
|
|
|
} |
|
353
|
|
|
} |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
def test_get_user_by_id(self): |
|
357
|
|
|
self.mock_get.return_value = Mock() |
|
358
|
|
|
self.mock_get.return_value.json.return_value = self.data |
|
359
|
|
|
self.mock_get.return_value.status_code = 200 |
|
360
|
|
|
|
|
361
|
|
|
user = self.user.get_user_by_id( |
|
362
|
|
|
"usr-f1801430-51e1-4718-8fca-778887087bad") |
|
363
|
|
|
|
|
364
|
|
|
self.assertIsInstance(user, User) |
|
365
|
|
|
|
|
366
|
|
|
def test_get_my_id(self): |
|
367
|
|
|
self.mock_get.return_value = Mock() |
|
368
|
|
|
self.mock_get.return_value.json.return_value = self.data |
|
369
|
|
|
self.mock_get.return_value.status_code = 200 |
|
370
|
|
|
|
|
371
|
|
|
test = self.user.get_my_id() |
|
372
|
|
|
reference = "usr-f1801430-51e1-4718-8fca-778887087bad" |
|
373
|
|
|
|
|
374
|
|
|
self.assertEqual(reference, test) |
|
375
|
|
|
|
|
376
|
|
|
def test_create_user(self): |
|
377
|
|
|
reference = "usr-2a28ca65-2c2f-41e7-9aa5-e829830c6c71" |
|
378
|
|
|
self.mock_post.return_value = Mock() |
|
379
|
|
|
self.mock_post.return_value.text = reference |
|
380
|
|
|
self.mock_post.return_value.status_code = 200 |
|
381
|
|
|
|
|
382
|
|
|
test = self.user.create_user( |
|
383
|
|
|
user="newuser", |
|
384
|
|
|
password="changeme", |
|
385
|
|
|
confirmPwd="changeme", |
|
386
|
|
|
email="[email protected]", |
|
387
|
|
|
full_name="New User", |
|
388
|
|
|
organisation="Test" |
|
389
|
|
|
) |
|
390
|
|
|
|
|
391
|
|
|
self.assertEqual(reference, test) |
|
392
|
|
|
|
|
393
|
|
|
def test_create_team(self): |
|
394
|
|
|
with open(os.path.join(data_path, "newTeam.json")) as handle: |
|
395
|
|
|
data = json.load(handle) |
|
396
|
|
|
|
|
397
|
|
|
self.mock_post.return_value = Mock() |
|
398
|
|
|
self.mock_post.return_value.json.return_value = data |
|
399
|
|
|
self.mock_post.return_value.status_code = 201 |
|
400
|
|
|
|
|
401
|
|
|
team = self.user.create_team( |
|
402
|
|
|
description="test description", |
|
403
|
|
|
centreName="test Center") |
|
404
|
|
|
|
|
405
|
|
|
self.assertIsInstance(team, Team) |
|
406
|
|
|
|
|
407
|
|
|
def read_teams(self): |
|
408
|
|
|
with open(os.path.join(data_path, "userTeams.json")) as handle: |
|
409
|
|
|
data = json.load(handle) |
|
410
|
|
|
|
|
411
|
|
|
self.mock_get.return_value = Mock() |
|
412
|
|
|
self.mock_get.return_value.json.return_value = data |
|
413
|
|
|
self.mock_get.return_value.status_code = 200 |
|
414
|
|
|
|
|
415
|
|
|
def test_get_teams(self): |
|
416
|
|
|
# initialize |
|
417
|
|
|
self.read_teams() |
|
418
|
|
|
|
|
419
|
|
|
# get user teams |
|
420
|
|
|
teams = self.user.get_teams() |
|
421
|
|
|
|
|
422
|
|
|
self.assertIsInstance(teams, list) |
|
423
|
|
|
self.assertEqual(len(teams), 1) |
|
424
|
|
|
|
|
425
|
|
|
team = teams[0] |
|
426
|
|
|
self.assertIsInstance(team, Team) |
|
427
|
|
|
|
|
428
|
|
|
def test_get_team_by_name(self): |
|
429
|
|
|
# initialize |
|
430
|
|
|
self.read_teams() |
|
431
|
|
|
|
|
432
|
|
|
# get a specific team |
|
433
|
|
|
team = self.user.get_team_by_name("subs.dev-team-1") |
|
434
|
|
|
self.assertIsInstance(team, Team) |
|
435
|
|
|
|
|
436
|
|
|
# get a team I dont't belong to |
|
437
|
|
|
self.assertRaisesRegex( |
|
438
|
|
|
NameError, |
|
439
|
|
|
"team: .* not found", |
|
440
|
|
|
self.user.get_team_by_name, |
|
441
|
|
|
"subs.dev-team-2") |
|
442
|
|
|
|
|
443
|
|
|
def test_add_user2team(self): |
|
444
|
|
|
with open(os.path.join(data_path, "user2team.json")) as handle: |
|
445
|
|
|
data = json.load(handle) |
|
446
|
|
|
|
|
447
|
|
|
self.mock_put.return_value = Mock() |
|
448
|
|
|
self.mock_put.return_value.json.return_value = data |
|
449
|
|
|
self.mock_put.return_value.status_code = 200 |
|
450
|
|
|
|
|
451
|
|
|
domain = self.user.add_user_to_team( |
|
452
|
|
|
user_id='dom-36ccaae5-1ce1-41f9-b65c-d349994e9c80', |
|
453
|
|
|
domain_id='usr-d8749acf-6a22-4438-accc-cc8d1877ba36') |
|
454
|
|
|
|
|
455
|
|
|
self.assertIsInstance(domain, Domain) |
|
456
|
|
|
|
|
457
|
|
|
def read_myDomain(self): |
|
458
|
|
|
with open(os.path.join(data_path, "myDomain.json")) as handle: |
|
459
|
|
|
data = json.load(handle) |
|
460
|
|
|
|
|
461
|
|
|
self.mock_get.return_value = Mock() |
|
462
|
|
|
self.mock_get.return_value.json.return_value = data |
|
463
|
|
|
self.mock_get.return_value.status_code = 200 |
|
464
|
|
|
|
|
465
|
|
|
def test_get_domains(self): |
|
466
|
|
|
# initialize |
|
467
|
|
|
self.read_myDomain() |
|
468
|
|
|
|
|
469
|
|
|
# get user teams |
|
470
|
|
|
domains = self.user.get_domains() |
|
471
|
|
|
|
|
472
|
|
|
self.assertIsInstance(domains, list) |
|
473
|
|
|
self.assertEqual(len(domains), 2) |
|
474
|
|
|
|
|
475
|
|
|
for domain in domains: |
|
476
|
|
|
self.assertIsInstance(domain, Domain) |
|
477
|
|
|
|
|
478
|
|
|
def test_get_domain_by_name(self): |
|
479
|
|
|
# initialize |
|
480
|
|
|
self.read_myDomain() |
|
481
|
|
|
|
|
482
|
|
|
# get a specific team |
|
483
|
|
|
domain = self.user.get_domain_by_name("subs.test-team-1") |
|
484
|
|
|
self.assertIsInstance(domain, Domain) |
|
485
|
|
|
|
|
486
|
|
|
# get a team I dont't belong to |
|
487
|
|
|
self.assertRaisesRegex( |
|
488
|
|
|
NameError, |
|
489
|
|
|
"domain: .* not found", |
|
490
|
|
|
self.user.get_domain_by_name, |
|
491
|
|
|
"subs.dev-team-2") |
|
492
|
|
|
|
|
493
|
|
|
|
|
494
|
|
|
class DomainTest(TestCase): |
|
495
|
|
|
@classmethod |
|
496
|
|
|
def setup_class(cls): |
|
497
|
|
|
cls.mock_post_patcher = patch('pyUSIrest.client.requests.post') |
|
498
|
|
|
cls.mock_post = cls.mock_post_patcher.start() |
|
499
|
|
|
|
|
500
|
|
|
cls.mock_get_patcher = patch('pyUSIrest.client.requests.get') |
|
501
|
|
|
cls.mock_get = cls.mock_get_patcher.start() |
|
502
|
|
|
|
|
503
|
|
|
@classmethod |
|
504
|
|
|
def teardown_class(cls): |
|
505
|
|
|
cls.mock_post_patcher.stop() |
|
506
|
|
|
|
|
507
|
|
|
def setUp(self): |
|
508
|
|
|
self.auth = Auth(token=generate_token()) |
|
509
|
|
|
|
|
510
|
|
|
# read domain data (a list of domains) |
|
511
|
|
|
with open(os.path.join(data_path, "myDomain.json")) as handle: |
|
512
|
|
|
data = json.load(handle) |
|
513
|
|
|
|
|
514
|
|
|
self.domain = Domain(self.auth, data=data[0]) |
|
515
|
|
|
|
|
516
|
|
|
def test_str(self): |
|
517
|
|
|
test = self.domain.__str__() |
|
518
|
|
|
self.assertIsInstance(test, str) |
|
519
|
|
|
|
|
520
|
|
|
def test_create_profile(self): |
|
521
|
|
|
with open(os.path.join(data_path, "domainProfile.json")) as handle: |
|
522
|
|
|
data = json.load(handle) |
|
523
|
|
|
|
|
524
|
|
|
self.mock_post.return_value = Mock() |
|
525
|
|
|
self.mock_post.return_value.json.return_value = data |
|
526
|
|
|
self.mock_post.return_value.status_code = 201 |
|
527
|
|
|
|
|
528
|
|
|
self.domain.domainReference = ("dom-b38d6175-61e8-4d40-98da-" |
|
529
|
|
|
"df9188d91c82") |
|
530
|
|
|
|
|
531
|
|
|
self.domain.create_profile( |
|
532
|
|
|
attributes={ |
|
533
|
|
|
"cost_center": "ABC123", |
|
534
|
|
|
"address": "South Building, EMBL-EBI, Wellcome Genome Campus," |
|
535
|
|
|
"Hinxton, Cambridgeshire, CB10 1SD" |
|
536
|
|
|
}) |
|
537
|
|
|
|
|
538
|
|
|
def read_myUsers(self): |
|
539
|
|
|
with open(os.path.join(data_path, "domainUsers.json")) as handle: |
|
540
|
|
|
data = json.load(handle) |
|
541
|
|
|
|
|
542
|
|
|
self.mock_get.return_value = Mock() |
|
543
|
|
|
self.mock_get.return_value.json.return_value = data |
|
544
|
|
|
self.mock_get.return_value.status_code = 200 |
|
545
|
|
|
|
|
546
|
|
|
def test_users(self): |
|
547
|
|
|
# initialize |
|
548
|
|
|
self.read_myUsers() |
|
549
|
|
|
|
|
550
|
|
|
# get my users |
|
551
|
|
|
users = self.domain.users |
|
552
|
|
|
|
|
553
|
|
|
self.assertIsInstance(users, list) |
|
554
|
|
|
self.assertEqual(len(users), 2) |
|
555
|
|
|
|
|
556
|
|
|
for user in users: |
|
557
|
|
|
self.assertIsInstance(user, User) |
|
558
|
|
|
|
|
559
|
|
|
|
|
560
|
|
|
class TeamTest(TestCase): |
|
561
|
|
|
@classmethod |
|
562
|
|
|
def setup_class(cls): |
|
563
|
|
|
cls.mock_get_patcher = patch('pyUSIrest.client.requests.get') |
|
564
|
|
|
cls.mock_get = cls.mock_get_patcher.start() |
|
565
|
|
|
|
|
566
|
|
|
cls.mock_post_patcher = patch('pyUSIrest.client.requests.post') |
|
567
|
|
|
cls.mock_post = cls.mock_post_patcher.start() |
|
568
|
|
|
|
|
569
|
|
|
cls.mock_put_patcher = patch('pyUSIrest.client.requests.put') |
|
570
|
|
|
cls.mock_put = cls.mock_put_patcher.start() |
|
571
|
|
|
|
|
572
|
|
|
@classmethod |
|
573
|
|
|
def teardown_class(cls): |
|
574
|
|
|
cls.mock_get_patcher.stop() |
|
575
|
|
|
cls.mock_post_patcher.stop() |
|
576
|
|
|
cls.mock_put_patcher.stop() |
|
577
|
|
|
|
|
578
|
|
|
def setUp(self): |
|
579
|
|
|
self.auth = Auth(token=generate_token()) |
|
580
|
|
|
|
|
581
|
|
|
with open(os.path.join(data_path, "team.json")) as handle: |
|
582
|
|
|
data = json.load(handle) |
|
583
|
|
|
|
|
584
|
|
|
self.team = Team(self.auth, data=data) |
|
585
|
|
|
|
|
586
|
|
|
def test_str(self): |
|
587
|
|
|
test = self.team.__str__() |
|
588
|
|
|
self.assertIsInstance(test, str) |
|
589
|
|
|
|
|
590
|
|
|
def mocked_create_submission(*args, **kwargs): |
|
591
|
|
|
class MockResponse: |
|
592
|
|
|
def __init__(self, json_data, status_code): |
|
593
|
|
|
self.json_data = json_data |
|
594
|
|
|
self.status_code = status_code |
|
595
|
|
|
self.text = "MockResponse not implemented: %s" % (args[0]) |
|
596
|
|
|
|
|
597
|
|
|
def json(self): |
|
598
|
|
|
return self.json_data |
|
599
|
|
|
|
|
600
|
|
|
with open(os.path.join(data_path, "newSubmission.json")) as handle: |
|
601
|
|
|
data = json.load(handle) |
|
602
|
|
|
|
|
603
|
|
|
with open(os.path.join(data_path, "submissionStatus1.json")) as handle: |
|
604
|
|
|
status = json.load(handle) |
|
605
|
|
|
|
|
606
|
|
|
if args[0] == ( |
|
607
|
|
|
"https://submission-test.ebi.ac.uk/api/teams/subs.test" |
|
608
|
|
|
"-team-1/submissions"): |
|
609
|
|
|
return MockResponse(data, 201) |
|
610
|
|
|
|
|
611
|
|
|
elif args[0] == ( |
|
612
|
|
|
"https://submission-test.ebi.ac.uk/api/submissions/" |
|
613
|
|
|
"c8c86558-8d3a-4ac5-8638-7aa354291d61"): |
|
614
|
|
|
return MockResponse(data, 200) |
|
615
|
|
|
|
|
616
|
|
|
elif args[0] == ( |
|
617
|
|
|
"https://submission-test.ebi.ac.uk/api/submissions/" |
|
618
|
|
|
"c8c86558-8d3a-4ac5-8638-7aa354291d61/submissionStatus"): |
|
619
|
|
|
return MockResponse(status, 200) |
|
620
|
|
|
|
|
621
|
|
|
return MockResponse(None, 404) |
|
622
|
|
|
|
|
623
|
|
|
@patch('requests.get', side_effect=mocked_create_submission) |
|
624
|
|
|
@patch('requests.post', side_effect=mocked_create_submission) |
|
625
|
|
|
def test_create_submission(self, mock_get, mock_post): |
|
626
|
|
|
submission = self.team.create_submission() |
|
627
|
|
|
self.assertIsInstance(submission, Submission) |
|
628
|
|
|
|
|
629
|
|
|
def test_get_submission(self): |
|
630
|
|
|
with open(os.path.join(data_path, "teamSubmissions.json")) as handle: |
|
631
|
|
|
data = json.load(handle) |
|
632
|
|
|
|
|
633
|
|
|
self.mock_get.return_value = Mock() |
|
634
|
|
|
self.mock_get.return_value.json.return_value = data |
|
635
|
|
|
self.mock_get.return_value.status_code = 200 |
|
636
|
|
|
|
|
637
|
|
|
submissions = self.team.get_submissions() |
|
638
|
|
|
|
|
639
|
|
|
self.assertIsInstance(submissions, list) |
|
640
|
|
|
self.assertEqual(len(submissions), 2) |
|
641
|
|
|
|
|
642
|
|
|
# testing filtering |
|
643
|
|
|
draft = self.team.get_submissions(status="Draft") |
|
644
|
|
|
|
|
645
|
|
|
self.assertIsInstance(draft, list) |
|
646
|
|
|
self.assertEqual(len(draft), 1) |
|
647
|
|
|
self.assertIsInstance(draft[0], Submission) |
|
648
|
|
|
|
|
649
|
|
|
|
|
650
|
|
|
class SubmissionTest(TestCase): |
|
651
|
|
|
@classmethod |
|
652
|
|
|
def setup_class(cls): |
|
653
|
|
|
cls.mock_get_patcher = patch('pyUSIrest.client.requests.get') |
|
654
|
|
|
cls.mock_get = cls.mock_get_patcher.start() |
|
655
|
|
|
|
|
656
|
|
|
cls.mock_post_patcher = patch('pyUSIrest.client.requests.post') |
|
657
|
|
|
cls.mock_post = cls.mock_post_patcher.start() |
|
658
|
|
|
|
|
659
|
|
|
cls.mock_put_patcher = patch('pyUSIrest.client.requests.put') |
|
660
|
|
|
cls.mock_put = cls.mock_put_patcher.start() |
|
661
|
|
|
|
|
662
|
|
|
cls.mock_patch_patcher = patch('pyUSIrest.client.requests.patch') |
|
663
|
|
|
cls.mock_patch = cls.mock_patch_patcher.start() |
|
664
|
|
|
|
|
665
|
|
|
cls.mock_delete_patcher = patch('pyUSIrest.client.requests.delete') |
|
666
|
|
|
cls.mock_delete = cls.mock_delete_patcher.start() |
|
667
|
|
|
|
|
668
|
|
|
@classmethod |
|
669
|
|
|
def teardown_class(cls): |
|
670
|
|
|
cls.mock_get_patcher.stop() |
|
671
|
|
|
cls.mock_post_patcher.stop() |
|
672
|
|
|
cls.mock_put_patcher.stop() |
|
673
|
|
|
cls.mock_patch_patcher.stop() |
|
674
|
|
|
cls.mock_delete_patcher.stop() |
|
675
|
|
|
|
|
676
|
|
|
def setUp(self): |
|
677
|
|
|
self.auth = Auth(token=generate_token()) |
|
678
|
|
|
|
|
679
|
|
|
with open(os.path.join(data_path, "newSubmission.json")) as handle: |
|
680
|
|
|
data = json.load(handle) |
|
681
|
|
|
|
|
682
|
|
|
self.submission = Submission(self.auth, data=data) |
|
683
|
|
|
|
|
684
|
|
|
with open(os.path.join(data_path, "contents.json")) as handle: |
|
685
|
|
|
self.content = json.load(handle) |
|
686
|
|
|
|
|
687
|
|
|
# defining samples |
|
688
|
|
|
self.sample1 = { |
|
689
|
|
|
'alias': 'animal_1', |
|
690
|
|
|
'title': 'animal_title', |
|
691
|
|
|
'releaseDate': '2018-07-13', |
|
692
|
|
|
'taxonId': 9940, |
|
693
|
|
|
'attributes': { |
|
694
|
|
|
'material': [ |
|
695
|
|
|
{'value': 'organism', |
|
696
|
|
|
'terms': [ |
|
697
|
|
|
{'url': 'http://purl.obolibrary.org/obo/OBI_0100026'} |
|
698
|
|
|
]} |
|
699
|
|
|
], |
|
700
|
|
|
'project': [{'value': 'test'}] |
|
701
|
|
|
}, |
|
702
|
|
|
'sampleRelationships': []} |
|
703
|
|
|
|
|
704
|
|
|
self.sample2 = { |
|
705
|
|
|
'alias': 'sample_1', |
|
706
|
|
|
'title': 'sample_title', |
|
707
|
|
|
'releaseDate': '2018-07-13', |
|
708
|
|
|
'taxonId': 9940, |
|
709
|
|
|
'description': 'a description', |
|
710
|
|
|
'attributes': { |
|
711
|
|
|
'material': [ |
|
712
|
|
|
{'value': 'specimen from organism', |
|
713
|
|
|
'terms': [ |
|
714
|
|
|
{'url': 'http://purl.obolibrary.org/obo/OBI_0001479'} |
|
715
|
|
|
]} |
|
716
|
|
|
], |
|
717
|
|
|
'project': [{'value': 'test'}] |
|
718
|
|
|
}, |
|
719
|
|
|
'sampleRelationships': [{ |
|
720
|
|
|
'alias': 'animal_1', |
|
721
|
|
|
'relationshipNature': 'derived from'}] |
|
722
|
|
|
} |
|
723
|
|
|
|
|
724
|
|
|
def test_str(self): |
|
725
|
|
|
with open(os.path.join(data_path, "submissionStatus1.json")) as handle: |
|
726
|
|
|
data = json.load(handle) |
|
727
|
|
|
|
|
728
|
|
|
self.mock_get.return_value = Mock() |
|
729
|
|
|
self.mock_get.return_value.json.return_value = data |
|
730
|
|
|
self.mock_get.return_value.status_code = 200 |
|
731
|
|
|
|
|
732
|
|
|
test = self.submission.__str__() |
|
733
|
|
|
self.assertIsInstance(test, str) |
|
734
|
|
|
|
|
735
|
|
|
def create_sample(self, sample): |
|
736
|
|
|
self.mock_get.return_value = Mock() |
|
737
|
|
|
self.mock_get.return_value.json.return_value = self.content |
|
738
|
|
|
self.mock_get.return_value.status_code = 200 |
|
739
|
|
|
|
|
740
|
|
|
with open(os.path.join(data_path, "%s.json" % (sample))) as handle: |
|
741
|
|
|
data = json.load(handle) |
|
742
|
|
|
|
|
743
|
|
|
self.mock_post.return_value = Mock() |
|
744
|
|
|
self.mock_post.return_value.json.return_value = data |
|
745
|
|
|
self.mock_post.return_value.status_code = 201 |
|
746
|
|
|
|
|
747
|
|
|
return self.submission.create_sample(getattr(self, sample)) |
|
748
|
|
|
|
|
749
|
|
|
def test_create_sample(self): |
|
750
|
|
|
sample1 = self.create_sample("sample1") |
|
751
|
|
|
self.assertIsInstance(sample1, Sample) |
|
752
|
|
|
|
|
753
|
|
|
sample2 = self.create_sample("sample2") |
|
754
|
|
|
self.assertIsInstance(sample2, Sample) |
|
755
|
|
|
|
|
756
|
|
|
def mocked_get_samples(*args, **kwargs): |
|
757
|
|
|
class MockResponse: |
|
758
|
|
|
def __init__(self, json_data, status_code): |
|
759
|
|
|
self.json_data = json_data |
|
760
|
|
|
self.status_code = status_code |
|
761
|
|
|
self.text = "MockResponse not implemented: %s" % (args[0]) |
|
762
|
|
|
|
|
763
|
|
|
def json(self): |
|
764
|
|
|
return self.json_data |
|
765
|
|
|
|
|
766
|
|
|
get_samples_link = ( |
|
767
|
|
|
"https://submission-test.ebi.ac.uk/api/submissions/" |
|
768
|
|
|
"c8c86558-8d3a-4ac5-8638-7aa354291d61/contents/samples") |
|
769
|
|
|
|
|
770
|
|
|
with open(os.path.join(data_path, "samples.json")) as handle: |
|
771
|
|
|
samples = json.load(handle) |
|
772
|
|
|
|
|
773
|
|
|
with open(os.path.join(data_path, "validation1.json")) as handle: |
|
774
|
|
|
validation1 = json.load(handle) |
|
775
|
|
|
|
|
776
|
|
|
with open(os.path.join(data_path, "validation2.json")) as handle: |
|
777
|
|
|
validation2 = json.load(handle) |
|
778
|
|
|
|
|
779
|
|
|
# followin content -> samples |
|
780
|
|
|
if args[0] == get_samples_link: |
|
781
|
|
|
return MockResponse(samples, 200) |
|
782
|
|
|
|
|
783
|
|
|
# sample1 validation result |
|
784
|
|
|
elif args[0] == ( |
|
785
|
|
|
'https://submission-test.ebi.ac.uk/api/samples/90c8f449-' |
|
786
|
|
|
'b3c2-4238-a22b-fd03bc02a5d2/validationResult'): |
|
787
|
|
|
return MockResponse(validation1, 200) |
|
788
|
|
|
|
|
789
|
|
|
# sample1 validtation result |
|
790
|
|
|
elif args[0] == ( |
|
791
|
|
|
'https://submission-test.ebi.ac.uk/api/samples/58cb010a-' |
|
792
|
|
|
'3a89-42b7-8ccd-67b6f8b6dd4c/validationResult'): |
|
793
|
|
|
return MockResponse(validation2, 200) |
|
794
|
|
|
|
|
795
|
|
|
return MockResponse(None, 404) |
|
796
|
|
|
|
|
797
|
|
|
# We patch 'requests.get' with our own method. The mock object is passed |
|
798
|
|
|
# in to our test case method. |
|
799
|
|
|
@patch('requests.get', side_effect=mocked_get_samples) |
|
800
|
|
|
def test_get_samples(self, mock_get): |
|
801
|
|
|
samples = self.submission.get_samples(validationResult='Complete') |
|
802
|
|
|
|
|
803
|
|
|
self.assertIsInstance(samples, list) |
|
804
|
|
|
self.assertEqual(len(samples), 2) |
|
805
|
|
|
|
|
806
|
|
|
def mocked_get_empty_samples(*args, **kwargs): |
|
807
|
|
|
"""Simulate a submission with no samples at all""" |
|
808
|
|
|
|
|
809
|
|
|
class MockResponse: |
|
810
|
|
|
def __init__(self, json_data, status_code): |
|
811
|
|
|
self.json_data = json_data |
|
812
|
|
|
self.status_code = status_code |
|
813
|
|
|
self.text = "MockResponse not implemented: %s" % (args[0]) |
|
814
|
|
|
|
|
815
|
|
|
def json(self): |
|
816
|
|
|
return self.json_data |
|
817
|
|
|
|
|
818
|
|
|
get_samples_link = ( |
|
819
|
|
|
"https://submission-test.ebi.ac.uk/api/submissions/" |
|
820
|
|
|
"c8c86558-8d3a-4ac5-8638-7aa354291d61/contents/samples") |
|
821
|
|
|
|
|
822
|
|
|
with open(os.path.join(data_path, "empty_samples.json")) as handle: |
|
823
|
|
|
samples = json.load(handle) |
|
824
|
|
|
|
|
825
|
|
|
# followin content -> samples |
|
826
|
|
|
if args[0] == get_samples_link: |
|
827
|
|
|
return MockResponse(samples, 200) |
|
828
|
|
|
|
|
829
|
|
|
# default response |
|
830
|
|
|
return MockResponse(None, 404) |
|
831
|
|
|
|
|
832
|
|
|
# patch a request.get to return 0 samples for a submission |
|
833
|
|
|
@patch('requests.get', side_effect=mocked_get_empty_samples) |
|
834
|
|
|
def test_get_empty_samples(self, mock_get): |
|
835
|
|
|
samples = self.submission.get_samples(validationResult='Complete') |
|
836
|
|
|
|
|
837
|
|
|
self.assertIsInstance(samples, list) |
|
838
|
|
|
self.assertEqual(len(samples), 0) |
|
839
|
|
|
|
|
840
|
|
|
def test_get_status(self): |
|
841
|
|
|
with open(os.path.join(data_path, "validationResults.json")) as handle: |
|
842
|
|
|
data = json.load(handle) |
|
843
|
|
|
|
|
844
|
|
|
self.mock_get.return_value = Mock() |
|
845
|
|
|
self.mock_get.return_value.json.return_value = data |
|
846
|
|
|
self.mock_get.return_value.status_code = 200 |
|
847
|
|
|
|
|
848
|
|
|
statuses = self.submission.get_status() |
|
849
|
|
|
self.assertEqual(statuses['Complete'], 2) |
|
850
|
|
|
|
|
851
|
|
|
def test_check_ready(self): |
|
852
|
|
|
with open(os.path.join( |
|
853
|
|
|
data_path, "availableSubmissionStatuses.json")) as handle: |
|
854
|
|
|
data = json.load(handle) |
|
855
|
|
|
|
|
856
|
|
|
self.mock_get.return_value = Mock() |
|
857
|
|
|
self.mock_get.return_value.json.return_value = data |
|
858
|
|
|
self.mock_get.return_value.status_code = 200 |
|
859
|
|
|
|
|
860
|
|
|
check = self.submission.check_ready() |
|
861
|
|
|
self.assertTrue(check) |
|
862
|
|
|
|
|
863
|
|
|
def mocked_finalize(*args, **kwargs): |
|
864
|
|
|
class MockResponse: |
|
865
|
|
|
def __init__(self, json_data, status_code): |
|
866
|
|
|
self.json_data = json_data |
|
867
|
|
|
self.status_code = status_code |
|
868
|
|
|
self.text = "MockResponse not implemented: %s" % (args[0]) |
|
869
|
|
|
|
|
870
|
|
|
def json(self): |
|
871
|
|
|
return self.json_data |
|
872
|
|
|
|
|
873
|
|
|
check_ready_link = ( |
|
874
|
|
|
"https://submission-test.ebi.ac.uk/api/submissions/c8c86558-" |
|
875
|
|
|
"8d3a-4ac5-8638-7aa354291d61/availableSubmissionStatuses") |
|
876
|
|
|
|
|
877
|
|
|
with open(os.path.join( |
|
878
|
|
|
data_path, "availableSubmissionStatuses.json")) as handle: |
|
879
|
|
|
check_ready_data = json.load(handle) |
|
880
|
|
|
|
|
881
|
|
|
validation_link = ( |
|
882
|
|
|
"https://submission-test.ebi.ac.uk/api/validationResults/search/" |
|
883
|
|
|
"by-submission?submissionId=c8c86558-8d3a-4ac5-8638-7aa354291d61") |
|
884
|
|
|
|
|
885
|
|
|
with open(os.path.join(data_path, "validationResults.json")) as handle: |
|
886
|
|
|
validation_data = json.load(handle) |
|
887
|
|
|
|
|
888
|
|
|
self_link = ( |
|
889
|
|
|
"https://submission-test.ebi.ac.uk/api/submissions/" |
|
890
|
|
|
"c8c86558-8d3a-4ac5-8638-7aa354291d61") |
|
891
|
|
|
|
|
892
|
|
|
with open(os.path.join(data_path, "newSubmission.json")) as handle: |
|
893
|
|
|
self_data = json.load(handle) |
|
894
|
|
|
|
|
895
|
|
|
status_link = ( |
|
896
|
|
|
"https://submission-test.ebi.ac.uk/api/submissions/c8c86558-" |
|
897
|
|
|
"8d3a-4ac5-8638-7aa354291d61/submissionStatus") |
|
898
|
|
|
|
|
899
|
|
|
with open(os.path.join(data_path, "submissionStatus2.json")) as handle: |
|
900
|
|
|
status_data = json.load(handle) |
|
901
|
|
|
|
|
902
|
|
|
if args[0] == check_ready_link: |
|
903
|
|
|
return MockResponse(check_ready_data, 200) |
|
904
|
|
|
|
|
905
|
|
|
elif args[0] == validation_link: |
|
906
|
|
|
return MockResponse(validation_data, 200) |
|
907
|
|
|
|
|
908
|
|
|
elif args[0] == self_link: |
|
909
|
|
|
return MockResponse(self_data, 200) |
|
910
|
|
|
|
|
911
|
|
|
elif args[0] == status_link: |
|
912
|
|
|
return MockResponse(status_data, 200) |
|
913
|
|
|
|
|
914
|
|
|
return MockResponse(None, 404) |
|
915
|
|
|
|
|
916
|
|
|
@patch('requests.get', side_effect=mocked_finalize) |
|
917
|
|
|
def test_finalize(self, mock_get): |
|
918
|
|
|
self.mock_put.return_value = Mock() |
|
919
|
|
|
self.mock_put.return_value.json.return_value = {} |
|
920
|
|
|
self.mock_put.return_value.status_code = 200 |
|
921
|
|
|
|
|
922
|
|
|
document = self.submission.finalize() |
|
923
|
|
|
self.assertIsInstance(document, Document) |
|
924
|
|
|
|
|
925
|
|
|
def test_delete(self): |
|
926
|
|
|
self.mock_delete.return_value = Mock() |
|
927
|
|
|
self.mock_delete.return_value.last_response = '' |
|
928
|
|
|
self.mock_delete.return_value.status_code = 204 |
|
929
|
|
|
|
|
930
|
|
|
self.submission.delete() |
|
931
|
|
|
|
|
932
|
|
|
|
|
933
|
|
|
class SampleTest(TestCase): |
|
934
|
|
|
@classmethod |
|
935
|
|
|
def setup_class(cls): |
|
936
|
|
|
cls.mock_get_patcher = patch('pyUSIrest.client.requests.get') |
|
937
|
|
|
cls.mock_get = cls.mock_get_patcher.start() |
|
938
|
|
|
|
|
939
|
|
|
cls.mock_patch_patcher = patch('pyUSIrest.client.requests.patch') |
|
940
|
|
|
cls.mock_patch = cls.mock_patch_patcher.start() |
|
941
|
|
|
|
|
942
|
|
|
cls.mock_delete_patcher = patch('pyUSIrest.client.requests.delete') |
|
943
|
|
|
cls.mock_delete = cls.mock_delete_patcher.start() |
|
944
|
|
|
|
|
945
|
|
|
@classmethod |
|
946
|
|
|
def teardown_class(cls): |
|
947
|
|
|
cls.mock_get_patcher.stop() |
|
948
|
|
|
cls.mock_patch_patcher.stop() |
|
949
|
|
|
cls.mock_delete_patcher.stop() |
|
950
|
|
|
|
|
951
|
|
|
def setUp(self): |
|
952
|
|
|
self.auth = Auth(token=generate_token()) |
|
953
|
|
|
|
|
954
|
|
|
with open(os.path.join(data_path, "sample2.json")) as handle: |
|
955
|
|
|
self.data = json.load(handle) |
|
956
|
|
|
|
|
957
|
|
|
self.sample = Sample(self.auth, data=self.data) |
|
958
|
|
|
|
|
959
|
|
|
def test_str(self): |
|
960
|
|
|
test = self.sample.__str__() |
|
961
|
|
|
self.assertIsInstance(test, str) |
|
962
|
|
|
|
|
963
|
|
|
def test_patch(self): |
|
964
|
|
|
self.mock_patch.return_value = Mock() |
|
965
|
|
|
self.mock_patch.return_value.json.return_value = self.data |
|
966
|
|
|
self.mock_patch.return_value.status_code = 200 |
|
967
|
|
|
|
|
968
|
|
|
self.mock_get.return_value = Mock() |
|
969
|
|
|
self.mock_get.return_value.json.return_value = self.data |
|
970
|
|
|
self.mock_get.return_value.status_code = 200 |
|
971
|
|
|
|
|
972
|
|
|
self.sample.patch(sample_data={'title': 'new title'}) |
|
973
|
|
|
|
|
974
|
|
|
def test_delete(self): |
|
975
|
|
|
self.mock_delete.return_value = Mock() |
|
976
|
|
|
self.mock_delete.return_value.last_response = '' |
|
977
|
|
|
self.mock_delete.return_value.status_code = 204 |
|
978
|
|
|
|
|
979
|
|
|
self.sample.delete() |
|
980
|
|
|
|