1 | #!/usr/bin/env python3 |
||
2 | # -*- coding: utf-8 -*- |
||
3 | """ |
||
4 | Created on Fri Jan 10 11:34:59 2020 |
||
5 | |||
6 | @author: Paolo Cozzi <[email protected]> |
||
7 | """ |
||
8 | |||
9 | import os |
||
10 | import json |
||
11 | import types |
||
12 | |||
13 | from collections import defaultdict |
||
14 | from unittest.mock import patch, Mock |
||
15 | from unittest import TestCase |
||
16 | |||
17 | from pyUSIrest.auth import Auth |
||
18 | from pyUSIrest.usi import Team, User, Domain, Submission |
||
19 | |||
20 | from .common import DATA_PATH |
||
21 | from .test_auth import generate_token |
||
22 | |||
23 | |||
24 | class DomainTest(TestCase): |
||
25 | @classmethod |
||
26 | def setup_class(cls): |
||
27 | cls.mock_post_patcher = patch('requests.Session.post') |
||
28 | cls.mock_post = cls.mock_post_patcher.start() |
||
29 | |||
30 | cls.mock_get_patcher = patch('requests.Session.get') |
||
31 | cls.mock_get = cls.mock_get_patcher.start() |
||
32 | |||
33 | @classmethod |
||
34 | def teardown_class(cls): |
||
35 | cls.mock_post_patcher.stop() |
||
36 | |||
37 | def setUp(self): |
||
38 | self.auth = Auth(token=generate_token()) |
||
39 | |||
40 | # read domain data (a list of domains) |
||
41 | with open(os.path.join(DATA_PATH, "myDomain.json")) as handle: |
||
42 | data = json.load(handle) |
||
43 | |||
44 | self.domain = Domain(self.auth, data=data[0]) |
||
45 | |||
46 | def test_str(self): |
||
47 | test = self.domain.__str__() |
||
48 | self.assertIsInstance(test, str) |
||
49 | |||
50 | def test_create_profile(self): |
||
51 | with open(os.path.join(DATA_PATH, "domainProfile.json")) as handle: |
||
52 | data = json.load(handle) |
||
53 | |||
54 | self.mock_post.return_value = Mock() |
||
55 | self.mock_post.return_value.json.return_value = data |
||
56 | self.mock_post.return_value.status_code = 201 |
||
57 | |||
58 | self.domain.domainReference = ("dom-b38d6175-61e8-4d40-98da-" |
||
59 | "df9188d91c82") |
||
60 | |||
61 | self.domain.create_profile( |
||
62 | attributes={ |
||
63 | "cost_center": "ABC123", |
||
64 | "address": "South Building, EMBL-EBI, Wellcome Genome Campus," |
||
65 | "Hinxton, Cambridgeshire, CB10 1SD" |
||
66 | }) |
||
67 | |||
68 | def read_myUsers(self): |
||
69 | with open(os.path.join(DATA_PATH, "domainUsers.json")) as handle: |
||
70 | data = json.load(handle) |
||
71 | |||
72 | self.mock_get.return_value = Mock() |
||
73 | self.mock_get.return_value.json.return_value = data |
||
74 | self.mock_get.return_value.status_code = 200 |
||
75 | |||
76 | def test_users(self): |
||
77 | # initialize |
||
78 | self.read_myUsers() |
||
79 | |||
80 | # get my users |
||
81 | users = self.domain.users |
||
82 | |||
83 | self.assertIsInstance(users, list) |
||
84 | self.assertEqual(len(users), 2) |
||
85 | |||
86 | for user in users: |
||
87 | self.assertIsInstance(user, User) |
||
88 | |||
89 | |||
90 | class TeamTest(TestCase): |
||
91 | @classmethod |
||
92 | def setup_class(cls): |
||
93 | cls.mock_get_patcher = patch('requests.Session.get') |
||
94 | cls.mock_get = cls.mock_get_patcher.start() |
||
95 | |||
96 | cls.mock_post_patcher = patch('requests.Session.post') |
||
97 | cls.mock_post = cls.mock_post_patcher.start() |
||
98 | |||
99 | cls.mock_put_patcher = patch('requests.Session.put') |
||
100 | cls.mock_put = cls.mock_put_patcher.start() |
||
101 | |||
102 | @classmethod |
||
103 | def teardown_class(cls): |
||
104 | cls.mock_get_patcher.stop() |
||
105 | cls.mock_post_patcher.stop() |
||
106 | cls.mock_put_patcher.stop() |
||
107 | |||
108 | def setUp(self): |
||
109 | self.auth = Auth(token=generate_token()) |
||
110 | |||
111 | with open(os.path.join(DATA_PATH, "team.json")) as handle: |
||
112 | data = json.load(handle) |
||
113 | |||
114 | self.team = Team(self.auth, data=data) |
||
115 | |||
116 | def test_str(self): |
||
117 | test = self.team.__str__() |
||
118 | self.assertIsInstance(test, str) |
||
119 | |||
120 | def mocked_create_submission(*args, **kwargs): |
||
121 | class MockResponse: |
||
122 | def __init__(self, json_data, status_code): |
||
123 | self.json_data = json_data |
||
124 | self.status_code = status_code |
||
125 | self.text = "MockResponse not implemented: %s" % (args[0]) |
||
126 | |||
127 | def json(self): |
||
128 | return self.json_data |
||
129 | |||
130 | with open(os.path.join(DATA_PATH, "newSubmission.json")) as handle: |
||
131 | data = json.load(handle) |
||
132 | |||
133 | with open(os.path.join(DATA_PATH, "submissionStatus1.json")) as handle: |
||
134 | status = json.load(handle) |
||
135 | |||
136 | if args[0] == ( |
||
137 | "https://submission-test.ebi.ac.uk/api/teams/subs.test" |
||
138 | "-team-1/submissions"): |
||
139 | return MockResponse(data, 201) |
||
140 | |||
141 | elif args[0] == ( |
||
142 | "https://submission-test.ebi.ac.uk/api/submissions/" |
||
143 | "c8c86558-8d3a-4ac5-8638-7aa354291d61"): |
||
144 | return MockResponse(data, 200) |
||
145 | |||
146 | elif args[0] == ( |
||
147 | "https://submission-test.ebi.ac.uk/api/submissions/" |
||
148 | "c8c86558-8d3a-4ac5-8638-7aa354291d61/submissionStatus"): |
||
149 | return MockResponse(status, 200) |
||
150 | |||
151 | return MockResponse(None, 404) |
||
152 | |||
153 | @patch('requests.Session.get', side_effect=mocked_create_submission) |
||
154 | @patch('requests.Session.post', side_effect=mocked_create_submission) |
||
155 | def test_create_submission(self, mock_get, mock_post): |
||
156 | submission = self.team.create_submission() |
||
157 | self.assertIsInstance(submission, Submission) |
||
158 | |||
159 | def mocked_get_submission(*args, **kwargs): |
||
160 | class MockResponse: |
||
161 | def __init__(self, json_data, status_code): |
||
162 | self.json_data = json_data |
||
163 | self.status_code = status_code |
||
164 | self.text = "MockResponse not implemented: %s" % (args[0]) |
||
165 | |||
166 | def json(self): |
||
167 | return self.json_data |
||
168 | |||
169 | # this variable will collect all replies |
||
170 | replies = defaultdict(lambda: MockResponse(None, 404)) |
||
171 | |||
172 | # a custom function to set up replies for link |
||
173 | View Code Duplication | def set_reply(url, filename, status=200): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
174 | # referring to the upper replies variable |
||
175 | nonlocal replies |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
176 | |||
177 | # open data file |
||
178 | with open(os.path.join(DATA_PATH, filename)) as handle: |
||
179 | data = json.load(handle) |
||
180 | |||
181 | # track reply to URL |
||
182 | replies[url] = MockResponse(data, status) |
||
183 | |||
184 | set_reply( |
||
185 | "https://submission-test.ebi.ac.uk/api/submissions/search/" |
||
186 | "by-team?teamName=subs.test-team-1", |
||
187 | "teamSubmissions.json") |
||
188 | |||
189 | # to reload status |
||
190 | submission_prefix = "https://submission-test.ebi.ac.uk/api/submissions" |
||
191 | status_suffix = "submissionStatus" |
||
192 | |||
193 | status_link1 = "/".join([ |
||
194 | submission_prefix, |
||
195 | "87e7abda-81a8-4b5e-a1c0-323f7f0a4e43", |
||
196 | status_suffix]) |
||
197 | |||
198 | status_link2 = "/".join([ |
||
199 | submission_prefix, |
||
200 | "8b05e7f2-92c1-4651-94cb-9101f351f000", |
||
201 | status_suffix]) |
||
202 | |||
203 | set_reply(status_link1, "submissionStatus1.json") |
||
204 | |||
205 | set_reply(status_link2, "submissionStatus2.json") |
||
206 | |||
207 | return replies[args[0]] |
||
208 | |||
209 | @patch('requests.Session.get', side_effect=mocked_get_submission) |
||
210 | def test_get_submission(self, mock_get): |
||
211 | submissions = self.team.get_submissions() |
||
212 | |||
213 | # submissions is now a generator |
||
214 | self.assertIsInstance(submissions, types.GeneratorType) |
||
215 | |||
216 | # convert it into a list |
||
217 | submissions = list(submissions) |
||
218 | self.assertEqual(len(submissions), 2) |
||
219 | |||
220 | # testing filtering |
||
221 | draft = self.team.get_submissions(status="Draft") |
||
222 | |||
223 | # submissions is now a generator |
||
224 | self.assertIsInstance(draft, types.GeneratorType) |
||
225 | |||
226 | # convert it into a list |
||
227 | draft = list(draft) |
||
228 | self.assertEqual(len(draft), 1) |
||
229 | |||
230 | self.assertIsInstance(draft[0], Submission) |
||
231 |