diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2012-01-12 23:21:38 +0100 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2012-01-12 23:21:38 +0100 |
commit | c60c3106c700f5f42ad4fcf82f327da98ec5eb9e (patch) | |
tree | 570302046f68591b05a7178152c610917c71fe45 | |
parent | 0c4b9334c93170ce6df17506f48e3937090aec05 (diff) | |
download | factory-boy-c60c3106c700f5f42ad4fcf82f327da98ec5eb9e.tar factory-boy-c60c3106c700f5f42ad4fcf82f327da98ec5eb9e.tar.gz |
Add __repr__ / __str__ to a couple of objects.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
-rw-r--r-- | factory/base.py | 4 | ||||
-rw-r--r-- | factory/containers.py | 14 | ||||
-rw-r--r-- | tests/test_base.py | 7 | ||||
-rw-r--r-- | tests/test_containers.py | 9 |
4 files changed, 32 insertions, 2 deletions
diff --git a/factory/base.py b/factory/base.py index 19d441b..b2f437d 100644 --- a/factory/base.py +++ b/factory/base.py @@ -341,6 +341,10 @@ class Factory(BaseFactory): # from turning it into an instance method. _creation_function = (DJANGO_CREATION,) + def __str__(self): + return '<%s for %s>' % (self.__class__.__name__, + getattr(self, CLASS_ATTRIBUTE_ASSOCIATED_CLASS).__name__) + @classmethod def set_creation_function(cls, creation_function): """Set the creation function for this class. diff --git a/factory/containers.py b/factory/containers.py index 497e98c..dd11f5f 100644 --- a/factory/containers.py +++ b/factory/containers.py @@ -49,13 +49,21 @@ class LazyStub(object): __initialized = False - def __init__(self, attrs, containers=()): + def __init__(self, attrs, containers=(), target_class=object): self.__attrs = attrs self.__values = {} self.__pending = [] self.__containers = containers + self.__target_class = target_class self.__initialized = True + def __repr__(self): + return '<LazyStub for %s>' % self.__target_class.__name__ + + def __str__(self): + return '<LazyStub for %s with %s>' % ( + self.__target_class.__name__, self.__attrs.keys()) + def __fill__(self): """Fill this LazyStub, computing values of all defined attributes. @@ -258,7 +266,9 @@ class AttributeBuilder(object): v = OrderedDeclarationWrapper(v, self.factory.sequence) wrapped_attrs[k] = v - return LazyStub(wrapped_attrs, containers=self._containers).__fill__() + stub = LazyStub(wrapped_attrs, containers=self._containers, + target_class=self.factory) + return stub.__fill__() class StubObject(object): diff --git a/tests/test_base.py b/tests/test_base.py index 97749f6..4f77421 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -56,6 +56,13 @@ class SafetyTestCase(unittest.TestCase): class FactoryTestCase(unittest.TestCase): + def testDisplay(self): + class TestObjectFactory(base.Factory): + FACTORY_FOR = FakeDjangoModel + + self.assertIn('TestObjectFactory', str(TestObjectFactory)) + self.assertIn('FakeDjangoModel', str(TestObjectFactory)) + def testLazyAttributeNonExistentParam(self): class TestObjectFactory(base.Factory): one = declarations.LazyAttribute(lambda a: a.does_not_exist ) diff --git a/tests/test_containers.py b/tests/test_containers.py index effb060..6e58573 100644 --- a/tests/test_containers.py +++ b/tests/test_containers.py @@ -80,6 +80,15 @@ class LazyStubTestCase(unittest.TestCase): self.assertRaises(containers.CyclicDefinitionError, getattr, stub, 'one') + def test_representation(self): + class RandomObj(object): + pass + + stub = containers.LazyStub({'one': 1, 'two': 2}, target_class=RandomObj) + self.assertIn('RandomObj', repr(stub)) + self.assertIn('RandomObj', str(stub)) + self.assertIn('one', str(stub)) + class OrderedDeclarationMock(declarations.OrderedDeclaration): pass |