Add default value setting for Configuration provider

This commit is contained in:
Roman Mogilatov 2017-07-07 17:43:12 +03:00
parent 70e82ca7d8
commit d4f4c47242
3 changed files with 1285 additions and 1217 deletions

File diff suppressed because it is too large Load Diff

View File

@ -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):

View File

@ -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())