mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-11 17:10:57 +03:00
Add tests + refactoring
This commit is contained in:
parent
2d3aa72cbd
commit
1cd11e4af2
File diff suppressed because it is too large
Load Diff
|
@ -2504,7 +2504,7 @@ cdef class Dict(Provider):
|
|||
|
||||
:rtype: str
|
||||
"""
|
||||
return represent_provider(provider=self, provides=dict(self.kwargs))
|
||||
return represent_provider(provider=self, provides=self.kwargs)
|
||||
|
||||
@property
|
||||
def kwargs(self):
|
||||
|
@ -2706,7 +2706,7 @@ cdef class Resource(Provider):
|
|||
if self.__initialized:
|
||||
return self.__resource
|
||||
|
||||
if _is_resource_subclass(self.__initializer):
|
||||
if self._is_resource_subclass(self.__initializer):
|
||||
initializer = self.__initializer()
|
||||
self.__resource = __call(
|
||||
initializer.init,
|
||||
|
@ -2746,6 +2746,15 @@ cdef class Resource(Provider):
|
|||
self.__initialized = True
|
||||
return self.__resource
|
||||
|
||||
@staticmethod
|
||||
def _is_resource_subclass(instance):
|
||||
if sys.version_info < (3, 5):
|
||||
return False
|
||||
if not isinstance(instance, CLASS_TYPES):
|
||||
return
|
||||
from . import resources
|
||||
return issubclass(instance, resources.Resource)
|
||||
|
||||
|
||||
cdef class Container(Provider):
|
||||
"""Container provider provides an instance of declarative container.
|
||||
|
@ -3357,12 +3366,3 @@ def merge_dicts(dict1, dict2):
|
|||
result = dict1.copy()
|
||||
result.update(dict2)
|
||||
return result
|
||||
|
||||
|
||||
def _is_resource_subclass(instance):
|
||||
if sys.version_info < (3, 5):
|
||||
return False
|
||||
if not isinstance(instance, CLASS_TYPES):
|
||||
return
|
||||
from . import resources
|
||||
return issubclass(instance, resources.Resource)
|
|
@ -25,7 +25,7 @@ class ResourceTests(unittest.TestCase):
|
|||
# + Initializer: function
|
||||
# + Initializer: generator
|
||||
# + Initializer: base class
|
||||
# - Initializer: unknown type
|
||||
# + Initializer: unknown type
|
||||
# + Init() and shutdown() methods
|
||||
# + Initialized
|
||||
# + Args
|
||||
|
@ -112,6 +112,11 @@ class ResourceTests(unittest.TestCase):
|
|||
self.assertEqual(TestResource.init_counter, 2)
|
||||
self.assertEqual(TestResource.shutdown_counter, 2)
|
||||
|
||||
def test_init_not_callable(self):
|
||||
provider = providers.Resource(1)
|
||||
with self.assertRaises(errors.Error):
|
||||
provider.init()
|
||||
|
||||
def test_init_and_shutdown(self):
|
||||
def _init():
|
||||
_init.init_counter += 1
|
||||
|
|
Loading…
Reference in New Issue
Block a user