def test_missing_required(self): # should raise an error if the value is required and no default is provided (STRING_PROP) config_dict = {'INT_PROP': 2} with self.assertRaises(ValueError): SampleConfig(loaders=[DictLoader(config_dict)]) # should not raise an error if the value is required and a default is provided (INT_PROP) config_dict = {'STRING_PROP': 'blah blah'} config = SampleConfig(loaders=[DictLoader(config_dict)]) self.assertEqual(config.INT_PROP, 0) self.assertEqual(config.STRING_PROP, 'blah blah')
def test_allow_nones(self): # test that intentional None value in first priority loader is not overriden config_dict = { 'STRING_PROP': '1', 'INT_PROP': 3, 'ANY_TYPE_PROP': None } os.environ['ANY_TYPE_PROP'] = 'this should not get through' self.env_keys_set.append('ANY_TYPE_PROP') config_env_second = SampleConfig(loaders=[DictLoader(config_dict), EnvLoader()]) self.assertIsNone(config_env_second.ANY_TYPE_PROP)
def test_as_dict(self): config_dict = { 'STRING_PROP': '1', 'INT_PROP': '2', 'ANY_TYPE_PROP': [1, 2, 3], } expected_dict = { 'STRING_PROP': u'1', 'INT_PROP': 2, 'ANY_TYPE_PROP': [1, 2, 3], 'DIFFERENT_KEY_PROP': None, 'ONLY_IN_ENV': None } config = SampleConfig(loaders=[DictLoader(config_dict)]) self.assertDictEqual(expected_dict, config.as_dict())
def test_varz(self): config_dict = { 'STRING_PROP': 'abc', 'INT_PROP': 2, 'DifferentKey': ['whatever'] } config = SampleConfig([DictLoader(config_dict)]) expected_varz = { 'INT_PROP': 2, 'DIFFERENT_KEY_PROP': ['whatever'], 'ONLY_IN_ENV': None, 'ANY_TYPE_PROP': None } self.assertDictEqual(config.varz, expected_varz)
def test_from_dict(self): config_dict = { 'STRING_PROP': '1', 'INT_PROP': '2', 'ANY_TYPE_PROP': [1, 2, 3], 'NOT_A_PROPERTY': 'boo' } # construct config from dict config = SampleConfig(loaders=[DictLoader(config_dict)]) self.assertEqual(config.STRING_PROP, '1') self.assertEqual(config.INT_PROP, 2) self.assertEqual(config.ANY_TYPE_PROP, [1, 2, 3]) with self.assertRaises(AttributeError): config.NOT_A_PROPERTY
def test_loader_priority(self): config_dict = { 'STRING_PROP': '1', 'INT_PROP': '2', 'ANY_TYPE_PROP': [1, 2, 3], 'NOT_A_PROPERTY': 'boo' } # construct config with env overrides config_with_env = SampleConfig(loaders=[EnvLoader(), DictLoader(config_dict)]) self.assertEqual(config_with_env.STRING_PROP, 'let\'s see if overrides work') self.assertEqual(config_with_env.INT_PROP, 2) self.assertEqual(config_with_env.ONLY_IN_ENV, 'this is only in the env!') with self.assertRaises(AttributeError): config_with_env.NOT_A_PROPERTY
def test_dict_loader(self): config_dict = {'KEY': 'value', 'foo': 'bar'} loader = DictLoader(config_dict) self.assertTrue(loader.exists('KEY')) self.assertTrue('KEY' in loader) self.assertTrue(loader.exists('foo')) self.assertTrue('foo' in loader) self.assertFalse(loader.exists('bar')) self.assertFalse('bar' in loader) self.assertEqual(loader.get('KEY'), 'value') self.assertEqual(loader.get('foo'), 'bar')
def test_fail_validation(self): config_dict = {'STRING_PROP': '1', 'INT_PROP': 'not an int'} with self.assertRaises(ConversionError): SampleConfig(loaders=[DictLoader(config_dict)])