Added Raw Value Loading to Test Fixture

This patch adds a method by which raw key/value pairs may be
loaded into the test fixture, without them being registered
first. Tests have been provided.

Change-Id: I53d77e4782bf97aefee11edd489ff1c9ff6c69a3
This commit is contained in:
Michael Krotscheck 2015-04-09 07:55:48 -07:00
parent 1e9033aa80
commit e30131a1ef
2 changed files with 54 additions and 0 deletions

View File

@ -116,3 +116,27 @@ class Config(fixtures.Fixture):
"""
for opt in opts:
self.register_cli_opt(opt, group=group)
def load_raw_values(self, group=None, **kwargs):
"""Load raw values into the configuration without registering them.
This method adds a series of parameters into the current config
instance, as if they had been loaded by a ConfigParser. This method
does not require that you register the configuration options first,
however the values loaded will not be accessible until you do.
"""
# Make sure the namespace exists for our tests.
if not self.conf._namespace:
self.conf.__call__(args=[])
# Default out the group name
group = 'DEFAULT' if not group else group
raw_config = dict()
raw_config[group] = dict()
for key, value in six.iteritems(kwargs):
# Parsed values are an array of raw strings.
raw_config[group][key] = [str(value)]
self.conf._namespace._add_parsed_config_file(raw_config, raw_config)

View File

@ -85,3 +85,33 @@ class ConfigTestCase(base.BaseTestCase):
opt.default)
self.config_fixture.cleanUp()
self.assertRaises(cfg.NoSuchOptError, conf.get, 'new_test_opt')
def test_load_raw_values(self):
self.config_fixture.load_raw_values(first_test_opt='loaded_value_1',
second_test_opt='loaded_value_2')
# Must not be registered.
self.assertRaises(cfg.NoSuchOptError, conf.get, 'first_test_opt')
self.assertRaises(cfg.NoSuchOptError, conf.get, 'second_test_opt')
opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1')
opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2')
self.config_fixture.register_opt(opt1)
self.config_fixture.register_opt(opt2)
self.assertEqual(conf.first_test_opt, 'loaded_value_1')
self.assertEqual(conf.second_test_opt, 'loaded_value_2')
# Cleanup.
self.config_fixture.cleanUp()
# Must no longer be registered.
self.assertRaises(cfg.NoSuchOptError, conf.get, 'first_test_opt')
self.assertRaises(cfg.NoSuchOptError, conf.get, 'second_test_opt')
# Even when registered, must be default.
self.config_fixture.register_opt(opt1)
self.config_fixture.register_opt(opt2)
self.assertEqual(conf.first_test_opt, 'initial_value_1')
self.assertEqual(conf.second_test_opt, 'initial_value_2')