Skip to content

Commit

Permalink
Add more careful norm_text check
Browse files Browse the repository at this point in the history
This makes sure it's not none before checking if it can be stripped
  • Loading branch information
cthoyt committed Feb 21, 2024
1 parent fc7e4db commit 421f362
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 20 deletions.
4 changes: 2 additions & 2 deletions gilda/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def __init__(self, norm_text, text, db, id, entry_name, status, source,
organism=None, source_db=None, source_id=None):
if not text:
raise ValueError('Text for Term cannot be empty')
if not norm_text.strip():
raise ValueError('Normalized text for Term cannot be empty')
if not norm_text or not norm_text.strip():
raise ValueError('Normalized text for Term cannot be None nor empty')
self.norm_text = norm_text
self.text = text
self.db = db
Expand Down
101 changes: 83 additions & 18 deletions gilda/tests/test_term.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,97 @@
import pytest

from gilda.term import Term, get_identifiers_url


def test_invalid():
with pytest.raises(ValueError):
Term(
None,
"Mitochondria",
"GO",
"GO:0005739",
"mitochondrion",
"synonym",
"mesh",
None,
"MESH",
"D008928",
)

with pytest.raises(ValueError):
Term(
"",
"Mitochondria",
"GO",
"GO:0005739",
"mitochondrion",
"synonym",
"mesh",
None,
"MESH",
"D008928",
)

with pytest.raises(ValueError):
Term(
" ",
"Mitochondria",
"GO",
"GO:0005739",
"mitochondrion",
"synonym",
"mesh",
None,
"MESH",
"D008928",
)


def test_standalone_get_url():
assert get_identifiers_url('UP', 'P12345') == \
'https://identifiers.org/uniprot:P12345'
assert get_identifiers_url('HGNC', '12345') == \
'https://identifiers.org/hgnc:12345'
assert get_identifiers_url('CHEBI', 'CHEBI:12345') == \
'https://identifiers.org/CHEBI:12345'
assert (
get_identifiers_url("UP", "P12345") == "https://identifiers.org/uniprot:P12345"
)
assert get_identifiers_url("HGNC", "12345") == "https://identifiers.org/hgnc:12345"
assert (
get_identifiers_url("CHEBI", "CHEBI:12345")
== "https://identifiers.org/CHEBI:12345"
)


def test_term_get_url():
term = Term(db='CHEBI', id='CHEBI:12345', entry_name='X',
norm_text='x', text='X', source='test', status='name')
assert term.get_curie() == \
'CHEBI:12345'
assert term.get_idenfiers_url() == \
'https://identifiers.org/CHEBI:12345'
term = Term(
db="CHEBI",
id="CHEBI:12345",
entry_name="X",
norm_text="x",
text="X",
source="test",
status="name",
)
assert term.get_curie() == "CHEBI:12345"
assert term.get_idenfiers_url() == "https://identifiers.org/CHEBI:12345"
assert term.get_groundings() == {(term.db, term.id)}
assert term.get_namespaces() == {term.db}


def test_term_source_db_id():
term = Term('mitochondria', 'Mitochondria', 'GO', 'GO:0005739',
'mitochondrion', 'synonym', 'mesh', None, 'MESH', 'D008928')
assert term.source_db == 'MESH'
assert term.source_id == 'D008928'
assert term.get_groundings() == {(term.db, term.id),
(term.source_db, term.source_id)}
term = Term(
"mitochondria",
"Mitochondria",
"GO",
"GO:0005739",
"mitochondrion",
"synonym",
"mesh",
None,
"MESH",
"D008928",
)
assert term.source_db == "MESH"
assert term.source_id == "D008928"
assert term.get_groundings() == {
(term.db, term.id),
(term.source_db, term.source_id),
}

assert term.get_namespaces() == {term.db, term.source_db}

0 comments on commit 421f362

Please sign in to comment.