import numpy as np from uuid import uuid4, UUID import os from hdmf.container import AbstractContainer, Container, Data, HERDManager from hdmf.common.resources import HERD from hdmf.testing import TestCase from hdmf.utils import docval from hdmf.common import DynamicTable, VectorData, DynamicTableRegion from hdmf.backends.hdf5.h5tools import HDF5IO class Subcontainer(Container): pass class ContainerWithChild(Container): __fields__ = ({'name': 'field1', 'child': True}, ) @docval({'name': 'field1', 'doc': 'field1 doc', 'type': None, 'default': None}) def __init__(self, **kwargs): super().__init__('test name') self.field1 = kwargs['field1'] class TestHERDManager(TestCase): def test_link_and_get_resources(self): em = HERDManager() er = HERD() em.link_resources(er) er_get = em.get_linked_resources() self.assertEqual(er, er_get) class TestContainer(TestCase): def setUp(self): self.path = "test_container.h5" self.path2 = "test_container2.h5" def tearDown(self): if os.path.exists(self.path): os.remove(self.path) if os.path.exists(self.path2): os.remove(self.path2) def test_new(self): """Test that __new__ properly sets parent and other fields. """ parent_obj = Container('obj1') child_object_id = str(uuid4()) child_obj = Container.__new__(Container, parent=parent_obj, object_id=child_object_id, container_source="test_source") self.assertIs(child_obj.parent, parent_obj) self.assertIs(parent_obj.children[0], child_obj) self.assertEqual(child_obj.object_id, child_object_id) self.assertFalse(child_obj._in_construct_mode) self.assertTrue(child_obj.modified) def test_get_data_type(self): obj = Container('obj1') dt = obj.data_type self.assertEqual(dt, 'Container') def test_new_object_id_none(self): """Test that passing object_id=None to __new__ is OK and results in a non-None object ID being assigned. """ parent_obj = Container('obj1') child_obj = Container.__new__(Container, parent=parent_obj, object_id=None) self.assertIsNotNone(child_obj.object_id) UUID(child_obj.object_id, version=4) # raises ValueError if invalid def test_new_construct_mode(self): """Test that passing in_construct_mode to __new__ sets _in_construct_mode and _in_construct_mode can be reset. """ parent_obj = Container('obj1') child_obj = Container.__new__(Container, parent=parent_obj, object_id=None, in_construct_mode=True) self.assertTrue(child_obj._in_construct_mode) child_obj._in_construct_mode = False self.assertFalse(child_obj._in_construct_mode) def test_init(self): """Test that __init__ properly sets object ID and other fields. """ obj = Container('obj1') self.assertIsNotNone(obj.object_id) UUID(obj.object_id, version=4) # raises ValueError if invalid self.assertFalse(obj._in_construct_mode) self.assertTrue(obj.modified) self.assertEqual(obj.children, tuple()) self.assertIsNone(obj.parent) self.assertEqual(obj.name, 'obj1') self.assertIsNone(obj.read_io) def test_read_io_none(self): """Test that __init__ properly sets read_io to None""" obj = Container('obj1') self.assertIsNone(obj.read_io) def test_read_io_setter(self): """Test setting the read IO property""" obj = Container('obj1') # Bad value for read_io with self.assertRaises(TypeError): obj.read_io = "test" # Set read_io with HDF5IO(self.path, mode='w') as temp_io: obj.read_io = temp_io self.assertIs(obj.read_io, temp_io) # test that setting the read_io object to the same io object is OK obj.read_io = temp_io # Check that setting read_io to another io object fails with HDF5IO(self.path2, mode='w') as temp_io2: with self.assertRaises(ValueError): obj.read_io = temp_io2 def test_get_read_io_on_self(self): """Test that get_read_io works when the container is set on the container""" obj = Container('obj1') self.assertIsNone(obj.get_read_io()) with HDF5IO(self.path, mode='w') as temp_io: obj.read_io = temp_io re_io = obj.get_read_io() self.assertIs(re_io, temp_io) def test_get_read_io_on_parent(self): """Test that get_read_io works when the container is set on the parent""" parent_obj = Container('obj1') child_obj = Container('obj2') child_obj.parent = parent_obj with HDF5IO(self.path, mode='w') as temp_io: parent_obj.read_io = temp_io self.assertIsNone(child_obj.read_io) self.assertIs(child_obj.get_read_io(), temp_io) def test_del_read_io(self): class TestContainer(AbstractContainer): def __init__(self): raise ValueError("Error") with self.assertRaises(ValueError): TestContainer() def test_set_parent(self): """Test that parent setter properly sets parent """ parent_obj = Container('obj1') child_obj = Container('obj2') child_obj.parent = parent_obj self.assertIs(child_obj.parent, parent_obj) self.assertIs(parent_obj.children[0], child_obj) def test_set_parent_overwrite(self): """Test that parent setter properly blocks overwriting """ parent_obj = Container('obj1') child_obj = Container('obj2') child_obj.parent = parent_obj self.assertIs(parent_obj.children[0], child_obj) another_obj = Container('obj3') with self.assertRaisesWith(ValueError, 'Cannot reassign parent to Container: %s. Parent is already: %s.' % (repr(child_obj), repr(child_obj.parent))): child_obj.parent = another_obj self.assertIs(child_obj.parent, parent_obj) self.assertIs(parent_obj.children[0], child_obj) def test_set_parent_overwrite_proxy(self): """Test that parent setter properly blocks overwriting with proxy/object """ child_obj = Container('obj2') child_obj.parent = object() with self.assertRaisesRegex(ValueError, r"Got None for parent of '[^/]+' - cannot overwrite Proxy with NoneType"): child_obj.parent = None def test_slash_restriction(self): self.assertRaises(ValueError, Container, 'bad/name') def test_set_modified_parent(self): """Test that set modified properly sets parent modified """ parent_obj = Container('obj1') child_obj = Container('obj2') child_obj.parent = parent_obj parent_obj.set_modified(False) child_obj.set_modified(False) self.assertFalse(child_obj.parent.modified) child_obj.set_modified() self.assertTrue(child_obj.parent.modified) def test_all_children(self): col1 = VectorData( name='Species_1', description='...', data=['Homo sapiens'], ) species = DynamicTable(name='species', description='My species', columns=[col1]) obj = species.all_objects self.assertEqual(sorted(list(obj.keys())), sorted([species.object_id, species.id.object_id, col1.object_id])) def test_add_child(self): """Test that add child creates deprecation warning and also properly sets child's parent and modified """ parent_obj = Container('obj1') child_obj = Container('obj2') parent_obj.set_modified(False) with self.assertWarnsWith(DeprecationWarning, 'add_child is deprecated. Set the parent attribute instead.'): parent_obj.add_child(child_obj) self.assertIs(child_obj.parent, parent_obj) self.assertTrue(parent_obj.modified) self.assertIs(parent_obj.children[0], child_obj) def test_parent_set_link_warning(self): col1 = VectorData( name='col1', description='column #1', data=[1, 2], ) col2 = VectorData( name='col2', description='column #2', data=['a', 'b'], ) # this table will have two rows with ids 0 and 1 table = DynamicTable( name='my table', description='an example table', columns=[col1, col2], ) dtr_col = DynamicTableRegion( name='table1_ref', description='references rows of earlier table', data=[0, 1, 0, 0], # refers to row indices of the 'table' variable table=table ) data_col = VectorData( name='col2', description='column #2', data=['a', 'a', 'a', 'b'], ) table2 = DynamicTable( name='my_table', description='an example table', columns=[dtr_col, data_col], ) with self.assertWarns(Warning): table2.parent=ContainerWithChild() def test_set_parent_exists(self): """Test that setting a parent a second time does nothing """ parent_obj = Container('obj1') child_obj = Container('obj2') child_obj3 = Container('obj3') child_obj.parent = parent_obj child_obj.parent = parent_obj child_obj3.parent = parent_obj self.assertEqual(len(parent_obj.children), 2) self.assertIs(parent_obj.children[0], child_obj) self.assertIs(parent_obj.children[1], child_obj3) def test_reassign_container_source(self): """Test that reassign container source throws error """ parent_obj = Container('obj1') parent_obj.container_source = 'a source' with self.assertRaisesWith(Exception, 'cannot reassign container_source'): parent_obj.container_source = 'some other source' def test_repr(self): parent_obj = Container('obj1') self.assertRegex(str(parent_obj), r"obj1 hdmf.container.Container at 0x\d+") def test_type_hierarchy(self): self.assertEqual(Container.type_hierarchy(), (Container, AbstractContainer, object)) self.assertEqual(Subcontainer.type_hierarchy(), (Subcontainer, Container, AbstractContainer, object)) def test_generate_new_id_parent(self): """Test that generate_new_id sets a new ID on the container and its children and sets modified on all.""" parent_obj = Container('obj1') child_obj = Container('obj2') child_obj.parent = parent_obj old_parent_id = parent_obj.object_id old_child_id = child_obj.object_id parent_obj.set_modified(False) child_obj.set_modified(False) parent_obj.generate_new_id() self.assertNotEqual(old_parent_id, parent_obj.object_id) self.assertNotEqual(old_child_id, child_obj.object_id) self.assertTrue(parent_obj.modified) self.assertTrue(child_obj.modified) def test_generate_new_id_child(self): """Test that generate_new_id sets a new ID on the container and not its parent and sets modified on both.""" parent_obj = Container('obj1') child_obj = Container('obj2') child_obj.parent = parent_obj old_parent_id = parent_obj.object_id old_child_id = child_obj.object_id parent_obj.set_modified(False) child_obj.set_modified(False) child_obj.generate_new_id() self.assertEqual(old_parent_id, parent_obj.object_id) self.assertNotEqual(old_child_id, child_obj.object_id) self.assertTrue(parent_obj.modified) self.assertTrue(child_obj.modified) def test_generate_new_id_parent_no_recurse(self): """Test that generate_new_id(recurse=False) sets a new ID on the container and not its children.""" parent_obj = Container('obj1') child_obj = Container('obj2') child_obj.parent = parent_obj old_parent_id = parent_obj.object_id old_child_id = child_obj.object_id parent_obj.set_modified(False) child_obj.set_modified(False) parent_obj.generate_new_id(recurse=False) self.assertNotEqual(old_parent_id, parent_obj.object_id) self.assertEqual(old_child_id, child_obj.object_id) self.assertTrue(parent_obj.modified) self.assertFalse(child_obj.modified) def test_remove_child(self): """Test that removing a child removes only the child. """ parent_obj = Container('obj1') child_obj = Container('obj2') child_obj3 = Container('obj3') child_obj.parent = parent_obj child_obj3.parent = parent_obj parent_obj._remove_child(child_obj) self.assertIsNone(child_obj.parent) self.assertTupleEqual(parent_obj.children, (child_obj3, )) self.assertTrue(parent_obj.modified) self.assertTrue(child_obj.modified) def test_remove_child_noncontainer(self): """Test that removing a non-Container child raises an error. """ msg = "Cannot remove non-AbstractContainer object from children." with self.assertRaisesWith(ValueError, msg): Container('obj1')._remove_child(object()) def test_remove_child_nonchild(self): """Test that removing a non-Container child raises an error. """ msg = "Container 'dummy' is not a child of Container 'obj1'." with self.assertRaisesWith(ValueError, msg): Container('obj1')._remove_child(Container('dummy')) def test_reset_parent(self): """Test that removing a child removes only the child. """ parent_obj = Container('obj1') child_obj = Container('obj2') child_obj3 = Container('obj3') child_obj.parent = parent_obj child_obj3.parent = parent_obj child_obj.reset_parent() self.assertIsNone(child_obj.parent) self.assertTupleEqual(parent_obj.children, (child_obj3, )) self.assertTrue(parent_obj.modified) self.assertTrue(child_obj.modified) def test_reset_parent_parent_noncontainer(self): """Test that resetting a parent that is not a container raises an error. """ obj = Container('obj1') obj.parent = object() msg = "Cannot reset parent when parent is not an AbstractContainer: %s" % repr(obj.parent) with self.assertRaisesWith(ValueError, msg): obj.reset_parent() def test_reset_parent_no_parent(self): """Test that resetting a non-existent parent has no effect. """ obj = Container('obj1') obj.reset_parent() self.assertIsNone(obj.parent) def test_get_ancestors(self): """Test that get_ancestors returns the correct ancestors. """ grandparent_obj = Container('obj1') parent_obj = Container('obj2') child_obj = Container('obj3') parent_obj.parent = grandparent_obj child_obj.parent = parent_obj self.assertTupleEqual(grandparent_obj.get_ancestors(), tuple()) self.assertTupleEqual(parent_obj.get_ancestors(), (grandparent_obj, )) self.assertTupleEqual(child_obj.get_ancestors(), (parent_obj, grandparent_obj)) class TestHTMLRepr(TestCase): class ContainerWithChildAndData(Container): __fields__ = ( {'name': 'child', 'child': True}, "data", "str" ) @docval( {'name': 'child', 'doc': 'field1 doc', 'type': Container}, {'name': "data", "doc": 'data', 'type': list, "default": None}, {'name': "str", "doc": 'str', 'type': str, "default": None}, ) def __init__(self, **kwargs): super().__init__('test name') self.child = kwargs['child'] self.data = kwargs['data'] self.str = kwargs['str'] def test_repr_html_(self): child_obj1 = Container('test child 1') obj1 = self.ContainerWithChildAndData(child=child_obj1, data=[1, 2, 3], str="hello") assert obj1._repr_html_() == ( '\n \n \n ' '\n