Skip to content

Commit ff96305

Browse files
committed
Mocked tests to avoid failing call to ontology service.
1 parent 593855f commit ff96305

1 file changed

Lines changed: 56 additions & 35 deletions

File tree

tests/utils/test_isatools_utils.py

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -152,25 +152,44 @@ def test_detect_graph_process_pooling_batch_on_mtbls(self):
152152

153153

154154
class TestOlsSearch(unittest.TestCase):
155-
@patch("isatools.net.ols")
156-
def test_get_ontologies(self, mock_get):
157-
def _side_effect():
158-
return self._make_requests_response(self._load_fixture("ontologies.json"))
155+
def _load_fixture(self, name):
156+
data_path = Path(__file__).parent / "fixtures" / name
157+
return data_path.read_bytes()
158+
159+
def _make_requests_response(self, body_bytes, status=200, headers=None):
160+
m = Mock()
161+
m.status_code = status
162+
m.content = body_bytes
163+
m.text = body_bytes.decode("utf-8")
164+
165+
def _json():
166+
return json.loads(m.text)
167+
168+
m.json = _json
169+
m.headers = headers or {}
170+
return m
159171

160-
mock_get.side_effect = _side_effect
172+
def _make_urlopen_response(self, body_bytes):
173+
m = Mock()
174+
m.read.return_value = body_bytes
175+
return m
176+
177+
def _make_ols_search_response(self, docs):
178+
body = {"response": {"docs": docs}}
179+
return self._make_requests_response(json.dumps(body).encode("utf-8"))
161180

181+
@patch("isatools.net.ols.urlopen")
182+
def test_get_ontologies(self, mock_urlopen):
183+
mock_urlopen.return_value = self._make_urlopen_response(self._load_fixture("ontologies.json"))
162184
ontology_sources = ols.get_ols_ontologies()
163185
self.assertGreater(len(ontology_sources), 0)
164186
self.assertIsInstance(ontology_sources, list)
165187
self.assertIsInstance(ontology_sources[0], OntologySource)
166188

167189
# @unittest.skip("efo is not available from https://www.ebi.ac.uk/ols4/api/ontologies")
168-
@patch("isatools.net.ols")
169-
def test_get_ontology0(self, mock_get):
170-
def _side_effect():
171-
return self._make_requests_response(self._load_fixture("ontologies.json"))
172-
173-
mock_get.side_effect = _side_effect
190+
@patch("isatools.net.ols.urlopen")
191+
def test_get_ontology0(self, mock_urlopen):
192+
mock_urlopen.return_value = self._make_urlopen_response(self._load_fixture("ontologies.json"))
174193
ontology_source = ols.get_ols_ontology("ado", 0)
175194
self.assertIsInstance(ontology_source, OntologySource)
176195
self.assertEqual(ontology_source.name, "ado")
@@ -179,12 +198,9 @@ def _side_effect():
179198
self.assertIsInstance(ontology_source.version, str)
180199
self.assertEqual(ontology_source.description, "Alzheimer's Disease Ontology (ADO)")
181200

182-
@patch("isatools.net.ols")
183-
def test_get_ontology1(self, mock_get):
184-
def _side_effect():
185-
return self._make_requests_response(self._load_fixture("ontologies.json"))
186-
187-
mock_get.side_effect = _side_effect
201+
@patch("isatools.net.ols.urlopen")
202+
def test_get_ontology1(self, mock_urlopen):
203+
mock_urlopen.return_value = self._make_urlopen_response(self._load_fixture("ontologies.json"))
188204
ontology_source = ols.get_ols_ontology("stato", 0)
189205
self.assertIsInstance(ontology_source, OntologySource)
190206
self.assertEqual(ontology_source.name, "stato")
@@ -193,12 +209,9 @@ def _side_effect():
193209
self.assertIsInstance(ontology_source.version, str)
194210
self.assertEqual(ontology_source.description, "STATO: the statistical methods ontology")
195211

196-
@patch("isatools.net.ols")
197-
def test_get_ontology2(self, mock_get):
198-
def _side_effect():
199-
return self._make_requests_response(self._load_fixture("ontologies.json"))
200-
201-
mock_get.side_effect = _side_effect
212+
@patch("isatools.net.ols.urlopen")
213+
def test_get_ontology2(self, mock_urlopen):
214+
mock_urlopen.return_value = self._make_urlopen_response(self._load_fixture("ontologies.json"))
202215
ontology_source = ols.get_ols_ontology("efo", 0)
203216
self.assertIsInstance(ontology_source, OntologySource)
204217
self.assertEqual(ontology_source.name, "efo")
@@ -208,12 +221,16 @@ def _side_effect():
208221
self.assertEqual(ontology_source.description, "Experimental Factor Ontology")
209222

210223
# @unittest.skip("efo is not available from https://www.ebi.ac.uk/ols4/api/ontologies")
211-
@patch("isatools.net.ols")
212-
def test_search_for_term_p0(self, mock_get):
213-
def _side_effect():
214-
return self._make_requests_response(self._load_fixture("ontologies.json"))
215-
216-
mock_get.side_effect = _side_effect
224+
@patch("requests.get")
225+
@patch("isatools.net.ols.urlopen")
226+
def test_search_for_term_p0(self, mock_urlopen, mock_requests_get):
227+
mock_urlopen.return_value = self._make_urlopen_response(self._load_fixture("ontologies.json"))
228+
mock_requests_get.return_value = self._make_ols_search_response(
229+
[
230+
{"label": "mobile phase", "iri": "http://purl.obolibrary.org/obo/CHMO_0000995"},
231+
{"label": "mobile phase velocity", "iri": "http://example.org/CHMO_FAKE"},
232+
]
233+
)
217234
ontology_source = ols.get_ols_ontology("chmo", 0)
218235
ontology_annotations = ols.search_ols("mobile phase", ontology_source)
219236
self.assertIsInstance(ontology_annotations, list)
@@ -224,12 +241,16 @@ def _side_effect():
224241
self.assertIn("http://purl.obolibrary.org/obo/CHMO_0000995", [oa.term_accession for oa in ontology_annotations])
225242
self.assertEqual(ontology_annotations[-1].term_source, ontology_source)
226243

227-
@patch("isatools.net.ols")
228-
def test_search_for_term_p1(self, mock_get):
229-
def _side_effect():
230-
return self._make_requests_response(self._load_fixture("ontologies.json"))
231-
232-
mock_get.side_effect = _side_effect
244+
@patch("requests.get")
245+
@patch("isatools.net.ols.urlopen")
246+
def test_search_for_term_p1(self, mock_urlopen, mock_requests_get):
247+
mock_urlopen.return_value = self._make_urlopen_response(self._load_fixture("ontologies.json"))
248+
mock_requests_get.return_value = self._make_ols_search_response(
249+
[
250+
{"label": "time", "iri": "http://www.ebi.ac.uk/efo/EFO_0000721"},
251+
{"label": "time series design", "iri": "http://www.ebi.ac.uk/efo/EFO_FAKE"},
252+
]
253+
)
233254
ontology_source = ols.get_ols_ontology("efo", 0)
234255
ontology_annotations = ols.search_ols("time", ontology_source)
235256
self.assertIsInstance(ontology_annotations, list)

0 commit comments

Comments
 (0)