示例#1
0
class TestBaseConfig(unittest.TestCase):
    """Test BaseConfig class"""
    def setUp(self):
        self.cfg = BaseConfig({'foo':'bar'})

    def tearDown(self):
        del self.cfg

    def test_create_cfg_from_dict(self):
        """Test create configuration from dictionary"""
        self.assertIsInstance(self.cfg, BaseConfig)
        self.assertDictEqual(self.cfg, {'foo':'bar'})
        self.assertListEqual(self.cfg.sections, ['foo'])

    def test_create_cfg_from_nested_dict(self):
        """Test create configuration from nested dictionary"""
        cfg = BaseConfig({'foo': BaseConfig({'bar':'foobar'})})
        self.assertIsInstance(cfg, BaseConfig)
        self.assertIsInstance(cfg['foo'], BaseConfig)
        self.assertDictEqual(cfg['foo'], {'bar':'foobar'})
        self.assertListEqual(cfg.sections, ['foo'])
        self.assertListEqual(cfg['foo'].sections, ['bar'])

    def test_create_cfg_from_args(self):
        """Test create configuration from *args"""
        cfg = BaseConfig(foo="bar", bar=BaseConfig(foo="bar", bar="foo"))
        self.assertSetEqual (set(cfg.sections), set(['bar', 'foo']))
        self.assertIsInstance(cfg['bar'], BaseConfig)
        del cfg

    def test_create_cfg_from_args_kwargs(self):
        """Test create configuration from *args and **kwargs"""
        cfg = BaseConfig(foo="bar", bar=BaseConfig(foo="bar", bar="foo"), **{'foobar':'bar', 'barfoo':BaseConfig({'foo':'bar'})})
        self.assertSetEqual (set(cfg.sections), set(['bar', 'foo', 'foobar', 'barfoo']))
        self.assertIsInstance(cfg['bar'], BaseConfig)
        self.assertIsInstance(cfg['barfoo'], BaseConfig)
        del cfg

    @raises(TypeError)
    def test_add_section_dict(self):
        """Test adding a section to config as dict"""
        self.cfg.add_section({'foobar':'bar'})

    def test_add_section_str(self):
        """Test adding a section to config as str"""
        self.cfg.add_section('foobar')
        self.assertSetEqual(set(self.cfg.sections), set(['foo', 'foobar']))

    def test_update_config(self):
        """Test updating configuration with another configuration object. """
        cfg2 = BaseConfig({'bar':'foo'})
        self.cfg.update(cfg2)
        self.assertDictEqual(self.cfg, {'foo':'bar', 'bar':'foo'})
        del cfg2

    def test_update_config_same_section(self):
        """Test updating configuration with another configuration object whose
        sections overlap. Note that this will overwrite the first
        configuration. FIXME: should this be intended behaviour?
        """
        cfg2 = BaseConfig({'foo':'foobar'})
        self.cfg.update(cfg2)
        self.assertDictEqual(self.cfg, {'foo':'foobar'})
        del cfg2

    def test_update_config_nested(self):
        """Test updating configuration with another nested configuration"""
        cfg2 = BaseConfig({'bar': BaseConfig({'foo':'bar'})})
        self.cfg.update(cfg2)
        self.assertDictEqual(self.cfg, {'foo':'bar', 'bar':{'foo':'bar'}})
        del cfg2

    def test_setting_config_section_to_dict(self):
        """Test setting a configuration section to a dict"""
        self.cfg['foo'] = {'foo':'bar'}
        self.assertIsInstance(self.cfg['foo'], BaseConfig)

    def test_baseconfig_from_nested_dictionary(self):
        """Instantiate BaseConfig object from nested dictionary"""
        d = BaseConfig({'foo':'bar', 'bar' : {'foo':'bar', 'bar':{'foo':'bar', 'bar':{'foo':'bar'}}}})
        def assert_sections(d):
            for k,v in d.items():
                if isinstance(v,dict):
                    self.assertIsInstance(v, BaseConfig)
        assert_sections(d)

    def test_getitem(self):
        """Test getting an item from BaseConfig"""
        d = BaseConfig({'foo' : {'bar' : 'foo'}, 'bar' : lambda: "bar", 'foobar' : lambda x: x, 'barfoo' : lambda x: x['foo'], 'foofoo' : None})
        self.assertIsInstance(d['foo'], BaseConfig)
        self.assertDictEqual(d['foo'], {'bar' : 'foo'})
        self.assertIsInstance(d['foo']['bar'], str)
        self.assertEqual(d['foo']['bar'], 'foo')
        self.assertIsInstance(d['bar'], str)
        self.assertEqual(d['bar'], 'bar')
        self.assertIsInstance(d['foobar', "test"], str)
        self.assertEqual(d['foobar', "test"], "test")
        self.assertIsInstance(d['barfoo', {'foo' : "test"}], str)
        self.assertEqual(d['barfoo', {'foo' : "test"}], "test")
        self.assertIsNone (d['foofoo'])
示例#2
0
 def setUp(self):
     self.cfg = BaseConfig({'foo':'bar'})