Merge pull request #175 from ets-labs/feature/add-provider-attribute

Add ".provider" attribute for all providers
This commit is contained in:
Roman Mogylatov 2017-10-13 10:20:08 -07:00 committed by GitHub
commit 94a5bc21d2
6 changed files with 2123 additions and 1991 deletions

View File

@ -10,6 +10,8 @@ follows `Semantic versioning`_
Development version
-------------------
- 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.
- Regenerate C sources using Cython 0.27.1.

View File

@ -80,6 +80,9 @@ Actually, there are three ways for creating factory delegates:
:py:class:`Delegate` provider.
+ ``Factory(...).delegate()`` - calling factory :py:meth:`Factory.delegate`
method, that returns delegate wrapper for current factory.
+ ``Factory(...).provider`` - getting factory :py:attr:`Factory.provider`
attribute, that returns delegate wrapper for current factory (alias of
``Factory(...).delegate()`` method).
Example:

View File

@ -51,3 +51,18 @@ 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)
# 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)
@property
def provider(self):
"""Return provider's delegate.
:rtype: :py:class:`Delegate`
"""
return self.delegate()
cpdef object _provide(self, tuple args, dict kwargs):
"""Providing strategy implementation.

View File

@ -32,6 +32,19 @@ class ProviderTests(unittest.TestCase):
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):
overriding_provider = providers.Provider()
self.provider.override(overriding_provider)