diff options
author | Alexey Kotlyarov <a@koterpillar.com> | 2015-12-08 12:56:00 +1100 |
---|---|---|
committer | Alexey Kotlyarov <a@koterpillar.com> | 2015-12-08 12:56:00 +1100 |
commit | 819ffaf9efe0d5a3eee85afc847ceb6969242833 (patch) | |
tree | 1bc096705ff68c0f9be84e23bd203ea5d838ecfa | |
parent | be85908f5205810083c524a25c7da565788f2c03 (diff) | |
download | factory-boy-819ffaf9efe0d5a3eee85afc847ceb6969242833.tar factory-boy-819ffaf9efe0d5a3eee85afc847ceb6969242833.tar.gz |
Test LazyValues handling CyclicDefinitionError
-rw-r--r-- | tests/test_containers.py | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/tests/test_containers.py b/tests/test_containers.py index 083b306..9107b0d 100644 --- a/tests/test_containers.py +++ b/tests/test_containers.py @@ -78,18 +78,40 @@ class LazyStubTestCase(unittest.TestCase): self.assertEqual(2, stub.factory_parent.rank) self.assertEqual(1, stub.factory_parent.factory_parent.rank) - def test_cyclic_definition(self): - class LazyAttr(containers.LazyValue): - def __init__(self, attrname): - self.attrname = attrname + class LazyAttr(containers.LazyValue): + def __init__(self, attrname): + self.attrname = attrname - def evaluate(self, obj, container=None): - return 1 + getattr(obj, self.attrname) + def evaluate(self, obj, container=None): + return 1 + getattr(obj, self.attrname) - stub = containers.LazyStub({'one': LazyAttr('two'), 'two': LazyAttr('one')}) + def test_cyclic_definition(self): + stub = containers.LazyStub({ + 'one': self.LazyAttr('two'), + 'two': self.LazyAttr('one'), + }) self.assertRaises(containers.CyclicDefinitionError, getattr, stub, 'one') + 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 containers.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) + def test_representation(self): class RandomObj(object): pass |