mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Add tests for providers.Config & refactoring of it
This commit is contained in:
parent
1736a6e008
commit
47d33e524c
|
@ -303,23 +303,27 @@ class Config(Provider):
|
||||||
|
|
||||||
def __getattr__(self, item):
|
def __getattr__(self, item):
|
||||||
"""Return instance of deferred config."""
|
"""Return instance of deferred config."""
|
||||||
return _DeferredConfig(parents=(item,),
|
return _ChildConfig(parents=(item,), root_config=self)
|
||||||
root_config=self)
|
|
||||||
|
|
||||||
def __call__(self, paths=None):
|
def __call__(self, paths=None):
|
||||||
"""Return provided instance."""
|
"""Return provided instance."""
|
||||||
value = self.value
|
value = self.value
|
||||||
if paths:
|
if paths:
|
||||||
for path in paths:
|
for path in paths:
|
||||||
|
try:
|
||||||
value = value[path]
|
value = value[path]
|
||||||
|
except KeyError:
|
||||||
|
raise Error('Config key '
|
||||||
|
'"{}" is undefined'.format('.'.join(paths)))
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
class _DeferredConfig(Provider):
|
class _ChildConfig(Provider):
|
||||||
|
|
||||||
"""Deferred config provider.
|
"""Child config provider.
|
||||||
|
|
||||||
Deferred config providers provide an value from the root config object.
|
Child config provide an value from the root config object according to
|
||||||
|
the current path in the config tree.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__slots__ = ('parents', 'root_config')
|
__slots__ = ('parents', 'root_config')
|
||||||
|
@ -328,11 +332,11 @@ class _DeferredConfig(Provider):
|
||||||
"""Initializer."""
|
"""Initializer."""
|
||||||
self.parents = parents
|
self.parents = parents
|
||||||
self.root_config = root_config
|
self.root_config = root_config
|
||||||
super(_DeferredConfig, self).__init__()
|
super(_ChildConfig, self).__init__()
|
||||||
|
|
||||||
def __getattr__(self, item):
|
def __getattr__(self, item):
|
||||||
"""Return instance of deferred config."""
|
"""Return instance of deferred config."""
|
||||||
return _DeferredConfig(parents=self.parents + (item,),
|
return _ChildConfig(parents=self.parents + (item,),
|
||||||
root_config=self.root_config)
|
root_config=self.root_config)
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
|
|
|
@ -480,7 +480,7 @@ class CallableTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_is_provider(self):
|
def test_is_provider(self):
|
||||||
"""Test `is_provider` check."""
|
"""Test `is_provider` check."""
|
||||||
self.assertTrue(is_provider(Callable(map)))
|
self.assertTrue(is_provider(self.provider))
|
||||||
|
|
||||||
def test_call(self):
|
def test_call(self):
|
||||||
"""Test provider call."""
|
"""Test provider call."""
|
||||||
|
@ -517,4 +517,59 @@ class ConfigTests(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""Set test cases environment up."""
|
"""Set test cases environment up."""
|
||||||
|
self.initial_data = dict(key='value',
|
||||||
|
category=dict(setting='setting_value'))
|
||||||
|
self.provider = Config(self.initial_data)
|
||||||
|
|
||||||
|
def test_is_provider(self):
|
||||||
|
"""Test `is_provider` check."""
|
||||||
|
self.assertTrue(is_provider(self.provider))
|
||||||
|
|
||||||
|
def test_init_without_initial_value(self):
|
||||||
|
"""Test provider's creation with no initial value."""
|
||||||
|
self.assertEqual(Config()(), dict())
|
||||||
|
|
||||||
|
def test_call(self):
|
||||||
|
"""Test returning of config value."""
|
||||||
|
self.assertEqual(self.provider(), self.initial_data)
|
||||||
|
|
||||||
|
def test_update_from(self):
|
||||||
|
"""Test update of config value."""
|
||||||
|
self.assertEqual(self.provider(), self.initial_data)
|
||||||
|
|
||||||
|
self.initial_data['key'] = 'other_value'
|
||||||
|
self.provider.update_from(self.initial_data)
|
||||||
|
self.assertEqual(self.provider(), self.initial_data)
|
||||||
|
|
||||||
|
def test_call_child(self):
|
||||||
|
"""Test returning of child config values."""
|
||||||
|
category = self.provider.category
|
||||||
|
category_setting = self.provider.category.setting
|
||||||
|
|
||||||
|
self.assertTrue(is_provider(category))
|
||||||
|
self.assertTrue(is_provider(category_setting))
|
||||||
|
|
||||||
|
self.assertEqual(category(), self.initial_data['category'])
|
||||||
|
self.assertEqual(category_setting(),
|
||||||
|
self.initial_data['category']['setting'])
|
||||||
|
|
||||||
|
def test_call_deferred_child_and_update_from(self):
|
||||||
|
"""Test returning of deferred child config values."""
|
||||||
self.provider = Config()
|
self.provider = Config()
|
||||||
|
category = self.provider.category
|
||||||
|
category_setting = self.provider.category.setting
|
||||||
|
|
||||||
|
self.assertTrue(is_provider(category))
|
||||||
|
self.assertTrue(is_provider(category_setting))
|
||||||
|
|
||||||
|
self.provider.update_from(self.initial_data)
|
||||||
|
|
||||||
|
self.assertEqual(category(), self.initial_data['category'])
|
||||||
|
self.assertEqual(category_setting(),
|
||||||
|
self.initial_data['category']['setting'])
|
||||||
|
|
||||||
|
def test_call_deferred_child_with_empty_value(self):
|
||||||
|
"""Test returning of deferred child config values."""
|
||||||
|
self.provider = Config()
|
||||||
|
category_setting = self.provider.category.setting
|
||||||
|
self.assertRaises(Error, category_setting)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user