mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 11:04:01 +03:00
Remove providers.Scoped because it being has not yet justified
This commit is contained in:
parent
eaf5179c76
commit
6bbf7e3526
|
@ -8,6 +8,5 @@ Examples:
|
|||
- [Config provider.](examples/config_provider.py)
|
||||
- [Delegate.](examples/delegate.py)
|
||||
- [External dependency.](examples/external_dependency.py)
|
||||
- [Scoped provider.](examples/scoped_provider.py)
|
||||
- [Overrides.](examples/overrides.py)
|
||||
- [Usage.](examples/usage.py)
|
||||
|
|
|
@ -1,46 +0,0 @@
|
|||
"""Scoped provider examples."""
|
||||
|
||||
from objects import AbstractCatalog
|
||||
|
||||
from objects.providers import Singleton
|
||||
from objects.providers import Scoped
|
||||
|
||||
from objects.injections import InitArg
|
||||
from objects.injections import Attribute
|
||||
|
||||
import sqlite3
|
||||
|
||||
|
||||
class ObjectA(object):
|
||||
|
||||
"""Example class ObjectA, that has dependency on database."""
|
||||
|
||||
def __init__(self, db):
|
||||
"""Initializer."""
|
||||
self.db = db
|
||||
|
||||
|
||||
class Catalog(AbstractCatalog):
|
||||
|
||||
"""Catalog of objects providers."""
|
||||
|
||||
database = Singleton(sqlite3.Connection,
|
||||
InitArg('database', ':memory:'),
|
||||
Attribute('row_factory', sqlite3.Row))
|
||||
""":type: (objects.Provider) -> sqlite3.Connection"""
|
||||
|
||||
object_a = Scoped(ObjectA,
|
||||
InitArg('db', database))
|
||||
""":type: (objects.Provider) -> ObjectA"""
|
||||
|
||||
|
||||
# Making one more scope using provider methods.
|
||||
Catalog.object_a.in_scope('request')
|
||||
|
||||
object_a1 = Catalog.object_a()
|
||||
object_a2 = Catalog.object_a()
|
||||
|
||||
Catalog.object_a.out_of_scope('request')
|
||||
|
||||
assert object_a1 is object_a2
|
||||
assert object_a1.db is object_a2.db
|
|
@ -6,7 +6,6 @@ from .providers import Provider
|
|||
from .providers import Delegate
|
||||
from .providers import NewInstance
|
||||
from .providers import Singleton
|
||||
from .providers import Scoped
|
||||
from .providers import ExternalDependency
|
||||
from .providers import Class
|
||||
from .providers import Object
|
||||
|
@ -30,7 +29,6 @@ __all__ = ('AbstractCatalog',
|
|||
'Delegate',
|
||||
'NewInstance',
|
||||
'Singleton',
|
||||
'Scoped',
|
||||
'ExternalDependency',
|
||||
'Class',
|
||||
'Object',
|
||||
|
|
|
@ -140,48 +140,6 @@ class Singleton(NewInstance):
|
|||
self.instance = None
|
||||
|
||||
|
||||
class Scoped(NewInstance):
|
||||
|
||||
"""Scoped provider.
|
||||
|
||||
Scoped provider will create instance once for every scope and return it
|
||||
on every call.
|
||||
"""
|
||||
|
||||
__slots__ = ('current_scope', 'scopes_to_instances')
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Initializer."""
|
||||
self.current_scope = None
|
||||
self.scopes_to_instances = dict()
|
||||
super(Scoped, self).__init__(*args, **kwargs)
|
||||
|
||||
def in_scope(self, scope):
|
||||
"""Set provider in "in scope" state."""
|
||||
self.current_scope = scope
|
||||
|
||||
def out_of_scope(self, scope):
|
||||
"""Set provider in "out of scope" state."""
|
||||
self.current_scope = None
|
||||
try:
|
||||
del self.scopes_to_instances[scope]
|
||||
except KeyError:
|
||||
raise Error('Trying to move out of undefined scope '
|
||||
'"{0}"'.format(scope))
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
"""Return provided instance."""
|
||||
if not self.current_scope:
|
||||
raise Error('Trying to provide {0} '.format(self.provides) +
|
||||
'while provider has no active scope')
|
||||
try:
|
||||
instance = self.scopes_to_instances[self.current_scope]
|
||||
except KeyError:
|
||||
instance = super(Scoped, self).__call__(*args, **kwargs)
|
||||
self.scopes_to_instances[self.current_scope] = instance
|
||||
return instance
|
||||
|
||||
|
||||
class ExternalDependency(Provider):
|
||||
|
||||
"""External dependency provider.
|
||||
|
|
|
@ -6,7 +6,6 @@ from objects.providers import Provider
|
|||
from objects.providers import Delegate
|
||||
from objects.providers import NewInstance
|
||||
from objects.providers import Singleton
|
||||
from objects.providers import Scoped
|
||||
from objects.providers import ExternalDependency
|
||||
from objects.providers import Class
|
||||
from objects.providers import Object
|
||||
|
@ -307,94 +306,6 @@ class SingletonTests(unittest.TestCase):
|
|||
self.assertIsNot(instance1, instance2)
|
||||
|
||||
|
||||
class ScopedTests(unittest.TestCase):
|
||||
|
||||
"""Scoped test cases."""
|
||||
|
||||
APPLICATION_SCOPE = 'application'
|
||||
REQUEST_SCOPE = 'request'
|
||||
|
||||
def setUp(self):
|
||||
"""Set test cases environment up."""
|
||||
self.provider = Scoped(object)
|
||||
|
||||
def test_is_provider(self):
|
||||
"""Test `is_provider` check."""
|
||||
self.assertTrue(is_provider(self.provider))
|
||||
|
||||
def test_call(self):
|
||||
"""Test creation and returning of scope single object."""
|
||||
self.provider.in_scope(self.APPLICATION_SCOPE)
|
||||
|
||||
instance1 = self.provider()
|
||||
instance2 = self.provider()
|
||||
|
||||
self.assertIsInstance(instance1, object)
|
||||
self.assertIsInstance(instance2, object)
|
||||
self.assertIs(instance1, instance2)
|
||||
|
||||
def test_call_several_scopes(self):
|
||||
"""Test creation of several scopes instances."""
|
||||
self.provider.in_scope(self.APPLICATION_SCOPE)
|
||||
app_instance1 = self.provider()
|
||||
app_instance2 = self.provider()
|
||||
|
||||
self.provider.in_scope(self.REQUEST_SCOPE)
|
||||
request_instance1 = self.provider()
|
||||
request_instance2 = self.provider()
|
||||
|
||||
self.assertIsInstance(app_instance1, object)
|
||||
self.assertIsInstance(app_instance2, object)
|
||||
self.assertIs(app_instance1, app_instance2)
|
||||
|
||||
self.assertIsInstance(request_instance1, object)
|
||||
self.assertIsInstance(request_instance2, object)
|
||||
self.assertIs(request_instance1, request_instance2)
|
||||
|
||||
self.provider.in_scope(self.APPLICATION_SCOPE)
|
||||
app_instance3 = self.provider()
|
||||
self.assertIsInstance(app_instance3, object)
|
||||
self.assertIs(app_instance3, app_instance1)
|
||||
self.assertIs(app_instance3, app_instance2)
|
||||
|
||||
self.provider.in_scope(self.REQUEST_SCOPE)
|
||||
request_instance3 = self.provider()
|
||||
self.assertIsInstance(request_instance3, object)
|
||||
self.assertIs(request_instance3, request_instance1)
|
||||
self.assertIs(request_instance3, request_instance2)
|
||||
|
||||
def test_call_not_in_scope(self):
|
||||
"""Test creation of instance with no active scope."""
|
||||
self.assertRaises(Error, self.provider)
|
||||
|
||||
def test_call_in_out_scope(self):
|
||||
"""Test creation of instances within in and out of scope."""
|
||||
self.provider.in_scope(self.REQUEST_SCOPE)
|
||||
instance1 = self.provider()
|
||||
instance2 = self.provider()
|
||||
self.provider.out_of_scope(self.REQUEST_SCOPE)
|
||||
|
||||
self.provider.in_scope(self.REQUEST_SCOPE)
|
||||
instance3 = self.provider()
|
||||
instance4 = self.provider()
|
||||
self.provider.out_of_scope(self.REQUEST_SCOPE)
|
||||
|
||||
self.assertIs(instance1, instance2)
|
||||
self.assertIs(instance3, instance4)
|
||||
|
||||
self.assertIsNot(instance1, instance3)
|
||||
self.assertIsNot(instance2, instance3)
|
||||
|
||||
self.assertIsNot(instance1, instance4)
|
||||
self.assertIsNot(instance2, instance4)
|
||||
|
||||
def test_out_of_scope(self):
|
||||
"""Test call `out_of_scope()` on provider that has no such scope."""
|
||||
self.assertRaises(Error,
|
||||
self.provider.out_of_scope,
|
||||
self.REQUEST_SCOPE)
|
||||
|
||||
|
||||
class ExternalDependencyTests(unittest.TestCase):
|
||||
|
||||
"""ExternalDependency test cases."""
|
||||
|
|
Loading…
Reference in New Issue
Block a user