mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 01:26:51 +03:00
Remove ExternalDependency.satisfy() in favor of inherited Provider.override()
This commit is contained in:
parent
3b880d12a5
commit
27bf9e0a8e
|
@ -49,9 +49,9 @@ class Catalog(AbstractCatalog):
|
|||
|
||||
|
||||
# Satisfaction of external dependency.
|
||||
Catalog.database.satisfy(Singleton(sqlite3.Connection,
|
||||
InitArg('database', ':memory:'),
|
||||
Attribute('row_factory', sqlite3.Row)))
|
||||
Catalog.database.override(Singleton(sqlite3.Connection,
|
||||
InitArg('database', ':memory:'),
|
||||
Attribute('row_factory', sqlite3.Row)))
|
||||
|
||||
# Catalog static provides.
|
||||
a1, a2 = Catalog.object_a(), Catalog.object_a()
|
||||
|
|
|
@ -181,26 +181,25 @@ class Scoped(NewInstance):
|
|||
|
||||
class ExternalDependency(Provider):
|
||||
|
||||
"""External dependency provider."""
|
||||
"""External dependency provider.
|
||||
|
||||
__slots__ = ('instance_of', 'dependency')
|
||||
Those provider is used when dependency obviously have to be overridden by
|
||||
the client's code, but it's interface is known.
|
||||
"""
|
||||
|
||||
__slots__ = ('instance_of',)
|
||||
|
||||
def __init__(self, instance_of):
|
||||
"""Initializer."""
|
||||
self.instance_of = instance_of
|
||||
self.dependency = None
|
||||
super(ExternalDependency, self).__init__()
|
||||
|
||||
def satisfy(self, provider):
|
||||
"""Satisfy an external dependency."""
|
||||
self.dependency = ensure_is_provider(provider)
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
"""Return provided instance."""
|
||||
if not self.dependency:
|
||||
raise Error('Dependency is not satisfied')
|
||||
if not self.overridden:
|
||||
raise Error('Dependency is not defined')
|
||||
|
||||
instance = self.dependency.__call__(*args, **kwargs)
|
||||
instance = self.last_overriding(*args, **kwargs)
|
||||
|
||||
if not isinstance(instance, self.instance_of):
|
||||
raise Error('{0} is not an '.format(instance) +
|
||||
|
|
|
@ -407,17 +407,17 @@ class ExternalDependencyTests(unittest.TestCase):
|
|||
"""Test `is_provider` check."""
|
||||
self.assertTrue(is_provider(self.provider))
|
||||
|
||||
def test_call_satisfied(self):
|
||||
"""Test call of satisfied external dependency."""
|
||||
self.provider.satisfy(NewInstance(list))
|
||||
def test_call_overridden(self):
|
||||
"""Test call of overridden external dependency."""
|
||||
self.provider.override(NewInstance(list))
|
||||
self.assertIsInstance(self.provider(), list)
|
||||
|
||||
def test_call_satisfied_but_not_instance_of(self):
|
||||
"""Test call of satisfied external dependency, but not instance of."""
|
||||
self.provider.satisfy(NewInstance(dict))
|
||||
def test_call_overridden_but_not_instance_of(self):
|
||||
"""Test call of overridden external dependency, but not instance of."""
|
||||
self.provider.override(NewInstance(dict))
|
||||
self.assertRaises(Error, self.provider)
|
||||
|
||||
def test_call_not_satisfied(self):
|
||||
def test_call_not_overridden(self):
|
||||
"""Test call of not satisfied external dependency."""
|
||||
self.assertRaises(Error, self.provider)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user