Skip to content

Commit 7de640e

Browse files
committed
Adding tests for test_get_elements_applicable_by_identifier
1 parent da1603b commit 7de640e

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

tests/test_utils/test_schemaview.py

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,35 +1962,71 @@ def test_dynamic_enum(schema_view_with_imports: SchemaView) -> None:
19621962
assert set(e.include[0].reachable_from.source_nodes) == {"GO:0007049", "GO:0022403"}
19631963

19641964

1965+
# dictionary mapping class name to id_prefixes
1966+
KS_PREFIXES_BY_CLASS = {PERSON: {"orcid", "doi", "zfin", "wb"}, ORGANIZATION: {"pmid", "zfin", "wb"}}
1967+
1968+
19651969
def test_get_elements_applicable_by_prefix(schema_view_no_imports: SchemaView) -> None:
19661970
"""Test get_elements_applicable_by_prefix method."""
19671971
view = schema_view_no_imports
19681972
# create a dictionary mapping class name to id_prefixes
19691973
prefixes = {el: set(view.get_element(el).id_prefixes) for el in [PERSON, ORGANIZATION]}
19701974

1971-
assert prefixes[PERSON] == {"ORCID", "DOI", "ZFIN", "WB"}
1972-
assert prefixes[ORGANIZATION] == {"PMID", "ZFIN", "WB"}
1975+
for el in [PERSON, ORGANIZATION]:
1976+
assert prefixes[el] == {prfx.upper() for prfx in KS_PREFIXES_BY_CLASS[el]}
19731977

19741978
for prefix in ["ORCID", "DOI", "ZFIN", "PMID", "WB"]:
19751979
els_applicable_by_prefix = view.get_elements_applicable_by_prefix(prefix)
19761980
for el in [PERSON, ORGANIZATION]:
1977-
if prefix in prefixes[el]:
1981+
if prefix.lower() in KS_PREFIXES_BY_CLASS[el]:
19781982
assert el in els_applicable_by_prefix
19791983
else:
19801984
assert el not in els_applicable_by_prefix
19811985

19821986

1983-
def test_get_elements_applicable_by_identifier(schema_view_no_imports: SchemaView) -> None:
1987+
@pytest.mark.parametrize("prefix", ["ORCID", "DOI", "ZFIN", "PMID", "WB", "Pmid", "TEST", "rdfs", "some_crap"])
1988+
def test_get_elements_applicable_by_identifier(schema_view_no_imports: SchemaView, prefix: str) -> None:
19841989
"""Test get_elements_applicable_by_identifier method."""
19851990
view = schema_view_no_imports
1986-
elements = view.get_elements_applicable_by_identifier("ORCID:1234")
1987-
assert PERSON in elements
1988-
elements = view.get_elements_applicable_by_identifier("PMID:1234")
1989-
assert ORGANIZATION in elements
1990-
elements = view.get_elements_applicable_by_identifier("http://www.ncbi.nlm.nih.gov/pubmed/1234")
1991-
assert ORGANIZATION in elements
1992-
elements = view.get_elements_applicable_by_identifier("TEST:1234")
1993-
assert "anatomical entity" not in elements
1991+
# make sure imports are loaded
1992+
view.all_schema()
1993+
1994+
elements = view.get_elements_applicable_by_identifier(f"{prefix}:1234-5678-90")
1995+
# check in KS_PREFIXES_BY_CLASS to see whether we expect PERSON or ORGANIZATION to have this prefix
1996+
for el in [PERSON, ORGANIZATION]:
1997+
if prefix.lower() in KS_PREFIXES_BY_CLASS[el]:
1998+
assert el in elements
1999+
else:
2000+
assert el not in elements
2001+
2002+
no_els = False
2003+
if prefix.lower() not in {*KS_PREFIXES_BY_CLASS[PERSON], *KS_PREFIXES_BY_CLASS[ORGANIZATION]}:
2004+
assert elements == []
2005+
no_els = True
2006+
2007+
# Prefix => URL mapping; some URLs are valid, others are made up.
2008+
prefix_to_url = {
2009+
"DOI": "http://dx.doi.org/", # valid
2010+
"PMID": "http://www.ncbi.nlm.nih.gov/pubmed/", # valid
2011+
"rdfs": "http://www.w3.org/2000/01/rdf-schema#", # valid
2012+
"ZFIN": "http://zfin.org/", # valid
2013+
# made up URLs
2014+
"WB": "https://www.wormbase.org/get?name=",
2015+
"TEST": "https://www.test.com/id=",
2016+
"Pmid": "http://www.ncbi.nlm.nih.gov/pubmed/",
2017+
"ORCID": "http://orcids-r-us.com/orcid/",
2018+
"some_crap": "https://whatev.er/",
2019+
}
2020+
# These URLs are defined in the schema prefixes section
2021+
valid_urls = {"doi", "pmid", "rdfs", "zfin"}
2022+
2023+
# Get element by URL
2024+
# This will only successfully retrieve the element if the URL is in `valid_urls`
2025+
url_els = view.get_elements_applicable_by_identifier(f"{prefix_to_url[prefix]}1234-5678-90")
2026+
if no_els or prefix.lower() not in valid_urls:
2027+
assert url_els == []
2028+
else:
2029+
assert url_els == elements
19942030

19952031

19962032
# FIXME: improve test to actually test the annotations

0 commit comments

Comments
 (0)