python-dependency-injector/examples/providers/overriding_simple.py

37 lines
783 B
Python
Raw Normal View History

"""Simple providers overriding example."""
2016-06-06 11:54:05 +03:00
import dependency_injector.providers as providers
class User:
"""Example class User."""
2016-11-15 15:26:40 +03:00
# Users factory:
2015-11-23 18:02:48 +03:00
users_factory = providers.Factory(User)
# Creating several User objects:
user1 = users_factory()
user2 = users_factory()
# Making some asserts:
assert user1 is not user2
assert isinstance(user1, User) and isinstance(user2, User)
# Extending User:
class SuperUser(User):
"""Example class SuperUser."""
2016-11-15 15:26:40 +03:00
# Overriding users factory:
2015-11-23 18:02:48 +03:00
users_factory.override(providers.Factory(SuperUser))
# Creating some more User objects using overridden users factory:
user3 = users_factory()
user4 = users_factory()
# Making some asserts:
assert user4 is not user3
assert isinstance(user3, SuperUser) and isinstance(user4, SuperUser)