mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-29 04:53:59 +03:00
Add default value setting for Configuration provider
This commit is contained in:
parent
70e82ca7d8
commit
d4f4c47242
File diff suppressed because it is too large
Load Diff
|
@ -667,15 +667,20 @@ cdef class Configuration(Provider):
|
||||||
print(config.section1.option2()) # 2
|
print(config.section1.option2()) # 2
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, name):
|
def __init__(self, name, default=None):
|
||||||
"""Initializer.
|
"""Initializer.
|
||||||
|
|
||||||
:param name: Name of configuration unit.
|
:param name: Name of configuration unit.
|
||||||
:type name: str
|
:type name: str
|
||||||
|
|
||||||
|
:param default: Default values of configuration unit.
|
||||||
|
:type default: dict
|
||||||
"""
|
"""
|
||||||
self.__name = name
|
self.__name = name
|
||||||
self.__value = None
|
self.__value = None
|
||||||
self.__children = dict()
|
self.__children = dict()
|
||||||
|
if default is not None:
|
||||||
|
self.update(default)
|
||||||
super(Configuration, self).__init__()
|
super(Configuration, self).__init__()
|
||||||
|
|
||||||
def __deepcopy__(self, memo):
|
def __deepcopy__(self, memo):
|
||||||
|
|
|
@ -60,6 +60,21 @@ class ConfigTests(unittest.TestCase):
|
||||||
self.assertEqual(abc(), 1)
|
self.assertEqual(abc(), 1)
|
||||||
self.assertEqual(abd(), 2)
|
self.assertEqual(abd(), 2)
|
||||||
|
|
||||||
|
def test_providers_with_default_value(self):
|
||||||
|
|
||||||
|
self.config = providers.Configuration(
|
||||||
|
name='config', default={'a': {'b': {'c': 1, 'd': 2}}})
|
||||||
|
|
||||||
|
a = self.config.a
|
||||||
|
ab = self.config.a.b
|
||||||
|
abc = self.config.a.b.c
|
||||||
|
abd = self.config.a.b.d
|
||||||
|
|
||||||
|
self.assertEqual(a(), {'b': {'c': 1, 'd': 2}})
|
||||||
|
self.assertEqual(ab(), {'c': 1, 'd': 2})
|
||||||
|
self.assertEqual(abc(), 1)
|
||||||
|
self.assertEqual(abd(), 2)
|
||||||
|
|
||||||
def test_value_of_undefined_option(self):
|
def test_value_of_undefined_option(self):
|
||||||
self.assertIsNone(self.config.a())
|
self.assertIsNone(self.config.a())
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user