Skip to content

Commit c0f5f7c

Browse files
authored
Mock SPARQL calls in Query tests (#49)
1 parent 8b86082 commit c0f5f7c

File tree

1 file changed

+71
-6
lines changed

1 file changed

+71
-6
lines changed

test/test_query.py

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import pytest
2828

2929
from pandasaurus.query import Query
30-
from pandasaurus.utils.query_utils import run_sparql_query
31-
from pandasaurus.utils.sparql_queries import get_contextual_enrichment_query
3230

3331
blood_and_immune_test_data = get_blood_and_immune_test_data()
3432

@@ -244,8 +242,8 @@ def test_contextual_slim_enrichment(mocker):
244242
)
245243
q = Query(kidney_test_data)
246244
df = q.contextual_slim_enrichment(context_list)
247-
query_string = get_contextual_enrichment_query(context_list)
248-
object_list = kidney_test_data + [res.get("term") for res in run_sparql_query(query_string)]
245+
context_members = get_context_members_result()
246+
object_list = kidney_test_data + [res.get("term") for res in context_members]
249247
assert df["s"].isin(kidney_test_data).any()
250248
assert df["o"].isin(object_list).any()
251249
assert expected_contextual_df["s"].reset_index(drop=True).equals(df["s"].reset_index(drop=True))
@@ -312,6 +310,13 @@ def test_ancestor_enrichment_requires_positive_step(mocker):
312310

313311

314312
def test_synonym_lookup(mocker):
313+
mocker.patch(
314+
"pandasaurus.curie_validator.run_sparql_query",
315+
side_effect=[
316+
iter(get_enrichment_validate_curie_list_result()),
317+
iter(get_enrichment_find_obsolete_terms_data()),
318+
],
319+
)
315320
q = Query(["CL:0000084", "CL:0000813", "CL:0000815", "CL:0000900"])
316321

317322
mocker.patch(
@@ -330,12 +335,34 @@ def test_query():
330335
pass
331336

332337

333-
def test_update_obsoleted_terms():
338+
def test_update_obsoleted_terms(mocker):
334339
seed_list = ["CL:0000084", "CL:0011107"]
335340
expected_update_obsoleted_terms = [
336341
"IRI: CL:0000084, Label: T cell, Valid: True, Obsoleted: False",
337342
"IRI: CL:0000636, Label: Mueller cell, Valid: True, Obsoleted: False",
338343
]
344+
mocker.patch(
345+
"pandasaurus.curie_validator.run_sparql_query",
346+
side_effect=[
347+
iter(
348+
[
349+
{"label": "T cell", "term": "CL:0000084"},
350+
{"label": "obsolete Muller cell", "term": "CL:0011107"},
351+
]
352+
),
353+
iter(
354+
[
355+
{
356+
"depr_status": "true",
357+
"label": "obsolete Muller cell",
358+
"new_term": "CL:0000636",
359+
"new_term_label": "Mueller cell",
360+
"term": "CL:0011107",
361+
}
362+
]
363+
),
364+
],
365+
)
339366
q = Query(seed_list)
340367
q.update_obsoleted_terms()
341368
assert [str(term) for term in q._term_list] == expected_update_obsoleted_terms
@@ -359,6 +386,21 @@ def test_get_most_specific_objects(mocker):
359386
],
360387
)
361388

389+
mocker.patch(
390+
"pandasaurus.curie_validator.run_sparql_query",
391+
side_effect=[
392+
iter(get_enrichment_validate_curie_list_result()),
393+
iter(get_enrichment_find_obsolete_terms_data()),
394+
],
395+
)
396+
mocker.patch(
397+
"pandasaurus.curie_validator.run_sparql_query",
398+
side_effect=[
399+
iter(get_enrichment_validate_curie_list_result()),
400+
iter(get_enrichment_find_obsolete_terms_data()),
401+
],
402+
)
403+
362404
q = Query(seed_list)
363405
result_df = q.get_most_specific_objects("RO:0002215", "http://purl.obolibrary.org/obo/cl.owl")
364406

@@ -384,6 +426,14 @@ def test_get_most_specific_subjects(mocker):
384426
],
385427
)
386428

429+
mocker.patch(
430+
"pandasaurus.curie_validator.run_sparql_query",
431+
side_effect=[
432+
iter(get_enrichment_validate_curie_list_result()),
433+
iter(get_enrichment_find_obsolete_terms_data()),
434+
],
435+
)
436+
387437
q = Query(seed_list)
388438
result_df = q.get_most_specific_subjects("RO:0002215", "http://purl.obolibrary.org/obo/cl.owl")
389439

@@ -392,11 +442,26 @@ def test_get_most_specific_subjects(mocker):
392442

393443

394444
@pytest.fixture
395-
def enrichment_instance():
445+
def enrichment_instance(mocker):
446+
mocker.patch(
447+
"pandasaurus.curie_validator.run_sparql_query",
448+
side_effect=[
449+
iter(get_enrichment_validate_curie_list_result()),
450+
iter(get_enrichment_find_obsolete_terms_data()),
451+
],
452+
)
396453
return Query(blood_and_immune_test_data)
397454

398455

399456
def test_parent_enrichment(enrichment_instance, mocker):
457+
mocker.patch(
458+
"pandasaurus.query.run_sparql_query",
459+
side_effect=[
460+
iter(get_ancestor_object_list()),
461+
iter(get_ancestor_enrichment_result()),
462+
iter(get_ancestor_enrichment_result()),
463+
],
464+
)
400465
ancestor_enrichment_spy = mocker.spy(enrichment_instance, "ancestor_enrichment")
401466
enrichment_instance.parent_enrichment()
402467
ancestor_enrichment_spy.assert_called_once_with(1)

0 commit comments

Comments
 (0)