2015-11-22 00:59:36 +03:00
|
|
|
"""`Factory` providers delegation example."""
|
2015-07-20 18:46:45 +03:00
|
|
|
|
2016-06-08 16:39:53 +03:00
|
|
|
import collections
|
2016-06-08 16:54:47 +03:00
|
|
|
|
2016-06-08 16:39:53 +03:00
|
|
|
import dependency_injector.providers as providers
|
|
|
|
|
|
|
|
|
|
|
|
Photo = collections.namedtuple('Photo', [])
|
2015-07-20 18:46:45 +03:00
|
|
|
|
|
|
|
|
2020-01-27 02:41:36 +03:00
|
|
|
class User:
|
2016-06-08 16:39:53 +03:00
|
|
|
"""Example user model."""
|
2015-07-20 18:46:45 +03:00
|
|
|
|
|
|
|
def __init__(self, photos_factory):
|
2020-01-26 22:22:43 +03:00
|
|
|
"""Initialize instance."""
|
2015-07-20 18:46:45 +03:00
|
|
|
self.photos_factory = photos_factory
|
|
|
|
self._main_photo = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def main_photo(self):
|
|
|
|
"""Return user's main photo."""
|
|
|
|
if not self._main_photo:
|
|
|
|
self._main_photo = self.photos_factory()
|
|
|
|
return self._main_photo
|
|
|
|
|
|
|
|
|
2016-06-08 16:39:53 +03:00
|
|
|
# Defining User and Photo factories using DelegatedFactory provider:
|
|
|
|
photos_factory = providers.DelegatedFactory(Photo)
|
2020-08-26 23:22:54 +03:00
|
|
|
users_factory = providers.Factory(
|
2020-07-18 07:40:14 +03:00
|
|
|
User,
|
|
|
|
photos_factory=photos_factory,
|
|
|
|
)
|
2016-06-08 16:39:53 +03:00
|
|
|
|
|
|
|
# or using Delegate(Factory(...))
|
|
|
|
|
|
|
|
photos_factory = providers.Factory(Photo)
|
2020-07-18 07:40:14 +03:00
|
|
|
users_factory = providers.Factory(
|
|
|
|
User,
|
|
|
|
photos_factory=providers.Delegate(photos_factory),
|
|
|
|
)
|
2016-06-08 16:39:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
# or using Factory(...).delegate()
|
2015-07-20 18:46:45 +03:00
|
|
|
|
2015-11-22 00:59:36 +03:00
|
|
|
photos_factory = providers.Factory(Photo)
|
2020-07-18 07:40:14 +03:00
|
|
|
users_factory = providers.Factory(
|
|
|
|
User,
|
|
|
|
photos_factory=photos_factory.delegate(),
|
|
|
|
)
|
2015-07-20 18:46:45 +03:00
|
|
|
|
2016-06-08 16:39:53 +03:00
|
|
|
|
2015-07-20 18:46:45 +03:00
|
|
|
# Creating several User objects:
|
2020-07-18 07:40:14 +03:00
|
|
|
user1 = users_factory()
|
|
|
|
user2 = users_factory()
|
|
|
|
|
|
|
|
# Same as:
|
|
|
|
# user1 = User(photos_factory=photos_factory)
|
|
|
|
# user2 = User(photos_factory=photos_factory)
|
2015-07-20 18:46:45 +03:00
|
|
|
|
|
|
|
# Making some asserts:
|
|
|
|
assert isinstance(user1.main_photo, Photo)
|
|
|
|
assert isinstance(user2.main_photo, Photo)
|
2017-10-13 20:15:21 +03:00
|
|
|
|
|
|
|
# or using Factory(...).provider
|
|
|
|
|
|
|
|
photos_factory = providers.Factory(Photo)
|
2020-07-18 07:40:14 +03:00
|
|
|
users_factory = providers.Factory(
|
|
|
|
User,
|
|
|
|
photos_factory=photos_factory.provider,
|
|
|
|
)
|
2017-10-13 20:15:21 +03:00
|
|
|
|
|
|
|
# Creating several User objects:
|
2020-07-18 07:40:14 +03:00
|
|
|
user1 = users_factory()
|
|
|
|
user2 = users_factory()
|
|
|
|
|
|
|
|
# Same as:
|
|
|
|
# user1 = User(photos_factory=photos_factory)
|
|
|
|
# user2 = User(photos_factory=photos_factory)
|
2017-10-13 20:15:21 +03:00
|
|
|
|
|
|
|
# Making some asserts:
|
|
|
|
assert isinstance(user1.main_photo, Photo)
|
|
|
|
assert isinstance(user2.main_photo, Photo)
|