Skip to content

Commit

Permalink
Add additional test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Aug 23, 2023
1 parent 606217b commit d7edf95
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/biomappings/contribute/obo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ def update_obo(*, prefix: str, path: Union[str, Path]) -> None:
file.writelines(lines)


def update_obo_lines(*, lines: List[str], mappings: List[Dict[str, Any]]) -> List[str]:
def update_obo_lines(*, lines: List[str], mappings: List[Dict[str, Any]], progress: bool = True) -> List[str]:
"""Update the lines of an OBO file.
:param mappings: Mappings to add
:param lines: A list of lines of the file (still containing trailing newlines)
:param progress: Show a progress bar
:returns: New lines. Does not modify the original list.
"""
lines = deepcopy(lines)

for mapping in tqdm(mappings, unit="mapping", unit_scale=True):
for mapping in tqdm(mappings, unit="mapping", unit_scale=True, disable=not progress):
target_prefix = mapping["target prefix"]
target_prefix = bioregistry.get_preferred_prefix(target_prefix) or target_prefix
target_identifier = standardize_identifier(target_prefix, mapping["target identifier"])
Expand Down
57 changes: 54 additions & 3 deletions tests/test_contribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
class TestContributeOBO(unittest.TestCase):
"""A test case for contributing to OBO flat files."""

def test_addition(self):
"""Test adding a non-redundant mapping."""
def setUp(self) -> None:
"""Set up the test case with a specific mapping."""
mappings = get_curated_mappings("uberon")
sources = {mapping["source identifier"] for mapping in mappings}
self.assertIn("UBERON:0000018", sources, msg="Mappings are not loaded properly")
Expand All @@ -23,7 +23,10 @@ def test_addition(self):
and mappings["target prefix"] == "idomal"
]
self.assertEqual(1, len(mappings))
self.mappings = mappings

def test_addition(self):
"""Test adding a non-redundant mapping."""
original = dedent(
"""\
[Term]
Expand Down Expand Up @@ -61,5 +64,53 @@ def test_addition(self):
)
self.assertEqual(
expected.splitlines(),
update_obo_lines(mappings=mappings, lines=original.splitlines()),
update_obo_lines(mappings=self.mappings, lines=original.splitlines(), progress=False),
)

def test_skip_redundant_1(self):
"""Test skipping adding a mapping that doesn't have metadata."""
original = dedent(
"""\
[Term]
id: UBERON:0000018
name: compound eye
def: "A light sensing organ composed of ommatidia." [FB:gg, Wikipedia:Compound_eye]
subset: organ_slim
synonym: "adult compound eye" RELATED []
xref: BTO:0001921
xref: HAO:0000217
xref: IDOMAL:0002421
xref: TGMA:0000024
intersection_of: UBERON:0000970 ! eye
relationship: only_in_taxon NCBITaxon:6656 {source="PMID:21062451"} ! Arthropoda
property_value: seeAlso "https://github.com/obophenotype/uberon/issues/457" xsd:anyURI
"""
)
self.assertEqual(
original.splitlines(),
update_obo_lines(mappings=self.mappings, lines=original.splitlines(), progress=False),
)

def test_skip_redundant_2(self):
"""Test skipping adding a mapping that doesn't have metadata."""
original = dedent(
"""\
[Term]
id: UBERON:0000018
name: compound eye
def: "A light sensing organ composed of ommatidia." [FB:gg, Wikipedia:Compound_eye]
subset: organ_slim
synonym: "adult compound eye" RELATED []
xref: BTO:0001921
xref: HAO:0000217
xref: IDOMAL:0002421 {dcterms:contributor="https://orcid.org/0000-0003-4423-4370"} ! compound eye
xref: TGMA:0000024
intersection_of: UBERON:0000970 ! eye
relationship: only_in_taxon NCBITaxon:6656 {source="PMID:21062451"} ! Arthropoda
property_value: seeAlso "https://github.com/obophenotype/uberon/issues/457" xsd:anyURI
"""
)
self.assertEqual(
original.splitlines(),
update_obo_lines(mappings=self.mappings, lines=original.splitlines(), progress=False),
)

0 comments on commit d7edf95

Please sign in to comment.