mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-24 02:24:02 +03:00
Update quotes in tests
This commit is contained in:
parent
0b1e214135
commit
8ade2b7839
|
@ -3659,8 +3659,8 @@ static const char __pyx_k_to[] = "to";
|
||||||
static const char __pyx_k_0_1[] = "{0}={1}";
|
static const char __pyx_k_0_1[] = "{0}={1}";
|
||||||
static const char __pyx_k__10[] = "__";
|
static const char __pyx_k__10[] = "__";
|
||||||
static const char __pyx_k__30[] = ", ";
|
static const char __pyx_k__30[] = ", ";
|
||||||
static const char __pyx_k__31[] = "('";
|
static const char __pyx_k__31[] = "(\"";
|
||||||
static const char __pyx_k__32[] = "')";
|
static const char __pyx_k__32[] = "\")";
|
||||||
static const char __pyx_k__33[] = ")";
|
static const char __pyx_k__33[] = ")";
|
||||||
static const char __pyx_k_cls[] = "cls";
|
static const char __pyx_k_cls[] = "cls";
|
||||||
static const char __pyx_k_doc[] = "__doc__";
|
static const char __pyx_k_doc[] = "__doc__";
|
||||||
|
@ -78727,7 +78727,7 @@ static int __pyx_pf_19dependency_injector_9providers_16ProvidedInstance___init__
|
||||||
* super().__init__()
|
* super().__init__()
|
||||||
*
|
*
|
||||||
* def __repr__(self): # <<<<<<<<<<<<<<
|
* def __repr__(self): # <<<<<<<<<<<<<<
|
||||||
* return f'{self.__class__.__name__}(\'{self.__provides}\')'
|
* return f'{self.__class__.__name__}("{self.__provides}")'
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -78760,7 +78760,7 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_16ProvidedInstance_2_
|
||||||
/* "dependency_injector/providers.pyx":4054
|
/* "dependency_injector/providers.pyx":4054
|
||||||
*
|
*
|
||||||
* def __repr__(self):
|
* def __repr__(self):
|
||||||
* return f'{self.__class__.__name__}(\'{self.__provides}\')' # <<<<<<<<<<<<<<
|
* return f'{self.__class__.__name__}("{self.__provides}")' # <<<<<<<<<<<<<<
|
||||||
*
|
*
|
||||||
* def __deepcopy__(self, memo):
|
* def __deepcopy__(self, memo):
|
||||||
*/
|
*/
|
||||||
|
@ -78808,7 +78808,7 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_16ProvidedInstance_2_
|
||||||
* super().__init__()
|
* super().__init__()
|
||||||
*
|
*
|
||||||
* def __repr__(self): # <<<<<<<<<<<<<<
|
* def __repr__(self): # <<<<<<<<<<<<<<
|
||||||
* return f'{self.__class__.__name__}(\'{self.__provides}\')'
|
* return f'{self.__class__.__name__}("{self.__provides}")'
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -78826,7 +78826,7 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_16ProvidedInstance_2_
|
||||||
}
|
}
|
||||||
|
|
||||||
/* "dependency_injector/providers.pyx":4056
|
/* "dependency_injector/providers.pyx":4056
|
||||||
* return f'{self.__class__.__name__}(\'{self.__provides}\')'
|
* return f'{self.__class__.__name__}("{self.__provides}")'
|
||||||
*
|
*
|
||||||
* def __deepcopy__(self, memo): # <<<<<<<<<<<<<<
|
* def __deepcopy__(self, memo): # <<<<<<<<<<<<<<
|
||||||
* copied = memo.get(id(self))
|
* copied = memo.get(id(self))
|
||||||
|
@ -78982,7 +78982,7 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_16ProvidedInstance_4_
|
||||||
goto __pyx_L0;
|
goto __pyx_L0;
|
||||||
|
|
||||||
/* "dependency_injector/providers.pyx":4056
|
/* "dependency_injector/providers.pyx":4056
|
||||||
* return f'{self.__class__.__name__}(\'{self.__provides}\')'
|
* return f'{self.__class__.__name__}("{self.__provides}")'
|
||||||
*
|
*
|
||||||
* def __deepcopy__(self, memo): # <<<<<<<<<<<<<<
|
* def __deepcopy__(self, memo): # <<<<<<<<<<<<<<
|
||||||
* copied = memo.get(id(self))
|
* copied = memo.get(id(self))
|
||||||
|
@ -80355,7 +80355,7 @@ static int __pyx_pf_19dependency_injector_9providers_15AttributeGetter___init__(
|
||||||
* super().__init__()
|
* super().__init__()
|
||||||
*
|
*
|
||||||
* def __repr__(self): # <<<<<<<<<<<<<<
|
* def __repr__(self): # <<<<<<<<<<<<<<
|
||||||
* return f'{self.__class__.__name__}(\'{self.name}\')'
|
* return f'{self.__class__.__name__}("{self.name}")'
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -80388,7 +80388,7 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_15AttributeGetter_2__
|
||||||
/* "dependency_injector/providers.pyx":4110
|
/* "dependency_injector/providers.pyx":4110
|
||||||
*
|
*
|
||||||
* def __repr__(self):
|
* def __repr__(self):
|
||||||
* return f'{self.__class__.__name__}(\'{self.name}\')' # <<<<<<<<<<<<<<
|
* return f'{self.__class__.__name__}("{self.name}")' # <<<<<<<<<<<<<<
|
||||||
*
|
*
|
||||||
* def __deepcopy__(self, memo):
|
* def __deepcopy__(self, memo):
|
||||||
*/
|
*/
|
||||||
|
@ -80439,7 +80439,7 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_15AttributeGetter_2__
|
||||||
* super().__init__()
|
* super().__init__()
|
||||||
*
|
*
|
||||||
* def __repr__(self): # <<<<<<<<<<<<<<
|
* def __repr__(self): # <<<<<<<<<<<<<<
|
||||||
* return f'{self.__class__.__name__}(\'{self.name}\')'
|
* return f'{self.__class__.__name__}("{self.name}")'
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -80457,7 +80457,7 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_15AttributeGetter_2__
|
||||||
}
|
}
|
||||||
|
|
||||||
/* "dependency_injector/providers.pyx":4112
|
/* "dependency_injector/providers.pyx":4112
|
||||||
* return f'{self.__class__.__name__}(\'{self.name}\')'
|
* return f'{self.__class__.__name__}("{self.name}")'
|
||||||
*
|
*
|
||||||
* def __deepcopy__(self, memo): # <<<<<<<<<<<<<<
|
* def __deepcopy__(self, memo): # <<<<<<<<<<<<<<
|
||||||
* copied = memo.get(id(self))
|
* copied = memo.get(id(self))
|
||||||
|
@ -80642,7 +80642,7 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_15AttributeGetter_4__
|
||||||
goto __pyx_L0;
|
goto __pyx_L0;
|
||||||
|
|
||||||
/* "dependency_injector/providers.pyx":4112
|
/* "dependency_injector/providers.pyx":4112
|
||||||
* return f'{self.__class__.__name__}(\'{self.name}\')'
|
* return f'{self.__class__.__name__}("{self.name}")'
|
||||||
*
|
*
|
||||||
* def __deepcopy__(self, memo): # <<<<<<<<<<<<<<
|
* def __deepcopy__(self, memo): # <<<<<<<<<<<<<<
|
||||||
* copied = memo.get(id(self))
|
* copied = memo.get(id(self))
|
||||||
|
@ -82639,7 +82639,7 @@ static int __pyx_pf_19dependency_injector_9providers_10ItemGetter___init__(struc
|
||||||
* super().__init__()
|
* super().__init__()
|
||||||
*
|
*
|
||||||
* def __repr__(self): # <<<<<<<<<<<<<<
|
* def __repr__(self): # <<<<<<<<<<<<<<
|
||||||
* return f'{self.__class__.__name__}(\'{self.name}\')'
|
* return f'{self.__class__.__name__}("{self.name}")'
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -82672,7 +82672,7 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_10ItemGetter_2__repr_
|
||||||
/* "dependency_injector/providers.pyx":4192
|
/* "dependency_injector/providers.pyx":4192
|
||||||
*
|
*
|
||||||
* def __repr__(self):
|
* def __repr__(self):
|
||||||
* return f'{self.__class__.__name__}(\'{self.name}\')' # <<<<<<<<<<<<<<
|
* return f'{self.__class__.__name__}("{self.name}")' # <<<<<<<<<<<<<<
|
||||||
*
|
*
|
||||||
* def __deepcopy__(self, memo):
|
* def __deepcopy__(self, memo):
|
||||||
*/
|
*/
|
||||||
|
@ -82723,7 +82723,7 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_10ItemGetter_2__repr_
|
||||||
* super().__init__()
|
* super().__init__()
|
||||||
*
|
*
|
||||||
* def __repr__(self): # <<<<<<<<<<<<<<
|
* def __repr__(self): # <<<<<<<<<<<<<<
|
||||||
* return f'{self.__class__.__name__}(\'{self.name}\')'
|
* return f'{self.__class__.__name__}("{self.name}")'
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -82741,7 +82741,7 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_10ItemGetter_2__repr_
|
||||||
}
|
}
|
||||||
|
|
||||||
/* "dependency_injector/providers.pyx":4194
|
/* "dependency_injector/providers.pyx":4194
|
||||||
* return f'{self.__class__.__name__}(\'{self.name}\')'
|
* return f'{self.__class__.__name__}("{self.name}")'
|
||||||
*
|
*
|
||||||
* def __deepcopy__(self, memo): # <<<<<<<<<<<<<<
|
* def __deepcopy__(self, memo): # <<<<<<<<<<<<<<
|
||||||
* copied = memo.get(id(self))
|
* copied = memo.get(id(self))
|
||||||
|
@ -82926,7 +82926,7 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_10ItemGetter_4__deepc
|
||||||
goto __pyx_L0;
|
goto __pyx_L0;
|
||||||
|
|
||||||
/* "dependency_injector/providers.pyx":4194
|
/* "dependency_injector/providers.pyx":4194
|
||||||
* return f'{self.__class__.__name__}(\'{self.name}\')'
|
* return f'{self.__class__.__name__}("{self.name}")'
|
||||||
*
|
*
|
||||||
* def __deepcopy__(self, memo): # <<<<<<<<<<<<<<
|
* def __deepcopy__(self, memo): # <<<<<<<<<<<<<<
|
||||||
* copied = memo.get(id(self))
|
* copied = memo.get(id(self))
|
||||||
|
|
|
@ -4051,7 +4051,7 @@ cdef class ProvidedInstance(Provider):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'{self.__class__.__name__}(\'{self.__provides}\')'
|
return f'{self.__class__.__name__}("{self.__provides}")'
|
||||||
|
|
||||||
def __deepcopy__(self, memo):
|
def __deepcopy__(self, memo):
|
||||||
copied = memo.get(id(self))
|
copied = memo.get(id(self))
|
||||||
|
@ -4107,7 +4107,7 @@ cdef class AttributeGetter(Provider):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'{self.__class__.__name__}(\'{self.name}\')'
|
return f'{self.__class__.__name__}("{self.name}")'
|
||||||
|
|
||||||
def __deepcopy__(self, memo):
|
def __deepcopy__(self, memo):
|
||||||
copied = memo.get(id(self))
|
copied = memo.get(id(self))
|
||||||
|
@ -4189,7 +4189,7 @@ cdef class ItemGetter(Provider):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'{self.__class__.__name__}(\'{self.name}\')'
|
return f'{self.__class__.__name__}("{self.name}")'
|
||||||
|
|
||||||
def __deepcopy__(self, memo):
|
def __deepcopy__(self, memo):
|
||||||
copied = memo.get(id(self))
|
copied = memo.get(id(self))
|
||||||
|
|
|
@ -14,15 +14,15 @@ class Tester(object):
|
||||||
self.provider_modules = provider_modules
|
self.provider_modules = provider_modules
|
||||||
self.tests = [getattr(self, name)
|
self.tests = [getattr(self, name)
|
||||||
for name in dir(self)
|
for name in dir(self)
|
||||||
if name.startswith('test')]
|
if name.startswith("test")]
|
||||||
self.total_time = 0
|
self.total_time = 0
|
||||||
self.duration_factor = duration_factor
|
self.duration_factor = duration_factor
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
"""Run all tests for all provider modules."""
|
"""Run all tests for all provider modules."""
|
||||||
for module in self.provider_modules:
|
for module in self.provider_modules:
|
||||||
print('\n')
|
print("\n")
|
||||||
print('Running tests for module - "{module}":'
|
print("Running tests for module - \"{module}\":"
|
||||||
.format(module=module.__name__))
|
.format(module=module.__name__))
|
||||||
|
|
||||||
gc.disable()
|
gc.disable()
|
||||||
|
@ -30,13 +30,13 @@ class Tester(object):
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
test(module)
|
test(module)
|
||||||
self.total_time = time.time() - start_time
|
self.total_time = time.time() - start_time
|
||||||
print('Test "{test}" took - {time}'
|
print("Test \"{test}\" took - {time}"
|
||||||
.format(test=test.__name__,
|
.format(test=test.__name__,
|
||||||
time=self.total_time))
|
time=self.total_time))
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
|
||||||
gc.enable()
|
gc.enable()
|
||||||
print('\n')
|
print("\n")
|
||||||
|
|
||||||
def test_raw_3_kw_injections(self, providers):
|
def test_raw_3_kw_injections(self, providers):
|
||||||
"""Test 3 keyword argument injections."""
|
"""Test 3 keyword argument injections."""
|
||||||
|
@ -53,7 +53,7 @@ class Tester(object):
|
||||||
def __init__(self, a, b, c):
|
def __init__(self, a, b, c):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
for x in xrange(int(5000000 * self.duration_factor)):
|
for x in range(int(5000000 * self.duration_factor)):
|
||||||
Test(a=A(), b=B(), c=C())
|
Test(a=A(), b=B(), c=C())
|
||||||
|
|
||||||
def test_factory_3_factory_kw_injections(self, providers):
|
def test_factory_3_factory_kw_injections(self, providers):
|
||||||
|
@ -78,7 +78,7 @@ class Tester(object):
|
||||||
a=a_factory,
|
a=a_factory,
|
||||||
b=b_factory,
|
b=b_factory,
|
||||||
c=c_factory)
|
c=c_factory)
|
||||||
for x in xrange(int(5000000 * self.duration_factor)):
|
for x in range(int(5000000 * self.duration_factor)):
|
||||||
test_factory()
|
test_factory()
|
||||||
|
|
||||||
def test_abstract_factory_3_factory_kw_injections(self, providers):
|
def test_abstract_factory_3_factory_kw_injections(self, providers):
|
||||||
|
@ -104,7 +104,7 @@ class Tester(object):
|
||||||
a=a_factory,
|
a=a_factory,
|
||||||
b=b_factory,
|
b=b_factory,
|
||||||
c=c_factory))
|
c=c_factory))
|
||||||
for x in xrange(int(5000000 * self.duration_factor)):
|
for x in range(int(5000000 * self.duration_factor)):
|
||||||
test_factory()
|
test_factory()
|
||||||
|
|
||||||
def test_factory_6_factory_kw_injections_0_context(self, providers):
|
def test_factory_6_factory_kw_injections_0_context(self, providers):
|
||||||
|
@ -114,7 +114,7 @@ class Tester(object):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
test_factory = providers.Factory(Test, a=1, b=2, c=3, d=4, e=5, f=6)
|
test_factory = providers.Factory(Test, a=1, b=2, c=3, d=4, e=5, f=6)
|
||||||
for x in xrange(int(5000000 * self.duration_factor)):
|
for x in range(int(5000000 * self.duration_factor)):
|
||||||
test_factory()
|
test_factory()
|
||||||
|
|
||||||
def test_factory_6_factory_kw_injections_1_context(self, providers):
|
def test_factory_6_factory_kw_injections_1_context(self, providers):
|
||||||
|
@ -124,7 +124,7 @@ class Tester(object):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
test_factory = providers.Factory(Test, f=6)
|
test_factory = providers.Factory(Test, f=6)
|
||||||
for x in xrange(int(5000000 * self.duration_factor)):
|
for x in range(int(5000000 * self.duration_factor)):
|
||||||
test_factory(a=1, b=2, c=3, d=4, e=5)
|
test_factory(a=1, b=2, c=3, d=4, e=5)
|
||||||
|
|
||||||
def test_factory_6_factory_kw_injections_3_context(self, providers):
|
def test_factory_6_factory_kw_injections_3_context(self, providers):
|
||||||
|
@ -134,11 +134,11 @@ class Tester(object):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
test_factory = providers.Factory(Test, a=1, b=2, c=3)
|
test_factory = providers.Factory(Test, a=1, b=2, c=3)
|
||||||
for x in xrange(int(5000000 * self.duration_factor)):
|
for x in range(int(5000000 * self.duration_factor)):
|
||||||
test_factory(d=4, e=5, f=6)
|
test_factory(d=4, e=5, f=6)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
tester = Tester(
|
tester = Tester(
|
||||||
provider_modules=[
|
provider_modules=[
|
||||||
dependency_injector.providers,
|
dependency_injector.providers,
|
||||||
|
|
|
@ -16,7 +16,7 @@ class Cat(Animal):
|
||||||
|
|
||||||
# Test 1: to check the return type (class)
|
# Test 1: to check the return type (class)
|
||||||
provider1 = providers.Callable(Cat)
|
provider1 = providers.Callable(Cat)
|
||||||
animal1: Animal = provider1(1, 2, 3, b='1', c=2, e=0.0)
|
animal1: Animal = provider1(1, 2, 3, b="1", c=2, e=0.0)
|
||||||
|
|
||||||
# Test 2: to check the return type (class factory method)
|
# Test 2: to check the return type (class factory method)
|
||||||
provider2 = providers.Callable(Cat.create)
|
provider2 = providers.Callable(Cat.create)
|
||||||
|
@ -36,17 +36,17 @@ kwargs4: Dict[str, Any] = provider4.kwargs
|
||||||
provider5 = providers.Callable(Animal)
|
provider5 = providers.Callable(Animal)
|
||||||
provided5: providers.ProvidedInstance = provider5.provided
|
provided5: providers.ProvidedInstance = provider5.provided
|
||||||
attr_getter5: providers.AttributeGetter = provider5.provided.attr
|
attr_getter5: providers.AttributeGetter = provider5.provided.attr
|
||||||
item_getter5: providers.ItemGetter = provider5.provided['item']
|
item_getter5: providers.ItemGetter = provider5.provided["item"]
|
||||||
method_caller: providers.MethodCaller = provider5.provided.method.call(123, arg=324)
|
method_caller: providers.MethodCaller = provider5.provided.method.call(123, arg=324)
|
||||||
|
|
||||||
# Test 6: to check the DelegatedCallable
|
# Test 6: to check the DelegatedCallable
|
||||||
provider6 = providers.DelegatedCallable(Cat)
|
provider6 = providers.DelegatedCallable(Cat)
|
||||||
animal6: Animal = provider6(1, 2, 3, b='1', c=2, e=0.0)
|
animal6: Animal = provider6(1, 2, 3, b="1", c=2, e=0.0)
|
||||||
|
|
||||||
# Test 7: to check the AbstractCallable
|
# Test 7: to check the AbstractCallable
|
||||||
provider7 = providers.AbstractCallable(Animal)
|
provider7 = providers.AbstractCallable(Animal)
|
||||||
provider7.override(providers.Callable(Cat))
|
provider7.override(providers.Callable(Cat))
|
||||||
animal7: Animal = provider7(1, 2, 3, b='1', c=2, e=0.0)
|
animal7: Animal = provider7(1, 2, 3, b="1", c=2, e=0.0)
|
||||||
|
|
||||||
# Test 8: to check the CallableDelegate __init__
|
# Test 8: to check the CallableDelegate __init__
|
||||||
provider8 = providers.CallableDelegate(providers.Callable(lambda: None))
|
provider8 = providers.CallableDelegate(providers.Callable(lambda: None))
|
||||||
|
@ -54,8 +54,8 @@ provider8 = providers.CallableDelegate(providers.Callable(lambda: None))
|
||||||
# Test 9: to check the return type with await
|
# Test 9: to check the return type with await
|
||||||
provider9 = providers.Callable(Cat)
|
provider9 = providers.Callable(Cat)
|
||||||
async def _async9() -> None:
|
async def _async9() -> None:
|
||||||
animal1: Animal = await provider9(1, 2, 3, b='1', c=2, e=0.0) # type: ignore
|
animal1: Animal = await provider9(1, 2, 3, b="1", c=2, e=0.0) # type: ignore
|
||||||
animal2: Animal = await provider9.async_(1, 2, 3, b='1', c=2, e=0.0)
|
animal2: Animal = await provider9.async_(1, 2, 3, b="1", c=2, e=0.0)
|
||||||
|
|
||||||
# Test 10: to check the .provides
|
# Test 10: to check the .provides
|
||||||
provider10 = providers.Callable(Cat)
|
provider10 = providers.Callable(Cat)
|
||||||
|
|
|
@ -9,12 +9,12 @@ provider1 = providers.Factory(dict, a=config1.a)
|
||||||
# Test 2: to check the from_*() method
|
# Test 2: to check the from_*() method
|
||||||
config2 = providers.Configuration()
|
config2 = providers.Configuration()
|
||||||
config2.from_dict({})
|
config2.from_dict({})
|
||||||
config2.from_ini('config.ini')
|
config2.from_ini("config.ini")
|
||||||
config2.from_ini(Path('config.ini'))
|
config2.from_ini(Path("config.ini"))
|
||||||
|
|
||||||
config2.from_yaml('config.yml')
|
config2.from_yaml("config.yml")
|
||||||
config2.from_yaml(Path('config.yml'))
|
config2.from_yaml(Path("config.yml"))
|
||||||
config2.from_env('ENV', 'default')
|
config2.from_env("ENV", "default")
|
||||||
|
|
||||||
# Test 3: to check as_*() methods
|
# Test 3: to check as_*() methods
|
||||||
config3 = providers.Configuration()
|
config3 = providers.Configuration()
|
||||||
|
|
|
@ -63,4 +63,4 @@ class Container7(containers.DeclarativeContainer):
|
||||||
provider = providers.Factory(str)
|
provider = providers.Factory(str)
|
||||||
|
|
||||||
container7 = Container7()
|
container7 = Container7()
|
||||||
container7.override_providers(provider='new_value')
|
container7.override_providers(provider="new_value")
|
||||||
|
|
|
@ -18,7 +18,7 @@ class Cat(Animal):
|
||||||
|
|
||||||
# Test 1: to check the return type (class)
|
# Test 1: to check the return type (class)
|
||||||
provider1 = providers.Factory(Cat)
|
provider1 = providers.Factory(Cat)
|
||||||
animal1: Animal = provider1(1, 2, 3, b='1', c=2, e=0.0)
|
animal1: Animal = provider1(1, 2, 3, b="1", c=2, e=0.0)
|
||||||
|
|
||||||
# Test 2: to check the return type (class factory method)
|
# Test 2: to check the return type (class factory method)
|
||||||
provider2 = providers.Factory(Cat.create)
|
provider2 = providers.Factory(Cat.create)
|
||||||
|
@ -39,17 +39,17 @@ attributes4: Dict[str, Any] = provider4.attributes
|
||||||
provider5 = providers.Factory(Animal)
|
provider5 = providers.Factory(Animal)
|
||||||
provided5: providers.ProvidedInstance = provider5.provided
|
provided5: providers.ProvidedInstance = provider5.provided
|
||||||
attr_getter5: providers.AttributeGetter = provider5.provided.attr
|
attr_getter5: providers.AttributeGetter = provider5.provided.attr
|
||||||
item_getter5: providers.ItemGetter = provider5.provided['item']
|
item_getter5: providers.ItemGetter = provider5.provided["item"]
|
||||||
method_caller5: providers.MethodCaller = provider5.provided.method.call(123, arg=324)
|
method_caller5: providers.MethodCaller = provider5.provided.method.call(123, arg=324)
|
||||||
|
|
||||||
# Test 6: to check the DelegatedFactory
|
# Test 6: to check the DelegatedFactory
|
||||||
provider6 = providers.DelegatedFactory(Cat)
|
provider6 = providers.DelegatedFactory(Cat)
|
||||||
animal6: Animal = provider6(1, 2, 3, b='1', c=2, e=0.0)
|
animal6: Animal = provider6(1, 2, 3, b="1", c=2, e=0.0)
|
||||||
|
|
||||||
# Test 7: to check the AbstractFactory
|
# Test 7: to check the AbstractFactory
|
||||||
provider7 = providers.AbstractFactory(Animal)
|
provider7 = providers.AbstractFactory(Animal)
|
||||||
provider7.override(providers.Factory(Cat))
|
provider7.override(providers.Factory(Cat))
|
||||||
animal7: Animal = provider7(1, 2, 3, b='1', c=2, e=0.0)
|
animal7: Animal = provider7(1, 2, 3, b="1", c=2, e=0.0)
|
||||||
|
|
||||||
# Test 8: to check the FactoryDelegate __init__
|
# Test 8: to check the FactoryDelegate __init__
|
||||||
provider8 = providers.FactoryDelegate(providers.Factory(object))
|
provider8 = providers.FactoryDelegate(providers.Factory(object))
|
||||||
|
@ -61,7 +61,7 @@ provider9: providers.FactoryAggregate[str] = providers.FactoryAggregate(
|
||||||
)
|
)
|
||||||
factory_a_9: providers.Factory[str] = provider9.a
|
factory_a_9: providers.Factory[str] = provider9.a
|
||||||
factory_b_9: providers.Factory[str] = provider9.b
|
factory_b_9: providers.Factory[str] = provider9.b
|
||||||
val9: str = provider9('a')
|
val9: str = provider9("a")
|
||||||
|
|
||||||
provider9_set_non_string_keys: providers.FactoryAggregate[str] = providers.FactoryAggregate()
|
provider9_set_non_string_keys: providers.FactoryAggregate[str] = providers.FactoryAggregate()
|
||||||
provider9_set_non_string_keys.set_factories({Cat: providers.Factory(str, "str")})
|
provider9_set_non_string_keys.set_factories({Cat: providers.Factory(str, "str")})
|
||||||
|
@ -83,8 +83,8 @@ animal10: Animal = factory10()
|
||||||
# Test 11: to check the return type with await
|
# Test 11: to check the return type with await
|
||||||
provider11 = providers.Factory(Cat)
|
provider11 = providers.Factory(Cat)
|
||||||
async def _async11() -> None:
|
async def _async11() -> None:
|
||||||
animal1: Animal = await provider11(1, 2, 3, b='1', c=2, e=0.0) # type: ignore
|
animal1: Animal = await provider11(1, 2, 3, b="1", c=2, e=0.0) # type: ignore
|
||||||
animal2: Animal = await provider11.async_(1, 2, 3, b='1', c=2, e=0.0)
|
animal2: Animal = await provider11.async_(1, 2, 3, b="1", c=2, e=0.0)
|
||||||
|
|
||||||
# Test 12: to check class type from .provides
|
# Test 12: to check class type from .provides
|
||||||
provider12 = providers.Factory(Cat)
|
provider12 = providers.Factory(Cat)
|
||||||
|
|
|
@ -25,7 +25,7 @@ provider3 = providers.List(
|
||||||
)
|
)
|
||||||
provided3: providers.ProvidedInstance = provider3.provided
|
provided3: providers.ProvidedInstance = provider3.provided
|
||||||
attr_getter3: providers.AttributeGetter = provider3.provided.attr
|
attr_getter3: providers.AttributeGetter = provider3.provided.attr
|
||||||
item_getter3: providers.ItemGetter = provider3.provided['item']
|
item_getter3: providers.ItemGetter = provider3.provided["item"]
|
||||||
method_caller3: providers.MethodCaller = provider3.provided.method.call(123, arg=324)
|
method_caller3: providers.MethodCaller = provider3.provided.method.call(123, arg=324)
|
||||||
|
|
||||||
# Test 4: to check the return type with await
|
# Test 4: to check the return type with await
|
||||||
|
|
|
@ -11,7 +11,7 @@ var1: int = provider1()
|
||||||
provider2 = providers.Object(int)
|
provider2 = providers.Object(int)
|
||||||
provided2: providers.ProvidedInstance = provider2.provided
|
provided2: providers.ProvidedInstance = provider2.provided
|
||||||
attr_getter2: providers.AttributeGetter = provider2.provided.attr
|
attr_getter2: providers.AttributeGetter = provider2.provided.attr
|
||||||
item_getter2: providers.ItemGetter = provider2.provided['item']
|
item_getter2: providers.ItemGetter = provider2.provided["item"]
|
||||||
method_caller2: providers.MethodCaller = provider2.provided.method.call(123, arg=324)
|
method_caller2: providers.MethodCaller = provider2.provided.method.call(123, arg=324)
|
||||||
|
|
||||||
# Test 3: to check the return type with await
|
# Test 3: to check the return type with await
|
||||||
|
@ -21,5 +21,5 @@ async def _async3() -> None:
|
||||||
var2: int = await provider3.async_()
|
var2: int = await provider3.async_()
|
||||||
|
|
||||||
# Test 4: to check class type from provider
|
# Test 4: to check class type from provider
|
||||||
provider4 = providers.Object(int('1'))
|
provider4 = providers.Object(int("1"))
|
||||||
provided_provides: Optional[int] = provider4.provides
|
provided_provides: Optional[int] = provider4.provides
|
||||||
|
|
|
@ -5,7 +5,7 @@ from dependency_injector import providers
|
||||||
|
|
||||||
# Test 1: to check the return type
|
# Test 1: to check the return type
|
||||||
provider1 = providers.Selector(
|
provider1 = providers.Selector(
|
||||||
lambda: 'a',
|
lambda: "a",
|
||||||
a=providers.Factory(object),
|
a=providers.Factory(object),
|
||||||
b=providers.Factory(object),
|
b=providers.Factory(object),
|
||||||
)
|
)
|
||||||
|
@ -13,18 +13,18 @@ var1: Any = provider1()
|
||||||
|
|
||||||
# Test 2: to check the provided instance interface
|
# Test 2: to check the provided instance interface
|
||||||
provider2 = providers.Selector(
|
provider2 = providers.Selector(
|
||||||
lambda: 'a',
|
lambda: "a",
|
||||||
a=providers.Factory(object),
|
a=providers.Factory(object),
|
||||||
b=providers.Factory(object),
|
b=providers.Factory(object),
|
||||||
)
|
)
|
||||||
provided2: providers.ProvidedInstance = provider2.provided
|
provided2: providers.ProvidedInstance = provider2.provided
|
||||||
attr_getter2: providers.AttributeGetter = provider2.provided.attr
|
attr_getter2: providers.AttributeGetter = provider2.provided.attr
|
||||||
item_getter2: providers.ItemGetter = provider2.provided['item']
|
item_getter2: providers.ItemGetter = provider2.provided["item"]
|
||||||
method_caller2: providers.MethodCaller = provider2.provided.method.call(123, arg=324)
|
method_caller2: providers.MethodCaller = provider2.provided.method.call(123, arg=324)
|
||||||
|
|
||||||
# Test3 to check the getattr
|
# Test3 to check the getattr
|
||||||
provider3 = providers.Selector(
|
provider3 = providers.Selector(
|
||||||
lambda: 'a',
|
lambda: "a",
|
||||||
a=providers.Factory(object),
|
a=providers.Factory(object),
|
||||||
b=providers.Factory(object),
|
b=providers.Factory(object),
|
||||||
)
|
)
|
||||||
|
@ -32,7 +32,7 @@ attr3: providers.Provider = provider3.a
|
||||||
|
|
||||||
# Test 4: to check the return type with await
|
# Test 4: to check the return type with await
|
||||||
provider4 = providers.Selector(
|
provider4 = providers.Selector(
|
||||||
lambda: 'a',
|
lambda: "a",
|
||||||
a=providers.Factory(object),
|
a=providers.Factory(object),
|
||||||
b=providers.Factory(object),
|
b=providers.Factory(object),
|
||||||
)
|
)
|
||||||
|
|
|
@ -18,7 +18,7 @@ class Cat(Animal):
|
||||||
|
|
||||||
# Test 1: to check the return type (class)
|
# Test 1: to check the return type (class)
|
||||||
provider1 = providers.Singleton(Cat)
|
provider1 = providers.Singleton(Cat)
|
||||||
animal1: Animal = provider1(1, 2, 3, b='1', c=2, e=0.0)
|
animal1: Animal = provider1(1, 2, 3, b="1", c=2, e=0.0)
|
||||||
|
|
||||||
# Test 2: to check the return type (class factory method)
|
# Test 2: to check the return type (class factory method)
|
||||||
provider2 = providers.Singleton(Cat.create)
|
provider2 = providers.Singleton(Cat.create)
|
||||||
|
@ -39,12 +39,12 @@ attributes4: Dict[str, Any] = provider4.attributes
|
||||||
provider5 = providers.Singleton(Animal)
|
provider5 = providers.Singleton(Animal)
|
||||||
provided5: providers.ProvidedInstance = provider5.provided
|
provided5: providers.ProvidedInstance = provider5.provided
|
||||||
attr_getter5: providers.AttributeGetter = provider5.provided.attr
|
attr_getter5: providers.AttributeGetter = provider5.provided.attr
|
||||||
item_getter5: providers.ItemGetter = provider5.provided['item']
|
item_getter5: providers.ItemGetter = provider5.provided["item"]
|
||||||
method_caller5: providers.MethodCaller = provider5.provided.method.call(123, arg=324)
|
method_caller5: providers.MethodCaller = provider5.provided.method.call(123, arg=324)
|
||||||
|
|
||||||
# Test 6: to check the DelegatedSingleton
|
# Test 6: to check the DelegatedSingleton
|
||||||
provider6 = providers.DelegatedSingleton(Cat)
|
provider6 = providers.DelegatedSingleton(Cat)
|
||||||
animal6: Animal = provider6(1, 2, 3, b='1', c=2, e=0.0)
|
animal6: Animal = provider6(1, 2, 3, b="1", c=2, e=0.0)
|
||||||
|
|
||||||
# Test 7: to check the ThreadSafeSingleton
|
# Test 7: to check the ThreadSafeSingleton
|
||||||
provider7: providers.BaseSingleton[Animal] = providers.ThreadSafeSingleton(Cat)
|
provider7: providers.BaseSingleton[Animal] = providers.ThreadSafeSingleton(Cat)
|
||||||
|
@ -52,20 +52,20 @@ animal7: Animal = provider7()
|
||||||
|
|
||||||
# Test 8: to check the DelegatedThreadSafeSingleton
|
# Test 8: to check the DelegatedThreadSafeSingleton
|
||||||
provider8 = providers.DelegatedThreadSafeSingleton(Cat)
|
provider8 = providers.DelegatedThreadSafeSingleton(Cat)
|
||||||
animal8: Animal = provider8(1, 2, 3, b='1', c=2, e=0.0)
|
animal8: Animal = provider8(1, 2, 3, b="1", c=2, e=0.0)
|
||||||
|
|
||||||
# Test 9: to check the ThreadLocalSingleton
|
# Test 9: to check the ThreadLocalSingleton
|
||||||
provider9 = providers.ThreadLocalSingleton(Cat)
|
provider9 = providers.ThreadLocalSingleton(Cat)
|
||||||
animal9: Animal = provider9(1, 2, 3, b='1', c=2, e=0.0)
|
animal9: Animal = provider9(1, 2, 3, b="1", c=2, e=0.0)
|
||||||
|
|
||||||
# Test 10: to check the DelegatedThreadLocalSingleton
|
# Test 10: to check the DelegatedThreadLocalSingleton
|
||||||
provider10 = providers.DelegatedThreadLocalSingleton(Cat)
|
provider10 = providers.DelegatedThreadLocalSingleton(Cat)
|
||||||
animal10: Animal = provider10(1, 2, 3, b='1', c=2, e=0.0)
|
animal10: Animal = provider10(1, 2, 3, b="1", c=2, e=0.0)
|
||||||
|
|
||||||
# Test 11: to check the AbstractSingleton
|
# Test 11: to check the AbstractSingleton
|
||||||
provider11 = providers.AbstractSingleton(Animal)
|
provider11 = providers.AbstractSingleton(Animal)
|
||||||
provider11.override(providers.Singleton(Cat))
|
provider11.override(providers.Singleton(Cat))
|
||||||
animal11: Animal = provider11(1, 2, 3, b='1', c=2, e=0.0)
|
animal11: Animal = provider11(1, 2, 3, b="1", c=2, e=0.0)
|
||||||
|
|
||||||
# Test 12: to check the SingletonDelegate __init__
|
# Test 12: to check the SingletonDelegate __init__
|
||||||
provider12 = providers.SingletonDelegate(providers.Singleton(object))
|
provider12 = providers.SingletonDelegate(providers.Singleton(object))
|
||||||
|
@ -73,8 +73,8 @@ provider12 = providers.SingletonDelegate(providers.Singleton(object))
|
||||||
# Test 13: to check the return type with await
|
# Test 13: to check the return type with await
|
||||||
provider13 = providers.Singleton(Cat)
|
provider13 = providers.Singleton(Cat)
|
||||||
async def _async13() -> None:
|
async def _async13() -> None:
|
||||||
animal1: Animal = await provider13(1, 2, 3, b='1', c=2, e=0.0) # type: ignore
|
animal1: Animal = await provider13(1, 2, 3, b="1", c=2, e=0.0) # type: ignore
|
||||||
animal2: Animal = await provider13.async_(1, 2, 3, b='1', c=2, e=0.0)
|
animal2: Animal = await provider13.async_(1, 2, 3, b="1", c=2, e=0.0)
|
||||||
|
|
||||||
# Test 14: to check class from .provides
|
# Test 14: to check class from .provides
|
||||||
provider14 = providers.Singleton(Cat)
|
provider14 = providers.Singleton(Cat)
|
||||||
|
|
|
@ -18,12 +18,12 @@ def setup_test_loop(
|
||||||
loop = loop_factory()
|
loop = loop_factory()
|
||||||
try:
|
try:
|
||||||
module = loop.__class__.__module__
|
module = loop.__class__.__module__
|
||||||
skip_watcher = 'uvloop' in module
|
skip_watcher = "uvloop" in module
|
||||||
except AttributeError: # pragma: no cover
|
except AttributeError: # pragma: no cover
|
||||||
# Just in case
|
# Just in case
|
||||||
skip_watcher = True
|
skip_watcher = True
|
||||||
asyncio.set_event_loop(loop)
|
asyncio.set_event_loop(loop)
|
||||||
if sys.platform != 'win32' and not skip_watcher:
|
if sys.platform != "win32" and not skip_watcher:
|
||||||
policy = asyncio.get_event_loop_policy()
|
policy = asyncio.get_event_loop_policy()
|
||||||
watcher = asyncio.SafeChildWatcher() # type: ignore
|
watcher = asyncio.SafeChildWatcher() # type: ignore
|
||||||
watcher.attach_loop(loop)
|
watcher.attach_loop(loop)
|
||||||
|
|
|
@ -52,15 +52,15 @@ class DeclarativeContainerTests(unittest.TestCase):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
ContainerA.providers,
|
ContainerA.providers,
|
||||||
{
|
{
|
||||||
'p11': ContainerA.p11,
|
"p11": ContainerA.p11,
|
||||||
'p12': ContainerA.p12,
|
"p12": ContainerA.p12,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
ContainerA2.providers,
|
ContainerA2.providers,
|
||||||
{
|
{
|
||||||
'p11': p1,
|
"p11": p1,
|
||||||
'p12': p2,
|
"p12": p2,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -95,17 +95,17 @@ class DeclarativeContainerTests(unittest.TestCase):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
ContainerD.dependencies,
|
ContainerD.dependencies,
|
||||||
{
|
{
|
||||||
'p41': ContainerD.p41,
|
"p41": ContainerD.p41,
|
||||||
'p42': ContainerD.p42,
|
"p42": ContainerD.p42,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
ContainerE.dependencies,
|
ContainerE.dependencies,
|
||||||
{
|
{
|
||||||
'p41': ContainerD.p41,
|
"p41": ContainerD.p41,
|
||||||
'p42': ContainerD.p42,
|
"p42": ContainerD.p42,
|
||||||
'p51': ContainerE.p51,
|
"p51": ContainerE.p51,
|
||||||
'p52': ContainerE.p52,
|
"p52": ContainerE.p52,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -308,9 +308,9 @@ class DeclarativeContainerTests(unittest.TestCase):
|
||||||
self.assertIsNot(_Container1.p11, _Container2.p11)
|
self.assertIsNot(_Container1.p11, _Container2.p11)
|
||||||
self.assertIsNot(_Container1.p12, _Container2.p12)
|
self.assertIsNot(_Container1.p12, _Container2.p12)
|
||||||
|
|
||||||
self.assertEqual(_Container.p12(), {'p11': 0})
|
self.assertEqual(_Container.p12(), {"p11": 0})
|
||||||
self.assertEqual(_Container1.p12(), {'p11': 1})
|
self.assertEqual(_Container1.p12(), {"p11": 1})
|
||||||
self.assertEqual(_Container2.p12(), {'p11': 2})
|
self.assertEqual(_Container2.p12(), {"p11": 2})
|
||||||
|
|
||||||
self.assertEqual(_Container1.p13(), 11)
|
self.assertEqual(_Container1.p13(), 11)
|
||||||
self.assertEqual(_Container2.p13(), 22)
|
self.assertEqual(_Container2.p13(), 22)
|
||||||
|
@ -329,9 +329,9 @@ class DeclarativeContainerTests(unittest.TestCase):
|
||||||
new2 = New(p11=1)
|
new2 = New(p11=1)
|
||||||
new3 = New(p11=2)
|
new3 = New(p11=2)
|
||||||
|
|
||||||
self.assertEqual(new1.p13(), {'p12': {'p11': 0}})
|
self.assertEqual(new1.p13(), {"p12": {"p11": 0}})
|
||||||
self.assertEqual(new2.p13(), {'p12': {'p11': 1}})
|
self.assertEqual(new2.p13(), {"p12": {"p11": 1}})
|
||||||
self.assertEqual(new3.p13(), {'p12': {'p11': 2}})
|
self.assertEqual(new3.p13(), {"p12": {"p11": 2}})
|
||||||
|
|
||||||
def test_copy_with_replacing_subcontainer_providers(self):
|
def test_copy_with_replacing_subcontainer_providers(self):
|
||||||
# See: https://github.com/ets-labs/python-dependency-injector/issues/374
|
# See: https://github.com/ets-labs/python-dependency-injector/issues/374
|
||||||
|
@ -339,7 +339,7 @@ class DeclarativeContainerTests(unittest.TestCase):
|
||||||
foo = providers.Dependency(instance_of=str)
|
foo = providers.Dependency(instance_of=str)
|
||||||
|
|
||||||
def build_x():
|
def build_x():
|
||||||
return X(foo='1')
|
return X(foo="1")
|
||||||
|
|
||||||
class A(containers.DeclarativeContainer):
|
class A(containers.DeclarativeContainer):
|
||||||
x = providers.DependenciesContainer(**X.providers)
|
x = providers.DependenciesContainer(**X.providers)
|
||||||
|
@ -351,7 +351,7 @@ class DeclarativeContainerTests(unittest.TestCase):
|
||||||
|
|
||||||
b1 = B1()
|
b1 = B1()
|
||||||
|
|
||||||
self.assertEqual(b1.y(), '1')
|
self.assertEqual(b1.y(), "1")
|
||||||
|
|
||||||
def test_containers_attribute(self):
|
def test_containers_attribute(self):
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
|
@ -388,17 +388,17 @@ class DeclarativeContainerTests(unittest.TestCase):
|
||||||
|
|
||||||
container = _Container(p1=1)
|
container = _Container(p1=1)
|
||||||
|
|
||||||
self.assertEqual(container.p2(), {'p1': 1})
|
self.assertEqual(container.p2(), {"p1": 1})
|
||||||
self.assertIs(
|
self.assertIs(
|
||||||
container.p2.last_overriding.kwargs['p1'],
|
container.p2.last_overriding.kwargs["p1"],
|
||||||
container.p1,
|
container.p1,
|
||||||
)
|
)
|
||||||
self.assertIsNot(
|
self.assertIsNot(
|
||||||
container.p2.last_overriding.kwargs['p1'],
|
container.p2.last_overriding.kwargs["p1"],
|
||||||
_Container.p1,
|
_Container.p1,
|
||||||
)
|
)
|
||||||
self.assertIs(
|
self.assertIs(
|
||||||
_Container.p2.last_overriding.kwargs['p1'],
|
_Container.p2.last_overriding.kwargs["p1"],
|
||||||
_Container.p1,
|
_Container.p1,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -419,9 +419,9 @@ class DeclarativeContainerTests(unittest.TestCase):
|
||||||
def test_init_with_dependency_delegation(self):
|
def test_init_with_dependency_delegation(self):
|
||||||
# Bug:
|
# Bug:
|
||||||
# https://github.com/ets-labs/python-dependency-injector/issues/235
|
# https://github.com/ets-labs/python-dependency-injector/issues/235
|
||||||
A = collections.namedtuple('A', [])
|
A = collections.namedtuple("A", [])
|
||||||
B = collections.namedtuple('B', ['fa'])
|
B = collections.namedtuple("B", ["fa"])
|
||||||
C = collections.namedtuple('B', ['a'])
|
C = collections.namedtuple("B", ["a"])
|
||||||
|
|
||||||
class Services(containers.DeclarativeContainer):
|
class Services(containers.DeclarativeContainer):
|
||||||
a = providers.Dependency()
|
a = providers.Dependency()
|
||||||
|
@ -469,7 +469,7 @@ class DeclarativeContainerTests(unittest.TestCase):
|
||||||
self.assertIs(Container.container.parent, Container)
|
self.assertIs(Container.container.parent, Container)
|
||||||
|
|
||||||
def test_resolve_provider_name(self):
|
def test_resolve_provider_name(self):
|
||||||
self.assertEqual(ContainerA.resolve_provider_name(ContainerA.p11), 'p11')
|
self.assertEqual(ContainerA.resolve_provider_name(ContainerA.p11), "p11")
|
||||||
|
|
||||||
def test_resolve_provider_name_no_provider(self):
|
def test_resolve_provider_name_no_provider(self):
|
||||||
with self.assertRaises(errors.Error):
|
with self.assertRaises(errors.Error):
|
||||||
|
@ -483,7 +483,7 @@ class DeclarativeContainerTests(unittest.TestCase):
|
||||||
Container.dependency()
|
Container.dependency()
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(context.exception),
|
str(context.exception),
|
||||||
'Dependency "Container.dependency" is not defined',
|
"Dependency \"Container.dependency\" is not defined",
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_child_dependencies_container_parent_name(self):
|
def test_child_dependencies_container_parent_name(self):
|
||||||
|
@ -494,7 +494,7 @@ class DeclarativeContainerTests(unittest.TestCase):
|
||||||
Container.dependencies_container.dependency()
|
Container.dependencies_container.dependency()
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(context.exception),
|
str(context.exception),
|
||||||
'Dependency "Container.dependencies_container.dependency" is not defined',
|
"Dependency \"Container.dependencies_container.dependency\" is not defined",
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_child_container_parent_name(self):
|
def test_child_container_parent_name(self):
|
||||||
|
@ -508,7 +508,7 @@ class DeclarativeContainerTests(unittest.TestCase):
|
||||||
Container.child_container.dependency()
|
Container.child_container.dependency()
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(context.exception),
|
str(context.exception),
|
||||||
'Dependency "Container.child_container.dependency" is not defined',
|
"Dependency \"Container.child_container.dependency\" is not defined",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -529,11 +529,11 @@ class DeclarativeContainerWithCustomStringTests(unittest.TestCase):
|
||||||
self.provider = providers.Provider()
|
self.provider = providers.Provider()
|
||||||
|
|
||||||
def test_setattr(self):
|
def test_setattr(self):
|
||||||
setattr(self.container, self.CustomString('test_attr'), self.provider)
|
setattr(self.container, self.CustomString("test_attr"), self.provider)
|
||||||
self.assertIs(self.container.test_attr, self.provider)
|
self.assertIs(self.container.test_attr, self.provider)
|
||||||
|
|
||||||
def test_delattr(self):
|
def test_delattr(self):
|
||||||
setattr(self.container, self.CustomString('test_attr'), self.provider)
|
setattr(self.container, self.CustomString("test_attr"), self.provider)
|
||||||
delattr(self.container, self.CustomString('test_attr'))
|
delattr(self.container, self.CustomString("test_attr"))
|
||||||
with self.assertRaises(AttributeError):
|
with self.assertRaises(AttributeError):
|
||||||
self.container.test_attr
|
self.container.test_attr
|
||||||
|
|
|
@ -6,7 +6,7 @@ import os
|
||||||
_TOP_DIR = os.path.abspath(
|
_TOP_DIR = os.path.abspath(
|
||||||
os.path.sep.join((
|
os.path.sep.join((
|
||||||
os.path.dirname(__file__),
|
os.path.dirname(__file__),
|
||||||
'../',
|
"../",
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
import sys
|
import sys
|
||||||
|
@ -46,18 +46,18 @@ class AsyncResourcesTest(AsyncTestCase):
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
resource1 = providers.Resource(
|
resource1 = providers.Resource(
|
||||||
_resource,
|
_resource,
|
||||||
name='r1',
|
name="r1",
|
||||||
delay=0.03,
|
delay=0.03,
|
||||||
)
|
)
|
||||||
resource2 = providers.Resource(
|
resource2 = providers.Resource(
|
||||||
_resource,
|
_resource,
|
||||||
name='r2',
|
name="r2",
|
||||||
delay=0.02,
|
delay=0.02,
|
||||||
r1=resource1,
|
r1=resource1,
|
||||||
)
|
)
|
||||||
resource3 = providers.Resource(
|
resource3 = providers.Resource(
|
||||||
_resource,
|
_resource,
|
||||||
name='r3',
|
name="r3",
|
||||||
delay=0.01,
|
delay=0.01,
|
||||||
r2=resource2,
|
r2=resource2,
|
||||||
)
|
)
|
||||||
|
@ -65,20 +65,20 @@ class AsyncResourcesTest(AsyncTestCase):
|
||||||
container = Container()
|
container = Container()
|
||||||
|
|
||||||
self._run(container.init_resources())
|
self._run(container.init_resources())
|
||||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3'])
|
self.assertEqual(initialized_resources, ["r1", "r2", "r3"])
|
||||||
self.assertEqual(shutdown_resources, [])
|
self.assertEqual(shutdown_resources, [])
|
||||||
|
|
||||||
self._run(container.shutdown_resources())
|
self._run(container.shutdown_resources())
|
||||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3'])
|
self.assertEqual(initialized_resources, ["r1", "r2", "r3"])
|
||||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1'])
|
self.assertEqual(shutdown_resources, ["r3", "r2", "r1"])
|
||||||
|
|
||||||
self._run(container.init_resources())
|
self._run(container.init_resources())
|
||||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3', 'r1', 'r2', 'r3'])
|
self.assertEqual(initialized_resources, ["r1", "r2", "r3", "r1", "r2", "r3"])
|
||||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1'])
|
self.assertEqual(shutdown_resources, ["r3", "r2", "r1"])
|
||||||
|
|
||||||
self._run(container.shutdown_resources())
|
self._run(container.shutdown_resources())
|
||||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3', 'r1', 'r2', 'r3'])
|
self.assertEqual(initialized_resources, ["r1", "r2", "r3", "r1", "r2", "r3"])
|
||||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1', 'r3', 'r2', 'r1'])
|
self.assertEqual(shutdown_resources, ["r3", "r2", "r1", "r3", "r2", "r1"])
|
||||||
|
|
||||||
def test_shutdown_circular_dependencies_breaker(self):
|
def test_shutdown_circular_dependencies_breaker(self):
|
||||||
async def _resource(name, **_):
|
async def _resource(name, **_):
|
||||||
|
@ -87,16 +87,16 @@ class AsyncResourcesTest(AsyncTestCase):
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
resource1 = providers.Resource(
|
resource1 = providers.Resource(
|
||||||
_resource,
|
_resource,
|
||||||
name='r1',
|
name="r1",
|
||||||
)
|
)
|
||||||
resource2 = providers.Resource(
|
resource2 = providers.Resource(
|
||||||
_resource,
|
_resource,
|
||||||
name='r2',
|
name="r2",
|
||||||
r1=resource1,
|
r1=resource1,
|
||||||
)
|
)
|
||||||
resource3 = providers.Resource(
|
resource3 = providers.Resource(
|
||||||
_resource,
|
_resource,
|
||||||
name='r3',
|
name="r3",
|
||||||
r2=resource2,
|
r2=resource2,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -108,7 +108,7 @@ class AsyncResourcesTest(AsyncTestCase):
|
||||||
|
|
||||||
with self.assertRaises(RuntimeError) as context:
|
with self.assertRaises(RuntimeError) as context:
|
||||||
self._run(container.shutdown_resources())
|
self._run(container.shutdown_resources())
|
||||||
self.assertEqual(str(context.exception), 'Unable to resolve resources shutdown order')
|
self.assertEqual(str(context.exception), "Unable to resolve resources shutdown order")
|
||||||
|
|
||||||
def test_shutdown_sync_and_async_ordering(self):
|
def test_shutdown_sync_and_async_ordering(self):
|
||||||
initialized_resources = []
|
initialized_resources = []
|
||||||
|
@ -127,33 +127,33 @@ class AsyncResourcesTest(AsyncTestCase):
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
resource1 = providers.Resource(
|
resource1 = providers.Resource(
|
||||||
_sync_resource,
|
_sync_resource,
|
||||||
name='r1',
|
name="r1",
|
||||||
)
|
)
|
||||||
resource2 = providers.Resource(
|
resource2 = providers.Resource(
|
||||||
_sync_resource,
|
_sync_resource,
|
||||||
name='r2',
|
name="r2",
|
||||||
r1=resource1,
|
r1=resource1,
|
||||||
)
|
)
|
||||||
resource3 = providers.Resource(
|
resource3 = providers.Resource(
|
||||||
_async_resource,
|
_async_resource,
|
||||||
name='r3',
|
name="r3",
|
||||||
r2=resource2,
|
r2=resource2,
|
||||||
)
|
)
|
||||||
|
|
||||||
container = Container()
|
container = Container()
|
||||||
|
|
||||||
self._run(container.init_resources())
|
self._run(container.init_resources())
|
||||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3'])
|
self.assertEqual(initialized_resources, ["r1", "r2", "r3"])
|
||||||
self.assertEqual(shutdown_resources, [])
|
self.assertEqual(shutdown_resources, [])
|
||||||
|
|
||||||
self._run(container.shutdown_resources())
|
self._run(container.shutdown_resources())
|
||||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3'])
|
self.assertEqual(initialized_resources, ["r1", "r2", "r3"])
|
||||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1'])
|
self.assertEqual(shutdown_resources, ["r3", "r2", "r1"])
|
||||||
|
|
||||||
self._run(container.init_resources())
|
self._run(container.init_resources())
|
||||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3', 'r1', 'r2', 'r3'])
|
self.assertEqual(initialized_resources, ["r1", "r2", "r3", "r1", "r2", "r3"])
|
||||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1'])
|
self.assertEqual(shutdown_resources, ["r3", "r2", "r1"])
|
||||||
|
|
||||||
self._run(container.shutdown_resources())
|
self._run(container.shutdown_resources())
|
||||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3', 'r1', 'r2', 'r3'])
|
self.assertEqual(initialized_resources, ["r1", "r2", "r3", "r1", "r2", "r3"])
|
||||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1', 'r3', 'r2', 'r1'])
|
self.assertEqual(shutdown_resources, ["r3", "r2", "r1", "r3", "r2", "r1"])
|
||||||
|
|
|
@ -28,7 +28,7 @@ class DeclarativeContainerInstanceTests(unittest.TestCase):
|
||||||
container = ContainerA()
|
container = ContainerA()
|
||||||
container.a1 = providers.Dependency()
|
container.a1 = providers.Dependency()
|
||||||
container.a2 = providers.DependenciesContainer()
|
container.a2 = providers.DependenciesContainer()
|
||||||
self.assertEqual(container.dependencies, {'a1': container.a1, 'a2': container.a2})
|
self.assertEqual(container.dependencies, {"a1": container.a1, "a2": container.a2})
|
||||||
|
|
||||||
def test_set_get_del_providers(self):
|
def test_set_get_del_providers(self):
|
||||||
p13 = providers.Provider()
|
p13 = providers.Provider()
|
||||||
|
@ -212,36 +212,36 @@ class DeclarativeContainerInstanceTests(unittest.TestCase):
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
resource1 = providers.Resource(
|
resource1 = providers.Resource(
|
||||||
_resource,
|
_resource,
|
||||||
name='r1',
|
name="r1",
|
||||||
)
|
)
|
||||||
resource2 = providers.Resource(
|
resource2 = providers.Resource(
|
||||||
_resource,
|
_resource,
|
||||||
name='r2',
|
name="r2",
|
||||||
r1=resource1,
|
r1=resource1,
|
||||||
)
|
)
|
||||||
resource3 = providers.Resource(
|
resource3 = providers.Resource(
|
||||||
_resource,
|
_resource,
|
||||||
name='r3',
|
name="r3",
|
||||||
r2=resource2,
|
r2=resource2,
|
||||||
)
|
)
|
||||||
|
|
||||||
container = Container()
|
container = Container()
|
||||||
|
|
||||||
container.init_resources()
|
container.init_resources()
|
||||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3'])
|
self.assertEqual(initialized_resources, ["r1", "r2", "r3"])
|
||||||
self.assertEqual(shutdown_resources, [])
|
self.assertEqual(shutdown_resources, [])
|
||||||
|
|
||||||
container.shutdown_resources()
|
container.shutdown_resources()
|
||||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3'])
|
self.assertEqual(initialized_resources, ["r1", "r2", "r3"])
|
||||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1'])
|
self.assertEqual(shutdown_resources, ["r3", "r2", "r1"])
|
||||||
|
|
||||||
container.init_resources()
|
container.init_resources()
|
||||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3', 'r1', 'r2', 'r3'])
|
self.assertEqual(initialized_resources, ["r1", "r2", "r3", "r1", "r2", "r3"])
|
||||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1'])
|
self.assertEqual(shutdown_resources, ["r3", "r2", "r1"])
|
||||||
|
|
||||||
container.shutdown_resources()
|
container.shutdown_resources()
|
||||||
self.assertEqual(initialized_resources, ['r1', 'r2', 'r3', 'r1', 'r2', 'r3'])
|
self.assertEqual(initialized_resources, ["r1", "r2", "r3", "r1", "r2", "r3"])
|
||||||
self.assertEqual(shutdown_resources, ['r3', 'r2', 'r1', 'r3', 'r2', 'r1'])
|
self.assertEqual(shutdown_resources, ["r3", "r2", "r1", "r3", "r2", "r1"])
|
||||||
|
|
||||||
def test_shutdown_resources_circular_dependencies_breaker(self):
|
def test_shutdown_resources_circular_dependencies_breaker(self):
|
||||||
def _resource(name, **_):
|
def _resource(name, **_):
|
||||||
|
@ -250,16 +250,16 @@ class DeclarativeContainerInstanceTests(unittest.TestCase):
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
resource1 = providers.Resource(
|
resource1 = providers.Resource(
|
||||||
_resource,
|
_resource,
|
||||||
name='r1',
|
name="r1",
|
||||||
)
|
)
|
||||||
resource2 = providers.Resource(
|
resource2 = providers.Resource(
|
||||||
_resource,
|
_resource,
|
||||||
name='r2',
|
name="r2",
|
||||||
r1=resource1,
|
r1=resource1,
|
||||||
)
|
)
|
||||||
resource3 = providers.Resource(
|
resource3 = providers.Resource(
|
||||||
_resource,
|
_resource,
|
||||||
name='r3',
|
name="r3",
|
||||||
r2=resource2,
|
r2=resource2,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -271,7 +271,7 @@ class DeclarativeContainerInstanceTests(unittest.TestCase):
|
||||||
|
|
||||||
with self.assertRaises(RuntimeError) as context:
|
with self.assertRaises(RuntimeError) as context:
|
||||||
container.shutdown_resources()
|
container.shutdown_resources()
|
||||||
self.assertEqual(str(context.exception), 'Unable to resolve resources shutdown order')
|
self.assertEqual(str(context.exception), "Unable to resolve resources shutdown order")
|
||||||
|
|
||||||
def test_init_shutdown_nested_resources(self):
|
def test_init_shutdown_nested_resources(self):
|
||||||
def _init1():
|
def _init1():
|
||||||
|
@ -416,16 +416,16 @@ class DeclarativeContainerInstanceTests(unittest.TestCase):
|
||||||
with self.assertRaises(errors.Error) as context:
|
with self.assertRaises(errors.Error) as context:
|
||||||
container.check_dependencies()
|
container.check_dependencies()
|
||||||
|
|
||||||
self.assertIn('Container "Container" has undefined dependencies:', str(context.exception))
|
self.assertIn("Container \"Container\" has undefined dependencies:", str(context.exception))
|
||||||
self.assertIn('"Container.dependency"', str(context.exception))
|
self.assertIn("\"Container.dependency\"", str(context.exception))
|
||||||
self.assertIn('"Container.dependencies_container.dependency"', str(context.exception))
|
self.assertIn("\"Container.dependencies_container.dependency\"", str(context.exception))
|
||||||
self.assertIn('"Container.sub_container.dependency"', str(context.exception))
|
self.assertIn("\"Container.sub_container.dependency\"", str(context.exception))
|
||||||
|
|
||||||
def test_check_dependencies_all_defined(self):
|
def test_check_dependencies_all_defined(self):
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
dependency = providers.Dependency()
|
dependency = providers.Dependency()
|
||||||
|
|
||||||
container = Container(dependency='provided')
|
container = Container(dependency="provided")
|
||||||
result = container.check_dependencies()
|
result = container.check_dependencies()
|
||||||
|
|
||||||
self.assertIsNone(result)
|
self.assertIsNone(result)
|
||||||
|
@ -440,11 +440,11 @@ class DeclarativeContainerInstanceTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_parent_name_declarative_parent(self):
|
def test_parent_name_declarative_parent(self):
|
||||||
container = ContainerA()
|
container = ContainerA()
|
||||||
self.assertEqual(container.parent_name, 'ContainerA')
|
self.assertEqual(container.parent_name, "ContainerA")
|
||||||
|
|
||||||
def test_parent_name(self):
|
def test_parent_name(self):
|
||||||
container = ContainerA()
|
container = ContainerA()
|
||||||
self.assertEqual(container.parent_name, 'ContainerA')
|
self.assertEqual(container.parent_name, "ContainerA")
|
||||||
|
|
||||||
def test_parent_name_with_deep_parenting(self):
|
def test_parent_name_with_deep_parenting(self):
|
||||||
class Container2(containers.DeclarativeContainer):
|
class Container2(containers.DeclarativeContainer):
|
||||||
|
@ -456,7 +456,7 @@ class DeclarativeContainerInstanceTests(unittest.TestCase):
|
||||||
container = providers.Container(Container2)
|
container = providers.Container(Container2)
|
||||||
|
|
||||||
container = Container1()
|
container = Container1()
|
||||||
self.assertEqual(container.container().name.parent_name, 'Container1.container.name')
|
self.assertEqual(container.container().name.parent_name, "Container1.container.name")
|
||||||
|
|
||||||
def test_parent_name_is_none(self):
|
def test_parent_name_is_none(self):
|
||||||
container = containers.DynamicContainer()
|
container = containers.DynamicContainer()
|
||||||
|
@ -479,7 +479,7 @@ class DeclarativeContainerInstanceTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_resolve_provider_name(self):
|
def test_resolve_provider_name(self):
|
||||||
container = ContainerA()
|
container = ContainerA()
|
||||||
self.assertEqual(container.resolve_provider_name(container.p11), 'p11')
|
self.assertEqual(container.resolve_provider_name(container.p11), "p11")
|
||||||
|
|
||||||
def test_resolve_provider_name_no_provider(self):
|
def test_resolve_provider_name_no_provider(self):
|
||||||
container = ContainerA()
|
container = ContainerA()
|
||||||
|
@ -496,11 +496,11 @@ class SelfTests(unittest.TestCase):
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
__self__ = providers.Self()
|
__self__ = providers.Self()
|
||||||
foo = providers.Callable(call_bar, __self__)
|
foo = providers.Callable(call_bar, __self__)
|
||||||
bar = providers.Object('hello')
|
bar = providers.Object("hello")
|
||||||
|
|
||||||
container = Container()
|
container = Container()
|
||||||
|
|
||||||
self.assertIs(container.foo(), 'hello')
|
self.assertIs(container.foo(), "hello")
|
||||||
|
|
||||||
def test_self_attribute_implicit(self):
|
def test_self_attribute_implicit(self):
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
|
@ -531,7 +531,7 @@ class SelfTests(unittest.TestCase):
|
||||||
container = Container()
|
container = Container()
|
||||||
|
|
||||||
self.assertIs(container.__self__, container.foo)
|
self.assertIs(container.__self__, container.foo)
|
||||||
self.assertEqual(set(container.__self__.alt_names), {'foo'})
|
self.assertEqual(set(container.__self__.alt_names), {"foo"})
|
||||||
|
|
||||||
def test_self_attribute_alt_name_explicit_1(self):
|
def test_self_attribute_alt_name_explicit_1(self):
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
|
@ -543,7 +543,7 @@ class SelfTests(unittest.TestCase):
|
||||||
|
|
||||||
self.assertIs(container.__self__, container.foo)
|
self.assertIs(container.__self__, container.foo)
|
||||||
self.assertIs(container.__self__, container.bar)
|
self.assertIs(container.__self__, container.bar)
|
||||||
self.assertEqual(set(container.__self__.alt_names), {'foo', 'bar'})
|
self.assertEqual(set(container.__self__.alt_names), {"foo", "bar"})
|
||||||
|
|
||||||
def test_self_attribute_alt_name_explicit_2(self):
|
def test_self_attribute_alt_name_explicit_2(self):
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
|
@ -554,7 +554,7 @@ class SelfTests(unittest.TestCase):
|
||||||
|
|
||||||
self.assertIs(container.__self__, container.foo)
|
self.assertIs(container.__self__, container.foo)
|
||||||
self.assertIs(container.__self__, container.bar)
|
self.assertIs(container.__self__, container.bar)
|
||||||
self.assertEqual(set(container.__self__.alt_names), {'foo', 'bar'})
|
self.assertEqual(set(container.__self__.alt_names), {"foo", "bar"})
|
||||||
|
|
||||||
def test_providers_attribute_1(self):
|
def test_providers_attribute_1(self):
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
|
@ -595,14 +595,14 @@ class SelfTests(unittest.TestCase):
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
__self__ = providers.Self()
|
__self__ = providers.Self()
|
||||||
foo = providers.Callable(call_bar, __self__)
|
foo = providers.Callable(call_bar, __self__)
|
||||||
bar = providers.Object('hello')
|
bar = providers.Object("hello")
|
||||||
|
|
||||||
container1 = Container()
|
container1 = Container()
|
||||||
container2 = providers.deepcopy(container1)
|
container2 = providers.deepcopy(container1)
|
||||||
container1.bar.override('bye')
|
container1.bar.override("bye")
|
||||||
|
|
||||||
self.assertIs(container1.foo(), 'bye')
|
self.assertIs(container1.foo(), "bye")
|
||||||
self.assertIs(container2.foo(), 'hello')
|
self.assertIs(container2.foo(), "hello")
|
||||||
|
|
||||||
def test_deepcopy_alt_names_1(self):
|
def test_deepcopy_alt_names_1(self):
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
|
@ -646,7 +646,7 @@ class SelfTests(unittest.TestCase):
|
||||||
class SubContainer(containers.DeclarativeContainer):
|
class SubContainer(containers.DeclarativeContainer):
|
||||||
__self__ = providers.Self()
|
__self__ = providers.Self()
|
||||||
foo = providers.Callable(call_bar, __self__)
|
foo = providers.Callable(call_bar, __self__)
|
||||||
bar = providers.Object('hello')
|
bar = providers.Object("hello")
|
||||||
|
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
sub_container = providers.Container(SubContainer)
|
sub_container = providers.Container(SubContainer)
|
||||||
|
@ -655,7 +655,7 @@ class SelfTests(unittest.TestCase):
|
||||||
|
|
||||||
container = Container()
|
container = Container()
|
||||||
|
|
||||||
self.assertIs(container.baz(), 'hello')
|
self.assertIs(container.baz(), "hello")
|
||||||
|
|
||||||
def test_with_container_provider_overriding(self):
|
def test_with_container_provider_overriding(self):
|
||||||
def call_bar(container):
|
def call_bar(container):
|
||||||
|
@ -664,16 +664,16 @@ class SelfTests(unittest.TestCase):
|
||||||
class SubContainer(containers.DeclarativeContainer):
|
class SubContainer(containers.DeclarativeContainer):
|
||||||
__self__ = providers.Self()
|
__self__ = providers.Self()
|
||||||
foo = providers.Callable(call_bar, __self__)
|
foo = providers.Callable(call_bar, __self__)
|
||||||
bar = providers.Object('hello')
|
bar = providers.Object("hello")
|
||||||
|
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
sub_container = providers.Container(SubContainer, bar='bye')
|
sub_container = providers.Container(SubContainer, bar="bye")
|
||||||
|
|
||||||
baz = providers.Callable(lambda value: value, sub_container.foo)
|
baz = providers.Callable(lambda value: value, sub_container.foo)
|
||||||
|
|
||||||
container = Container()
|
container = Container()
|
||||||
|
|
||||||
self.assertIs(container.baz(), 'bye')
|
self.assertIs(container.baz(), "bye")
|
||||||
|
|
||||||
def test_with_container_provider_self(self):
|
def test_with_container_provider_self(self):
|
||||||
class SubContainer(containers.DeclarativeContainer):
|
class SubContainer(containers.DeclarativeContainer):
|
||||||
|
@ -702,19 +702,19 @@ class DynamicContainerWithCustomStringTests(unittest.TestCase):
|
||||||
self.provider = providers.Provider()
|
self.provider = providers.Provider()
|
||||||
|
|
||||||
def test_setattr(self):
|
def test_setattr(self):
|
||||||
setattr(self.container, self.CustomString('test_attr'), self.provider)
|
setattr(self.container, self.CustomString("test_attr"), self.provider)
|
||||||
self.assertIs(self.container.test_attr, self.provider)
|
self.assertIs(self.container.test_attr, self.provider)
|
||||||
|
|
||||||
def test_delattr(self):
|
def test_delattr(self):
|
||||||
setattr(self.container, self.CustomString('test_attr'), self.provider)
|
setattr(self.container, self.CustomString("test_attr"), self.provider)
|
||||||
delattr(self.container, self.CustomString('test_attr'))
|
delattr(self.container, self.CustomString("test_attr"))
|
||||||
with self.assertRaises(AttributeError):
|
with self.assertRaises(AttributeError):
|
||||||
self.container.test_attr
|
self.container.test_attr
|
||||||
|
|
||||||
def test_set_provider(self):
|
def test_set_provider(self):
|
||||||
self.container.set_provider(self.CustomString('test_attr'), self.provider)
|
self.container.set_provider(self.CustomString("test_attr"), self.provider)
|
||||||
self.assertIs(self.container.test_attr, self.provider)
|
self.assertIs(self.container.test_attr, self.provider)
|
||||||
|
|
||||||
def test_set_providers(self):
|
def test_set_providers(self):
|
||||||
self.container.set_providers(**{self.CustomString('test_attr'): self.provider})
|
self.container.set_providers(**{self.CustomString("test_attr"): self.provider})
|
||||||
self.assertIs(self.container.test_attr, self.provider)
|
self.assertIs(self.container.test_attr, self.provider)
|
||||||
|
|
|
@ -11,11 +11,11 @@ class TraverseProviderTests(unittest.TestCase):
|
||||||
dict,
|
dict,
|
||||||
foo=providers.Resource(
|
foo=providers.Resource(
|
||||||
dict,
|
dict,
|
||||||
foo='bar'
|
foo="bar"
|
||||||
),
|
),
|
||||||
bar=providers.Resource(
|
bar=providers.Resource(
|
||||||
dict,
|
dict,
|
||||||
foo='bar'
|
foo="bar"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -23,8 +23,8 @@ class TraverseProviderTests(unittest.TestCase):
|
||||||
all_providers = list(container.traverse())
|
all_providers = list(container.traverse())
|
||||||
|
|
||||||
self.assertIn(container.obj_factory, all_providers)
|
self.assertIn(container.obj_factory, all_providers)
|
||||||
self.assertIn(container.obj_factory.kwargs['foo'], all_providers)
|
self.assertIn(container.obj_factory.kwargs["foo"], all_providers)
|
||||||
self.assertIn(container.obj_factory.kwargs['bar'], all_providers)
|
self.assertIn(container.obj_factory.kwargs["bar"], all_providers)
|
||||||
self.assertEqual(len(all_providers), 3)
|
self.assertEqual(len(all_providers), 3)
|
||||||
|
|
||||||
def test_nested_providers_with_filtering(self):
|
def test_nested_providers_with_filtering(self):
|
||||||
|
@ -33,19 +33,19 @@ class TraverseProviderTests(unittest.TestCase):
|
||||||
dict,
|
dict,
|
||||||
foo=providers.Resource(
|
foo=providers.Resource(
|
||||||
dict,
|
dict,
|
||||||
foo='bar'
|
foo="bar"
|
||||||
),
|
),
|
||||||
bar=providers.Resource(
|
bar=providers.Resource(
|
||||||
dict,
|
dict,
|
||||||
foo='bar'
|
foo="bar"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
container = Container()
|
container = Container()
|
||||||
all_providers = list(container.traverse(types=[providers.Resource]))
|
all_providers = list(container.traverse(types=[providers.Resource]))
|
||||||
|
|
||||||
self.assertIn(container.obj_factory.kwargs['foo'], all_providers)
|
self.assertIn(container.obj_factory.kwargs["foo"], all_providers)
|
||||||
self.assertIn(container.obj_factory.kwargs['bar'], all_providers)
|
self.assertIn(container.obj_factory.kwargs["bar"], all_providers)
|
||||||
self.assertEqual(len(all_providers), 2)
|
self.assertEqual(len(all_providers), 2)
|
||||||
|
|
||||||
|
|
||||||
|
@ -57,19 +57,19 @@ class TraverseProviderDeclarativeTests(unittest.TestCase):
|
||||||
dict,
|
dict,
|
||||||
foo=providers.Resource(
|
foo=providers.Resource(
|
||||||
dict,
|
dict,
|
||||||
foo='bar'
|
foo="bar"
|
||||||
),
|
),
|
||||||
bar=providers.Resource(
|
bar=providers.Resource(
|
||||||
dict,
|
dict,
|
||||||
foo='bar'
|
foo="bar"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
all_providers = list(Container.traverse())
|
all_providers = list(Container.traverse())
|
||||||
|
|
||||||
self.assertIn(Container.obj_factory, all_providers)
|
self.assertIn(Container.obj_factory, all_providers)
|
||||||
self.assertIn(Container.obj_factory.kwargs['foo'], all_providers)
|
self.assertIn(Container.obj_factory.kwargs["foo"], all_providers)
|
||||||
self.assertIn(Container.obj_factory.kwargs['bar'], all_providers)
|
self.assertIn(Container.obj_factory.kwargs["bar"], all_providers)
|
||||||
self.assertEqual(len(all_providers), 3)
|
self.assertEqual(len(all_providers), 3)
|
||||||
|
|
||||||
def test_nested_providers_with_filtering(self):
|
def test_nested_providers_with_filtering(self):
|
||||||
|
@ -78,16 +78,16 @@ class TraverseProviderDeclarativeTests(unittest.TestCase):
|
||||||
dict,
|
dict,
|
||||||
foo=providers.Resource(
|
foo=providers.Resource(
|
||||||
dict,
|
dict,
|
||||||
foo='bar'
|
foo="bar"
|
||||||
),
|
),
|
||||||
bar=providers.Resource(
|
bar=providers.Resource(
|
||||||
dict,
|
dict,
|
||||||
foo='bar'
|
foo="bar"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
all_providers = list(Container.traverse(types=[providers.Resource]))
|
all_providers = list(Container.traverse(types=[providers.Resource]))
|
||||||
|
|
||||||
self.assertIn(Container.obj_factory.kwargs['foo'], all_providers)
|
self.assertIn(Container.obj_factory.kwargs["foo"], all_providers)
|
||||||
self.assertIn(Container.obj_factory.kwargs['bar'], all_providers)
|
self.assertIn(Container.obj_factory.kwargs["bar"], all_providers)
|
||||||
self.assertEqual(len(all_providers), 2)
|
self.assertEqual(len(all_providers), 2)
|
||||||
|
|
|
@ -8,22 +8,22 @@ from dependency_injector.ext import aiohttp
|
||||||
|
|
||||||
|
|
||||||
async def index(_):
|
async def index(_):
|
||||||
return web.Response(text='Hello World!')
|
return web.Response(text="Hello World!")
|
||||||
|
|
||||||
|
|
||||||
async def test(_):
|
async def test(_):
|
||||||
return web.Response(text='Test!')
|
return web.Response(text="Test!")
|
||||||
|
|
||||||
|
|
||||||
class Test(web.View):
|
class Test(web.View):
|
||||||
async def get(self):
|
async def get(self):
|
||||||
return web.Response(text='Test class-based!')
|
return web.Response(text="Test class-based!")
|
||||||
|
|
||||||
|
|
||||||
@web.middleware
|
@web.middleware
|
||||||
async def middleware(request, handler):
|
async def middleware(request, handler):
|
||||||
resp = await handler(request)
|
resp = await handler(request)
|
||||||
resp.text = resp.text + ' wink1'
|
resp.text = resp.text + " wink1"
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ class ApplicationContainer(containers.DeclarativeContainer):
|
||||||
web.Application,
|
web.Application,
|
||||||
middlewares=providers.List(
|
middlewares=providers.List(
|
||||||
aiohttp.Middleware(middleware),
|
aiohttp.Middleware(middleware),
|
||||||
aiohttp.MiddlewareFactory(middleware_factory, text=' wink2'),
|
aiohttp.MiddlewareFactory(middleware_factory, text=" wink2"),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -61,33 +61,33 @@ class ApplicationTests(AioHTTPTestCase):
|
||||||
app = container.app()
|
app = container.app()
|
||||||
app.container = container
|
app.container = container
|
||||||
app.add_routes([
|
app.add_routes([
|
||||||
web.get('/', container.index_view.as_view()),
|
web.get("/", container.index_view.as_view()),
|
||||||
web.get('/test', container.test_view.as_view(), name='test'),
|
web.get("/test", container.test_view.as_view(), name="test"),
|
||||||
web.get('/test-class', container.test_class_view.as_view()),
|
web.get("/test-class", container.test_class_view.as_view()),
|
||||||
])
|
])
|
||||||
return app
|
return app
|
||||||
|
|
||||||
@unittest_run_loop
|
@unittest_run_loop
|
||||||
async def test_index(self):
|
async def test_index(self):
|
||||||
response = await self.client.get('/')
|
response = await self.client.get("/")
|
||||||
|
|
||||||
self.assertEqual(response.status, 200)
|
self.assertEqual(response.status, 200)
|
||||||
self.assertEqual(await response.text(), 'Hello World! wink2 wink1')
|
self.assertEqual(await response.text(), "Hello World! wink2 wink1")
|
||||||
|
|
||||||
@unittest_run_loop
|
@unittest_run_loop
|
||||||
async def test_test(self):
|
async def test_test(self):
|
||||||
response = await self.client.get('/test')
|
response = await self.client.get("/test")
|
||||||
|
|
||||||
self.assertEqual(response.status, 200)
|
self.assertEqual(response.status, 200)
|
||||||
self.assertEqual(await response.text(), 'Test! wink2 wink1')
|
self.assertEqual(await response.text(), "Test! wink2 wink1")
|
||||||
|
|
||||||
@unittest_run_loop
|
@unittest_run_loop
|
||||||
async def test_test_class_based(self):
|
async def test_test_class_based(self):
|
||||||
response = await self.client.get('/test-class')
|
response = await self.client.get("/test-class")
|
||||||
|
|
||||||
self.assertEqual(response.status, 200)
|
self.assertEqual(response.status, 200)
|
||||||
self.assertEqual(await response.text(), 'Test class-based! wink2 wink1')
|
self.assertEqual(await response.text(), "Test class-based! wink2 wink1")
|
||||||
|
|
||||||
@unittest_run_loop
|
@unittest_run_loop
|
||||||
async def test_endpoints(self):
|
async def test_endpoints(self):
|
||||||
self.assertEqual(str(self.app.router['test'].url_for()), '/test')
|
self.assertEqual(str(self.app.router["test"].url_for()), "/test")
|
||||||
|
|
|
@ -9,16 +9,16 @@ from dependency_injector.ext import flask
|
||||||
|
|
||||||
|
|
||||||
def index():
|
def index():
|
||||||
return 'Hello World!'
|
return "Hello World!"
|
||||||
|
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
return 'Test!'
|
return "Test!"
|
||||||
|
|
||||||
|
|
||||||
class Test(MethodView):
|
class Test(MethodView):
|
||||||
def get(self):
|
def get(self):
|
||||||
return 'Test class-based!'
|
return "Test class-based!"
|
||||||
|
|
||||||
|
|
||||||
class ApplicationContainer(containers.DeclarativeContainer):
|
class ApplicationContainer(containers.DeclarativeContainer):
|
||||||
|
@ -34,9 +34,9 @@ def create_app():
|
||||||
container = ApplicationContainer()
|
container = ApplicationContainer()
|
||||||
app = container.app()
|
app = container.app()
|
||||||
app.container = container
|
app.container = container
|
||||||
app.add_url_rule('/', view_func=container.index_view.as_view())
|
app.add_url_rule("/", view_func=container.index_view.as_view())
|
||||||
app.add_url_rule('/test', 'test-test', view_func=container.test_view.as_view())
|
app.add_url_rule("/test", "test-test", view_func=container.test_view.as_view())
|
||||||
app.add_url_rule('/test-class', view_func=container.test_class_view.as_view('test-class'))
|
app.add_url_rule("/test-class", view_func=container.test_class_view.as_view("test-class"))
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ class ApplicationTests(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.app = create_app()
|
self.app = create_app()
|
||||||
self.app.config['SERVER_NAME'] = 'test-server.com'
|
self.app.config["SERVER_NAME"] = "test-server.com"
|
||||||
self.client = self.app.test_client()
|
self.client = self.app.test_client()
|
||||||
self.client.__enter__()
|
self.client.__enter__()
|
||||||
|
|
||||||
|
@ -52,25 +52,25 @@ class ApplicationTests(unittest.TestCase):
|
||||||
self.client.__exit__(None, None, None)
|
self.client.__exit__(None, None, None)
|
||||||
|
|
||||||
def test_index(self):
|
def test_index(self):
|
||||||
response = self.client.get('/')
|
response = self.client.get("/")
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(response.data, b'Hello World!')
|
self.assertEqual(response.data, b"Hello World!")
|
||||||
|
|
||||||
def test_test(self):
|
def test_test(self):
|
||||||
response = self.client.get('/test')
|
response = self.client.get("/test")
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(response.data, b'Test!')
|
self.assertEqual(response.data, b"Test!")
|
||||||
|
|
||||||
def test_test_class_based(self):
|
def test_test_class_based(self):
|
||||||
response = self.client.get('/test-class')
|
response = self.client.get("/test-class")
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(response.data, b'Test class-based!')
|
self.assertEqual(response.data, b"Test class-based!")
|
||||||
|
|
||||||
def test_endpoints(self):
|
def test_endpoints(self):
|
||||||
with self.app.app_context():
|
with self.app.app_context():
|
||||||
self.assertEqual(url_for('index'), 'http://test-server.com/')
|
self.assertEqual(url_for("index"), "http://test-server.com/")
|
||||||
self.assertEqual(url_for('test-test'), 'http://test-server.com/test')
|
self.assertEqual(url_for("test-test"), "http://test-server.com/test")
|
||||||
self.assertEqual(url_for('test-class'), 'http://test-server.com/test-class')
|
self.assertEqual(url_for("test-class"), "http://test-server.com/test-class")
|
||||||
|
|
|
@ -80,48 +80,48 @@ class _BaseSingletonTestCase(object):
|
||||||
self.assertIsInstance(instance2, Example)
|
self.assertIsInstance(instance2, Example)
|
||||||
|
|
||||||
def test_call_with_init_positional_args(self):
|
def test_call_with_init_positional_args(self):
|
||||||
provider = self.singleton_cls(Example, 'i1', 'i2')
|
provider = self.singleton_cls(Example, "i1", "i2")
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
|
||||||
self.assertEqual(instance1.init_arg1, 'i1')
|
self.assertEqual(instance1.init_arg1, "i1")
|
||||||
self.assertEqual(instance1.init_arg2, 'i2')
|
self.assertEqual(instance1.init_arg2, "i2")
|
||||||
|
|
||||||
self.assertEqual(instance2.init_arg1, 'i1')
|
self.assertEqual(instance2.init_arg1, "i1")
|
||||||
self.assertEqual(instance2.init_arg2, 'i2')
|
self.assertEqual(instance2.init_arg2, "i2")
|
||||||
|
|
||||||
self.assertIs(instance1, instance2)
|
self.assertIs(instance1, instance2)
|
||||||
self.assertIsInstance(instance1, Example)
|
self.assertIsInstance(instance1, Example)
|
||||||
self.assertIsInstance(instance2, Example)
|
self.assertIsInstance(instance2, Example)
|
||||||
|
|
||||||
def test_call_with_init_keyword_args(self):
|
def test_call_with_init_keyword_args(self):
|
||||||
provider = self.singleton_cls(Example, init_arg1='i1', init_arg2='i2')
|
provider = self.singleton_cls(Example, init_arg1="i1", init_arg2="i2")
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
|
||||||
self.assertEqual(instance1.init_arg1, 'i1')
|
self.assertEqual(instance1.init_arg1, "i1")
|
||||||
self.assertEqual(instance1.init_arg2, 'i2')
|
self.assertEqual(instance1.init_arg2, "i2")
|
||||||
|
|
||||||
self.assertEqual(instance2.init_arg1, 'i1')
|
self.assertEqual(instance2.init_arg1, "i1")
|
||||||
self.assertEqual(instance2.init_arg2, 'i2')
|
self.assertEqual(instance2.init_arg2, "i2")
|
||||||
|
|
||||||
self.assertIs(instance1, instance2)
|
self.assertIs(instance1, instance2)
|
||||||
self.assertIsInstance(instance1, Example)
|
self.assertIsInstance(instance1, Example)
|
||||||
self.assertIsInstance(instance2, Example)
|
self.assertIsInstance(instance2, Example)
|
||||||
|
|
||||||
def test_call_with_init_positional_and_keyword_args(self):
|
def test_call_with_init_positional_and_keyword_args(self):
|
||||||
provider = self.singleton_cls(Example, 'i1', init_arg2='i2')
|
provider = self.singleton_cls(Example, "i1", init_arg2="i2")
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
|
||||||
self.assertEqual(instance1.init_arg1, 'i1')
|
self.assertEqual(instance1.init_arg1, "i1")
|
||||||
self.assertEqual(instance1.init_arg2, 'i2')
|
self.assertEqual(instance1.init_arg2, "i2")
|
||||||
|
|
||||||
self.assertEqual(instance2.init_arg1, 'i1')
|
self.assertEqual(instance2.init_arg1, "i1")
|
||||||
self.assertEqual(instance2.init_arg2, 'i2')
|
self.assertEqual(instance2.init_arg2, "i2")
|
||||||
|
|
||||||
self.assertIs(instance1, instance2)
|
self.assertIs(instance1, instance2)
|
||||||
self.assertIsInstance(instance1, Example)
|
self.assertIsInstance(instance1, Example)
|
||||||
|
@ -129,16 +129,16 @@ class _BaseSingletonTestCase(object):
|
||||||
|
|
||||||
def test_call_with_attributes(self):
|
def test_call_with_attributes(self):
|
||||||
provider = self.singleton_cls(Example)
|
provider = self.singleton_cls(Example)
|
||||||
provider.add_attributes(attribute1='a1', attribute2='a2')
|
provider.add_attributes(attribute1="a1", attribute2="a2")
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
|
||||||
self.assertEqual(instance1.attribute1, 'a1')
|
self.assertEqual(instance1.attribute1, "a1")
|
||||||
self.assertEqual(instance1.attribute2, 'a2')
|
self.assertEqual(instance1.attribute2, "a2")
|
||||||
|
|
||||||
self.assertEqual(instance2.attribute1, 'a1')
|
self.assertEqual(instance2.attribute1, "a1")
|
||||||
self.assertEqual(instance2.attribute2, 'a2')
|
self.assertEqual(instance2.attribute2, "a2")
|
||||||
|
|
||||||
self.assertIs(instance1, instance2)
|
self.assertIs(instance1, instance2)
|
||||||
self.assertIsInstance(instance1, Example)
|
self.assertIsInstance(instance1, Example)
|
||||||
|
@ -285,8 +285,8 @@ class _BaseSingletonTestCase(object):
|
||||||
provider.add_kwargs(a1=dependent_provider1, a2=dependent_provider2)
|
provider.add_kwargs(a1=dependent_provider1, a2=dependent_provider2)
|
||||||
|
|
||||||
provider_copy = providers.deepcopy(provider)
|
provider_copy = providers.deepcopy(provider)
|
||||||
dependent_provider_copy1 = provider_copy.kwargs['a1']
|
dependent_provider_copy1 = provider_copy.kwargs["a1"]
|
||||||
dependent_provider_copy2 = provider_copy.kwargs['a2']
|
dependent_provider_copy2 = provider_copy.kwargs["a2"]
|
||||||
|
|
||||||
self.assertNotEqual(provider.kwargs, provider_copy.kwargs)
|
self.assertNotEqual(provider.kwargs, provider_copy.kwargs)
|
||||||
|
|
||||||
|
@ -304,8 +304,8 @@ class _BaseSingletonTestCase(object):
|
||||||
provider.add_attributes(a1=dependent_provider1, a2=dependent_provider2)
|
provider.add_attributes(a1=dependent_provider1, a2=dependent_provider2)
|
||||||
|
|
||||||
provider_copy = providers.deepcopy(provider)
|
provider_copy = providers.deepcopy(provider)
|
||||||
dependent_provider_copy1 = provider_copy.attributes['a1']
|
dependent_provider_copy1 = provider_copy.attributes["a1"]
|
||||||
dependent_provider_copy2 = provider_copy.attributes['a2']
|
dependent_provider_copy2 = provider_copy.attributes["a2"]
|
||||||
|
|
||||||
self.assertNotEqual(provider.attributes, provider_copy.attributes)
|
self.assertNotEqual(provider.attributes, provider_copy.attributes)
|
||||||
|
|
||||||
|
@ -342,8 +342,8 @@ class _BaseSingletonTestCase(object):
|
||||||
self.assertIsNot(provider, provider_copy)
|
self.assertIsNot(provider, provider_copy)
|
||||||
self.assertIsInstance(provider_copy, providers.Singleton)
|
self.assertIsInstance(provider_copy, providers.Singleton)
|
||||||
self.assertIs(provider.args[0], sys.stdin)
|
self.assertIs(provider.args[0], sys.stdin)
|
||||||
self.assertIs(provider.kwargs['a2'], sys.stdout)
|
self.assertIs(provider.kwargs["a2"], sys.stdout)
|
||||||
self.assertIs(provider.attributes['a3'], sys.stderr)
|
self.assertIs(provider.attributes["a3"], sys.stderr)
|
||||||
|
|
||||||
def test_reset(self):
|
def test_reset(self):
|
||||||
provider = self.singleton_cls(object)
|
provider = self.singleton_cls(object)
|
||||||
|
@ -364,12 +364,12 @@ class _BaseSingletonTestCase(object):
|
||||||
|
|
||||||
dependent_instance = dependent_singleton()
|
dependent_instance = dependent_singleton()
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
self.assertIs(instance1['dependency'], dependent_instance)
|
self.assertIs(instance1["dependency"], dependent_instance)
|
||||||
|
|
||||||
provider.reset()
|
provider.reset()
|
||||||
|
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
self.assertIs(instance1['dependency'], dependent_instance)
|
self.assertIs(instance1["dependency"], dependent_instance)
|
||||||
|
|
||||||
self.assertIsNot(instance1, instance2)
|
self.assertIsNot(instance1, instance2)
|
||||||
|
|
||||||
|
@ -396,13 +396,13 @@ class _BaseSingletonTestCase(object):
|
||||||
|
|
||||||
dependent_instance1 = dependent_singleton()
|
dependent_instance1 = dependent_singleton()
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
self.assertIs(instance1['dependency'], dependent_instance1)
|
self.assertIs(instance1["dependency"], dependent_instance1)
|
||||||
|
|
||||||
provider.full_reset()
|
provider.full_reset()
|
||||||
|
|
||||||
dependent_instance2 = dependent_singleton()
|
dependent_instance2 = dependent_singleton()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
self.assertIsNot(instance2['dependency'], dependent_instance1)
|
self.assertIsNot(instance2["dependency"], dependent_instance1)
|
||||||
self.assertIsNot(dependent_instance1, dependent_instance2)
|
self.assertIsNot(dependent_instance1, dependent_instance2)
|
||||||
self.assertIsNot(instance1, instance2)
|
self.assertIsNot(instance1, instance2)
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ import os
|
||||||
_TOP_DIR = os.path.abspath(
|
_TOP_DIR = os.path.abspath(
|
||||||
os.path.sep.join((
|
os.path.sep.join((
|
||||||
os.path.dirname(__file__),
|
os.path.dirname(__file__),
|
||||||
'../',
|
"../",
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
import sys
|
import sys
|
||||||
|
@ -192,7 +192,7 @@ class FactoryTests(AsyncTestCase):
|
||||||
|
|
||||||
def test_injection_error(self):
|
def test_injection_error(self):
|
||||||
async def init_resource():
|
async def init_resource():
|
||||||
raise Exception('Something went wrong')
|
raise Exception("Something went wrong")
|
||||||
|
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
resource_with_error = providers.Resource(init_resource)
|
resource_with_error = providers.Resource(init_resource)
|
||||||
|
@ -207,11 +207,11 @@ class FactoryTests(AsyncTestCase):
|
||||||
|
|
||||||
with self.assertRaises(Exception) as context:
|
with self.assertRaises(Exception) as context:
|
||||||
self._run(container.client())
|
self._run(container.client())
|
||||||
self.assertEqual(str(context.exception), 'Something went wrong')
|
self.assertEqual(str(context.exception), "Something went wrong")
|
||||||
|
|
||||||
def test_injection_runtime_error_async_provides(self):
|
def test_injection_runtime_error_async_provides(self):
|
||||||
async def create_client(*args, **kwargs):
|
async def create_client(*args, **kwargs):
|
||||||
raise Exception('Something went wrong')
|
raise Exception("Something went wrong")
|
||||||
|
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
resource = providers.Resource(init_resource, providers.Object(RESOURCE1))
|
resource = providers.Resource(init_resource, providers.Object(RESOURCE1))
|
||||||
|
@ -226,7 +226,7 @@ class FactoryTests(AsyncTestCase):
|
||||||
|
|
||||||
with self.assertRaises(Exception) as context:
|
with self.assertRaises(Exception) as context:
|
||||||
self._run(container.client())
|
self._run(container.client())
|
||||||
self.assertEqual(str(context.exception), 'Something went wrong')
|
self.assertEqual(str(context.exception), "Something went wrong")
|
||||||
|
|
||||||
def test_injection_call_error_async_provides(self):
|
def test_injection_call_error_async_provides(self):
|
||||||
async def create_client(): # <-- no args defined
|
async def create_client(): # <-- no args defined
|
||||||
|
@ -302,7 +302,7 @@ class FactoryTests(AsyncTestCase):
|
||||||
|
|
||||||
@attribute_set_error.setter
|
@attribute_set_error.setter
|
||||||
def attribute_set_error(self, value):
|
def attribute_set_error(self, value):
|
||||||
raise Exception('Something went wrong')
|
raise Exception("Something went wrong")
|
||||||
|
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
resource = providers.Resource(init_resource, providers.Object(RESOURCE1))
|
resource = providers.Resource(init_resource, providers.Object(RESOURCE1))
|
||||||
|
@ -318,11 +318,11 @@ class FactoryTests(AsyncTestCase):
|
||||||
|
|
||||||
with self.assertRaises(Exception) as context:
|
with self.assertRaises(Exception) as context:
|
||||||
self._run(container.client())
|
self._run(container.client())
|
||||||
self.assertEqual(str(context.exception), 'Something went wrong')
|
self.assertEqual(str(context.exception), "Something went wrong")
|
||||||
|
|
||||||
def test_attributes_injection_runtime_error(self):
|
def test_attributes_injection_runtime_error(self):
|
||||||
async def init_resource():
|
async def init_resource():
|
||||||
raise Exception('Something went wrong')
|
raise Exception("Something went wrong")
|
||||||
|
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
resource = providers.Resource(init_resource)
|
resource = providers.Resource(init_resource)
|
||||||
|
@ -339,7 +339,7 @@ class FactoryTests(AsyncTestCase):
|
||||||
|
|
||||||
with self.assertRaises(Exception) as context:
|
with self.assertRaises(Exception) as context:
|
||||||
self._run(container.client())
|
self._run(container.client())
|
||||||
self.assertEqual(str(context.exception), 'Something went wrong')
|
self.assertEqual(str(context.exception), "Something went wrong")
|
||||||
|
|
||||||
def test_async_instance_and_sync_attributes_injection(self):
|
def test_async_instance_and_sync_attributes_injection(self):
|
||||||
class ContainerWithAttributes(containers.DeclarativeContainer):
|
class ContainerWithAttributes(containers.DeclarativeContainer):
|
||||||
|
@ -406,11 +406,11 @@ class FactoryAggregateTests(AsyncTestCase):
|
||||||
|
|
||||||
self.assertTrue(provider.is_async_mode_undefined())
|
self.assertTrue(provider.is_async_mode_undefined())
|
||||||
|
|
||||||
created_object1 = self._run(provider('object1'))
|
created_object1 = self._run(provider("object1"))
|
||||||
self.assertIs(created_object1, object1)
|
self.assertIs(created_object1, object1)
|
||||||
self.assertTrue(provider.is_async_mode_enabled())
|
self.assertTrue(provider.is_async_mode_enabled())
|
||||||
|
|
||||||
created_object2 = self._run(provider('object2'))
|
created_object2 = self._run(provider("object2"))
|
||||||
self.assertIs(created_object2, object2)
|
self.assertIs(created_object2, object2)
|
||||||
|
|
||||||
|
|
||||||
|
@ -696,7 +696,7 @@ class ProvidedInstanceTests(AsyncTestCase):
|
||||||
class TestContainer(containers.DeclarativeContainer):
|
class TestContainer(containers.DeclarativeContainer):
|
||||||
resource = providers.Resource(init_resource, providers.Object(RESOURCE1))
|
resource = providers.Resource(init_resource, providers.Object(RESOURCE1))
|
||||||
client = providers.Factory(TestClient, resource=resource)
|
client = providers.Factory(TestClient, resource=resource)
|
||||||
service = providers.Factory(TestService, resource=client.provided['resource'])
|
service = providers.Factory(TestService, resource=client.provided["resource"])
|
||||||
|
|
||||||
container = TestContainer()
|
container = TestContainer()
|
||||||
|
|
||||||
|
@ -721,7 +721,7 @@ class ProvidedInstanceTests(AsyncTestCase):
|
||||||
container = TestContainer()
|
container = TestContainer()
|
||||||
|
|
||||||
with self.assertRaises(RuntimeError):
|
with self.assertRaises(RuntimeError):
|
||||||
self._run(container.client.provided['item']())
|
self._run(container.client.provided["item"]())
|
||||||
|
|
||||||
def test_provided_item_undefined_item(self):
|
def test_provided_item_undefined_item(self):
|
||||||
class TestContainer(containers.DeclarativeContainer):
|
class TestContainer(containers.DeclarativeContainer):
|
||||||
|
@ -731,7 +731,7 @@ class ProvidedInstanceTests(AsyncTestCase):
|
||||||
container = TestContainer()
|
container = TestContainer()
|
||||||
|
|
||||||
with self.assertRaises(KeyError):
|
with self.assertRaises(KeyError):
|
||||||
self._run(container.client.provided['item']())
|
self._run(container.client.provided["item"]())
|
||||||
|
|
||||||
def test_provided_method_call(self):
|
def test_provided_method_call(self):
|
||||||
class TestClient:
|
class TestClient:
|
||||||
|
@ -878,15 +878,15 @@ class ListTests(AsyncTestCase):
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
resources = providers.List(
|
resources = providers.List(
|
||||||
providers.Resource(create_resource, 'foo'),
|
providers.Resource(create_resource, "foo"),
|
||||||
providers.Resource(create_resource, 'bar')
|
providers.Resource(create_resource, "bar")
|
||||||
)
|
)
|
||||||
|
|
||||||
container = Container()
|
container = Container()
|
||||||
resources = self._run(container.resources())
|
resources = self._run(container.resources())
|
||||||
|
|
||||||
self.assertEqual(resources[0], 'foo')
|
self.assertEqual(resources[0], "foo")
|
||||||
self.assertEqual(resources[1], 'bar')
|
self.assertEqual(resources[1], "bar")
|
||||||
|
|
||||||
|
|
||||||
class DictTests(AsyncTestCase):
|
class DictTests(AsyncTestCase):
|
||||||
|
@ -898,15 +898,15 @@ class DictTests(AsyncTestCase):
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
resources = providers.Dict(
|
resources = providers.Dict(
|
||||||
foo=providers.Resource(create_resource, 'foo'),
|
foo=providers.Resource(create_resource, "foo"),
|
||||||
bar=providers.Resource(create_resource, 'bar')
|
bar=providers.Resource(create_resource, "bar")
|
||||||
)
|
)
|
||||||
|
|
||||||
container = Container()
|
container = Container()
|
||||||
resources = self._run(container.resources())
|
resources = self._run(container.resources())
|
||||||
|
|
||||||
self.assertEqual(resources['foo'], 'foo')
|
self.assertEqual(resources["foo"], "foo")
|
||||||
self.assertEqual(resources['bar'], 'bar')
|
self.assertEqual(resources["bar"], "bar")
|
||||||
|
|
||||||
|
|
||||||
class OverrideTests(AsyncTestCase):
|
class OverrideTests(AsyncTestCase):
|
||||||
|
@ -1097,10 +1097,10 @@ class AsyncProvidersWithAsyncDependenciesTests(AsyncTestCase):
|
||||||
def test_injections(self):
|
def test_injections(self):
|
||||||
# See: https://github.com/ets-labs/python-dependency-injector/issues/368
|
# See: https://github.com/ets-labs/python-dependency-injector/issues/368
|
||||||
async def async_db_provider():
|
async def async_db_provider():
|
||||||
return {'db': 'ok'}
|
return {"db": "ok"}
|
||||||
|
|
||||||
async def async_service(db=None):
|
async def async_service(db=None):
|
||||||
return {'service': 'ok', 'db': db}
|
return {"service": "ok", "db": db}
|
||||||
|
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
|
@ -1110,7 +1110,7 @@ class AsyncProvidersWithAsyncDependenciesTests(AsyncTestCase):
|
||||||
container = Container()
|
container = Container()
|
||||||
service = self._run(container.service())
|
service = self._run(container.service())
|
||||||
|
|
||||||
self.assertEqual(service, {'service': 'ok', 'db': {'db': 'ok'}})
|
self.assertEqual(service, {"service": "ok", "db": {"db": "ok"}})
|
||||||
|
|
||||||
|
|
||||||
class AsyncProviderWithAwaitableObjectTests(AsyncTestCase):
|
class AsyncProviderWithAwaitableObjectTests(AsyncTestCase):
|
||||||
|
@ -1118,7 +1118,7 @@ class AsyncProviderWithAwaitableObjectTests(AsyncTestCase):
|
||||||
def test(self):
|
def test(self):
|
||||||
class SomeResource:
|
class SomeResource:
|
||||||
def __await__(self):
|
def __await__(self):
|
||||||
raise RuntimeError('Should never happen')
|
raise RuntimeError("Should never happen")
|
||||||
|
|
||||||
async def init_resource():
|
async def init_resource():
|
||||||
pool = SomeResource()
|
pool = SomeResource()
|
||||||
|
@ -1148,7 +1148,7 @@ class AsyncProviderWithAwaitableObjectTests(AsyncTestCase):
|
||||||
def test_without_init_resources(self):
|
def test_without_init_resources(self):
|
||||||
class SomeResource:
|
class SomeResource:
|
||||||
def __await__(self):
|
def __await__(self):
|
||||||
raise RuntimeError('Should never happen')
|
raise RuntimeError("Should never happen")
|
||||||
|
|
||||||
async def init_resource():
|
async def init_resource():
|
||||||
pool = SomeResource()
|
pool = SomeResource()
|
||||||
|
|
|
@ -23,7 +23,7 @@ class ProviderTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_delegate(self):
|
def test_delegate(self):
|
||||||
with warnings.catch_warnings():
|
with warnings.catch_warnings():
|
||||||
warnings.simplefilter('ignore')
|
warnings.simplefilter("ignore")
|
||||||
delegate1 = self.provider.delegate()
|
delegate1 = self.provider.delegate()
|
||||||
delegate2 = self.provider.delegate()
|
delegate2 = self.provider.delegate()
|
||||||
|
|
||||||
|
@ -143,8 +143,8 @@ class ProviderTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
self.assertEqual(repr(self.provider),
|
self.assertEqual(repr(self.provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'Provider() at {0}>'.format(hex(id(self.provider))))
|
"Provider() at {0}>".format(hex(id(self.provider))))
|
||||||
|
|
||||||
|
|
||||||
class ObjectProviderTests(unittest.TestCase):
|
class ObjectProviderTests(unittest.TestCase):
|
||||||
|
@ -225,8 +225,8 @@ class ObjectProviderTests(unittest.TestCase):
|
||||||
some_object = object()
|
some_object = object()
|
||||||
provider = providers.Object(some_object)
|
provider = providers.Object(some_object)
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'Object({0}) at {1}>'.format(
|
"Object({0}) at {1}>".format(
|
||||||
repr(some_object),
|
repr(some_object),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
@ -248,8 +248,8 @@ class SelfProviderTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_set_alt_names(self):
|
def test_set_alt_names(self):
|
||||||
provider = providers.Self()
|
provider = providers.Self()
|
||||||
provider.set_alt_names({'foo', 'bar', 'baz'})
|
provider.set_alt_names({"foo", "bar", "baz"})
|
||||||
self.assertEqual(set(provider.alt_names), {'foo', 'bar', 'baz'})
|
self.assertEqual(set(provider.alt_names), {"foo", "bar", "baz"})
|
||||||
|
|
||||||
def test_deepcopy(self):
|
def test_deepcopy(self):
|
||||||
provider = providers.Self()
|
provider = providers.Self()
|
||||||
|
@ -287,8 +287,8 @@ class SelfProviderTests(unittest.TestCase):
|
||||||
container = containers.DeclarativeContainer()
|
container = containers.DeclarativeContainer()
|
||||||
provider = providers.Self(container)
|
provider = providers.Self(container)
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'Self({0}) at {1}>'.format(
|
"Self({0}) at {1}>".format(
|
||||||
repr(container),
|
repr(container),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
@ -324,8 +324,8 @@ class DelegateTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
self.assertEqual(repr(self.delegate),
|
self.assertEqual(repr(self.delegate),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'Delegate({0}) at {1}>'.format(
|
"Delegate({0}) at {1}>".format(
|
||||||
repr(self.delegated),
|
repr(self.delegated),
|
||||||
hex(id(self.delegate))))
|
hex(id(self.delegate))))
|
||||||
|
|
||||||
|
@ -375,22 +375,22 @@ class DependencyTests(unittest.TestCase):
|
||||||
self.assertIsInstance(self.provider.provided, providers.ProvidedInstance)
|
self.assertIsInstance(self.provider.provided, providers.ProvidedInstance)
|
||||||
|
|
||||||
def test_default(self):
|
def test_default(self):
|
||||||
provider = providers.Dependency(instance_of=dict, default={'foo': 'bar'})
|
provider = providers.Dependency(instance_of=dict, default={"foo": "bar"})
|
||||||
self.assertEqual(provider(), {'foo': 'bar'})
|
self.assertEqual(provider(), {"foo": "bar"})
|
||||||
|
|
||||||
def test_default_attribute(self):
|
def test_default_attribute(self):
|
||||||
provider = providers.Dependency(instance_of=dict, default={'foo': 'bar'})
|
provider = providers.Dependency(instance_of=dict, default={"foo": "bar"})
|
||||||
self.assertEqual(provider.default(), {'foo': 'bar'})
|
self.assertEqual(provider.default(), {"foo": "bar"})
|
||||||
|
|
||||||
def test_default_provider(self):
|
def test_default_provider(self):
|
||||||
provider = providers.Dependency(instance_of=dict, default=providers.Factory(dict, foo='bar'))
|
provider = providers.Dependency(instance_of=dict, default=providers.Factory(dict, foo="bar"))
|
||||||
self.assertEqual(provider.default(), {'foo': 'bar'})
|
self.assertEqual(provider.default(), {"foo": "bar"})
|
||||||
|
|
||||||
def test_default_attribute_provider(self):
|
def test_default_attribute_provider(self):
|
||||||
default = providers.Factory(dict, foo='bar')
|
default = providers.Factory(dict, foo="bar")
|
||||||
provider = providers.Dependency(instance_of=dict, default=default)
|
provider = providers.Dependency(instance_of=dict, default=default)
|
||||||
|
|
||||||
self.assertEqual(provider.default(), {'foo': 'bar'})
|
self.assertEqual(provider.default(), {"foo": "bar"})
|
||||||
self.assertIs(provider.default, default)
|
self.assertIs(provider.default, default)
|
||||||
|
|
||||||
def test_is_defined(self):
|
def test_is_defined(self):
|
||||||
|
@ -399,11 +399,11 @@ class DependencyTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_is_defined_when_overridden(self):
|
def test_is_defined_when_overridden(self):
|
||||||
provider = providers.Dependency()
|
provider = providers.Dependency()
|
||||||
provider.override('value')
|
provider.override("value")
|
||||||
self.assertTrue(provider.is_defined)
|
self.assertTrue(provider.is_defined)
|
||||||
|
|
||||||
def test_is_defined_with_default(self):
|
def test_is_defined_with_default(self):
|
||||||
provider = providers.Dependency(default='value')
|
provider = providers.Dependency(default="value")
|
||||||
self.assertTrue(provider.is_defined)
|
self.assertTrue(provider.is_defined)
|
||||||
|
|
||||||
def test_call_overridden(self):
|
def test_call_overridden(self):
|
||||||
|
@ -417,7 +417,7 @@ class DependencyTests(unittest.TestCase):
|
||||||
def test_call_undefined(self):
|
def test_call_undefined(self):
|
||||||
with self.assertRaises(errors.Error) as context:
|
with self.assertRaises(errors.Error) as context:
|
||||||
self.provider()
|
self.provider()
|
||||||
self.assertEqual(str(context.exception), 'Dependency is not defined')
|
self.assertEqual(str(context.exception), "Dependency is not defined")
|
||||||
|
|
||||||
def test_call_undefined_error_message_with_container_instance_parent(self):
|
def test_call_undefined_error_message_with_container_instance_parent(self):
|
||||||
class UserService:
|
class UserService:
|
||||||
|
@ -437,7 +437,7 @@ class DependencyTests(unittest.TestCase):
|
||||||
with self.assertRaises(errors.Error) as context:
|
with self.assertRaises(errors.Error) as context:
|
||||||
container.user_service()
|
container.user_service()
|
||||||
|
|
||||||
self.assertEqual(str(context.exception), 'Dependency "Container.database" is not defined')
|
self.assertEqual(str(context.exception), "Dependency \"Container.database\" is not defined")
|
||||||
|
|
||||||
def test_call_undefined_error_message_with_container_provider_parent_deep(self):
|
def test_call_undefined_error_message_with_container_provider_parent_deep(self):
|
||||||
class Database:
|
class Database:
|
||||||
|
@ -473,7 +473,7 @@ class DependencyTests(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(context.exception),
|
str(context.exception),
|
||||||
'Dependency "Container.services.gateways.database_client" is not defined',
|
"Dependency \"Container.services.gateways.database_client\" is not defined",
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_call_undefined_error_message_with_dependenciescontainer_provider_parent(self):
|
def test_call_undefined_error_message_with_dependenciescontainer_provider_parent(self):
|
||||||
|
@ -496,7 +496,7 @@ class DependencyTests(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(context.exception),
|
str(context.exception),
|
||||||
'Dependency "Services.gateways.database_client" is not defined',
|
"Dependency \"Services.gateways.database_client\" is not defined",
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_assign_parent(self):
|
def test_assign_parent(self):
|
||||||
|
@ -511,13 +511,13 @@ class DependencyTests(unittest.TestCase):
|
||||||
container = containers.DynamicContainer()
|
container = containers.DynamicContainer()
|
||||||
provider = providers.Dependency()
|
provider = providers.Dependency()
|
||||||
container.name = provider
|
container.name = provider
|
||||||
self.assertEqual(provider.parent_name, 'name')
|
self.assertEqual(provider.parent_name, "name")
|
||||||
|
|
||||||
def test_parent_name_with_deep_parenting(self):
|
def test_parent_name_with_deep_parenting(self):
|
||||||
provider = providers.Dependency()
|
provider = providers.Dependency()
|
||||||
container = providers.DependenciesContainer(name=provider)
|
container = providers.DependenciesContainer(name=provider)
|
||||||
_ = providers.DependenciesContainer(container=container)
|
_ = providers.DependenciesContainer(container=container)
|
||||||
self.assertEqual(provider.parent_name, 'container.name')
|
self.assertEqual(provider.parent_name, "container.name")
|
||||||
|
|
||||||
def test_parent_name_is_none(self):
|
def test_parent_name_is_none(self):
|
||||||
provider = providers.DependenciesContainer()
|
provider = providers.DependenciesContainer()
|
||||||
|
@ -541,18 +541,18 @@ class DependencyTests(unittest.TestCase):
|
||||||
default = providers.Configuration()
|
default = providers.Configuration()
|
||||||
|
|
||||||
provider = providers.Dependency(default=default)
|
provider = providers.Dependency(default=default)
|
||||||
provider.from_dict({'foo': 'bar'})
|
provider.from_dict({"foo": "bar"})
|
||||||
|
|
||||||
self.assertEqual(default(), {'foo': 'bar'})
|
self.assertEqual(default(), {"foo": "bar"})
|
||||||
|
|
||||||
def test_forward_attr_to_overriding(self):
|
def test_forward_attr_to_overriding(self):
|
||||||
overriding = providers.Configuration()
|
overriding = providers.Configuration()
|
||||||
|
|
||||||
provider = providers.Dependency()
|
provider = providers.Dependency()
|
||||||
provider.override(overriding)
|
provider.override(overriding)
|
||||||
provider.from_dict({'foo': 'bar'})
|
provider.from_dict({"foo": "bar"})
|
||||||
|
|
||||||
self.assertEqual(overriding(), {'foo': 'bar'})
|
self.assertEqual(overriding(), {"foo": "bar"})
|
||||||
|
|
||||||
def test_forward_attr_to_none(self):
|
def test_forward_attr_to_none(self):
|
||||||
provider = providers.Dependency()
|
provider = providers.Dependency()
|
||||||
|
@ -592,7 +592,7 @@ class DependencyTests(unittest.TestCase):
|
||||||
self.assertIsInstance(overriding_provider_copy, providers.Provider)
|
self.assertIsInstance(overriding_provider_copy, providers.Provider)
|
||||||
|
|
||||||
def test_deep_copy_default_object(self):
|
def test_deep_copy_default_object(self):
|
||||||
default = {'foo': 'bar'}
|
default = {"foo": "bar"}
|
||||||
provider = providers.Dependency(dict, default=default)
|
provider = providers.Dependency(dict, default=default)
|
||||||
|
|
||||||
provider_copy = providers.deepcopy(provider)
|
provider_copy = providers.deepcopy(provider)
|
||||||
|
@ -607,12 +607,12 @@ class DependencyTests(unittest.TestCase):
|
||||||
|
|
||||||
provider_copy = providers.deepcopy(provider)
|
provider_copy = providers.deepcopy(provider)
|
||||||
|
|
||||||
self.assertEqual(provider_copy(), {'foo': bar})
|
self.assertEqual(provider_copy(), {"foo": bar})
|
||||||
self.assertEqual(provider_copy.default(), {'foo': bar})
|
self.assertEqual(provider_copy.default(), {"foo": bar})
|
||||||
self.assertIs(provider_copy()['foo'], bar)
|
self.assertIs(provider_copy()["foo"], bar)
|
||||||
|
|
||||||
def test_with_container_default_object(self):
|
def test_with_container_default_object(self):
|
||||||
default = {'foo': 'bar'}
|
default = {"foo": "bar"}
|
||||||
|
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
provider = providers.Dependency(dict, default=default)
|
provider = providers.Dependency(dict, default=default)
|
||||||
|
@ -630,9 +630,9 @@ class DependencyTests(unittest.TestCase):
|
||||||
|
|
||||||
container = Container()
|
container = Container()
|
||||||
|
|
||||||
self.assertEqual(container.provider(), {'foo': bar})
|
self.assertEqual(container.provider(), {"foo": bar})
|
||||||
self.assertEqual(container.provider.default(), {'foo': bar})
|
self.assertEqual(container.provider.default(), {"foo": bar})
|
||||||
self.assertIs(container.provider()['foo'], bar)
|
self.assertIs(container.provider()["foo"], bar)
|
||||||
|
|
||||||
def test_with_container_default_provider_with_overriding(self):
|
def test_with_container_default_provider_with_overriding(self):
|
||||||
bar = object()
|
bar = object()
|
||||||
|
@ -643,14 +643,14 @@ class DependencyTests(unittest.TestCase):
|
||||||
|
|
||||||
container = Container(provider=providers.Factory(dict, foo=providers.Object(baz)))
|
container = Container(provider=providers.Factory(dict, foo=providers.Object(baz)))
|
||||||
|
|
||||||
self.assertEqual(container.provider(), {'foo': baz})
|
self.assertEqual(container.provider(), {"foo": baz})
|
||||||
self.assertEqual(container.provider.default(), {'foo': bar})
|
self.assertEqual(container.provider.default(), {"foo": bar})
|
||||||
self.assertIs(container.provider()['foo'], baz)
|
self.assertIs(container.provider()["foo"], baz)
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
self.assertEqual(repr(self.provider),
|
self.assertEqual(repr(self.provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'Dependency({0}) at {1}>'.format(
|
"Dependency({0}) at {1}>".format(
|
||||||
repr(list),
|
repr(list),
|
||||||
hex(id(self.provider))))
|
hex(id(self.provider))))
|
||||||
|
|
||||||
|
@ -661,8 +661,8 @@ class DependencyTests(unittest.TestCase):
|
||||||
container = Container()
|
container = Container()
|
||||||
|
|
||||||
self.assertEqual(repr(container.dependency),
|
self.assertEqual(repr(container.dependency),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'Dependency({0}) at {1}, container name: "Container.dependency">'.format(
|
"Dependency({0}) at {1}, container name: \"Container.dependency\">".format(
|
||||||
repr(int),
|
repr(int),
|
||||||
hex(id(container.dependency))))
|
hex(id(container.dependency))))
|
||||||
|
|
||||||
|
@ -687,7 +687,7 @@ class DependenciesContainerTests(unittest.TestCase):
|
||||||
self.container = self.Container()
|
self.container = self.Container()
|
||||||
|
|
||||||
def test_getattr(self):
|
def test_getattr(self):
|
||||||
has_dependency = hasattr(self.provider, 'dependency')
|
has_dependency = hasattr(self.provider, "dependency")
|
||||||
dependency = self.provider.dependency
|
dependency = self.provider.dependency
|
||||||
|
|
||||||
self.assertIsInstance(dependency, providers.Dependency)
|
self.assertIsInstance(dependency, providers.Dependency)
|
||||||
|
@ -706,8 +706,8 @@ class DependenciesContainerTests(unittest.TestCase):
|
||||||
def test_providers(self):
|
def test_providers(self):
|
||||||
dependency1 = self.provider.dependency1
|
dependency1 = self.provider.dependency1
|
||||||
dependency2 = self.provider.dependency2
|
dependency2 = self.provider.dependency2
|
||||||
self.assertEqual(self.provider.providers, {'dependency1': dependency1,
|
self.assertEqual(self.provider.providers, {"dependency1": dependency1,
|
||||||
'dependency2': dependency2})
|
"dependency2": dependency2})
|
||||||
|
|
||||||
def test_override(self):
|
def test_override(self):
|
||||||
dependency = self.provider.dependency
|
dependency = self.provider.dependency
|
||||||
|
@ -744,13 +744,13 @@ class DependenciesContainerTests(unittest.TestCase):
|
||||||
container = containers.DynamicContainer()
|
container = containers.DynamicContainer()
|
||||||
provider = providers.DependenciesContainer()
|
provider = providers.DependenciesContainer()
|
||||||
container.name = provider
|
container.name = provider
|
||||||
self.assertEqual(provider.parent_name, 'name')
|
self.assertEqual(provider.parent_name, "name")
|
||||||
|
|
||||||
def test_parent_name_with_deep_parenting(self):
|
def test_parent_name_with_deep_parenting(self):
|
||||||
provider = providers.DependenciesContainer()
|
provider = providers.DependenciesContainer()
|
||||||
container = providers.DependenciesContainer(name=provider)
|
container = providers.DependenciesContainer(name=provider)
|
||||||
_ = providers.DependenciesContainer(container=container)
|
_ = providers.DependenciesContainer(container=container)
|
||||||
self.assertEqual(provider.parent_name, 'container.name')
|
self.assertEqual(provider.parent_name, "container.name")
|
||||||
|
|
||||||
def test_parent_name_is_none(self):
|
def test_parent_name_is_none(self):
|
||||||
provider = providers.DependenciesContainer()
|
provider = providers.DependenciesContainer()
|
||||||
|
@ -783,7 +783,7 @@ class DependenciesContainerTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_resolve_provider_name(self):
|
def test_resolve_provider_name(self):
|
||||||
container = providers.DependenciesContainer()
|
container = providers.DependenciesContainer()
|
||||||
self.assertEqual(container.resolve_provider_name(container.name), 'name')
|
self.assertEqual(container.resolve_provider_name(container.name), "name")
|
||||||
|
|
||||||
def test_resolve_provider_name_no_provider(self):
|
def test_resolve_provider_name_no_provider(self):
|
||||||
container = providers.DependenciesContainer()
|
container = providers.DependenciesContainer()
|
||||||
|
|
|
@ -154,8 +154,8 @@ class CallableTests(unittest.TestCase):
|
||||||
provider.add_kwargs(a1=dependent_provider1, a2=dependent_provider2)
|
provider.add_kwargs(a1=dependent_provider1, a2=dependent_provider2)
|
||||||
|
|
||||||
provider_copy = providers.deepcopy(provider)
|
provider_copy = providers.deepcopy(provider)
|
||||||
dependent_provider_copy1 = provider_copy.kwargs['a1']
|
dependent_provider_copy1 = provider_copy.kwargs["a1"]
|
||||||
dependent_provider_copy2 = provider_copy.kwargs['a2']
|
dependent_provider_copy2 = provider_copy.kwargs["a2"]
|
||||||
|
|
||||||
self.assertNotEqual(provider.kwargs, provider_copy.kwargs)
|
self.assertNotEqual(provider.kwargs, provider_copy.kwargs)
|
||||||
|
|
||||||
|
@ -193,14 +193,14 @@ class CallableTests(unittest.TestCase):
|
||||||
self.assertIsNot(provider, provider_copy)
|
self.assertIsNot(provider, provider_copy)
|
||||||
self.assertIsInstance(provider_copy, providers.Callable)
|
self.assertIsInstance(provider_copy, providers.Callable)
|
||||||
self.assertIs(provider.args[0], sys.stdin)
|
self.assertIs(provider.args[0], sys.stdin)
|
||||||
self.assertIs(provider.kwargs['a2'], sys.stdout)
|
self.assertIs(provider.kwargs["a2"], sys.stdout)
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
provider = providers.Callable(_example)
|
provider = providers.Callable(_example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'Callable({0}) at {1}>'.format(
|
"Callable({0}) at {1}>".format(
|
||||||
repr(_example),
|
repr(_example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
@ -223,8 +223,8 @@ class DelegatedCallableTests(unittest.TestCase):
|
||||||
provider = providers.DelegatedCallable(_example)
|
provider = providers.DelegatedCallable(_example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'DelegatedCallable({0}) at {1}>'.format(
|
"DelegatedCallable({0}) at {1}>".format(
|
||||||
repr(_example),
|
repr(_example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
@ -275,8 +275,8 @@ class AbstractCallableTests(unittest.TestCase):
|
||||||
provider = providers.AbstractCallable(_example)
|
provider = providers.AbstractCallable(_example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'AbstractCallable({0}) at {1}>'.format(
|
"AbstractCallable({0}) at {1}>".format(
|
||||||
repr(_example),
|
repr(_example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -7,20 +7,20 @@ import unittest
|
||||||
from dependency_injector import containers, providers, errors
|
from dependency_injector import containers, providers, errors
|
||||||
|
|
||||||
|
|
||||||
TEST_VALUE_1 = 'core_section_value1'
|
TEST_VALUE_1 = "core_section_value1"
|
||||||
TEST_CONFIG_1 = {
|
TEST_CONFIG_1 = {
|
||||||
'core': {
|
"core": {
|
||||||
'section': {
|
"section": {
|
||||||
'value': TEST_VALUE_1,
|
"value": TEST_VALUE_1,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_VALUE_2 = 'core_section_value2'
|
TEST_VALUE_2 = "core_section_value2"
|
||||||
TEST_CONFIG_2 = {
|
TEST_CONFIG_2 = {
|
||||||
'core': {
|
"core": {
|
||||||
'section': {
|
"section": {
|
||||||
'value': TEST_VALUE_2,
|
"value": TEST_VALUE_2,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -31,12 +31,12 @@ def _copied(value):
|
||||||
|
|
||||||
|
|
||||||
class TestCore(containers.DeclarativeContainer):
|
class TestCore(containers.DeclarativeContainer):
|
||||||
config = providers.Configuration('core')
|
config = providers.Configuration("core")
|
||||||
value_getter = providers.Callable(lambda _: _, config.section.value)
|
value_getter = providers.Callable(lambda _: _, config.section.value)
|
||||||
|
|
||||||
|
|
||||||
class TestApplication(containers.DeclarativeContainer):
|
class TestApplication(containers.DeclarativeContainer):
|
||||||
config = providers.Configuration('config')
|
config = providers.Configuration("config")
|
||||||
core = providers.Container(TestCore, config=config.core)
|
core = providers.Container(TestCore, config=config.core)
|
||||||
dict_factory = providers.Factory(dict, value=core.value_getter)
|
dict_factory = providers.Factory(dict, value=core.value_getter)
|
||||||
|
|
||||||
|
@ -45,22 +45,22 @@ class ContainerTests(unittest.TestCase):
|
||||||
|
|
||||||
def test(self):
|
def test(self):
|
||||||
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
||||||
self.assertEqual(application.dict_factory(), {'value': TEST_VALUE_1})
|
self.assertEqual(application.dict_factory(), {"value": TEST_VALUE_1})
|
||||||
|
|
||||||
def test_double_override(self):
|
def test_double_override(self):
|
||||||
application = TestApplication()
|
application = TestApplication()
|
||||||
application.config.override(_copied(TEST_CONFIG_1))
|
application.config.override(_copied(TEST_CONFIG_1))
|
||||||
application.config.override(_copied(TEST_CONFIG_2))
|
application.config.override(_copied(TEST_CONFIG_2))
|
||||||
self.assertEqual(application.dict_factory(), {'value': TEST_VALUE_2})
|
self.assertEqual(application.dict_factory(), {"value": TEST_VALUE_2})
|
||||||
|
|
||||||
def test_override(self):
|
def test_override(self):
|
||||||
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
|
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
|
||||||
class D(containers.DeclarativeContainer):
|
class D(containers.DeclarativeContainer):
|
||||||
foo = providers.Object('foo')
|
foo = providers.Object("foo")
|
||||||
|
|
||||||
class A(containers.DeclarativeContainer):
|
class A(containers.DeclarativeContainer):
|
||||||
d = providers.DependenciesContainer()
|
d = providers.DependenciesContainer()
|
||||||
bar = providers.Callable(lambda f: f + '++', d.foo.provided)
|
bar = providers.Callable(lambda f: f + "++", d.foo.provided)
|
||||||
|
|
||||||
class B(containers.DeclarativeContainer):
|
class B(containers.DeclarativeContainer):
|
||||||
d = providers.Container(D)
|
d = providers.Container(D)
|
||||||
|
@ -69,7 +69,7 @@ class ContainerTests(unittest.TestCase):
|
||||||
|
|
||||||
b = B(d=D())
|
b = B(d=D())
|
||||||
result = b.a().bar()
|
result = b.a().bar()
|
||||||
self.assertEqual(result, 'foo++')
|
self.assertEqual(result, "foo++")
|
||||||
|
|
||||||
def test_override_not_root_provider(self):
|
def test_override_not_root_provider(self):
|
||||||
# See: https://github.com/ets-labs/python-dependency-injector/issues/379
|
# See: https://github.com/ets-labs/python-dependency-injector/issues/379
|
||||||
|
@ -102,37 +102,37 @@ class ContainerTests(unittest.TestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
container_using_factory = TestContainer(settings=dict(
|
container_using_factory = TestContainer(settings=dict(
|
||||||
container='using_factory',
|
container="using_factory",
|
||||||
foo='bar'
|
foo="bar"
|
||||||
))
|
))
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
container_using_factory.root_container().print_settings(),
|
container_using_factory.root_container().print_settings(),
|
||||||
{'container': 'using_factory', 'foo': 'bar'},
|
{"container": "using_factory", "foo": "bar"},
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
container_using_factory.not_root_container().print_settings(),
|
container_using_factory.not_root_container().print_settings(),
|
||||||
{'container': 'using_factory', 'foo': 'bar'},
|
{"container": "using_factory", "foo": "bar"},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
container_using_container = TestContainer(settings=dict(
|
container_using_container = TestContainer(settings=dict(
|
||||||
container='using_container',
|
container="using_container",
|
||||||
foo='bar'
|
foo="bar"
|
||||||
))
|
))
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
container_using_container.root_container().print_settings(),
|
container_using_container.root_container().print_settings(),
|
||||||
{'container': 'using_container', 'foo': 'bar'},
|
{"container": "using_container", "foo": "bar"},
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
container_using_container.not_root_container().print_settings(),
|
container_using_container.not_root_container().print_settings(),
|
||||||
{'container': 'using_container', 'foo': 'bar'},
|
{"container": "using_container", "foo": "bar"},
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_override_by_not_a_container(self):
|
def test_override_by_not_a_container(self):
|
||||||
provider = providers.Container(TestCore)
|
provider = providers.Container(TestCore)
|
||||||
|
|
||||||
with self.assertRaises(errors.Error):
|
with self.assertRaises(errors.Error):
|
||||||
provider.override(providers.Object('foo'))
|
provider.override(providers.Object("foo"))
|
||||||
|
|
||||||
def test_lazy_overriding(self):
|
def test_lazy_overriding(self):
|
||||||
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
|
# See: https://github.com/ets-labs/python-dependency-injector/issues/354
|
||||||
|
@ -151,7 +151,7 @@ class ContainerTests(unittest.TestCase):
|
||||||
|
|
||||||
b = B(d=D())
|
b = B(d=D())
|
||||||
result = b.a().bar()
|
result = b.a().bar()
|
||||||
self.assertEqual(result, 'foo++')
|
self.assertEqual(result, "foo++")
|
||||||
|
|
||||||
def test_lazy_overriding_deep(self):
|
def test_lazy_overriding_deep(self):
|
||||||
# Extended version of test_lazy_overriding()
|
# Extended version of test_lazy_overriding()
|
||||||
|
@ -174,49 +174,49 @@ class ContainerTests(unittest.TestCase):
|
||||||
|
|
||||||
b = B(d=D())
|
b = B(d=D())
|
||||||
result = b.a().c().bar()
|
result = b.a().c().bar()
|
||||||
self.assertEqual(result, 'foo++')
|
self.assertEqual(result, "foo++")
|
||||||
|
|
||||||
def test_reset_last_overriding(self):
|
def test_reset_last_overriding(self):
|
||||||
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
||||||
application.core.override(TestCore(config=_copied(TEST_CONFIG_2['core'])))
|
application.core.override(TestCore(config=_copied(TEST_CONFIG_2["core"])))
|
||||||
|
|
||||||
application.core.reset_last_overriding()
|
application.core.reset_last_overriding()
|
||||||
|
|
||||||
self.assertEqual(application.dict_factory(), {'value': TEST_VALUE_1})
|
self.assertEqual(application.dict_factory(), {"value": TEST_VALUE_1})
|
||||||
|
|
||||||
def test_reset_last_overriding_only_overridden(self):
|
def test_reset_last_overriding_only_overridden(self):
|
||||||
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
||||||
application.core.override(providers.DependenciesContainer(config=_copied(TEST_CONFIG_2['core'])))
|
application.core.override(providers.DependenciesContainer(config=_copied(TEST_CONFIG_2["core"])))
|
||||||
|
|
||||||
application.core.reset_last_overriding()
|
application.core.reset_last_overriding()
|
||||||
|
|
||||||
self.assertEqual(application.dict_factory(), {'value': TEST_VALUE_1})
|
self.assertEqual(application.dict_factory(), {"value": TEST_VALUE_1})
|
||||||
|
|
||||||
def test_override_context_manager(self):
|
def test_override_context_manager(self):
|
||||||
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
||||||
overriding_core = TestCore(config=_copied(TEST_CONFIG_2['core']))
|
overriding_core = TestCore(config=_copied(TEST_CONFIG_2["core"]))
|
||||||
|
|
||||||
with application.core.override(overriding_core) as context_core:
|
with application.core.override(overriding_core) as context_core:
|
||||||
self.assertEqual(application.dict_factory(), {'value': TEST_VALUE_2})
|
self.assertEqual(application.dict_factory(), {"value": TEST_VALUE_2})
|
||||||
self.assertIs(context_core(), overriding_core)
|
self.assertIs(context_core(), overriding_core)
|
||||||
|
|
||||||
self.assertEqual(application.dict_factory(), {'value': TEST_VALUE_1})
|
self.assertEqual(application.dict_factory(), {"value": TEST_VALUE_1})
|
||||||
|
|
||||||
def test_reset_override(self):
|
def test_reset_override(self):
|
||||||
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
||||||
application.core.override(TestCore(config=_copied(TEST_CONFIG_2['core'])))
|
application.core.override(TestCore(config=_copied(TEST_CONFIG_2["core"])))
|
||||||
|
|
||||||
application.core.reset_override()
|
application.core.reset_override()
|
||||||
|
|
||||||
self.assertEqual(application.dict_factory(), {'value': None})
|
self.assertEqual(application.dict_factory(), {"value": None})
|
||||||
|
|
||||||
def test_reset_override_only_overridden(self):
|
def test_reset_override_only_overridden(self):
|
||||||
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
application = TestApplication(config=_copied(TEST_CONFIG_1))
|
||||||
application.core.override(providers.DependenciesContainer(config=_copied(TEST_CONFIG_2['core'])))
|
application.core.override(providers.DependenciesContainer(config=_copied(TEST_CONFIG_2["core"])))
|
||||||
|
|
||||||
application.core.reset_override()
|
application.core.reset_override()
|
||||||
|
|
||||||
self.assertEqual(application.dict_factory(), {'value': None})
|
self.assertEqual(application.dict_factory(), {"value": None})
|
||||||
|
|
||||||
def test_assign_parent(self):
|
def test_assign_parent(self):
|
||||||
parent = providers.DependenciesContainer()
|
parent = providers.DependenciesContainer()
|
||||||
|
@ -230,13 +230,13 @@ class ContainerTests(unittest.TestCase):
|
||||||
container = containers.DynamicContainer()
|
container = containers.DynamicContainer()
|
||||||
provider = providers.Container(TestCore)
|
provider = providers.Container(TestCore)
|
||||||
container.name = provider
|
container.name = provider
|
||||||
self.assertEqual(provider.parent_name, 'name')
|
self.assertEqual(provider.parent_name, "name")
|
||||||
|
|
||||||
def test_parent_name_with_deep_parenting(self):
|
def test_parent_name_with_deep_parenting(self):
|
||||||
provider = providers.Container(TestCore)
|
provider = providers.Container(TestCore)
|
||||||
container = providers.DependenciesContainer(name=provider)
|
container = providers.DependenciesContainer(name=provider)
|
||||||
_ = providers.DependenciesContainer(container=container)
|
_ = providers.DependenciesContainer(container=container)
|
||||||
self.assertEqual(provider.parent_name, 'container.name')
|
self.assertEqual(provider.parent_name, "container.name")
|
||||||
|
|
||||||
def test_parent_name_is_none(self):
|
def test_parent_name_is_none(self):
|
||||||
provider = providers.Container(TestCore)
|
provider = providers.Container(TestCore)
|
||||||
|
@ -258,7 +258,7 @@ class ContainerTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_resolve_provider_name(self):
|
def test_resolve_provider_name(self):
|
||||||
container = providers.Container(TestCore)
|
container = providers.Container(TestCore)
|
||||||
self.assertEqual(container.resolve_provider_name(container.value_getter), 'value_getter')
|
self.assertEqual(container.resolve_provider_name(container.value_getter), "value_getter")
|
||||||
|
|
||||||
def test_resolve_provider_name_no_provider(self):
|
def test_resolve_provider_name_no_provider(self):
|
||||||
container = providers.Container(TestCore)
|
container = providers.Container(TestCore)
|
||||||
|
|
|
@ -14,7 +14,7 @@ import os
|
||||||
_TOP_DIR = os.path.abspath(
|
_TOP_DIR = os.path.abspath(
|
||||||
os.path.sep.join((
|
os.path.sep.join((
|
||||||
os.path.dirname(__file__),
|
os.path.dirname(__file__),
|
||||||
'../',
|
"../",
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
import sys
|
import sys
|
||||||
|
@ -172,8 +172,8 @@ class CoroutineTests(AsyncTestCase):
|
||||||
provider.add_kwargs(a1=dependent_provider1, a2=dependent_provider2)
|
provider.add_kwargs(a1=dependent_provider1, a2=dependent_provider2)
|
||||||
|
|
||||||
provider_copy = providers.deepcopy(provider)
|
provider_copy = providers.deepcopy(provider)
|
||||||
dependent_provider_copy1 = provider_copy.kwargs['a1']
|
dependent_provider_copy1 = provider_copy.kwargs["a1"]
|
||||||
dependent_provider_copy2 = provider_copy.kwargs['a2']
|
dependent_provider_copy2 = provider_copy.kwargs["a2"]
|
||||||
|
|
||||||
self.assertNotEqual(provider.kwargs, provider_copy.kwargs)
|
self.assertNotEqual(provider.kwargs, provider_copy.kwargs)
|
||||||
|
|
||||||
|
@ -205,8 +205,8 @@ class CoroutineTests(AsyncTestCase):
|
||||||
provider = providers.Coroutine(_example)
|
provider = providers.Coroutine(_example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'Coroutine({0}) at {1}>'.format(
|
"Coroutine({0}) at {1}>".format(
|
||||||
repr(_example),
|
repr(_example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
@ -229,8 +229,8 @@ class DelegatedCoroutineTests(unittest.TestCase):
|
||||||
provider = providers.DelegatedCoroutine(_example)
|
provider = providers.DelegatedCoroutine(_example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'DelegatedCoroutine({0}) at {1}>'.format(
|
"DelegatedCoroutine({0}) at {1}>".format(
|
||||||
repr(_example),
|
repr(_example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
@ -243,11 +243,11 @@ class AbstractCoroutineTests(AsyncTestCase):
|
||||||
|
|
||||||
def test_call_overridden_by_coroutine(self):
|
def test_call_overridden_by_coroutine(self):
|
||||||
with warnings.catch_warnings():
|
with warnings.catch_warnings():
|
||||||
warnings.simplefilter('ignore')
|
warnings.simplefilter("ignore")
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def _abstract_example():
|
def _abstract_example():
|
||||||
raise RuntimeError('Should not be raised')
|
raise RuntimeError("Should not be raised")
|
||||||
|
|
||||||
provider = providers.AbstractCoroutine(_abstract_example)
|
provider = providers.AbstractCoroutine(_abstract_example)
|
||||||
provider.override(providers.Coroutine(_example))
|
provider.override(providers.Coroutine(_example))
|
||||||
|
@ -256,11 +256,11 @@ class AbstractCoroutineTests(AsyncTestCase):
|
||||||
|
|
||||||
def test_call_overridden_by_delegated_coroutine(self):
|
def test_call_overridden_by_delegated_coroutine(self):
|
||||||
with warnings.catch_warnings():
|
with warnings.catch_warnings():
|
||||||
warnings.simplefilter('ignore')
|
warnings.simplefilter("ignore")
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def _abstract_example():
|
def _abstract_example():
|
||||||
raise RuntimeError('Should not be raised')
|
raise RuntimeError("Should not be raised")
|
||||||
|
|
||||||
provider = providers.AbstractCoroutine(_abstract_example)
|
provider = providers.AbstractCoroutine(_abstract_example)
|
||||||
provider.override(providers.DelegatedCoroutine(_example))
|
provider.override(providers.DelegatedCoroutine(_example))
|
||||||
|
@ -289,8 +289,8 @@ class AbstractCoroutineTests(AsyncTestCase):
|
||||||
provider = providers.AbstractCoroutine(_example)
|
provider = providers.AbstractCoroutine(_example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'AbstractCoroutine({0}) at {1}>'.format(
|
"AbstractCoroutine({0}) at {1}>".format(
|
||||||
repr(_example),
|
repr(_example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
|
|
@ -19,110 +19,110 @@ class DictTests(unittest.TestCase):
|
||||||
def test_init_with_non_string_keys(self):
|
def test_init_with_non_string_keys(self):
|
||||||
a1 = object()
|
a1 = object()
|
||||||
a2 = object()
|
a2 = object()
|
||||||
provider = providers.Dict({a1: 'i1', a2: 'i2'})
|
provider = providers.Dict({a1: "i1", a2: "i2"})
|
||||||
|
|
||||||
dict1 = provider()
|
dict1 = provider()
|
||||||
dict2 = provider()
|
dict2 = provider()
|
||||||
|
|
||||||
self.assertEqual(dict1, {a1: 'i1', a2: 'i2'})
|
self.assertEqual(dict1, {a1: "i1", a2: "i2"})
|
||||||
self.assertEqual(dict2, {a1: 'i1', a2: 'i2'})
|
self.assertEqual(dict2, {a1: "i1", a2: "i2"})
|
||||||
|
|
||||||
self.assertIsNot(dict1, dict2)
|
self.assertIsNot(dict1, dict2)
|
||||||
|
|
||||||
def test_init_with_string_and_non_string_keys(self):
|
def test_init_with_string_and_non_string_keys(self):
|
||||||
a1 = object()
|
a1 = object()
|
||||||
provider = providers.Dict({a1: 'i1'}, a2='i2')
|
provider = providers.Dict({a1: "i1"}, a2="i2")
|
||||||
|
|
||||||
dict1 = provider()
|
dict1 = provider()
|
||||||
dict2 = provider()
|
dict2 = provider()
|
||||||
|
|
||||||
self.assertEqual(dict1, {a1: 'i1', 'a2': 'i2'})
|
self.assertEqual(dict1, {a1: "i1", "a2": "i2"})
|
||||||
self.assertEqual(dict2, {a1: 'i1', 'a2': 'i2'})
|
self.assertEqual(dict2, {a1: "i1", "a2": "i2"})
|
||||||
|
|
||||||
self.assertIsNot(dict1, dict2)
|
self.assertIsNot(dict1, dict2)
|
||||||
|
|
||||||
def test_call_with_init_keyword_args(self):
|
def test_call_with_init_keyword_args(self):
|
||||||
provider = providers.Dict(a1='i1', a2='i2')
|
provider = providers.Dict(a1="i1", a2="i2")
|
||||||
|
|
||||||
dict1 = provider()
|
dict1 = provider()
|
||||||
dict2 = provider()
|
dict2 = provider()
|
||||||
|
|
||||||
self.assertEqual(dict1, {'a1': 'i1', 'a2': 'i2'})
|
self.assertEqual(dict1, {"a1": "i1", "a2": "i2"})
|
||||||
self.assertEqual(dict2, {'a1': 'i1', 'a2': 'i2'})
|
self.assertEqual(dict2, {"a1": "i1", "a2": "i2"})
|
||||||
|
|
||||||
self.assertIsNot(dict1, dict2)
|
self.assertIsNot(dict1, dict2)
|
||||||
|
|
||||||
def test_call_with_context_keyword_args(self):
|
def test_call_with_context_keyword_args(self):
|
||||||
provider = providers.Dict(a1='i1', a2='i2')
|
provider = providers.Dict(a1="i1", a2="i2")
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
provider(a3='i3', a4='i4'),
|
provider(a3="i3", a4="i4"),
|
||||||
{'a1': 'i1', 'a2': 'i2', 'a3': 'i3', 'a4': 'i4'},
|
{"a1": "i1", "a2": "i2", "a3": "i3", "a4": "i4"},
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_call_with_provider(self):
|
def test_call_with_provider(self):
|
||||||
provider = providers.Dict(
|
provider = providers.Dict(
|
||||||
a1=providers.Factory(str, 'i1'),
|
a1=providers.Factory(str, "i1"),
|
||||||
a2=providers.Factory(str, 'i2'),
|
a2=providers.Factory(str, "i2"),
|
||||||
)
|
)
|
||||||
self.assertEqual(provider(), {'a1': 'i1', 'a2': 'i2'})
|
self.assertEqual(provider(), {"a1": "i1", "a2": "i2"})
|
||||||
|
|
||||||
def test_fluent_interface(self):
|
def test_fluent_interface(self):
|
||||||
provider = providers.Dict() \
|
provider = providers.Dict() \
|
||||||
.add_kwargs(a1='i1', a2='i2')
|
.add_kwargs(a1="i1", a2="i2")
|
||||||
self.assertEqual(provider(), {'a1': 'i1', 'a2': 'i2'})
|
self.assertEqual(provider(), {"a1": "i1", "a2": "i2"})
|
||||||
|
|
||||||
def test_add_kwargs(self):
|
def test_add_kwargs(self):
|
||||||
provider = providers.Dict() \
|
provider = providers.Dict() \
|
||||||
.add_kwargs(a1='i1') \
|
.add_kwargs(a1="i1") \
|
||||||
.add_kwargs(a2='i2')
|
.add_kwargs(a2="i2")
|
||||||
self.assertEqual(provider.kwargs, {'a1': 'i1', 'a2': 'i2'})
|
self.assertEqual(provider.kwargs, {"a1": "i1", "a2": "i2"})
|
||||||
|
|
||||||
def test_add_kwargs_non_string_keys(self):
|
def test_add_kwargs_non_string_keys(self):
|
||||||
a1 = object()
|
a1 = object()
|
||||||
a2 = object()
|
a2 = object()
|
||||||
provider = providers.Dict() \
|
provider = providers.Dict() \
|
||||||
.add_kwargs({a1: 'i1'}) \
|
.add_kwargs({a1: "i1"}) \
|
||||||
.add_kwargs({a2: 'i2'})
|
.add_kwargs({a2: "i2"})
|
||||||
self.assertEqual(provider.kwargs, {a1: 'i1', a2: 'i2'})
|
self.assertEqual(provider.kwargs, {a1: "i1", a2: "i2"})
|
||||||
|
|
||||||
def test_add_kwargs_string_and_non_string_keys(self):
|
def test_add_kwargs_string_and_non_string_keys(self):
|
||||||
a2 = object()
|
a2 = object()
|
||||||
provider = providers.Dict() \
|
provider = providers.Dict() \
|
||||||
.add_kwargs(a1='i1') \
|
.add_kwargs(a1="i1") \
|
||||||
.add_kwargs({a2: 'i2'})
|
.add_kwargs({a2: "i2"})
|
||||||
self.assertEqual(provider.kwargs, {'a1': 'i1', a2: 'i2'})
|
self.assertEqual(provider.kwargs, {"a1": "i1", a2: "i2"})
|
||||||
|
|
||||||
def test_set_kwargs(self):
|
def test_set_kwargs(self):
|
||||||
provider = providers.Dict() \
|
provider = providers.Dict() \
|
||||||
.add_kwargs(a1='i1', a2='i2') \
|
.add_kwargs(a1="i1", a2="i2") \
|
||||||
.set_kwargs(a3='i3', a4='i4')
|
.set_kwargs(a3="i3", a4="i4")
|
||||||
self.assertEqual(provider.kwargs, {'a3': 'i3', 'a4': 'i4'})
|
self.assertEqual(provider.kwargs, {"a3": "i3", "a4": "i4"})
|
||||||
|
|
||||||
def test_set_kwargs_non_string_keys(self):
|
def test_set_kwargs_non_string_keys(self):
|
||||||
a3 = object()
|
a3 = object()
|
||||||
a4 = object()
|
a4 = object()
|
||||||
provider = providers.Dict() \
|
provider = providers.Dict() \
|
||||||
.add_kwargs(a1='i1', a2='i2') \
|
.add_kwargs(a1="i1", a2="i2") \
|
||||||
.set_kwargs({a3: 'i3', a4: 'i4'})
|
.set_kwargs({a3: "i3", a4: "i4"})
|
||||||
self.assertEqual(provider.kwargs, {a3: 'i3', a4: 'i4'})
|
self.assertEqual(provider.kwargs, {a3: "i3", a4: "i4"})
|
||||||
|
|
||||||
def test_set_kwargs_string_and_non_string_keys(self):
|
def test_set_kwargs_string_and_non_string_keys(self):
|
||||||
a3 = object()
|
a3 = object()
|
||||||
provider = providers.Dict() \
|
provider = providers.Dict() \
|
||||||
.add_kwargs(a1='i1', a2='i2') \
|
.add_kwargs(a1="i1", a2="i2") \
|
||||||
.set_kwargs({a3: 'i3'}, a4='i4')
|
.set_kwargs({a3: "i3"}, a4="i4")
|
||||||
self.assertEqual(provider.kwargs, {a3: 'i3', 'a4': 'i4'})
|
self.assertEqual(provider.kwargs, {a3: "i3", "a4": "i4"})
|
||||||
|
|
||||||
def test_clear_kwargs(self):
|
def test_clear_kwargs(self):
|
||||||
provider = providers.Dict() \
|
provider = providers.Dict() \
|
||||||
.add_kwargs(a1='i1', a2='i2') \
|
.add_kwargs(a1="i1", a2="i2") \
|
||||||
.clear_kwargs()
|
.clear_kwargs()
|
||||||
self.assertEqual(provider.kwargs, {})
|
self.assertEqual(provider.kwargs, {})
|
||||||
|
|
||||||
def test_call_overridden(self):
|
def test_call_overridden(self):
|
||||||
provider = providers.Dict(a1='i1', a2='i2')
|
provider = providers.Dict(a1="i1", a2="i2")
|
||||||
overriding_provider1 = providers.Dict(a2='i2', a3='i3')
|
overriding_provider1 = providers.Dict(a2="i2", a3="i3")
|
||||||
overriding_provider2 = providers.Dict(a3='i3', a4='i4')
|
overriding_provider2 = providers.Dict(a3="i3", a4="i4")
|
||||||
|
|
||||||
provider.override(overriding_provider1)
|
provider.override(overriding_provider1)
|
||||||
provider.override(overriding_provider2)
|
provider.override(overriding_provider2)
|
||||||
|
@ -131,11 +131,11 @@ class DictTests(unittest.TestCase):
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
|
||||||
self.assertIsNot(instance1, instance2)
|
self.assertIsNot(instance1, instance2)
|
||||||
self.assertEqual(instance1, {'a3': 'i3', 'a4': 'i4'})
|
self.assertEqual(instance1, {"a3": "i3", "a4": "i4"})
|
||||||
self.assertEqual(instance2, {'a3': 'i3', 'a4': 'i4'})
|
self.assertEqual(instance2, {"a3": "i3", "a4": "i4"})
|
||||||
|
|
||||||
def test_deepcopy(self):
|
def test_deepcopy(self):
|
||||||
provider = providers.Dict(a1='i1', a2='i2')
|
provider = providers.Dict(a1="i1", a2="i2")
|
||||||
|
|
||||||
provider_copy = providers.deepcopy(provider)
|
provider_copy = providers.deepcopy(provider)
|
||||||
|
|
||||||
|
@ -144,8 +144,8 @@ class DictTests(unittest.TestCase):
|
||||||
self.assertIsInstance(provider, providers.Dict)
|
self.assertIsInstance(provider, providers.Dict)
|
||||||
|
|
||||||
def test_deepcopy_from_memo(self):
|
def test_deepcopy_from_memo(self):
|
||||||
provider = providers.Dict(a1='i1', a2='i2')
|
provider = providers.Dict(a1="i1", a2="i2")
|
||||||
provider_copy_memo = providers.Dict(a1='i1', a2='i2')
|
provider_copy_memo = providers.Dict(a1="i1", a2="i2")
|
||||||
|
|
||||||
provider_copy = providers.deepcopy(
|
provider_copy = providers.deepcopy(
|
||||||
provider,
|
provider,
|
||||||
|
@ -162,8 +162,8 @@ class DictTests(unittest.TestCase):
|
||||||
provider.add_kwargs(d1=dependent_provider1, d2=dependent_provider2)
|
provider.add_kwargs(d1=dependent_provider1, d2=dependent_provider2)
|
||||||
|
|
||||||
provider_copy = providers.deepcopy(provider)
|
provider_copy = providers.deepcopy(provider)
|
||||||
dependent_provider_copy1 = provider_copy.kwargs['d1']
|
dependent_provider_copy1 = provider_copy.kwargs["d1"]
|
||||||
dependent_provider_copy2 = provider_copy.kwargs['d2']
|
dependent_provider_copy2 = provider_copy.kwargs["d2"]
|
||||||
|
|
||||||
self.assertNotEqual(provider.kwargs, provider_copy.kwargs)
|
self.assertNotEqual(provider.kwargs, provider_copy.kwargs)
|
||||||
|
|
||||||
|
@ -218,14 +218,14 @@ class DictTests(unittest.TestCase):
|
||||||
|
|
||||||
self.assertIsNot(provider, provider_copy)
|
self.assertIsNot(provider, provider_copy)
|
||||||
self.assertIsInstance(provider_copy, providers.Dict)
|
self.assertIsInstance(provider_copy, providers.Dict)
|
||||||
self.assertIs(provider.kwargs['stdin'], sys.stdin)
|
self.assertIs(provider.kwargs["stdin"], sys.stdin)
|
||||||
self.assertIs(provider.kwargs['stdout'], sys.stdout)
|
self.assertIs(provider.kwargs["stdout"], sys.stdout)
|
||||||
self.assertIs(provider.kwargs['stderr'], sys.stderr)
|
self.assertIs(provider.kwargs["stderr"], sys.stderr)
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
provider = providers.Dict(a1=1, a2=2)
|
provider = providers.Dict(a1=1, a2=2)
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'Dict({0}) at {1}>'.format(
|
"Dict({0}) at {1}>".format(
|
||||||
repr(provider.kwargs),
|
repr(provider.kwargs),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
|
@ -86,48 +86,48 @@ class FactoryTests(unittest.TestCase):
|
||||||
self.assertIsInstance(instance2, Example)
|
self.assertIsInstance(instance2, Example)
|
||||||
|
|
||||||
def test_call_with_init_positional_args(self):
|
def test_call_with_init_positional_args(self):
|
||||||
provider = providers.Factory(Example, 'i1', 'i2')
|
provider = providers.Factory(Example, "i1", "i2")
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
|
||||||
self.assertEqual(instance1.init_arg1, 'i1')
|
self.assertEqual(instance1.init_arg1, "i1")
|
||||||
self.assertEqual(instance1.init_arg2, 'i2')
|
self.assertEqual(instance1.init_arg2, "i2")
|
||||||
|
|
||||||
self.assertEqual(instance2.init_arg1, 'i1')
|
self.assertEqual(instance2.init_arg1, "i1")
|
||||||
self.assertEqual(instance2.init_arg2, 'i2')
|
self.assertEqual(instance2.init_arg2, "i2")
|
||||||
|
|
||||||
self.assertIsNot(instance1, instance2)
|
self.assertIsNot(instance1, instance2)
|
||||||
self.assertIsInstance(instance1, Example)
|
self.assertIsInstance(instance1, Example)
|
||||||
self.assertIsInstance(instance2, Example)
|
self.assertIsInstance(instance2, Example)
|
||||||
|
|
||||||
def test_call_with_init_keyword_args(self):
|
def test_call_with_init_keyword_args(self):
|
||||||
provider = providers.Factory(Example, init_arg1='i1', init_arg2='i2')
|
provider = providers.Factory(Example, init_arg1="i1", init_arg2="i2")
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
|
||||||
self.assertEqual(instance1.init_arg1, 'i1')
|
self.assertEqual(instance1.init_arg1, "i1")
|
||||||
self.assertEqual(instance1.init_arg2, 'i2')
|
self.assertEqual(instance1.init_arg2, "i2")
|
||||||
|
|
||||||
self.assertEqual(instance2.init_arg1, 'i1')
|
self.assertEqual(instance2.init_arg1, "i1")
|
||||||
self.assertEqual(instance2.init_arg2, 'i2')
|
self.assertEqual(instance2.init_arg2, "i2")
|
||||||
|
|
||||||
self.assertIsNot(instance1, instance2)
|
self.assertIsNot(instance1, instance2)
|
||||||
self.assertIsInstance(instance1, Example)
|
self.assertIsInstance(instance1, Example)
|
||||||
self.assertIsInstance(instance2, Example)
|
self.assertIsInstance(instance2, Example)
|
||||||
|
|
||||||
def test_call_with_init_positional_and_keyword_args(self):
|
def test_call_with_init_positional_and_keyword_args(self):
|
||||||
provider = providers.Factory(Example, 'i1', init_arg2='i2')
|
provider = providers.Factory(Example, "i1", init_arg2="i2")
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
|
||||||
self.assertEqual(instance1.init_arg1, 'i1')
|
self.assertEqual(instance1.init_arg1, "i1")
|
||||||
self.assertEqual(instance1.init_arg2, 'i2')
|
self.assertEqual(instance1.init_arg2, "i2")
|
||||||
|
|
||||||
self.assertEqual(instance2.init_arg1, 'i1')
|
self.assertEqual(instance2.init_arg1, "i1")
|
||||||
self.assertEqual(instance2.init_arg2, 'i2')
|
self.assertEqual(instance2.init_arg2, "i2")
|
||||||
|
|
||||||
self.assertIsNot(instance1, instance2)
|
self.assertIsNot(instance1, instance2)
|
||||||
self.assertIsInstance(instance1, Example)
|
self.assertIsInstance(instance1, Example)
|
||||||
|
@ -135,16 +135,16 @@ class FactoryTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_call_with_attributes(self):
|
def test_call_with_attributes(self):
|
||||||
provider = providers.Factory(Example)
|
provider = providers.Factory(Example)
|
||||||
provider.add_attributes(attribute1='a1', attribute2='a2')
|
provider.add_attributes(attribute1="a1", attribute2="a2")
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
|
||||||
self.assertEqual(instance1.attribute1, 'a1')
|
self.assertEqual(instance1.attribute1, "a1")
|
||||||
self.assertEqual(instance1.attribute2, 'a2')
|
self.assertEqual(instance1.attribute2, "a2")
|
||||||
|
|
||||||
self.assertEqual(instance2.attribute1, 'a1')
|
self.assertEqual(instance2.attribute1, "a1")
|
||||||
self.assertEqual(instance2.attribute2, 'a2')
|
self.assertEqual(instance2.attribute2, "a2")
|
||||||
|
|
||||||
self.assertIsNot(instance1, instance2)
|
self.assertIsNot(instance1, instance2)
|
||||||
self.assertIsInstance(instance1, Example)
|
self.assertIsInstance(instance1, Example)
|
||||||
|
@ -331,8 +331,8 @@ class FactoryTests(unittest.TestCase):
|
||||||
provider.add_kwargs(a1=dependent_provider1, a2=dependent_provider2)
|
provider.add_kwargs(a1=dependent_provider1, a2=dependent_provider2)
|
||||||
|
|
||||||
provider_copy = providers.deepcopy(provider)
|
provider_copy = providers.deepcopy(provider)
|
||||||
dependent_provider_copy1 = provider_copy.kwargs['a1']
|
dependent_provider_copy1 = provider_copy.kwargs["a1"]
|
||||||
dependent_provider_copy2 = provider_copy.kwargs['a2']
|
dependent_provider_copy2 = provider_copy.kwargs["a2"]
|
||||||
|
|
||||||
self.assertNotEqual(provider.kwargs, provider_copy.kwargs)
|
self.assertNotEqual(provider.kwargs, provider_copy.kwargs)
|
||||||
|
|
||||||
|
@ -350,8 +350,8 @@ class FactoryTests(unittest.TestCase):
|
||||||
provider.add_attributes(a1=dependent_provider1, a2=dependent_provider2)
|
provider.add_attributes(a1=dependent_provider1, a2=dependent_provider2)
|
||||||
|
|
||||||
provider_copy = providers.deepcopy(provider)
|
provider_copy = providers.deepcopy(provider)
|
||||||
dependent_provider_copy1 = provider_copy.attributes['a1']
|
dependent_provider_copy1 = provider_copy.attributes["a1"]
|
||||||
dependent_provider_copy2 = provider_copy.attributes['a2']
|
dependent_provider_copy2 = provider_copy.attributes["a2"]
|
||||||
|
|
||||||
self.assertNotEqual(provider.attributes, provider_copy.attributes)
|
self.assertNotEqual(provider.attributes, provider_copy.attributes)
|
||||||
|
|
||||||
|
@ -388,15 +388,15 @@ class FactoryTests(unittest.TestCase):
|
||||||
self.assertIsNot(provider, provider_copy)
|
self.assertIsNot(provider, provider_copy)
|
||||||
self.assertIsInstance(provider_copy, providers.Factory)
|
self.assertIsInstance(provider_copy, providers.Factory)
|
||||||
self.assertIs(provider.args[0], sys.stdin)
|
self.assertIs(provider.args[0], sys.stdin)
|
||||||
self.assertIs(provider.kwargs['a2'], sys.stdout)
|
self.assertIs(provider.kwargs["a2"], sys.stdout)
|
||||||
self.assertIs(provider.attributes['a3'], sys.stderr)
|
self.assertIs(provider.attributes["a3"], sys.stderr)
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
provider = providers.Factory(Example)
|
provider = providers.Factory(Example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'Factory({0}) at {1}>'.format(
|
"Factory({0}) at {1}>".format(
|
||||||
repr(Example),
|
repr(Example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
@ -419,8 +419,8 @@ class DelegatedFactoryTests(unittest.TestCase):
|
||||||
provider = providers.DelegatedFactory(Example)
|
provider = providers.DelegatedFactory(Example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'DelegatedFactory({0}) at {1}>'.format(
|
"DelegatedFactory({0}) at {1}>".format(
|
||||||
repr(Example),
|
repr(Example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
@ -465,8 +465,8 @@ class AbstractFactoryTests(unittest.TestCase):
|
||||||
provider = providers.AbstractFactory(Example)
|
provider = providers.AbstractFactory(Example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'AbstractFactory({0}) at {1}>'.format(
|
"AbstractFactory({0}) at {1}>".format(
|
||||||
repr(Example),
|
repr(Example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
@ -552,12 +552,12 @@ class FactoryAggregateTests(unittest.TestCase):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
provider.factories,
|
provider.factories,
|
||||||
{
|
{
|
||||||
'example_a': self.example_a_factory,
|
"example_a": self.example_a_factory,
|
||||||
'example_b': self.example_b_factory,
|
"example_b": self.example_b_factory,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
self.assertIsInstance(provider('example_a'), self.ExampleA)
|
self.assertIsInstance(provider("example_a"), self.ExampleA)
|
||||||
self.assertIsInstance(provider('example_b'), self.ExampleB)
|
self.assertIsInstance(provider("example_b"), self.ExampleB)
|
||||||
|
|
||||||
def test_set_factories_with_non_string_keys(self):
|
def test_set_factories_with_non_string_keys(self):
|
||||||
factory = providers.FactoryAggregate()
|
factory = providers.FactoryAggregate()
|
||||||
|
@ -594,9 +594,9 @@ class FactoryAggregateTests(unittest.TestCase):
|
||||||
self.assertIs(provider.set_factories(example_a=self.example_a_factory), provider)
|
self.assertIs(provider.set_factories(example_a=self.example_a_factory), provider)
|
||||||
|
|
||||||
def test_call(self):
|
def test_call(self):
|
||||||
object_a = self.factory_aggregate('example_a',
|
object_a = self.factory_aggregate("example_a",
|
||||||
1, 2, init_arg3=3, init_arg4=4)
|
1, 2, init_arg3=3, init_arg4=4)
|
||||||
object_b = self.factory_aggregate('example_b',
|
object_b = self.factory_aggregate("example_b",
|
||||||
11, 22, init_arg3=33, init_arg4=44)
|
11, 22, init_arg3=33, init_arg4=44)
|
||||||
|
|
||||||
self.assertIsInstance(object_a, self.ExampleA)
|
self.assertIsInstance(object_a, self.ExampleA)
|
||||||
|
@ -613,7 +613,7 @@ class FactoryAggregateTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_call_factory_name_as_kwarg(self):
|
def test_call_factory_name_as_kwarg(self):
|
||||||
object_a = self.factory_aggregate(
|
object_a = self.factory_aggregate(
|
||||||
factory_name='example_a',
|
factory_name="example_a",
|
||||||
init_arg1=1,
|
init_arg1=1,
|
||||||
init_arg2=2,
|
init_arg2=2,
|
||||||
init_arg3=3,
|
init_arg3=3,
|
||||||
|
@ -631,7 +631,7 @@ class FactoryAggregateTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_call_no_such_provider(self):
|
def test_call_no_such_provider(self):
|
||||||
with self.assertRaises(errors.NoSuchProviderError):
|
with self.assertRaises(errors.NoSuchProviderError):
|
||||||
self.factory_aggregate('unknown')
|
self.factory_aggregate("unknown")
|
||||||
|
|
||||||
def test_overridden(self):
|
def test_overridden(self):
|
||||||
with self.assertRaises(errors.Error):
|
with self.assertRaises(errors.Error):
|
||||||
|
@ -684,7 +684,7 @@ class FactoryAggregateTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
self.assertEqual(repr(self.factory_aggregate),
|
self.assertEqual(repr(self.factory_aggregate),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'FactoryAggregate({0}) at {1}>'.format(
|
"FactoryAggregate({0}) at {1}>".format(
|
||||||
repr(self.factory_aggregate.factories),
|
repr(self.factory_aggregate.factories),
|
||||||
hex(id(self.factory_aggregate))))
|
hex(id(self.factory_aggregate))))
|
||||||
|
|
|
@ -56,19 +56,19 @@ class PositionalInjectionTests(unittest.TestCase):
|
||||||
class NamedInjectionTests(unittest.TestCase):
|
class NamedInjectionTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_isinstance(self):
|
def test_isinstance(self):
|
||||||
injection = providers.NamedInjection('name', 1)
|
injection = providers.NamedInjection("name", 1)
|
||||||
self.assertIsInstance(injection, providers.Injection)
|
self.assertIsInstance(injection, providers.Injection)
|
||||||
|
|
||||||
def test_get_name(self):
|
def test_get_name(self):
|
||||||
injection = providers.NamedInjection('name', 123)
|
injection = providers.NamedInjection("name", 123)
|
||||||
self.assertEqual(injection.get_name(), 'name')
|
self.assertEqual(injection.get_name(), "name")
|
||||||
|
|
||||||
def test_get_value_with_not_provider(self):
|
def test_get_value_with_not_provider(self):
|
||||||
injection = providers.NamedInjection('name', 123)
|
injection = providers.NamedInjection("name", 123)
|
||||||
self.assertEqual(injection.get_value(), 123)
|
self.assertEqual(injection.get_value(), 123)
|
||||||
|
|
||||||
def test_get_value_with_factory(self):
|
def test_get_value_with_factory(self):
|
||||||
injection = providers.NamedInjection('name',
|
injection = providers.NamedInjection("name",
|
||||||
providers.Factory(object))
|
providers.Factory(object))
|
||||||
|
|
||||||
obj1 = injection.get_value()
|
obj1 = injection.get_value()
|
||||||
|
@ -80,12 +80,12 @@ class NamedInjectionTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_get_original_value(self):
|
def test_get_original_value(self):
|
||||||
provider = providers.Factory(object)
|
provider = providers.Factory(object)
|
||||||
injection = providers.NamedInjection('name', provider)
|
injection = providers.NamedInjection("name", provider)
|
||||||
self.assertIs(injection.get_original_value(), provider)
|
self.assertIs(injection.get_original_value(), provider)
|
||||||
|
|
||||||
def test_deepcopy(self):
|
def test_deepcopy(self):
|
||||||
provider = providers.Factory(object)
|
provider = providers.Factory(object)
|
||||||
injection = providers.NamedInjection('name', provider)
|
injection = providers.NamedInjection("name", provider)
|
||||||
|
|
||||||
injection_copy = providers.deepcopy(injection)
|
injection_copy = providers.deepcopy(injection)
|
||||||
|
|
||||||
|
@ -95,8 +95,8 @@ class NamedInjectionTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_deepcopy_memo(self):
|
def test_deepcopy_memo(self):
|
||||||
provider = providers.Factory(object)
|
provider = providers.Factory(object)
|
||||||
injection = providers.NamedInjection('name', provider)
|
injection = providers.NamedInjection("name", provider)
|
||||||
injection_copy_orig = providers.NamedInjection('name', provider)
|
injection_copy_orig = providers.NamedInjection("name", provider)
|
||||||
|
|
||||||
injection_copy = providers.deepcopy(
|
injection_copy = providers.deepcopy(
|
||||||
injection, {id(injection): injection_copy_orig})
|
injection, {id(injection): injection_copy_orig})
|
||||||
|
|
|
@ -17,20 +17,20 @@ class ListTests(unittest.TestCase):
|
||||||
self.assertIsInstance(provider.provided, providers.ProvidedInstance)
|
self.assertIsInstance(provider.provided, providers.ProvidedInstance)
|
||||||
|
|
||||||
def test_call_with_init_positional_args(self):
|
def test_call_with_init_positional_args(self):
|
||||||
provider = providers.List('i1', 'i2')
|
provider = providers.List("i1", "i2")
|
||||||
|
|
||||||
list1 = provider()
|
list1 = provider()
|
||||||
list2 = provider()
|
list2 = provider()
|
||||||
|
|
||||||
self.assertEqual(list1, ['i1', 'i2'])
|
self.assertEqual(list1, ["i1", "i2"])
|
||||||
self.assertEqual(list2, ['i1', 'i2'])
|
self.assertEqual(list2, ["i1", "i2"])
|
||||||
|
|
||||||
self.assertIsNot(list1, list2)
|
self.assertIsNot(list1, list2)
|
||||||
|
|
||||||
def test_call_with_context_args(self):
|
def test_call_with_context_args(self):
|
||||||
provider = providers.List('i1', 'i2')
|
provider = providers.List("i1", "i2")
|
||||||
|
|
||||||
self.assertEqual(provider('i3', 'i4'), ['i1', 'i2', 'i3', 'i4'])
|
self.assertEqual(provider("i3", "i4"), ["i1", "i2", "i3", "i4"])
|
||||||
|
|
||||||
def test_fluent_interface(self):
|
def test_fluent_interface(self):
|
||||||
provider = providers.List() \
|
provider = providers.List() \
|
||||||
|
@ -134,7 +134,7 @@ class ListTests(unittest.TestCase):
|
||||||
provider = providers.List(1, 2)
|
provider = providers.List(1, 2)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'List({0}) at {1}>'.format(
|
"List({0}) at {1}>".format(
|
||||||
repr(list(provider.args)),
|
repr(list(provider.args)),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
|
@ -32,7 +32,7 @@ class Client:
|
||||||
|
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
service = providers.Singleton(Service, value='foo')
|
service = providers.Singleton(Service, value="foo")
|
||||||
|
|
||||||
client_attribute = providers.Factory(
|
client_attribute = providers.Factory(
|
||||||
Client,
|
Client,
|
||||||
|
@ -74,30 +74,30 @@ class ProvidedInstanceTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_attribute(self):
|
def test_attribute(self):
|
||||||
client = self.container.client_attribute()
|
client = self.container.client_attribute()
|
||||||
self.assertEqual(client.value, 'foo')
|
self.assertEqual(client.value, "foo")
|
||||||
|
|
||||||
def test_item(self):
|
def test_item(self):
|
||||||
client = self.container.client_item()
|
client = self.container.client_item()
|
||||||
self.assertEqual(client.value, 'foo')
|
self.assertEqual(client.value, "foo")
|
||||||
|
|
||||||
def test_attribute_item(self):
|
def test_attribute_item(self):
|
||||||
client = self.container.client_attribute_item()
|
client = self.container.client_attribute_item()
|
||||||
self.assertEqual(client.value, 'foo')
|
self.assertEqual(client.value, "foo")
|
||||||
|
|
||||||
def test_method_call(self):
|
def test_method_call(self):
|
||||||
client = self.container.client_method_call()
|
client = self.container.client_method_call()
|
||||||
self.assertEqual(client.value, 'foo')
|
self.assertEqual(client.value, "foo")
|
||||||
|
|
||||||
def test_method_closure_call(self):
|
def test_method_closure_call(self):
|
||||||
client = self.container.client_method_closure_call()
|
client = self.container.client_method_closure_call()
|
||||||
self.assertEqual(client.value, 'foo')
|
self.assertEqual(client.value, "foo")
|
||||||
|
|
||||||
def test_provided_call(self):
|
def test_provided_call(self):
|
||||||
client = self.container.client_provided_call()
|
client = self.container.client_provided_call()
|
||||||
self.assertEqual(client.value, 'foo')
|
self.assertEqual(client.value, "foo")
|
||||||
|
|
||||||
def test_call_overridden(self):
|
def test_call_overridden(self):
|
||||||
value = 'bar'
|
value = "bar"
|
||||||
with self.container.service.override(Service(value)):
|
with self.container.service.override(Service(value)):
|
||||||
self.assertEqual(self.container.client_attribute().value, value)
|
self.assertEqual(self.container.client_attribute().value, value)
|
||||||
self.assertEqual(self.container.client_item().value, value)
|
self.assertEqual(self.container.client_item().value, value)
|
||||||
|
@ -107,21 +107,21 @@ class ProvidedInstanceTests(unittest.TestCase):
|
||||||
def test_repr_provided_instance(self):
|
def test_repr_provided_instance(self):
|
||||||
provider = self.container.service.provided
|
provider = self.container.service.provided
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
'ProvidedInstance(\'{0}\')'.format(repr(self.container.service)),
|
"ProvidedInstance(\"{0}\")".format(repr(self.container.service)),
|
||||||
repr(provider),
|
repr(provider),
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_repr_attribute_getter(self):
|
def test_repr_attribute_getter(self):
|
||||||
provider = self.container.service.provided.value
|
provider = self.container.service.provided.value
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
'AttributeGetter(\'value\')',
|
"AttributeGetter(\"value\")",
|
||||||
repr(provider),
|
repr(provider),
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_repr_item_getter(self):
|
def test_repr_item_getter(self):
|
||||||
provider = self.container.service.provided['test-test']
|
provider = self.container.service.provided["test-test"]
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
'ItemGetter(\'test-test\')',
|
"ItemGetter(\"test-test\")",
|
||||||
repr(provider),
|
repr(provider),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -139,21 +139,21 @@ class LazyInitTests(unittest.TestCase):
|
||||||
provides = providers.Object(object())
|
provides = providers.Object(object())
|
||||||
provider = providers.AttributeGetter()
|
provider = providers.AttributeGetter()
|
||||||
provider.set_provides(provides)
|
provider.set_provides(provides)
|
||||||
provider.set_name('__dict__')
|
provider.set_name("__dict__")
|
||||||
self.assertIs(provider.provides, provides)
|
self.assertIs(provider.provides, provides)
|
||||||
self.assertEqual(provider.name, '__dict__')
|
self.assertEqual(provider.name, "__dict__")
|
||||||
self.assertIs(provider.set_provides(providers.Provider()), provider)
|
self.assertIs(provider.set_provides(providers.Provider()), provider)
|
||||||
self.assertIs(provider.set_name('__dict__'), provider)
|
self.assertIs(provider.set_name("__dict__"), provider)
|
||||||
|
|
||||||
def test_item_getter(self):
|
def test_item_getter(self):
|
||||||
provides = providers.Object({'foo': 'bar'})
|
provides = providers.Object({"foo": "bar"})
|
||||||
provider = providers.ItemGetter()
|
provider = providers.ItemGetter()
|
||||||
provider.set_provides(provides)
|
provider.set_provides(provides)
|
||||||
provider.set_name('foo')
|
provider.set_name("foo")
|
||||||
self.assertIs(provider.provides, provides)
|
self.assertIs(provider.provides, provides)
|
||||||
self.assertEqual(provider.name, 'foo')
|
self.assertEqual(provider.name, "foo")
|
||||||
self.assertIs(provider.set_provides(providers.Provider()), provider)
|
self.assertIs(provider.set_provides(providers.Provider()), provider)
|
||||||
self.assertIs(provider.set_name('foo'), provider)
|
self.assertIs(provider.set_name("foo"), provider)
|
||||||
|
|
||||||
def test_method_caller(self):
|
def test_method_caller(self):
|
||||||
provides = providers.Object(lambda: 42)
|
provides = providers.Object(lambda: 42)
|
||||||
|
@ -167,25 +167,25 @@ class LazyInitTests(unittest.TestCase):
|
||||||
class ProvidedInstancePuzzleTests(unittest.TestCase):
|
class ProvidedInstancePuzzleTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_puzzled(self):
|
def test_puzzled(self):
|
||||||
service = providers.Singleton(Service, value='foo-bar')
|
service = providers.Singleton(Service, value="foo-bar")
|
||||||
|
|
||||||
dependency = providers.Object(
|
dependency = providers.Object(
|
||||||
{
|
{
|
||||||
'a': {
|
"a": {
|
||||||
'b': {
|
"b": {
|
||||||
'c1': 10,
|
"c1": 10,
|
||||||
'c2': lambda arg: {'arg': arg}
|
"c2": lambda arg: {"arg": arg}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
test_list = providers.List(
|
test_list = providers.List(
|
||||||
dependency.provided['a']['b']['c1'],
|
dependency.provided["a"]["b"]["c1"],
|
||||||
dependency.provided['a']['b']['c2'].call(22)['arg'],
|
dependency.provided["a"]["b"]["c2"].call(22)["arg"],
|
||||||
dependency.provided['a']['b']['c2'].call(service)['arg'],
|
dependency.provided["a"]["b"]["c2"].call(service)["arg"],
|
||||||
dependency.provided['a']['b']['c2'].call(service)['arg'].value,
|
dependency.provided["a"]["b"]["c2"].call(service)["arg"].value,
|
||||||
dependency.provided['a']['b']['c2'].call(service)['arg'].get_value.call(),
|
dependency.provided["a"]["b"]["c2"].call(service)["arg"].get_value.call(),
|
||||||
)
|
)
|
||||||
|
|
||||||
result = test_list()
|
result = test_list()
|
||||||
|
@ -196,8 +196,8 @@ class ProvidedInstancePuzzleTests(unittest.TestCase):
|
||||||
10,
|
10,
|
||||||
22,
|
22,
|
||||||
service(),
|
service(),
|
||||||
'foo-bar',
|
"foo-bar",
|
||||||
'foo-bar',
|
"foo-bar",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ import os
|
||||||
_TOP_DIR = os.path.abspath(
|
_TOP_DIR = os.path.abspath(
|
||||||
os.path.sep.join((
|
os.path.sep.join((
|
||||||
os.path.dirname(__file__),
|
os.path.dirname(__file__),
|
||||||
'../',
|
"../",
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
import sys
|
import sys
|
||||||
|
@ -173,7 +173,7 @@ class ResourceTests(unittest.TestCase):
|
||||||
class TestResource(resources.Resource):
|
class TestResource(resources.Resource):
|
||||||
def init(self):
|
def init(self):
|
||||||
...
|
...
|
||||||
self.assertTrue(hasattr(TestResource(), 'shutdown'))
|
self.assertTrue(hasattr(TestResource(), "shutdown"))
|
||||||
|
|
||||||
def test_init_not_callable(self):
|
def test_init_not_callable(self):
|
||||||
provider = providers.Resource(1)
|
provider = providers.Resource(1)
|
||||||
|
@ -229,15 +229,15 @@ class ResourceTests(unittest.TestCase):
|
||||||
self.assertFalse(provider.initialized)
|
self.assertFalse(provider.initialized)
|
||||||
|
|
||||||
def test_call_with_context_args(self):
|
def test_call_with_context_args(self):
|
||||||
provider = providers.Resource(init_fn, 'i1', 'i2')
|
provider = providers.Resource(init_fn, "i1", "i2")
|
||||||
self.assertEqual(provider('i3', i4=4), (('i1', 'i2', 'i3'), {'i4': 4}))
|
self.assertEqual(provider("i3", i4=4), (("i1", "i2", "i3"), {"i4": 4}))
|
||||||
|
|
||||||
def test_fluent_interface(self):
|
def test_fluent_interface(self):
|
||||||
provider = providers.Resource(init_fn) \
|
provider = providers.Resource(init_fn) \
|
||||||
.add_args(1, 2) \
|
.add_args(1, 2) \
|
||||||
.add_kwargs(a3=3, a4=4)
|
.add_kwargs(a3=3, a4=4)
|
||||||
|
|
||||||
self.assertEqual(provider(), ((1, 2), {'a3': 3, 'a4': 4}))
|
self.assertEqual(provider(), ((1, 2), {"a3": 3, "a4": 4}))
|
||||||
|
|
||||||
def test_set_args(self):
|
def test_set_args(self):
|
||||||
provider = providers.Resource(init_fn) \
|
provider = providers.Resource(init_fn) \
|
||||||
|
@ -253,13 +253,13 @@ class ResourceTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_set_kwargs(self):
|
def test_set_kwargs(self):
|
||||||
provider = providers.Resource(init_fn) \
|
provider = providers.Resource(init_fn) \
|
||||||
.add_kwargs(a1='i1', a2='i2') \
|
.add_kwargs(a1="i1", a2="i2") \
|
||||||
.set_kwargs(a3='i3', a4='i4')
|
.set_kwargs(a3="i3", a4="i4")
|
||||||
self.assertEqual(provider.kwargs, {'a3': 'i3', 'a4': 'i4'})
|
self.assertEqual(provider.kwargs, {"a3": "i3", "a4": "i4"})
|
||||||
|
|
||||||
def test_clear_kwargs(self):
|
def test_clear_kwargs(self):
|
||||||
provider = providers.Resource(init_fn) \
|
provider = providers.Resource(init_fn) \
|
||||||
.add_kwargs(a1='i1', a2='i2') \
|
.add_kwargs(a1="i1", a2="i2") \
|
||||||
.clear_kwargs()
|
.clear_kwargs()
|
||||||
self.assertEqual(provider.kwargs, {})
|
self.assertEqual(provider.kwargs, {})
|
||||||
|
|
||||||
|
@ -333,8 +333,8 @@ class ResourceTests(unittest.TestCase):
|
||||||
provider.add_kwargs(d1=dependent_provider1, d2=dependent_provider2)
|
provider.add_kwargs(d1=dependent_provider1, d2=dependent_provider2)
|
||||||
|
|
||||||
provider_copy = providers.deepcopy(provider)
|
provider_copy = providers.deepcopy(provider)
|
||||||
dependent_provider_copy1 = provider_copy.kwargs['d1']
|
dependent_provider_copy1 = provider_copy.kwargs["d1"]
|
||||||
dependent_provider_copy2 = provider_copy.kwargs['d2']
|
dependent_provider_copy2 = provider_copy.kwargs["d2"]
|
||||||
|
|
||||||
self.assertNotEqual(provider.kwargs, provider_copy.kwargs)
|
self.assertNotEqual(provider.kwargs, provider_copy.kwargs)
|
||||||
|
|
||||||
|
@ -377,7 +377,7 @@ class ResourceTests(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
repr(provider),
|
repr(provider),
|
||||||
'<dependency_injector.providers.Resource({0}) at {1}>'.format(
|
"<dependency_injector.providers.Resource({0}) at {1}>".format(
|
||||||
repr(init_fn),
|
repr(init_fn),
|
||||||
hex(id(provider)),
|
hex(id(provider)),
|
||||||
)
|
)
|
||||||
|
@ -506,7 +506,7 @@ class AsyncResourceTest(AsyncTestCase):
|
||||||
class TestAsyncResource(resources.AsyncResource):
|
class TestAsyncResource(resources.AsyncResource):
|
||||||
async def init(self):
|
async def init(self):
|
||||||
...
|
...
|
||||||
self.assertTrue(hasattr(TestAsyncResource(), 'shutdown'))
|
self.assertTrue(hasattr(TestAsyncResource(), "shutdown"))
|
||||||
self.assertTrue(inspect.iscoroutinefunction(TestAsyncResource.shutdown))
|
self.assertTrue(inspect.iscoroutinefunction(TestAsyncResource.shutdown))
|
||||||
|
|
||||||
def test_init_with_error(self):
|
def test_init_with_error(self):
|
||||||
|
@ -566,11 +566,11 @@ class AsyncResourceTest(AsyncTestCase):
|
||||||
# See: https://github.com/ets-labs/python-dependency-injector/issues/361
|
# See: https://github.com/ets-labs/python-dependency-injector/issues/361
|
||||||
async def init_db_connection(db_url: str):
|
async def init_db_connection(db_url: str):
|
||||||
await asyncio.sleep(0.001)
|
await asyncio.sleep(0.001)
|
||||||
yield {'connection': 'ok', 'url': db_url}
|
yield {"connection": "ok", "url": db_url}
|
||||||
|
|
||||||
async def init_user_session(db):
|
async def init_user_session(db):
|
||||||
await asyncio.sleep(0.001)
|
await asyncio.sleep(0.001)
|
||||||
yield {'session': 'ok', 'db': db}
|
yield {"session": "ok", "db": db}
|
||||||
|
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
config = providers.Configuration()
|
config = providers.Configuration()
|
||||||
|
@ -586,7 +586,7 @@ class AsyncResourceTest(AsyncTestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
container = Container(config={'db_url': 'postgres://...'})
|
container = Container(config={"db_url": "postgres://..."})
|
||||||
try:
|
try:
|
||||||
return await container.user_session()
|
return await container.user_session()
|
||||||
finally:
|
finally:
|
||||||
|
@ -596,7 +596,7 @@ class AsyncResourceTest(AsyncTestCase):
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
result,
|
result,
|
||||||
{'session': 'ok', 'db': {'connection': 'ok', 'url': 'postgres://...'}},
|
{"session": "ok", "db": {"connection": "ok", "url": "postgres://..."}},
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_init_and_shutdown_methods(self):
|
def test_init_and_shutdown_methods(self):
|
||||||
|
|
|
@ -24,10 +24,10 @@ class SelectorTests(unittest.TestCase):
|
||||||
provider.set_selector(self.selector)
|
provider.set_selector(self.selector)
|
||||||
provider.set_providers(one=one, two=two)
|
provider.set_providers(one=one, two=two)
|
||||||
|
|
||||||
self.assertEqual(provider.providers, {'one': one, 'two': two})
|
self.assertEqual(provider.providers, {"one": one, "two": two})
|
||||||
with self.selector.override('one'):
|
with self.selector.override("one"):
|
||||||
self.assertEqual(provider(), one())
|
self.assertEqual(provider(), one())
|
||||||
with self.selector.override('two'):
|
with self.selector.override("two"):
|
||||||
self.assertEqual(provider(), two())
|
self.assertEqual(provider(), two())
|
||||||
|
|
||||||
def test_set_selector_returns_self(self):
|
def test_set_selector_returns_self(self):
|
||||||
|
@ -49,10 +49,10 @@ class SelectorTests(unittest.TestCase):
|
||||||
two=providers.Object(2),
|
two=providers.Object(2),
|
||||||
)
|
)
|
||||||
|
|
||||||
with self.selector.override('one'):
|
with self.selector.override("one"):
|
||||||
self.assertEqual(provider(), 1)
|
self.assertEqual(provider(), 1)
|
||||||
|
|
||||||
with self.selector.override('two'):
|
with self.selector.override("two"):
|
||||||
self.assertEqual(provider(), 2)
|
self.assertEqual(provider(), 2)
|
||||||
|
|
||||||
def test_call_undefined_provider(self):
|
def test_call_undefined_provider(self):
|
||||||
|
@ -62,7 +62,7 @@ class SelectorTests(unittest.TestCase):
|
||||||
two=providers.Object(2),
|
two=providers.Object(2),
|
||||||
)
|
)
|
||||||
|
|
||||||
with self.selector.override('three'):
|
with self.selector.override("three"):
|
||||||
with self.assertRaises(errors.Error):
|
with self.assertRaises(errors.Error):
|
||||||
provider()
|
provider()
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ class SelectorTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_call_any_callable(self):
|
def test_call_any_callable(self):
|
||||||
provider = providers.Selector(
|
provider = providers.Selector(
|
||||||
functools.partial(next, itertools.cycle(['one', 'two'])),
|
functools.partial(next, itertools.cycle(["one", "two"])),
|
||||||
one=providers.Object(1),
|
one=providers.Object(1),
|
||||||
two=providers.Object(2),
|
two=providers.Object(2),
|
||||||
)
|
)
|
||||||
|
@ -95,11 +95,11 @@ class SelectorTests(unittest.TestCase):
|
||||||
one=providers.Callable(lambda *args, **kwargs: (args, kwargs)),
|
one=providers.Callable(lambda *args, **kwargs: (args, kwargs)),
|
||||||
)
|
)
|
||||||
|
|
||||||
with self.selector.override('one'):
|
with self.selector.override("one"):
|
||||||
args, kwargs = provider(1, 2, three=3, four=4)
|
args, kwargs = provider(1, 2, three=3, four=4)
|
||||||
|
|
||||||
self.assertEqual(args, (1, 2))
|
self.assertEqual(args, (1, 2))
|
||||||
self.assertEqual(kwargs, {'three': 3, 'four': 4})
|
self.assertEqual(kwargs, {"three": 3, "four": 4})
|
||||||
|
|
||||||
def test_getattr(self):
|
def test_getattr(self):
|
||||||
provider_one = providers.Object(1)
|
provider_one = providers.Object(1)
|
||||||
|
@ -135,7 +135,7 @@ class SelectorTests(unittest.TestCase):
|
||||||
provider.override(overriding_provider1)
|
provider.override(overriding_provider1)
|
||||||
provider.override(overriding_provider2)
|
provider.override(overriding_provider2)
|
||||||
|
|
||||||
with self.selector.override('sample'):
|
with self.selector.override("sample"):
|
||||||
self.assertEqual(provider(), 3)
|
self.assertEqual(provider(), 3)
|
||||||
|
|
||||||
def test_providers_attribute(self):
|
def test_providers_attribute(self):
|
||||||
|
@ -148,7 +148,7 @@ class SelectorTests(unittest.TestCase):
|
||||||
two=provider_two,
|
two=provider_two,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertEqual(provider.providers, {'one': provider_one, 'two': provider_two})
|
self.assertEqual(provider.providers, {"one": provider_one, "two": provider_two})
|
||||||
|
|
||||||
def test_deepcopy(self):
|
def test_deepcopy(self):
|
||||||
provider = providers.Selector(self.selector)
|
provider = providers.Selector(self.selector)
|
||||||
|
@ -198,13 +198,13 @@ class SelectorTests(unittest.TestCase):
|
||||||
self.assertIsNot(provider, provider_copy)
|
self.assertIsNot(provider, provider_copy)
|
||||||
self.assertIsInstance(provider_copy, providers.Selector)
|
self.assertIsInstance(provider_copy, providers.Selector)
|
||||||
|
|
||||||
with self.selector.override('stdin'):
|
with self.selector.override("stdin"):
|
||||||
self.assertIs(provider(), sys.stdin)
|
self.assertIs(provider(), sys.stdin)
|
||||||
|
|
||||||
with self.selector.override('stdout'):
|
with self.selector.override("stdout"):
|
||||||
self.assertIs(provider(), sys.stdout)
|
self.assertIs(provider(), sys.stdout)
|
||||||
|
|
||||||
with self.selector.override('stderr'):
|
with self.selector.override("stderr"):
|
||||||
self.assertIs(provider(), sys.stderr)
|
self.assertIs(provider(), sys.stderr)
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
|
@ -215,9 +215,9 @@ class SelectorTests(unittest.TestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertIn(
|
self.assertIn(
|
||||||
'<dependency_injector.providers.Selector({0}'.format(repr(self.selector)),
|
"<dependency_injector.providers.Selector({0}".format(repr(self.selector)),
|
||||||
repr(provider),
|
repr(provider),
|
||||||
)
|
)
|
||||||
self.assertIn('one={0}'.format(repr(provider.one)), repr(provider))
|
self.assertIn("one={0}".format(repr(provider.one)), repr(provider))
|
||||||
self.assertIn('two={0}'.format(repr(provider.two)), repr(provider))
|
self.assertIn("two={0}".format(repr(provider.two)), repr(provider))
|
||||||
self.assertIn('at {0}'.format(hex(id(provider))), repr(provider))
|
self.assertIn("at {0}".format(hex(id(provider))), repr(provider))
|
||||||
|
|
|
@ -18,8 +18,8 @@ class SingletonTests(_BaseSingletonTestCase, unittest.TestCase):
|
||||||
provider = self.singleton_cls(Example)
|
provider = self.singleton_cls(Example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'Singleton({0}) at {1}>'.format(
|
"Singleton({0}) at {1}>".format(
|
||||||
repr(Example),
|
repr(Example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
@ -36,8 +36,8 @@ class DelegatedSingletonTests(_BaseSingletonTestCase, unittest.TestCase):
|
||||||
provider = self.singleton_cls(Example)
|
provider = self.singleton_cls(Example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'DelegatedSingleton({0}) at {1}>'.format(
|
"DelegatedSingleton({0}) at {1}>".format(
|
||||||
repr(Example),
|
repr(Example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
@ -50,8 +50,8 @@ class ThreadLocalSingletonTests(_BaseSingletonTestCase, unittest.TestCase):
|
||||||
provider = providers.ThreadLocalSingleton(Example)
|
provider = providers.ThreadLocalSingleton(Example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'ThreadLocalSingleton({0}) at {1}>'.format(
|
"ThreadLocalSingleton({0}) at {1}>".format(
|
||||||
repr(Example),
|
repr(Example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
@ -92,8 +92,8 @@ class DelegatedThreadLocalSingletonTests(_BaseSingletonTestCase,
|
||||||
provider = self.singleton_cls(Example)
|
provider = self.singleton_cls(Example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'DelegatedThreadLocalSingleton({0}) at {1}>'.format(
|
"DelegatedThreadLocalSingleton({0}) at {1}>".format(
|
||||||
repr(Example),
|
repr(Example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
@ -106,8 +106,8 @@ class ThreadSafeSingletonTests(_BaseSingletonTestCase, unittest.TestCase):
|
||||||
provider = self.singleton_cls(Example)
|
provider = self.singleton_cls(Example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'ThreadSafeSingleton({0}) at {1}>'.format(
|
"ThreadSafeSingleton({0}) at {1}>".format(
|
||||||
repr(Example),
|
repr(Example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
@ -125,8 +125,8 @@ class DelegatedThreadSafeSingletonTests(_BaseSingletonTestCase,
|
||||||
provider = self.singleton_cls(Example)
|
provider = self.singleton_cls(Example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'DelegatedThreadSafeSingleton({0}) at {1}>'.format(
|
"DelegatedThreadSafeSingleton({0}) at {1}>".format(
|
||||||
repr(Example),
|
repr(Example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
@ -185,8 +185,8 @@ class AbstractSingletonTests(unittest.TestCase):
|
||||||
provider = providers.AbstractSingleton(Example)
|
provider = providers.AbstractSingleton(Example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'AbstractSingleton({0}) at {1}>'.format(
|
"AbstractSingleton({0}) at {1}>".format(
|
||||||
repr(Example),
|
repr(Example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
|
|
@ -13,8 +13,8 @@ class ContextLocalSingletonTests(_BaseSingletonTestCase, unittest.TestCase):
|
||||||
provider = providers.ContextLocalSingleton(Example)
|
provider = providers.ContextLocalSingleton(Example)
|
||||||
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.'
|
"<dependency_injector.providers."
|
||||||
'ContextLocalSingleton({0}) at {1}>'.format(
|
"ContextLocalSingleton({0}) at {1}>".format(
|
||||||
repr(Example),
|
repr(Example),
|
||||||
hex(id(provider))))
|
hex(id(provider))))
|
||||||
|
|
||||||
|
|
|
@ -101,7 +101,7 @@ class ProviderTests(unittest.TestCase):
|
||||||
class ObjectTests(unittest.TestCase):
|
class ObjectTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_traversal(self):
|
def test_traversal(self):
|
||||||
provider = providers.Object('string')
|
provider = providers.Object("string")
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
self.assertEqual(len(all_providers), 0)
|
self.assertEqual(len(all_providers), 0)
|
||||||
|
|
||||||
|
@ -250,9 +250,9 @@ class CallableTests(unittest.TestCase):
|
||||||
self.assertEqual(len(all_providers), 0)
|
self.assertEqual(len(all_providers), 0)
|
||||||
|
|
||||||
def test_traverse_args(self):
|
def test_traverse_args(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
provider = providers.Callable(list, 'foo', provider1, provider2)
|
provider = providers.Callable(list, "foo", provider1, provider2)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
|
||||||
|
@ -261,9 +261,9 @@ class CallableTests(unittest.TestCase):
|
||||||
self.assertIn(provider2, all_providers)
|
self.assertIn(provider2, all_providers)
|
||||||
|
|
||||||
def test_traverse_kwargs(self):
|
def test_traverse_kwargs(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
provider = providers.Callable(dict, foo='foo', bar=provider1, baz=provider2)
|
provider = providers.Callable(dict, foo="foo", bar=provider1, baz=provider2)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
|
||||||
|
@ -272,10 +272,10 @@ class CallableTests(unittest.TestCase):
|
||||||
self.assertIn(provider2, all_providers)
|
self.assertIn(provider2, all_providers)
|
||||||
|
|
||||||
def test_traverse_overridden(self):
|
def test_traverse_overridden(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
|
|
||||||
provider = providers.Callable(dict, 'foo')
|
provider = providers.Callable(dict, "foo")
|
||||||
provider.override(provider1)
|
provider.override(provider1)
|
||||||
provider.override(provider2)
|
provider.override(provider2)
|
||||||
|
|
||||||
|
@ -287,8 +287,8 @@ class CallableTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_traverse_provides(self):
|
def test_traverse_provides(self):
|
||||||
provider1 = providers.Callable(list)
|
provider1 = providers.Callable(list)
|
||||||
provider2 = providers.Object('bar')
|
provider2 = providers.Object("bar")
|
||||||
provider3 = providers.Object('baz')
|
provider3 = providers.Object("baz")
|
||||||
|
|
||||||
provider = providers.Callable(provider1, provider2)
|
provider = providers.Callable(provider1, provider2)
|
||||||
provider.override(provider3)
|
provider.override(provider3)
|
||||||
|
@ -304,7 +304,7 @@ class CallableTests(unittest.TestCase):
|
||||||
class ConfigurationTests(unittest.TestCase):
|
class ConfigurationTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_traverse(self):
|
def test_traverse(self):
|
||||||
config = providers.Configuration(default={'option1': {'option2': 'option2'}})
|
config = providers.Configuration(default={"option1": {"option2": "option2"}})
|
||||||
option1 = config.option1
|
option1 = config.option1
|
||||||
option2 = config.option1.option2
|
option2 = config.option1.option2
|
||||||
option3 = config.option1[config.option1.option2]
|
option3 = config.option1[config.option1.option2]
|
||||||
|
@ -327,7 +327,7 @@ class ConfigurationTests(unittest.TestCase):
|
||||||
self.assertIn(option, all_providers)
|
self.assertIn(option, all_providers)
|
||||||
|
|
||||||
def test_traverse_overridden(self):
|
def test_traverse_overridden(self):
|
||||||
options = {'option1': {'option2': 'option2'}}
|
options = {"option1": {"option2": "option2"}}
|
||||||
config = providers.Configuration()
|
config = providers.Configuration()
|
||||||
config.from_dict(options)
|
config.from_dict(options)
|
||||||
|
|
||||||
|
@ -339,7 +339,7 @@ class ConfigurationTests(unittest.TestCase):
|
||||||
self.assertIs(overridden, config.last_overriding)
|
self.assertIs(overridden, config.last_overriding)
|
||||||
|
|
||||||
def test_traverse_overridden_option_1(self):
|
def test_traverse_overridden_option_1(self):
|
||||||
options = {'option2': 'option2'}
|
options = {"option2": "option2"}
|
||||||
config = providers.Configuration()
|
config = providers.Configuration()
|
||||||
config.option1.from_dict(options)
|
config.option1.from_dict(options)
|
||||||
|
|
||||||
|
@ -350,7 +350,7 @@ class ConfigurationTests(unittest.TestCase):
|
||||||
self.assertIn(config.last_overriding, all_providers)
|
self.assertIn(config.last_overriding, all_providers)
|
||||||
|
|
||||||
def test_traverse_overridden_option_2(self):
|
def test_traverse_overridden_option_2(self):
|
||||||
options = {'option2': 'option2'}
|
options = {"option2": "option2"}
|
||||||
config = providers.Configuration()
|
config = providers.Configuration()
|
||||||
config.option1.from_dict(options)
|
config.option1.from_dict(options)
|
||||||
|
|
||||||
|
@ -367,9 +367,9 @@ class FactoryTests(unittest.TestCase):
|
||||||
self.assertEqual(len(all_providers), 0)
|
self.assertEqual(len(all_providers), 0)
|
||||||
|
|
||||||
def test_traverse_args(self):
|
def test_traverse_args(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
provider = providers.Factory(list, 'foo', provider1, provider2)
|
provider = providers.Factory(list, "foo", provider1, provider2)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
|
||||||
|
@ -378,9 +378,9 @@ class FactoryTests(unittest.TestCase):
|
||||||
self.assertIn(provider2, all_providers)
|
self.assertIn(provider2, all_providers)
|
||||||
|
|
||||||
def test_traverse_kwargs(self):
|
def test_traverse_kwargs(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
provider = providers.Factory(dict, foo='foo', bar=provider1, baz=provider2)
|
provider = providers.Factory(dict, foo="foo", bar=provider1, baz=provider2)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
|
||||||
|
@ -389,10 +389,10 @@ class FactoryTests(unittest.TestCase):
|
||||||
self.assertIn(provider2, all_providers)
|
self.assertIn(provider2, all_providers)
|
||||||
|
|
||||||
def test_traverse_attributes(self):
|
def test_traverse_attributes(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
provider = providers.Factory(dict)
|
provider = providers.Factory(dict)
|
||||||
provider.add_attributes(foo='foo', bar=provider1, baz=provider2)
|
provider.add_attributes(foo="foo", bar=provider1, baz=provider2)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
|
||||||
|
@ -401,10 +401,10 @@ class FactoryTests(unittest.TestCase):
|
||||||
self.assertIn(provider2, all_providers)
|
self.assertIn(provider2, all_providers)
|
||||||
|
|
||||||
def test_traverse_overridden(self):
|
def test_traverse_overridden(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
|
|
||||||
provider = providers.Factory(dict, 'foo')
|
provider = providers.Factory(dict, "foo")
|
||||||
provider.override(provider1)
|
provider.override(provider1)
|
||||||
provider.override(provider2)
|
provider.override(provider2)
|
||||||
|
|
||||||
|
@ -416,8 +416,8 @@ class FactoryTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_traverse_provides(self):
|
def test_traverse_provides(self):
|
||||||
provider1 = providers.Callable(list)
|
provider1 = providers.Callable(list)
|
||||||
provider2 = providers.Object('bar')
|
provider2 = providers.Object("bar")
|
||||||
provider3 = providers.Object('baz')
|
provider3 = providers.Object("baz")
|
||||||
|
|
||||||
provider = providers.Factory(provider1, provider2)
|
provider = providers.Factory(provider1, provider2)
|
||||||
provider.override(provider3)
|
provider.override(provider3)
|
||||||
|
@ -452,9 +452,9 @@ class BaseSingletonTests(unittest.TestCase):
|
||||||
self.assertEqual(len(all_providers), 0)
|
self.assertEqual(len(all_providers), 0)
|
||||||
|
|
||||||
def test_traverse_args(self):
|
def test_traverse_args(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
provider = providers.Singleton(list, 'foo', provider1, provider2)
|
provider = providers.Singleton(list, "foo", provider1, provider2)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
|
||||||
|
@ -463,9 +463,9 @@ class BaseSingletonTests(unittest.TestCase):
|
||||||
self.assertIn(provider2, all_providers)
|
self.assertIn(provider2, all_providers)
|
||||||
|
|
||||||
def test_traverse_kwargs(self):
|
def test_traverse_kwargs(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
provider = providers.Singleton(dict, foo='foo', bar=provider1, baz=provider2)
|
provider = providers.Singleton(dict, foo="foo", bar=provider1, baz=provider2)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
|
||||||
|
@ -474,10 +474,10 @@ class BaseSingletonTests(unittest.TestCase):
|
||||||
self.assertIn(provider2, all_providers)
|
self.assertIn(provider2, all_providers)
|
||||||
|
|
||||||
def test_traverse_attributes(self):
|
def test_traverse_attributes(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
provider = providers.Singleton(dict)
|
provider = providers.Singleton(dict)
|
||||||
provider.add_attributes(foo='foo', bar=provider1, baz=provider2)
|
provider.add_attributes(foo="foo", bar=provider1, baz=provider2)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
|
||||||
|
@ -486,10 +486,10 @@ class BaseSingletonTests(unittest.TestCase):
|
||||||
self.assertIn(provider2, all_providers)
|
self.assertIn(provider2, all_providers)
|
||||||
|
|
||||||
def test_traverse_overridden(self):
|
def test_traverse_overridden(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
|
|
||||||
provider = providers.Singleton(dict, 'foo')
|
provider = providers.Singleton(dict, "foo")
|
||||||
provider.override(provider1)
|
provider.override(provider1)
|
||||||
provider.override(provider2)
|
provider.override(provider2)
|
||||||
|
|
||||||
|
@ -501,8 +501,8 @@ class BaseSingletonTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_traverse_provides(self):
|
def test_traverse_provides(self):
|
||||||
provider1 = providers.Callable(list)
|
provider1 = providers.Callable(list)
|
||||||
provider2 = providers.Object('bar')
|
provider2 = providers.Object("bar")
|
||||||
provider3 = providers.Object('baz')
|
provider3 = providers.Object("baz")
|
||||||
|
|
||||||
provider = providers.Singleton(provider1, provider2)
|
provider = providers.Singleton(provider1, provider2)
|
||||||
provider.override(provider3)
|
provider.override(provider3)
|
||||||
|
@ -518,9 +518,9 @@ class BaseSingletonTests(unittest.TestCase):
|
||||||
class ListTests(unittest.TestCase):
|
class ListTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_traverse_args(self):
|
def test_traverse_args(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
provider = providers.List('foo', provider1, provider2)
|
provider = providers.List("foo", provider1, provider2)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
|
||||||
|
@ -529,11 +529,11 @@ class ListTests(unittest.TestCase):
|
||||||
self.assertIn(provider2, all_providers)
|
self.assertIn(provider2, all_providers)
|
||||||
|
|
||||||
def test_traverse_overridden(self):
|
def test_traverse_overridden(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
provider3 = providers.List(provider1, provider2)
|
provider3 = providers.List(provider1, provider2)
|
||||||
|
|
||||||
provider = providers.List('foo')
|
provider = providers.List("foo")
|
||||||
provider.override(provider3)
|
provider.override(provider3)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
@ -547,9 +547,9 @@ class ListTests(unittest.TestCase):
|
||||||
class DictTests(unittest.TestCase):
|
class DictTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_traverse_kwargs(self):
|
def test_traverse_kwargs(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
provider = providers.Dict(foo='foo', bar=provider1, baz=provider2)
|
provider = providers.Dict(foo="foo", bar=provider1, baz=provider2)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
|
||||||
|
@ -558,11 +558,11 @@ class DictTests(unittest.TestCase):
|
||||||
self.assertIn(provider2, all_providers)
|
self.assertIn(provider2, all_providers)
|
||||||
|
|
||||||
def test_traverse_overridden(self):
|
def test_traverse_overridden(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
provider3 = providers.Dict(bar=provider1, baz=provider2)
|
provider3 = providers.Dict(bar=provider1, baz=provider2)
|
||||||
|
|
||||||
provider = providers.Dict(foo='foo')
|
provider = providers.Dict(foo="foo")
|
||||||
provider.override(provider3)
|
provider.override(provider3)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
@ -581,9 +581,9 @@ class ResourceTests(unittest.TestCase):
|
||||||
self.assertEqual(len(all_providers), 0)
|
self.assertEqual(len(all_providers), 0)
|
||||||
|
|
||||||
def test_traverse_args(self):
|
def test_traverse_args(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
provider = providers.Resource(list, 'foo', provider1, provider2)
|
provider = providers.Resource(list, "foo", provider1, provider2)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
|
||||||
|
@ -592,9 +592,9 @@ class ResourceTests(unittest.TestCase):
|
||||||
self.assertIn(provider2, all_providers)
|
self.assertIn(provider2, all_providers)
|
||||||
|
|
||||||
def test_traverse_kwargs(self):
|
def test_traverse_kwargs(self):
|
||||||
provider1 = providers.Object('bar')
|
provider1 = providers.Object("bar")
|
||||||
provider2 = providers.Object('baz')
|
provider2 = providers.Object("baz")
|
||||||
provider = providers.Resource(dict, foo='foo', bar=provider1, baz=provider2)
|
provider = providers.Resource(dict, foo="foo", bar=provider1, baz=provider2)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
|
||||||
|
@ -606,7 +606,7 @@ class ResourceTests(unittest.TestCase):
|
||||||
provider1 = providers.Resource(list)
|
provider1 = providers.Resource(list)
|
||||||
provider2 = providers.Resource(tuple)
|
provider2 = providers.Resource(tuple)
|
||||||
|
|
||||||
provider = providers.Resource(dict, 'foo')
|
provider = providers.Resource(dict, "foo")
|
||||||
provider.override(provider1)
|
provider.override(provider1)
|
||||||
provider.override(provider2)
|
provider.override(provider2)
|
||||||
|
|
||||||
|
@ -676,7 +676,7 @@ class ContainerTests(unittest.TestCase):
|
||||||
class SelectorTests(unittest.TestCase):
|
class SelectorTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_traverse(self):
|
def test_traverse(self):
|
||||||
switch = lambda: 'provider1'
|
switch = lambda: "provider1"
|
||||||
provider1 = providers.Callable(list)
|
provider1 = providers.Callable(list)
|
||||||
provider2 = providers.Callable(dict)
|
provider2 = providers.Callable(dict)
|
||||||
|
|
||||||
|
@ -693,7 +693,7 @@ class SelectorTests(unittest.TestCase):
|
||||||
self.assertIn(provider2, all_providers)
|
self.assertIn(provider2, all_providers)
|
||||||
|
|
||||||
def test_traverse_switch(self):
|
def test_traverse_switch(self):
|
||||||
switch = providers.Callable(lambda: 'provider1')
|
switch = providers.Callable(lambda: "provider1")
|
||||||
provider1 = providers.Callable(list)
|
provider1 = providers.Callable(list)
|
||||||
provider2 = providers.Callable(dict)
|
provider2 = providers.Callable(dict)
|
||||||
|
|
||||||
|
@ -713,10 +713,10 @@ class SelectorTests(unittest.TestCase):
|
||||||
def test_traverse_overridden(self):
|
def test_traverse_overridden(self):
|
||||||
provider1 = providers.Callable(list)
|
provider1 = providers.Callable(list)
|
||||||
provider2 = providers.Callable(dict)
|
provider2 = providers.Callable(dict)
|
||||||
selector1 = providers.Selector(lambda: 'provider1', provider1=provider1)
|
selector1 = providers.Selector(lambda: "provider1", provider1=provider1)
|
||||||
|
|
||||||
provider = providers.Selector(
|
provider = providers.Selector(
|
||||||
lambda: 'provider2',
|
lambda: "provider2",
|
||||||
provider2=provider2,
|
provider2=provider2,
|
||||||
)
|
)
|
||||||
provider.override(selector1)
|
provider.override(selector1)
|
||||||
|
@ -788,7 +788,7 @@ class ItemGetterTests(unittest.TestCase):
|
||||||
def test_traverse(self):
|
def test_traverse(self):
|
||||||
provider1 = providers.Provider()
|
provider1 = providers.Provider()
|
||||||
provided = provider1.provided
|
provided = provider1.provided
|
||||||
provider = provided['item']
|
provider = provided["item"]
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
|
||||||
|
@ -801,7 +801,7 @@ class ItemGetterTests(unittest.TestCase):
|
||||||
provided = provider1.provided
|
provided = provider1.provided
|
||||||
provider2 = providers.Provider()
|
provider2 = providers.Provider()
|
||||||
|
|
||||||
provider = provided['item']
|
provider = provided["item"]
|
||||||
provider.override(provider2)
|
provider.override(provider2)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
@ -832,7 +832,7 @@ class MethodCallerTests(unittest.TestCase):
|
||||||
provided = provider1.provided
|
provided = provider1.provided
|
||||||
method = provided.method
|
method = provided.method
|
||||||
provider2 = providers.Provider()
|
provider2 = providers.Provider()
|
||||||
provider = method.call('foo', provider2)
|
provider = method.call("foo", provider2)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
|
||||||
|
@ -847,7 +847,7 @@ class MethodCallerTests(unittest.TestCase):
|
||||||
provided = provider1.provided
|
provided = provider1.provided
|
||||||
method = provided.method
|
method = provided.method
|
||||||
provider2 = providers.Provider()
|
provider2 = providers.Provider()
|
||||||
provider = method.call(foo='foo', bar=provider2)
|
provider = method.call(foo="foo", bar=provider2)
|
||||||
|
|
||||||
all_providers = list(provider.traverse())
|
all_providers = list(provider.traverse())
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ class IsProviderTests(unittest.TestCase):
|
||||||
self.assertFalse(providers.is_provider(providers.Provider))
|
self.assertFalse(providers.is_provider(providers.Provider))
|
||||||
|
|
||||||
def test_with_string(self):
|
def test_with_string(self):
|
||||||
self.assertFalse(providers.is_provider('some_string'))
|
self.assertFalse(providers.is_provider("some_string"))
|
||||||
|
|
||||||
def test_with_object(self):
|
def test_with_object(self):
|
||||||
self.assertFalse(providers.is_provider(object()))
|
self.assertFalse(providers.is_provider(object()))
|
||||||
|
@ -50,7 +50,7 @@ class EnsureIsProviderTests(unittest.TestCase):
|
||||||
def test_with_string(self):
|
def test_with_string(self):
|
||||||
self.assertRaises(errors.Error,
|
self.assertRaises(errors.Error,
|
||||||
providers.ensure_is_provider,
|
providers.ensure_is_provider,
|
||||||
'some_string')
|
"some_string")
|
||||||
|
|
||||||
def test_with_object(self):
|
def test_with_object(self):
|
||||||
self.assertRaises(errors.Error, providers.ensure_is_provider, object())
|
self.assertRaises(errors.Error, providers.ensure_is_provider, object())
|
||||||
|
|
|
@ -11,7 +11,7 @@ class BaseService:
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.logger = logging.getLogger(
|
self.logger = logging.getLogger(
|
||||||
f'{__name__}.{self.__class__.__name__}',
|
f"{__name__}.{self.__class__.__name__}",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,8 +22,8 @@ class UserService(BaseService):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def get_user(self, email: str) -> Dict[str, str]:
|
def get_user(self, email: str) -> Dict[str, str]:
|
||||||
self.logger.debug('User %s has been found in database', email)
|
self.logger.debug("User %s has been found in database", email)
|
||||||
return {'email': email, 'password_hash': '...'}
|
return {"email": email, "password_hash": "..."}
|
||||||
|
|
||||||
|
|
||||||
class AuthService(BaseService):
|
class AuthService(BaseService):
|
||||||
|
@ -36,8 +36,8 @@ class AuthService(BaseService):
|
||||||
def authenticate(self, user: Dict[str, str], password: str) -> None:
|
def authenticate(self, user: Dict[str, str], password: str) -> None:
|
||||||
assert password is not None
|
assert password is not None
|
||||||
self.logger.debug(
|
self.logger.debug(
|
||||||
'User %s has been successfully authenticated',
|
"User %s has been successfully authenticated",
|
||||||
user['email'],
|
user["email"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ class PhotoService(BaseService):
|
||||||
|
|
||||||
def upload_photo(self, user: Dict[str, str], photo_path: str) -> None:
|
def upload_photo(self, user: Dict[str, str], photo_path: str) -> None:
|
||||||
self.logger.debug(
|
self.logger.debug(
|
||||||
'Photo %s has been successfully uploaded by user %s',
|
"Photo %s has been successfully uploaded by user %s",
|
||||||
photo_path,
|
photo_path,
|
||||||
user['email'],
|
user["email"],
|
||||||
)
|
)
|
||||||
|
|
|
@ -9,7 +9,7 @@ from dependency_injector.wiring import inject, Provide
|
||||||
|
|
||||||
class Service:
|
class Service:
|
||||||
async def process(self) -> str:
|
async def process(self) -> str:
|
||||||
return 'Ok'
|
return "Ok"
|
||||||
|
|
||||||
|
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
|
@ -21,19 +21,19 @@ app = FastAPI()
|
||||||
security = HTTPBasic()
|
security = HTTPBasic()
|
||||||
|
|
||||||
|
|
||||||
@app.api_route('/')
|
@app.api_route("/")
|
||||||
@inject
|
@inject
|
||||||
async def index(service: Service = Depends(Provide[Container.service])):
|
async def index(service: Service = Depends(Provide[Container.service])):
|
||||||
result = await service.process()
|
result = await service.process()
|
||||||
return {'result': result}
|
return {"result": result}
|
||||||
|
|
||||||
|
|
||||||
@app.get('/auth')
|
@app.get("/auth")
|
||||||
@inject
|
@inject
|
||||||
def read_current_user(
|
def read_current_user(
|
||||||
credentials: HTTPBasicCredentials = Depends(security)
|
credentials: HTTPBasicCredentials = Depends(security)
|
||||||
):
|
):
|
||||||
return {'username': credentials.username, 'password': credentials.password}
|
return {"username": credentials.username, "password": credentials.password}
|
||||||
|
|
||||||
|
|
||||||
container = Container()
|
container = Container()
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
import sys
|
|
||||||
|
|
||||||
from flask import Flask, jsonify, request, current_app, session, g
|
from flask import Flask, jsonify, request, current_app, session, g
|
||||||
from flask import _request_ctx_stack, _app_ctx_stack
|
from flask import _request_ctx_stack, _app_ctx_stack
|
||||||
from dependency_injector import containers, providers
|
from dependency_injector import containers, providers
|
||||||
|
@ -12,7 +10,7 @@ _request_ctx_stack, _app_ctx_stack # noqa
|
||||||
|
|
||||||
class Service:
|
class Service:
|
||||||
def process(self) -> str:
|
def process(self) -> str:
|
||||||
return 'Ok'
|
return "Ok"
|
||||||
|
|
||||||
|
|
||||||
class Container(containers.DeclarativeContainer):
|
class Container(containers.DeclarativeContainer):
|
||||||
|
@ -23,12 +21,12 @@ class Container(containers.DeclarativeContainer):
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route("/")
|
||||||
@inject
|
@inject
|
||||||
def index(service: Service = Provide[Container.service]):
|
def index(service: Service = Provide[Container.service]):
|
||||||
result = service.process()
|
result = service.process()
|
||||||
return jsonify({'result': result})
|
return jsonify({"result": result})
|
||||||
|
|
||||||
|
|
||||||
container = Container()
|
container = Container()
|
||||||
container.wire(modules=[sys.modules[__name__]])
|
container.wire(modules=[__name__])
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
if 'pypy' not in sys.version.lower():
|
if "pypy" not in sys.version.lower():
|
||||||
import numpy # noqa
|
import numpy # noqa
|
||||||
from numpy import * # noqa
|
from numpy import * # noqa
|
||||||
|
|
||||||
|
|
|
@ -96,7 +96,7 @@ def test_provider_provider(service_provider: Callable[..., Service] = Provider[C
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def test_provided_instance(some_value: int = Provide[Container.service.provided.foo['bar'].call()]):
|
def test_provided_instance(some_value: int = Provide[Container.service.provided.foo["bar"].call()]):
|
||||||
return some_value
|
return some_value
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,4 +2,4 @@
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
|
|
||||||
|
|
||||||
__all__ = ('Queue',)
|
__all__ = ("Queue",)
|
||||||
|
|
|
@ -36,15 +36,15 @@ class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
async def async_injection(
|
async def async_injection(
|
||||||
resource1: object = Provide['resource1'],
|
resource1: object = Provide["resource1"],
|
||||||
resource2: object = Provide['resource2'],
|
resource2: object = Provide["resource2"],
|
||||||
):
|
):
|
||||||
return resource1, resource2
|
return resource1, resource2
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
async def async_injection_with_closing(
|
async def async_injection_with_closing(
|
||||||
resource1: object = Closing[Provide['resource1']],
|
resource1: object = Closing[Provide["resource1"]],
|
||||||
resource2: object = Closing[Provide['resource2']],
|
resource2: object = Closing[Provide["resource2"]],
|
||||||
):
|
):
|
||||||
return resource1, resource2
|
return resource1, resource2
|
||||||
|
|
|
@ -19,58 +19,58 @@ from .container import Container
|
||||||
from .service import Service
|
from .service import Service
|
||||||
|
|
||||||
|
|
||||||
service: Service = Provide['service']
|
service: Service = Provide["service"]
|
||||||
service_provider: Callable[..., Service] = Provider['service']
|
service_provider: Callable[..., Service] = Provider["service"]
|
||||||
undefined: Callable = Provide['undefined']
|
undefined: Callable = Provide["undefined"]
|
||||||
|
|
||||||
|
|
||||||
class TestClass:
|
class TestClass:
|
||||||
|
|
||||||
service: Service = Provide['service']
|
service: Service = Provide["service"]
|
||||||
service_provider: Callable[..., Service] = Provider['service']
|
service_provider: Callable[..., Service] = Provider["service"]
|
||||||
undefined: Callable = Provide['undefined']
|
undefined: Callable = Provide["undefined"]
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def __init__(self, service: Service = Provide['service']):
|
def __init__(self, service: Service = Provide["service"]):
|
||||||
self.service = service
|
self.service = service
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def method(self, service: Service = Provide['service']):
|
def method(self, service: Service = Provide["service"]):
|
||||||
return service
|
return service
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@inject
|
@inject
|
||||||
def class_method(cls, service: Service = Provide['service']):
|
def class_method(cls, service: Service = Provide["service"]):
|
||||||
return service
|
return service
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@inject
|
@inject
|
||||||
def static_method(service: Service = Provide['service']):
|
def static_method(service: Service = Provide["service"]):
|
||||||
return service
|
return service
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def test_function(service: Service = Provide['service']):
|
def test_function(service: Service = Provide["service"]):
|
||||||
return service
|
return service
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def test_function_provider(service_provider: Callable[..., Service] = Provider['service']):
|
def test_function_provider(service_provider: Callable[..., Service] = Provider["service"]):
|
||||||
service = service_provider()
|
service = service_provider()
|
||||||
return service
|
return service
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def test_config_value(
|
def test_config_value(
|
||||||
value_int: int = Provide['config.a.b.c', as_int()],
|
value_int: int = Provide["config.a.b.c", as_int()],
|
||||||
value_float: float = Provide['config.a.b.c', as_float()],
|
value_float: float = Provide["config.a.b.c", as_float()],
|
||||||
value_str: str = Provide['config.a.b.c', as_(str)],
|
value_str: str = Provide["config.a.b.c", as_(str)],
|
||||||
value_decimal: Decimal = Provide['config.a.b.c', as_(Decimal)],
|
value_decimal: Decimal = Provide["config.a.b.c", as_(Decimal)],
|
||||||
value_required: str = Provide['config.a.b.c', required()],
|
value_required: str = Provide["config.a.b.c", required()],
|
||||||
value_required_int: int = Provide['config.a.b.c', required().as_int()],
|
value_required_int: int = Provide["config.a.b.c", required().as_int()],
|
||||||
value_required_float: float = Provide['config.a.b.c', required().as_float()],
|
value_required_float: float = Provide["config.a.b.c", required().as_float()],
|
||||||
value_required_str: str = Provide['config.a.b.c', required().as_(str)],
|
value_required_str: str = Provide["config.a.b.c", required().as_(str)],
|
||||||
value_required_decimal: str = Provide['config.a.b.c', required().as_(Decimal)],
|
value_required_decimal: str = Provide["config.a.b.c", required().as_(Decimal)],
|
||||||
):
|
):
|
||||||
return (
|
return (
|
||||||
value_int,
|
value_int,
|
||||||
|
@ -87,36 +87,36 @@ def test_config_value(
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def test_config_value_required_undefined(
|
def test_config_value_required_undefined(
|
||||||
value_required: int = Provide['config.a.b.c', required()],
|
value_required: int = Provide["config.a.b.c", required()],
|
||||||
):
|
):
|
||||||
return value_required
|
return value_required
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def test_provide_provider(service_provider: Callable[..., Service] = Provide['service.provider']):
|
def test_provide_provider(service_provider: Callable[..., Service] = Provide["service.provider"]):
|
||||||
service = service_provider()
|
service = service_provider()
|
||||||
return service
|
return service
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def test_provided_instance(some_value: int = Provide['service', provided().foo['bar'].call()]):
|
def test_provided_instance(some_value: int = Provide["service", provided().foo["bar"].call()]):
|
||||||
return some_value
|
return some_value
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def test_subcontainer_provider(some_value: int = Provide['sub.int_object']):
|
def test_subcontainer_provider(some_value: int = Provide["sub.int_object"]):
|
||||||
return some_value
|
return some_value
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def test_config_invariant(some_value: int = Provide['config.option', invariant('config.switch')]):
|
def test_config_invariant(some_value: int = Provide["config.option", invariant("config.switch")]):
|
||||||
return some_value
|
return some_value
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def test_provide_from_different_containers(
|
def test_provide_from_different_containers(
|
||||||
service: Service = Provide['service'],
|
service: Service = Provide["service"],
|
||||||
some_value: int = Provide['int_object'],
|
some_value: int = Provide["int_object"],
|
||||||
):
|
):
|
||||||
return service, some_value
|
return service, some_value
|
||||||
|
|
||||||
|
@ -131,9 +131,9 @@ class ClassDecorator:
|
||||||
|
|
||||||
@ClassDecorator
|
@ClassDecorator
|
||||||
@inject
|
@inject
|
||||||
def test_class_decorator(service: Service = Provide['service']):
|
def test_class_decorator(service: Service = Provide["service"]):
|
||||||
return service
|
return service
|
||||||
|
|
||||||
|
|
||||||
def test_container(container: Container = Provide['<container>']):
|
def test_container(container: Container = Provide["<container>"]):
|
||||||
return container.service()
|
return container.service()
|
||||||
|
|
|
@ -4,5 +4,5 @@ from ...service import Service
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def test_function(service: Service = Provide['service']):
|
def test_function(service: Service = Provide["service"]):
|
||||||
return service
|
return service
|
||||||
|
|
|
@ -33,5 +33,5 @@ class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def test_function(service: Service = Closing[Provide['service']]):
|
def test_function(service: Service = Closing[Provide["service"]]):
|
||||||
return service
|
return service
|
||||||
|
|
|
@ -14,19 +14,19 @@ class FromSchemaTests(unittest.TestCase):
|
||||||
container = containers.DynamicContainer()
|
container = containers.DynamicContainer()
|
||||||
container.from_schema(
|
container.from_schema(
|
||||||
{
|
{
|
||||||
'version': '1',
|
"version": "1",
|
||||||
'container': {
|
"container": {
|
||||||
'provider1': {
|
"provider1": {
|
||||||
'provider': 'Factory',
|
"provider": "Factory",
|
||||||
'provides': 'list',
|
"provides": "list",
|
||||||
'args': [1, 2, 3],
|
"args": [1, 2, 3],
|
||||||
},
|
},
|
||||||
'provider2': {
|
"provider2": {
|
||||||
'provider': 'Factory',
|
"provider": "Factory",
|
||||||
'provides': 'dict',
|
"provides": "dict",
|
||||||
'kwargs': {
|
"kwargs": {
|
||||||
'one': 'container.provider1',
|
"one": "container.provider1",
|
||||||
'two': 2,
|
"two": 2,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -39,7 +39,7 @@ class FromSchemaTests(unittest.TestCase):
|
||||||
|
|
||||||
self.assertIsInstance(container.provider2, providers.Factory)
|
self.assertIsInstance(container.provider2, providers.Factory)
|
||||||
self.assertIs(container.provider2.provides, dict)
|
self.assertIs(container.provider2.provides, dict)
|
||||||
self.assertEqual(container.provider2.kwargs, {'one': container.provider1, 'two': 2})
|
self.assertEqual(container.provider2.kwargs, {"one": container.provider1, "two": 2})
|
||||||
|
|
||||||
|
|
||||||
class FromYamlSchemaTests(unittest.TestCase):
|
class FromYamlSchemaTests(unittest.TestCase):
|
||||||
|
@ -48,8 +48,8 @@ class FromYamlSchemaTests(unittest.TestCase):
|
||||||
container = containers.DynamicContainer()
|
container = containers.DynamicContainer()
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||||
schema_path = os.path.join(tmp_dir, 'schema.yml')
|
schema_path = os.path.join(tmp_dir, "schema.yml")
|
||||||
with open(schema_path, 'w') as file:
|
with open(schema_path, "w") as file:
|
||||||
file.write("""
|
file.write("""
|
||||||
version: "1"
|
version: "1"
|
||||||
container:
|
container:
|
||||||
|
@ -76,14 +76,14 @@ class FromYamlSchemaTests(unittest.TestCase):
|
||||||
|
|
||||||
self.assertIsInstance(container.provider2, providers.Factory)
|
self.assertIsInstance(container.provider2, providers.Factory)
|
||||||
self.assertIs(container.provider2.provides, dict)
|
self.assertIs(container.provider2.provides, dict)
|
||||||
self.assertEqual(container.provider2.kwargs, {'one': container.provider1, 'two': 2})
|
self.assertEqual(container.provider2.kwargs, {"one": container.provider1, "two": 2})
|
||||||
|
|
||||||
def test_with_loader(self):
|
def test_with_loader(self):
|
||||||
container = containers.DynamicContainer()
|
container = containers.DynamicContainer()
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||||
schema_path = os.path.join(tmp_dir, 'schema.yml')
|
schema_path = os.path.join(tmp_dir, "schema.yml")
|
||||||
with open(schema_path, 'w') as file:
|
with open(schema_path, "w") as file:
|
||||||
file.write("""
|
file.write("""
|
||||||
version: "1"
|
version: "1"
|
||||||
container:
|
container:
|
||||||
|
@ -109,13 +109,13 @@ class FromYamlSchemaTests(unittest.TestCase):
|
||||||
container = containers.DynamicContainer()
|
container = containers.DynamicContainer()
|
||||||
with no_yaml_module():
|
with no_yaml_module():
|
||||||
with self.assertRaises(errors.Error) as error:
|
with self.assertRaises(errors.Error) as error:
|
||||||
container.from_yaml_schema('./no-yaml-installed.yml')
|
container.from_yaml_schema("./no-yaml-installed.yml")
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
error.exception.args[0],
|
error.exception.args[0],
|
||||||
'Unable to load yaml schema - PyYAML is not installed. '
|
"Unable to load yaml schema - PyYAML is not installed. "
|
||||||
'Install PyYAML or install Dependency Injector with yaml extras: '
|
"Install PyYAML or install Dependency Injector with yaml extras: "
|
||||||
'"pip install dependency-injector[yaml]"',
|
"\"pip install dependency-injector[yaml]\"",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -125,24 +125,24 @@ class FromJsonSchemaTests(unittest.TestCase):
|
||||||
container = containers.DynamicContainer()
|
container = containers.DynamicContainer()
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||||
schema_path = os.path.join(tmp_dir, 'schema.json')
|
schema_path = os.path.join(tmp_dir, "schema.json")
|
||||||
with open(schema_path, 'w') as file:
|
with open(schema_path, "w") as file:
|
||||||
file.write(
|
file.write(
|
||||||
json.dumps(
|
json.dumps(
|
||||||
{
|
{
|
||||||
'version': '1',
|
"version": "1",
|
||||||
'container': {
|
"container": {
|
||||||
'provider1': {
|
"provider1": {
|
||||||
'provider': 'Factory',
|
"provider": "Factory",
|
||||||
'provides': 'list',
|
"provides": "list",
|
||||||
'args': [1, 2, 3],
|
"args": [1, 2, 3],
|
||||||
},
|
},
|
||||||
'provider2': {
|
"provider2": {
|
||||||
'provider': 'Factory',
|
"provider": "Factory",
|
||||||
'provides': 'dict',
|
"provides": "dict",
|
||||||
'kwargs': {
|
"kwargs": {
|
||||||
'one': 'container.provider1',
|
"one": "container.provider1",
|
||||||
'two': 2,
|
"two": 2,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -159,4 +159,4 @@ class FromJsonSchemaTests(unittest.TestCase):
|
||||||
|
|
||||||
self.assertIsInstance(container.provider2, providers.Factory)
|
self.assertIsInstance(container.provider2, providers.Factory)
|
||||||
self.assertIs(container.provider2.provides, dict)
|
self.assertIs(container.provider2.provides, dict)
|
||||||
self.assertEqual(container.provider2.kwargs, {'one': container.provider1, 'two': 2})
|
self.assertEqual(container.provider2.kwargs, {"one": container.provider1, "two": 2})
|
||||||
|
|
|
@ -8,13 +8,13 @@ import os
|
||||||
_TOP_DIR = os.path.abspath(
|
_TOP_DIR = os.path.abspath(
|
||||||
os.path.sep.join((
|
os.path.sep.join((
|
||||||
os.path.dirname(__file__),
|
os.path.dirname(__file__),
|
||||||
'../',
|
"../",
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
_SAMPLES_DIR = os.path.abspath(
|
_SAMPLES_DIR = os.path.abspath(
|
||||||
os.path.sep.join((
|
os.path.sep.join((
|
||||||
os.path.dirname(__file__),
|
os.path.dirname(__file__),
|
||||||
'../samples/',
|
"../samples/",
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
import sys
|
import sys
|
||||||
|
@ -27,17 +27,17 @@ class TestSchemaSingleContainer(unittest.TestCase):
|
||||||
|
|
||||||
def test(self):
|
def test(self):
|
||||||
container = containers.DynamicContainer()
|
container = containers.DynamicContainer()
|
||||||
container.from_yaml_schema(f'{_SAMPLES_DIR}/schemasample/container-single.yml')
|
container.from_yaml_schema(f"{_SAMPLES_DIR}/schemasample/container-single.yml")
|
||||||
container.config.from_dict({
|
container.config.from_dict({
|
||||||
'database': {
|
"database": {
|
||||||
'dsn': ':memory:',
|
"dsn": ":memory:",
|
||||||
},
|
},
|
||||||
'aws': {
|
"aws": {
|
||||||
'access_key_id': 'KEY',
|
"access_key_id": "KEY",
|
||||||
'secret_access_key': 'SECRET',
|
"secret_access_key": "SECRET",
|
||||||
},
|
},
|
||||||
'auth': {
|
"auth": {
|
||||||
'token_ttl': 3600,
|
"token_ttl": 3600,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -90,17 +90,17 @@ class TestSchemaMultipleContainers(unittest.TestCase):
|
||||||
|
|
||||||
def test(self):
|
def test(self):
|
||||||
container = containers.DynamicContainer()
|
container = containers.DynamicContainer()
|
||||||
container.from_yaml_schema(f'{_SAMPLES_DIR}/schemasample/container-multiple.yml')
|
container.from_yaml_schema(f"{_SAMPLES_DIR}/schemasample/container-multiple.yml")
|
||||||
container.core.config.from_dict({
|
container.core.config.from_dict({
|
||||||
'database': {
|
"database": {
|
||||||
'dsn': ':memory:',
|
"dsn": ":memory:",
|
||||||
},
|
},
|
||||||
'aws': {
|
"aws": {
|
||||||
'access_key_id': 'KEY',
|
"access_key_id": "KEY",
|
||||||
'secret_access_key': 'SECRET',
|
"secret_access_key": "SECRET",
|
||||||
},
|
},
|
||||||
'auth': {
|
"auth": {
|
||||||
'token_ttl': 3600,
|
"token_ttl": 3600,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -153,17 +153,17 @@ class TestSchemaMultipleContainersReordered(unittest.TestCase):
|
||||||
|
|
||||||
def test(self):
|
def test(self):
|
||||||
container = containers.DynamicContainer()
|
container = containers.DynamicContainer()
|
||||||
container.from_yaml_schema(f'{_SAMPLES_DIR}/schemasample/container-multiple-reordered.yml')
|
container.from_yaml_schema(f"{_SAMPLES_DIR}/schemasample/container-multiple-reordered.yml")
|
||||||
container.core.config.from_dict({
|
container.core.config.from_dict({
|
||||||
'database': {
|
"database": {
|
||||||
'dsn': ':memory:',
|
"dsn": ":memory:",
|
||||||
},
|
},
|
||||||
'aws': {
|
"aws": {
|
||||||
'access_key_id': 'KEY',
|
"access_key_id": "KEY",
|
||||||
'secret_access_key': 'SECRET',
|
"secret_access_key": "SECRET",
|
||||||
},
|
},
|
||||||
'auth': {
|
"auth": {
|
||||||
'token_ttl': 3600,
|
"token_ttl": 3600,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -216,17 +216,17 @@ class TestSchemaMultipleContainersWithInlineProviders(unittest.TestCase):
|
||||||
|
|
||||||
def test(self):
|
def test(self):
|
||||||
container = containers.DynamicContainer()
|
container = containers.DynamicContainer()
|
||||||
container.from_yaml_schema(f'{_SAMPLES_DIR}/schemasample/container-multiple-inline.yml')
|
container.from_yaml_schema(f"{_SAMPLES_DIR}/schemasample/container-multiple-inline.yml")
|
||||||
container.core.config.from_dict({
|
container.core.config.from_dict({
|
||||||
'database': {
|
"database": {
|
||||||
'dsn': ':memory:',
|
"dsn": ":memory:",
|
||||||
},
|
},
|
||||||
'aws': {
|
"aws": {
|
||||||
'access_key_id': 'KEY',
|
"access_key_id": "KEY",
|
||||||
'secret_access_key': 'SECRET',
|
"secret_access_key": "SECRET",
|
||||||
},
|
},
|
||||||
'auth': {
|
"auth": {
|
||||||
'token_ttl': 3600,
|
"token_ttl": 3600,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -277,18 +277,18 @@ class TestSchemaMultipleContainersWithInlineProviders(unittest.TestCase):
|
||||||
|
|
||||||
class TestSchemaBoto3Session(unittest.TestCase):
|
class TestSchemaBoto3Session(unittest.TestCase):
|
||||||
|
|
||||||
@unittest.skip('Boto3 tries to connect to the internet')
|
@unittest.skip("Boto3 tries to connect to the internet")
|
||||||
def test(self):
|
def test(self):
|
||||||
container = containers.DynamicContainer()
|
container = containers.DynamicContainer()
|
||||||
container.from_yaml_schema(f'{_SAMPLES_DIR}/schemasample/container-boto3-session.yml')
|
container.from_yaml_schema(f"{_SAMPLES_DIR}/schemasample/container-boto3-session.yml")
|
||||||
container.config.from_dict(
|
container.config.from_dict(
|
||||||
{
|
{
|
||||||
'aws_access_key_id': 'key',
|
"aws_access_key_id": "key",
|
||||||
'aws_secret_access_key': 'secret',
|
"aws_secret_access_key": "secret",
|
||||||
'aws_session_token': 'token',
|
"aws_session_token": "token",
|
||||||
'aws_region_name': 'us-east-1',
|
"aws_region_name": "us-east-1",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertEqual(container.s3_client().__class__.__name__, 'S3')
|
self.assertEqual(container.s3_client().__class__.__name__, "S3")
|
||||||
self.assertEqual(container.sqs_client().__class__.__name__, 'SQS')
|
self.assertEqual(container.sqs_client().__class__.__name__, "SQS")
|
||||||
|
|
|
@ -8,4 +8,4 @@ from dependency_injector import __version__
|
||||||
class VersionTest(unittest.TestCase):
|
class VersionTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_version_follows_semantic_versioning(self):
|
def test_version_follows_semantic_versioning(self):
|
||||||
self.assertEqual(len(__version__.split('.')), 3)
|
self.assertEqual(len(__version__.split(".")), 3)
|
||||||
|
|
|
@ -18,13 +18,13 @@ import os
|
||||||
_TOP_DIR = os.path.abspath(
|
_TOP_DIR = os.path.abspath(
|
||||||
os.path.sep.join((
|
os.path.sep.join((
|
||||||
os.path.dirname(__file__),
|
os.path.dirname(__file__),
|
||||||
'../',
|
"../",
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
_SAMPLES_DIR = os.path.abspath(
|
_SAMPLES_DIR = os.path.abspath(
|
||||||
os.path.sep.join((
|
os.path.sep.join((
|
||||||
os.path.dirname(__file__),
|
os.path.dirname(__file__),
|
||||||
'../samples/',
|
"../samples/",
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
import sys
|
import sys
|
||||||
|
@ -44,7 +44,7 @@ class WiringTest(unittest.TestCase):
|
||||||
container: Container
|
container: Container
|
||||||
|
|
||||||
def setUp(self) -> None:
|
def setUp(self) -> None:
|
||||||
self.container = Container(config={'a': {'b': {'c': 10}}})
|
self.container = Container(config={"a": {"b": {"c": 10}}})
|
||||||
self.container.wire(
|
self.container.wire(
|
||||||
modules=[module],
|
modules=[module],
|
||||||
packages=[package],
|
packages=[package],
|
||||||
|
@ -77,7 +77,7 @@ class WiringTest(unittest.TestCase):
|
||||||
self.container.wire(modules=[module_invalid_attr_injection])
|
self.container.wire(modules=[module_invalid_attr_injection])
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(context.exception),
|
str(context.exception),
|
||||||
'Unknown type of marker {0}'.format(module_invalid_attr_injection.service),
|
"Unknown type of marker {0}".format(module_invalid_attr_injection.service),
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_class_wiring(self):
|
def test_class_wiring(self):
|
||||||
|
@ -153,17 +153,17 @@ class WiringTest(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(value_int, 10)
|
self.assertEqual(value_int, 10)
|
||||||
self.assertEqual(value_float, 10.0)
|
self.assertEqual(value_float, 10.0)
|
||||||
self.assertEqual(value_str, '10')
|
self.assertEqual(value_str, "10")
|
||||||
self.assertEqual(value_decimal, Decimal(10))
|
self.assertEqual(value_decimal, Decimal(10))
|
||||||
self.assertEqual(value_required, 10)
|
self.assertEqual(value_required, 10)
|
||||||
self.assertEqual(value_required_int, 10)
|
self.assertEqual(value_required_int, 10)
|
||||||
self.assertEqual(value_required_float, 10.0)
|
self.assertEqual(value_required_float, 10.0)
|
||||||
self.assertEqual(value_required_str, '10')
|
self.assertEqual(value_required_str, "10")
|
||||||
self.assertEqual(value_required_decimal, Decimal(10))
|
self.assertEqual(value_required_decimal, Decimal(10))
|
||||||
|
|
||||||
def test_configuration_option_required_undefined(self):
|
def test_configuration_option_required_undefined(self):
|
||||||
self.container.config.reset_override()
|
self.container.config.reset_override()
|
||||||
with self.assertRaisesRegex(errors.Error, 'Undefined configuration option "config.a.b.c"'):
|
with self.assertRaisesRegex(errors.Error, "Undefined configuration option \"config.a.b.c\""):
|
||||||
module.test_config_value_required_undefined()
|
module.test_config_value_required_undefined()
|
||||||
|
|
||||||
def test_provide_provider(self):
|
def test_provide_provider(self):
|
||||||
|
@ -177,7 +177,7 @@ class WiringTest(unittest.TestCase):
|
||||||
def test_provided_instance(self):
|
def test_provided_instance(self):
|
||||||
class TestService:
|
class TestService:
|
||||||
foo = {
|
foo = {
|
||||||
'bar': lambda: 10,
|
"bar": lambda: 10,
|
||||||
}
|
}
|
||||||
|
|
||||||
with self.container.service.override(TestService()):
|
with self.container.service.override(TestService()):
|
||||||
|
@ -190,22 +190,22 @@ class WiringTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_config_invariant(self):
|
def test_config_invariant(self):
|
||||||
config = {
|
config = {
|
||||||
'option': {
|
"option": {
|
||||||
'a': 1,
|
"a": 1,
|
||||||
'b': 2,
|
"b": 2,
|
||||||
},
|
},
|
||||||
'switch': 'a',
|
"switch": "a",
|
||||||
}
|
}
|
||||||
self.container.config.from_dict(config)
|
self.container.config.from_dict(config)
|
||||||
|
|
||||||
value_default = module.test_config_invariant()
|
value_default = module.test_config_invariant()
|
||||||
self.assertEqual(value_default, 1)
|
self.assertEqual(value_default, 1)
|
||||||
|
|
||||||
with self.container.config.switch.override('a'):
|
with self.container.config.switch.override("a"):
|
||||||
value_a = module.test_config_invariant()
|
value_a = module.test_config_invariant()
|
||||||
self.assertEqual(value_a, 1)
|
self.assertEqual(value_a, 1)
|
||||||
|
|
||||||
with self.container.config.switch.override('b'):
|
with self.container.config.switch.override("b"):
|
||||||
value_b = module.test_config_invariant()
|
value_b = module.test_config_invariant()
|
||||||
self.assertEqual(value_b, 2)
|
self.assertEqual(value_b, 2)
|
||||||
|
|
||||||
|
@ -364,7 +364,7 @@ class WiringWithStringModuleAndPackageNamesTest(unittest.TestCase):
|
||||||
class ModuleAsPackageTest(unittest.TestCase):
|
class ModuleAsPackageTest(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.container = Container(config={'a': {'b': {'c': 10}}})
|
self.container = Container(config={"a": {"b": {"c": 10}}})
|
||||||
self.addCleanup(self.container.unwire)
|
self.addCleanup(self.container.unwire)
|
||||||
|
|
||||||
def test_module_as_package_wiring(self):
|
def test_module_as_package_wiring(self):
|
||||||
|
@ -484,7 +484,7 @@ class AutoLoaderTest(unittest.TestCase):
|
||||||
container: Container
|
container: Container
|
||||||
|
|
||||||
def setUp(self) -> None:
|
def setUp(self) -> None:
|
||||||
self.container = Container(config={'a': {'b': {'c': 10}}})
|
self.container = Container(config={"a": {"b": {"c": 10}}})
|
||||||
importlib.reload(module)
|
importlib.reload(module)
|
||||||
|
|
||||||
def tearDown(self) -> None:
|
def tearDown(self) -> None:
|
||||||
|
@ -500,7 +500,7 @@ class AutoLoaderTest(unittest.TestCase):
|
||||||
def test_register_container(self):
|
def test_register_container(self):
|
||||||
register_loader_containers(self.container)
|
register_loader_containers(self.container)
|
||||||
importlib.reload(module)
|
importlib.reload(module)
|
||||||
importlib.import_module('wiringsamples.imports')
|
importlib.import_module("wiringsamples.imports")
|
||||||
|
|
||||||
service = module.test_function()
|
service = module.test_function()
|
||||||
self.assertIsInstance(service, Service)
|
self.assertIsInstance(service, Service)
|
||||||
|
|
|
@ -18,13 +18,13 @@ import os
|
||||||
_TOP_DIR = os.path.abspath(
|
_TOP_DIR = os.path.abspath(
|
||||||
os.path.sep.join((
|
os.path.sep.join((
|
||||||
os.path.dirname(__file__),
|
os.path.dirname(__file__),
|
||||||
'../',
|
"../",
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
_SAMPLES_DIR = os.path.abspath(
|
_SAMPLES_DIR = os.path.abspath(
|
||||||
os.path.sep.join((
|
os.path.sep.join((
|
||||||
os.path.dirname(__file__),
|
os.path.dirname(__file__),
|
||||||
'../samples/',
|
"../samples/",
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
import sys
|
import sys
|
||||||
|
@ -43,7 +43,7 @@ class WiringTest(unittest.TestCase):
|
||||||
container: Container
|
container: Container
|
||||||
|
|
||||||
def setUp(self) -> None:
|
def setUp(self) -> None:
|
||||||
self.container = Container(config={'a': {'b': {'c': 10}}})
|
self.container = Container(config={"a": {"b": {"c": 10}}})
|
||||||
self.container.wire(
|
self.container.wire(
|
||||||
modules=[module],
|
modules=[module],
|
||||||
packages=[package],
|
packages=[package],
|
||||||
|
@ -143,17 +143,17 @@ class WiringTest(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(value_int, 10)
|
self.assertEqual(value_int, 10)
|
||||||
self.assertEqual(value_float, 10.0)
|
self.assertEqual(value_float, 10.0)
|
||||||
self.assertEqual(value_str, '10')
|
self.assertEqual(value_str, "10")
|
||||||
self.assertEqual(value_decimal, Decimal(10))
|
self.assertEqual(value_decimal, Decimal(10))
|
||||||
self.assertEqual(value_required, 10)
|
self.assertEqual(value_required, 10)
|
||||||
self.assertEqual(value_required_int, 10)
|
self.assertEqual(value_required_int, 10)
|
||||||
self.assertEqual(value_required_float, 10.0)
|
self.assertEqual(value_required_float, 10.0)
|
||||||
self.assertEqual(value_required_str, '10')
|
self.assertEqual(value_required_str, "10")
|
||||||
self.assertEqual(value_required_decimal, Decimal(10))
|
self.assertEqual(value_required_decimal, Decimal(10))
|
||||||
|
|
||||||
def test_configuration_option_required_undefined(self):
|
def test_configuration_option_required_undefined(self):
|
||||||
self.container.config.reset_override()
|
self.container.config.reset_override()
|
||||||
with self.assertRaisesRegex(errors.Error, 'Undefined configuration option "config.a.b.c"'):
|
with self.assertRaisesRegex(errors.Error, "Undefined configuration option \"config.a.b.c\""):
|
||||||
module.test_config_value_required_undefined()
|
module.test_config_value_required_undefined()
|
||||||
|
|
||||||
def test_provide_provider(self):
|
def test_provide_provider(self):
|
||||||
|
@ -163,7 +163,7 @@ class WiringTest(unittest.TestCase):
|
||||||
def test_provided_instance(self):
|
def test_provided_instance(self):
|
||||||
class TestService:
|
class TestService:
|
||||||
foo = {
|
foo = {
|
||||||
'bar': lambda: 10,
|
"bar": lambda: 10,
|
||||||
}
|
}
|
||||||
|
|
||||||
with self.container.service.override(TestService()):
|
with self.container.service.override(TestService()):
|
||||||
|
@ -176,22 +176,22 @@ class WiringTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_config_invariant(self):
|
def test_config_invariant(self):
|
||||||
config = {
|
config = {
|
||||||
'option': {
|
"option": {
|
||||||
'a': 1,
|
"a": 1,
|
||||||
'b': 2,
|
"b": 2,
|
||||||
},
|
},
|
||||||
'switch': 'a',
|
"switch": "a",
|
||||||
}
|
}
|
||||||
self.container.config.from_dict(config)
|
self.container.config.from_dict(config)
|
||||||
|
|
||||||
value_default = module.test_config_invariant()
|
value_default = module.test_config_invariant()
|
||||||
self.assertEqual(value_default, 1)
|
self.assertEqual(value_default, 1)
|
||||||
|
|
||||||
with self.container.config.switch.override('a'):
|
with self.container.config.switch.override("a"):
|
||||||
value_a = module.test_config_invariant()
|
value_a = module.test_config_invariant()
|
||||||
self.assertEqual(value_a, 1)
|
self.assertEqual(value_a, 1)
|
||||||
|
|
||||||
with self.container.config.switch.override('b'):
|
with self.container.config.switch.override("b"):
|
||||||
value_b = module.test_config_invariant()
|
value_b = module.test_config_invariant()
|
||||||
self.assertEqual(value_b, 2)
|
self.assertEqual(value_b, 2)
|
||||||
|
|
||||||
|
@ -418,7 +418,7 @@ class AutoLoaderTest(unittest.TestCase):
|
||||||
container: Container
|
container: Container
|
||||||
|
|
||||||
def setUp(self) -> None:
|
def setUp(self) -> None:
|
||||||
self.container = Container(config={'a': {'b': {'c': 10}}})
|
self.container = Container(config={"a": {"b": {"c": 10}}})
|
||||||
importlib.reload(module)
|
importlib.reload(module)
|
||||||
|
|
||||||
def tearDown(self) -> None:
|
def tearDown(self) -> None:
|
||||||
|
|
|
@ -5,13 +5,13 @@ import os
|
||||||
_TOP_DIR = os.path.abspath(
|
_TOP_DIR = os.path.abspath(
|
||||||
os.path.sep.join((
|
os.path.sep.join((
|
||||||
os.path.dirname(__file__),
|
os.path.dirname(__file__),
|
||||||
'../',
|
"../",
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
_SAMPLES_DIR = os.path.abspath(
|
_SAMPLES_DIR = os.path.abspath(
|
||||||
os.path.sep.join((
|
os.path.sep.join((
|
||||||
os.path.dirname(__file__),
|
os.path.dirname(__file__),
|
||||||
'../samples/',
|
"../samples/",
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
import sys
|
import sys
|
||||||
|
@ -29,7 +29,7 @@ class WiringFastAPITest(AsyncTestCase):
|
||||||
|
|
||||||
def setUp(self) -> None:
|
def setUp(self) -> None:
|
||||||
super().setUp()
|
super().setUp()
|
||||||
self.client = AsyncClient(app=web.app, base_url='http://test')
|
self.client = AsyncClient(app=web.app, base_url="http://test")
|
||||||
|
|
||||||
def tearDown(self) -> None:
|
def tearDown(self) -> None:
|
||||||
self._run(self.client.aclose())
|
self._run(self.client.aclose())
|
||||||
|
@ -38,15 +38,15 @@ class WiringFastAPITest(AsyncTestCase):
|
||||||
def test_depends_marker_injection(self):
|
def test_depends_marker_injection(self):
|
||||||
class ServiceMock:
|
class ServiceMock:
|
||||||
async def process(self):
|
async def process(self):
|
||||||
return 'Foo'
|
return "Foo"
|
||||||
|
|
||||||
with web.container.service.override(ServiceMock()):
|
with web.container.service.override(ServiceMock()):
|
||||||
response = self._run(self.client.get('/'))
|
response = self._run(self.client.get("/"))
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(response.json(), {'result': 'Foo'})
|
self.assertEqual(response.json(), {"result": "Foo"})
|
||||||
|
|
||||||
def test_depends_injection(self):
|
def test_depends_injection(self):
|
||||||
response = self._run(self.client.get('/auth', auth=('john_smith', 'secret')))
|
response = self._run(self.client.get("/auth", auth=("john_smith", "secret")))
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(response.json(), {'username': 'john_smith', 'password': 'secret'})
|
self.assertEqual(response.json(), {"username": "john_smith", "password": "secret"})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user