mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 01:26:51 +03:00
Add .provider attribute
This commit is contained in:
parent
9305b9db7d
commit
be65996e57
|
@ -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.
|
||||
|
||||
|
|
|
@ -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
|
@ -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.
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user