예제 #1
0
    def test_accessing_container(self):
        class LazyAttr(containers.LazyValue):
            def __init__(self, obj_attr, container_attr):
                self.obj_attr = obj_attr
                self.container_attr = container_attr

            def evaluate(self, obj, containers=()):
                if containers:
                    add = getattr(containers[0], self.container_attr)
                else:
                    add = 0
                return getattr(obj, self.obj_attr) + add

        class DummyContainer(object):
            three = 3

        stub = containers.LazyStub(
            {
                'one': LazyAttr('two', 'three'),
                'two': 2,
                'three': 42
            },
            containers=(DummyContainer(), ))

        self.assertEqual(5, stub.one)

        stub = containers.LazyStub(
            {
                'one': LazyAttr('two', 'three'),
                'two': 2,
                'three': 42
            },
            containers=())
        self.assertEqual(2, stub.one)
예제 #2
0
    def test_access_parent(self):
        """Test simple access to a stub' parent."""
        o1 = containers.LazyStub({'rank': 1})
        o2 = containers.LazyStub({'rank': 2}, (o1, ))
        stub = containers.LazyStub({'rank': 3}, (o2, o1))

        self.assertEqual(3, stub.rank)
        self.assertEqual(2, stub.factory_parent.rank)
        self.assertEqual(1, stub.factory_parent.factory_parent.rank)
예제 #3
0
    def test_cyclic_definition(self):
        stub = containers.LazyStub({
            'one': self.LazyAttr('two'),
            'two': self.LazyAttr('one'),
        })

        self.assertRaises(errors.CyclicDefinitionError, getattr, stub, 'one')
예제 #4
0
    def test_representation(self):
        class RandomObj(object):
            pass

        stub = containers.LazyStub({'one': 1, 'two': 2}, model_class=RandomObj)
        self.assertIn('RandomObj', repr(stub))
        self.assertIn('RandomObj', str(stub))
        self.assertIn('one', str(stub))
예제 #5
0
    def test_cyclic_definition(self):
        class LazyAttr(containers.LazyValue):
            def __init__(self, attrname):
                self.attrname = attrname

            def evaluate(self, obj, container=None):
                return 1 + getattr(obj, self.attrname)

        stub = containers.LazyStub({'one': LazyAttr('two'), 'two': LazyAttr('one')})

        self.assertRaises(containers.CyclicDefinitionError, getattr, stub, 'one')
예제 #6
0
    def test_cyclic_definition_rescue(self):
        class LazyAttrDefault(self.LazyAttr):
            def __init__(self, attname, defvalue):
                super(LazyAttrDefault, self).__init__(attname)
                self.defvalue = defvalue
            def evaluate(self, obj, container=None):
                try:
                    return super(LazyAttrDefault, self).evaluate(obj, container)
                except errors.CyclicDefinitionError:
                    return self.defvalue

        stub = containers.LazyStub({
            'one': LazyAttrDefault('two', 10),
            'two': self.LazyAttr('one'),
        })

        self.assertEqual(10, stub.one)
        self.assertEqual(11, stub.two)
예제 #7
0
    def test_reading_value(self):
        stub = containers.LazyStub({'one': 1, 'two': 2})
        self.assertEqual(1, stub.one)

        self.assertRaises(AttributeError, getattr, stub, 'three')
예제 #8
0
    def test_setting_values(self):
        stub = containers.LazyStub({'one': 1, 'two': 2})

        self.assertRaises(AttributeError, setattr, stub, 'one', 1)
예제 #9
0
    def test_basic(self):
        stub = containers.LazyStub({'one': 1, 'two': 2})

        self.assertEqual({'one': 1, 'two': 2}, stub.__fill__())