Add .provider attribute

This commit is contained in:
Roman Mogylatov 2017-10-13 10:15:21 -07:00
parent 9305b9db7d
commit be65996e57
5 changed files with 2120 additions and 1991 deletions

View File

@ -10,6 +10,8 @@ follows `Semantic versioning`_
Development version Development version
------------------- -------------------
- Add ``FactoryAggregate`` provider. - Add ``FactoryAggregate`` provider.
- Add ``Provider.provider`` dynamic attribute that return new provider's
delegate (alias of method ``Provider.delegate()``).
- Add support of six 1.11.0. - Add support of six 1.11.0.
- Regenerate C sources using Cython 0.27.1. - Regenerate C sources using Cython 0.27.1.

View File

@ -51,3 +51,18 @@ user2 = users_factory() # Same as: user2 = User(photos_factory=photos_factory)
# Making some asserts: # Making some asserts:
assert isinstance(user1.main_photo, Photo) assert isinstance(user1.main_photo, Photo)
assert isinstance(user2.main_photo, Photo) assert isinstance(user2.main_photo, Photo)
# or using Factory(...).provider
photos_factory = providers.Factory(Photo)
users_factory = providers.Factory(User,
photos_factory=photos_factory.provider)
# Creating several User objects:
user1 = users_factory() # Same as: user1 = User(photos_factory=photos_factory)
user2 = users_factory() # Same as: user2 = User(photos_factory=photos_factory)
# Making some asserts:
assert isinstance(user1.main_photo, Photo)
assert isinstance(user2.main_photo, Photo)

File diff suppressed because it is too large Load Diff

View File

@ -198,6 +198,14 @@ cdef class Provider(object):
""" """
return Delegate(self) return Delegate(self)
@property
def provider(self):
"""Return provider's delegate.
:rtype: :py:class:`Delegate`
"""
return self.delegate()
cpdef object _provide(self, tuple args, dict kwargs): cpdef object _provide(self, tuple args, dict kwargs):
"""Providing strategy implementation. """Providing strategy implementation.

View File

@ -32,6 +32,19 @@ class ProviderTests(unittest.TestCase):
self.assertIsNot(delegate1, delegate2) self.assertIsNot(delegate1, delegate2)
def test_provider(self):
delegate1 = self.provider.provider
self.assertIsInstance(delegate1, providers.Delegate)
self.assertIs(delegate1(), self.provider)
delegate2 = self.provider.provider
self.assertIsInstance(delegate2, providers.Delegate)
self.assertIs(delegate2(), self.provider)
self.assertIsNot(delegate1, delegate2)
def test_override(self): def test_override(self):
overriding_provider = providers.Provider() overriding_provider = providers.Provider()
self.provider.override(overriding_provider) self.provider.override(overriding_provider)