Example #1
0
    def test_child(self):
        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']

        child_obj1 = Container('test child 1')
        obj1 = ContainerWithChild(child_obj1)
        self.assertIs(child_obj1.parent, obj1)

        child_obj2 = Container('test child 2')
        obj3 = ContainerWithChild((child_obj1, child_obj2))
        self.assertIs(child_obj1.parent, obj1)  # child1 parent is already set
        self.assertIs(child_obj2.parent, obj3)  # child1 parent is already set

        child_obj3 = Container('test child 3')
        obj4 = ContainerWithChild({'test child 3': child_obj3})
        self.assertIs(child_obj3.parent, obj4)

        obj2 = ContainerWithChild()
        self.assertIsNone(obj2.field1)
 def test_add_list(self):
     """Test that adding a list to the attribute dict correctly adds the items."""
     obj1 = Container('obj1')
     obj2 = Container('obj2')
     foo = Foo()
     foo.add_container([obj1, obj2])
     self.assertDictEqual(foo.containers, {'obj1': obj1, 'obj2': obj2})
 def test_add_dict(self):
     """Test that adding a dict to the attribute dict correctly adds the input dict values."""
     obj1 = Container('obj1')
     obj2 = Container('obj2')
     foo = Foo()
     foo.add_container({'a': obj1, 'b': obj2})
     self.assertDictEqual(foo.containers, {'obj1': obj1, 'obj2': obj2})
Example #4
0
    def test_required_name(self):
        class ContainerRequiredName(Container):
            __fields__ = ({
                'name': 'field1',
                'required_name': 'field1 value'
            }, )

            @docval({
                'name': 'field1',
                'doc': 'field1 doc',
                'type': None,
                'default': None
            })
            def __init__(self, **kwargs):
                super().__init__('test name')
                self.field1 = kwargs['field1']

        msg = (
            "Field 'field1' on ContainerRequiredName has a required name and must be a subclass of "
            "AbstractContainer.")
        with self.assertRaisesWith(ValueError, msg):
            ContainerRequiredName('field1 value')

        obj1 = Container('test container')
        msg = "Field 'field1' on ContainerRequiredName must be named 'field1 value'."
        with self.assertRaisesWith(ValueError, msg):
            ContainerRequiredName(obj1)

        obj2 = Container('field1 value')
        obj3 = ContainerRequiredName(obj2)
        self.assertIs(obj3.field1, obj2)

        obj4 = ContainerRequiredName()
        self.assertIsNone(obj4.field1)
Example #5
0
 def test_constructor_object_id_none(self):
     """Test that setting object_id to None in __new__ is OK and the object ID is set on get
     """
     parent_obj = Container('obj1')
     child_obj = Container.__new__(Container,
                                   parent=parent_obj,
                                   object_id=None)
     self.assertIsNotNone(child_obj.object_id)
 def test_getter_none_multiple(self):
     """Test that calling the getter with no args and multiple items in the attribute dict raises an error."""
     obj1 = Container('obj1')
     obj2 = Container('obj2')
     foo = Foo([obj1, obj2])
     msg = "More than one element in containers of Foo 'Foo' -- must specify a name."
     with self.assertRaisesWith(ValueError, msg):
         foo.get_container()
 def test_getitem_multiple(self):
     """Test that an error is raised if the attribute dict has multiple values and no name is given to getitem."""
     obj1 = Container('obj1')
     obj2 = Container('obj2')
     foo = FooSingle([obj1, obj2])
     msg = "More than one Container in FooSingle 'FooSingle' -- must specify a name."
     with self.assertRaisesWith(ValueError, msg):
         foo[None]
Example #8
0
 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)
Example #9
0
 def test_add_single_not_parent_modified(self):
     """Test that adding a container with a parent to the attribute dict correctly marks the MCI as modified."""
     obj1 = Container('obj1')
     obj2 = Container('obj2')
     obj1.parent = obj2
     foo = Foo()
     foo.set_modified(False)  # set to False so that we can test whether add_container makes it True
     foo.add_container(obj1)
     self.assertTrue(foo.modified)
Example #10
0
 def test_constructor(self):
     """Test that constructor properly sets parent and both child and parent have an object_id
     """
     parent_obj = Container('obj1')
     child_obj = Container.__new__(Container, parent=parent_obj)
     self.assertIs(child_obj.parent, parent_obj)
     self.assertIs(parent_obj.children[0], child_obj)
     self.assertIsNotNone(parent_obj.object_id)
     self.assertIsNotNone(child_obj.object_id)
