2015-08-05 16:44:00 +03:00
|
|
|
"""Operating with catalog providers example."""
|
|
|
|
|
2015-09-02 00:24:19 +03:00
|
|
|
import dependency_injector as di
|
2015-08-05 16:44:00 +03:00
|
|
|
|
|
|
|
|
2015-11-10 11:42:29 +03:00
|
|
|
class CatalogA(di.DeclarativeCatalog):
|
2015-10-07 19:41:53 +03:00
|
|
|
"""Example catalog A."""
|
2015-08-05 16:44:00 +03:00
|
|
|
|
2015-09-02 00:24:19 +03:00
|
|
|
provider1 = di.Factory(object)
|
2015-10-26 13:52:52 +03:00
|
|
|
""":type: di.Provider -> object"""
|
2015-08-05 16:44:00 +03:00
|
|
|
|
|
|
|
|
2015-10-07 19:41:53 +03:00
|
|
|
class CatalogB(CatalogA):
|
|
|
|
"""Example catalog B."""
|
2015-08-05 16:44:00 +03:00
|
|
|
|
2015-10-07 19:41:53 +03:00
|
|
|
provider2 = di.Singleton(object)
|
2015-10-26 13:52:52 +03:00
|
|
|
""":type: di.Provider -> object"""
|
2015-08-05 16:44:00 +03:00
|
|
|
|
|
|
|
|
2015-10-07 19:41:53 +03:00
|
|
|
# Making some asserts for `providers` attribute:
|
|
|
|
assert CatalogA.providers == dict(provider1=CatalogA.provider1)
|
|
|
|
assert CatalogB.providers == dict(provider1=CatalogA.provider1,
|
|
|
|
provider2=CatalogB.provider2)
|
|
|
|
|
|
|
|
# Making some asserts for `cls_providers` attribute:
|
|
|
|
assert CatalogA.cls_providers == dict(provider1=CatalogA.provider1)
|
|
|
|
assert CatalogB.cls_providers == dict(provider2=CatalogB.provider2)
|
|
|
|
|
|
|
|
# Making some asserts for `inherited_providers` attribute:
|
|
|
|
assert CatalogA.inherited_providers == dict()
|
|
|
|
assert CatalogB.inherited_providers == dict(provider1=CatalogA.provider1)
|
|
|
|
|
|
|
|
# Making some asserts for `filter()` method:
|
|
|
|
assert CatalogB.filter(di.Factory) == dict(provider1=CatalogA.provider1)
|
|
|
|
assert CatalogB.filter(di.Singleton) == dict(provider2=CatalogB.provider2)
|