def test_constructor(self): a = Configuration('a', 5, 'int', validity='\d+', desc='bla', readonly=True, hidden=True) self.assertEqual('a', a.name) self.assertEqual(5, a.value) self.assertEqual('int', a.type) self.assertEqual(True, a.readonly) self.assertEqual(True, a.hidden) self.assertEqual('\d+', a.validity) self.assertEqual('bla', a.desc) c = Configuration() d = Configuration('d', 5, 'int', validity='\d+', desc='bla', parent=c) e = Configuration('e', parent=d) c.hidden = True c.readonly = True self.assertEqual('', c.name) self.assertEqual(None, c.value) self.assertEqual('', c.type) self.assertEqual(True, c.readonly) self.assertEqual(True, c.hidden) self.assertEqual('', c.validity) self.assertEqual('', c.desc) self.assertEqual('d', d.name) self.assertEqual(5, d.value) self.assertEqual('int', d.type) self.assertEqual(True, d.readonly) self.assertEqual(True, d.hidden) self.assertEqual('\d+', d.validity) self.assertEqual('bla', d.desc) self.assertEqual('d.e', e.name)
def test_errors_for_undefined_keys(self): cfg = Configuration() with self.assertRaises(KeyError): cfg['a'] with self.assertRaises(AttributeError): cfg.a with self.assertRaises(AttributeError): cfg.a = 9 with self.assertRaises(AttributeError): cfg.b.desc = 'schweinebacke'
def test_from_and_to_properties(self): properties = [Property('a'), Property('a.b', 5, int, '\d+', True, True, 'doc'), Property('b', 5, int, '\d+', True, True, 'doc')] conf = Configuration.from_properties(properties) assert properties == list(conf.to_properties()) assert 'a' in conf assert 'a.b' in conf assert 'b' in conf
def test_replace_cannot_add_new(self): Configuration().replace({'new': None})
def test_replace_changes_existing(self): conf = Configuration.from_properties([Property('b', 'old')]) newvalues = {'b': 'replaced'} assert newvalues == conf.replace(newvalues)
def test_update(self): conf = Configuration.from_properties([Property('b', 'old')]) newvalues = {'b': 'replaced', 'c': 'new'} assert newvalues == conf.update(newvalues)
def test_attribute_access(self): p = Property('b', 5, int, '\d+', True, True, 'doc') conf = Configuration.from_properties([p]) assert 5 == conf['b'] assert p == conf.property('b')
def test_from_mapping(self): mapping = {'a': None, 'a.b': 5, 'b': 7} assert mapping == Configuration.from_mapping(mapping)
def test_equals_works_with_dict(self): assert {} == Configuration() assert {'a': 1} != Configuration()
def test_constructor(self): from collections import Mapping assert isinstance(Configuration(), Mapping) assert not len(Configuration())