diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-05-03 01:15:21 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-05-03 01:15:21 +0200 |
commit | 2c8d2cd5c9bf3074bd70559a850d77bd3a28ae8d (patch) | |
tree | 4ffd3f7b25661e0fb36fc0fc41fb58def584f8c3 | |
parent | 56a5db608f457259f7e69a5e01225adaf7016a31 (diff) | |
download | factory-boy-2c8d2cd5c9bf3074bd70559a850d77bd3a28ae8d.tar factory-boy-2c8d2cd5c9bf3074bd70559a850d77bd3a28ae8d.tar.gz |
Add LazyStub.factory_parent (See #55).
-rw-r--r-- | docs/changelog.rst | 3 | ||||
-rw-r--r-- | docs/reference.rst | 18 | ||||
-rw-r--r-- | factory/containers.py | 1 | ||||
-rw-r--r-- | tests/test_containers.py | 10 |
4 files changed, 32 insertions, 0 deletions
diff --git a/docs/changelog.rst b/docs/changelog.rst index da22b13..0baa3f7 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -8,6 +8,9 @@ ChangeLog - Add :class:`~factory.fuzzy.FuzzyDate` thanks to `saulshanabrook <https://github.com/saulshanabrook>`_ - Add :class:`~factory.fuzzy.FuzzyDateTime` and :class:`~factory.fuzzy.FuzzyNaiveDateTime`. + - Add a :attr:`~factory.containers.LazyStub.factory_parent` attribute to the + :class:`~factory.containers.LazyStub` passed to :class:`~factory.LazyAttribute`, in order to access + fields defined in wrapping factories. *Deprecation:* diff --git a/docs/reference.rst b/docs/reference.rst index 09d9ceb..ad921fa 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -353,6 +353,11 @@ accept the object being built as sole argument, and return a value. 'leo@example.com' +The object passed to :class:`LazyAttribute` is not an instance of the target class, +but instead a :class:`~containers.LazyStub`: a temporary container that computes +the value of all declared fields. + + Decorator ~~~~~~~~~ @@ -798,6 +803,19 @@ Obviously, this "follow parents" hability also handles overriding some attribute 'cn' +This feature is also available to :class:`LazyAttribute` and :class:`LazyAttributeSequence`, +through the :attr:`~containers.LazyStub.factory_parent` attribute of the passed-in object: + +.. code-block:: python + + class CompanyFactory(factory.Factory): + FACTORY_FOR = Company + country = factory.SubFactory(CountryFactory) + owner = factory.SubFactory(UserFactory, + language=factory.LazyAttribute(lambda user: user.factory_parent.country.language), + ) + + Iterator """""""" diff --git a/factory/containers.py b/factory/containers.py index ee2ad82..b3b003e 100644 --- a/factory/containers.py +++ b/factory/containers.py @@ -55,6 +55,7 @@ class LazyStub(object): self.__pending = [] self.__containers = containers self.__target_class = target_class + self.factory_parent = containers[0] if containers else None self.__initialized = True def __repr__(self): diff --git a/tests/test_containers.py b/tests/test_containers.py index 70ed885..75e3237 100644 --- a/tests/test_containers.py +++ b/tests/test_containers.py @@ -68,6 +68,16 @@ class LazyStubTestCase(unittest.TestCase): containers=()) self.assertEqual(2, stub.one) + 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) + def test_cyclic_definition(self): class LazyAttr(containers.LazyValue): def __init__(self, attrname): |