Skip to content

Commit

Permalink
Merge pull request #144 from tcmitchell/142-check-identity
Browse files Browse the repository at this point in the history
Check identity in TopLevel constructor
  • Loading branch information
tcmitchell authored Jan 20, 2021
2 parents a4c7002 + c352d18 commit ae7ec29
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sbol3/toplevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
class TopLevel(Identified):

def __init__(self, identity: str, type_uri: str) -> None:
# Sanity check identity, which is required for a TopLevel
# More checking on identity happens in Identified, but Identified
# does not require an identity, only TopLevel does.
if not identity or not isinstance(identity, str):
raise ValueError('Identity must be a non-empty string')
super().__init__(identity, type_uri)
self.attachments = ReferencedObject(self, SBOL_HAS_ATTACHMENT, 0, math.inf)

Expand Down
11 changes: 11 additions & 0 deletions test/test_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ def test_round_trip(self):
# Compare specially
self.assertCountEqual([True, False], obj2.foo_bool)

def test_none_identity(self):
# Make sure a ValueError is raised if None is passed
# as a CustomTopLevel identity. And also if identity
# is an empty string or not a string.
with self.assertRaises(ValueError):
obj = CustomTopClass(None)
with self.assertRaises(ValueError):
obj = CustomTopClass('')
with self.assertRaises(ValueError):
obj = CustomTopClass(3)


class TestCustomIdentified(unittest.TestCase):

Expand Down

0 comments on commit ae7ec29

Please sign in to comment.