Skip to content

Commit

Permalink
Merge pull request #322 from tcmitchell/176-copy-document
Browse files Browse the repository at this point in the history
Propagate the new document pointer after copy
  • Loading branch information
tcmitchell authored Oct 4, 2021
2 parents 9cccb8d + 331f303 commit 9feb29f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
8 changes: 8 additions & 0 deletions sbol3/toplevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ def clone(self, new_identity: str) -> 'TopLevel':
obj.traverse(make_update_references_traverser(identity_map))
return obj

def copy(self, target_doc=None, target_namespace=None):
new_obj = super().copy(target_doc=target_doc, target_namespace=target_namespace)
# Need to set `document` on all children recursively. That's what happens when
# you assign to the `document` property of an Identified
new_obj.document = target_doc
# Comply with the contract of super.copy()
return new_obj


def make_erase_identity_traverser(identity_map: Dict[str, Identified])\
-> Callable[[Identified], None]:
Expand Down
29 changes: 28 additions & 1 deletion test/test_toplevel.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import posixpath
import unittest

Expand All @@ -14,8 +15,11 @@
sbol:type <https://identifiers.org/SBO:0000251> .
"""

MODULE_LOCATION = os.path.dirname(os.path.abspath(__file__))
SBOL3_LOCATION = os.path.join(MODULE_LOCATION, 'SBOLTestSuite', 'SBOL3')

class MyTestCase(unittest.TestCase):

class TestTopLevel(unittest.TestCase):

def setUp(self) -> None:
sbol3.set_defaults()
Expand Down Expand Up @@ -48,6 +52,29 @@ def test_no_namespace_in_file(self):
report = doc.validate()
self.assertTrue(len(report) > 0)

def test_copy(self):
# See https://github.com/SynBioDex/pySBOL3/issues/176 reopened
# Copying a tree of objects to a new document left the document
# pointer of the child objects unset. This caused "lookup" to
# fail.
dest_doc = sbol3.Document()

def check_document(i: sbol3.Identified):
# Verify that the object has a document, and that it is the
# expected document.
self.assertIsNotNone(i.document)
self.assertEqual(dest_doc, i.document)

test_path = os.path.join(SBOL3_LOCATION, 'multicellular',
'multicellular.nt')
doc = sbol3.Document()
doc.read(test_path)
for obj in doc.objects:
obj.copy(target_doc=dest_doc)
self.assertEqual(len(doc), len(dest_doc))
for obj in dest_doc.objects:
obj.traverse(check_document)


if __name__ == '__main__':
unittest.main()

0 comments on commit 9feb29f

Please sign in to comment.