Example #11
0
 def setUpContainer(self):
     containers = [
         Container('container1'),
         Container('container2'),
         Data('data1', [0, 1, 2, 3, 4]),
         Data('data2', [0.0, 1.0, 2.0, 3.0, 4.0]),
     ]
     multi_container = SimpleMultiContainer('multi', containers)
     return multi_container
    def test_override_init(self):
        """Test that overriding __init__ works."""
        obj1 = Container('obj1')
        obj2 = Container('obj2')
        containers = [obj1, obj2]

        baz = Baz(name='test_baz', other_arg=1, my_containers=containers)
        self.assertEqual(baz.name, 'test_baz')
        self.assertEqual(baz.other_arg, 1)
 def test_remove_non_child(self):
     """Test that removing a non-child container from the attribute dict resets the parent to None."""
     obj1 = Container('obj1')
     obj2 = Container('obj2')
     obj1.parent = obj2
     foo = Foo(obj1)
     del foo.containers['obj1']
     self.assertDictEqual(foo.containers, {})
     self.assertIs(obj1.parent, obj2)
 def test_add_single_not_parent(self):
     """Test that adding a container with a parent to the attribute dict correctly adds the container."""
     obj1 = Container('obj1')
     obj2 = Container('obj2')
     obj1.parent = obj2
     foo = Foo()
     foo.add_container(obj1)
     self.assertDictEqual(foo.containers, {'obj1': obj1})
     self.assertIs(obj1.parent, obj2)
Example #15
0
 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)
Example #16
0
 def test_override_property(self):
     """Test that overriding the attribute property works."""
     obj1 = Container('obj1')
     obj2 = Container('obj2')
     containers = [obj1, obj2]
     baz = Baz(name='test_baz', other_arg=1, my_containers=containers)
     self.assertDictEqual(baz.containers, {'my obj1': obj1, 'my obj2': obj2})
     self.assertFalse(isinstance(baz.containers, LabelledDict))
     self.assertIs(baz.get_container('my obj1'), obj1)
     baz.containers = {}
     self.assertDictEqual(baz.containers, {})
Example #17
0
 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)
Example #18
0
 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)
Example #19
0
 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.assertTupleEqual(parent_obj.children, (child_obj3, ))
     self.assertTrue(parent_obj.modified)
     self.assertTrue(child_obj.modified)
Example #20
0
    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)
Example #21
0
    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)
Example #22
0
    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_getitem_not_found(self):
     """Test that a KeyError is raised if the key is not found using getitem."""
     obj1 = Container('obj1')
     foo = FooSingle(obj1)
     msg = "\"'obj2' not found in FooSingle 'FooSingle'.\""
     with self.assertRaisesWith(KeyError, msg):
         foo['obj2']
Example #24
0
 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_init_multi(self):
     """Test that initializing the MCI with no arguments initializes the attribute dict empty."""
     obj1 = Container('obj1')
     data1 = Data('data1', [1, 2, 3])
     foo = Foo(containers=obj1, data=data1)
     self.assertDictEqual(foo.containers, {'obj1': obj1})
     self.assertDictEqual(foo.data, {'data1': data1})
 def test_add_single_dup(self):
     """Test that adding a container to the attribute dict correctly adds the container."""
     obj1 = Container('obj1')
     foo = Foo(obj1)
     msg = "'obj1' already exists in Foo 'Foo'"
     with self.assertRaisesWith(ValueError, msg):
         foo.add_container(obj1)
Example #27
0
    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_init_superclass_no_cls_conf(self):
        """Test that a subclass of an MCI class without a __clsconf__ can be initialized."""
        class Bar(MultiContainerInterface):

            pass

        class Qux(Bar):

            __clsconf__ = {
                'attr': 'containers',
                'add': 'add_container',
                'type': Container,
            }

        obj1 = Container('obj1')
        qux = Qux(obj1)
        self.assertDictEqual(qux.containers, {'obj1': obj1})
Example #29
0
    def test_setter_set_modified(self):
        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']

        child_obj1 = Container('test child 1')
        obj1 = ContainerWithChild()
        obj1.set_modified(False)  # set to False so that we can test that it is set to True next
        obj1.field1 = child_obj1
        self.assertTrue(obj1.modified)
        self.assertIs(obj1.field1, child_obj1)

        obj3 = ContainerWithChild()
        obj3.set_modified(False)  # set to False so that we can test that it is set to True next
        obj3.field1 = child_obj1  # child_obj1 already has a parent
        self.assertTrue(obj3.modified)
        self.assertIs(obj3.field1, child_obj1)
Example #30
0
 def __init__(self, name, my_containers):
     super().__init__(name=name)
     self.containers = my_containers
     self.add_container(Container('extra_container'))