示例#1
0
    def test_simple(self):
        config = ConfigStore()
        f = StringIO(SIMPLE_TOML_CONFIG)
        config.load_from_file(f)

        gravity = config.option('gravity', cast=bool, default=True)
        self.assertEqual(gravity.get(), True)

        gravity.set(False)
        self.assertEqual(gravity.get(), False)

        title = config.option('title', default=None)
        self.assertEqual(title.get(), 'something')

        n = config.option('testnumber', cast=int, default=None)
        self.assertEqual(n.get(), 42)

        s = config.option('testnumber', cast=str, default=None)
        self.assertEqual(s.get(), '42')
示例#2
0
    def test_get(self):
        config = ConfigStore()
        test = config.option('testthing')
        f = StringIO(SIMPLE_TOML_CONFIG)
        config.load_from_file(f)

        self.assertEqual(test.get(), None)

        test.set('something')

        self.assertEqual(test.get(), 'something')
示例#3
0
    def test_validation(self):
        config = ConfigStore()
        f = StringIO(SIMPLE_TOML_CONFIG)
        config.load_from_file(f)

        bounded = config.option('testboundednumber',
                                cast=int,
                                validate=lambda n: 0 < n < 11,
                                default=5)

        with self.assertRaises(ValueError):
            bounded.set(30)

        self.assertEqual(bounded.get(), 5)

        bounded.set(6)
        self.assertEqual(bounded.get(), 6)
示例#4
0
    def test_raw_loading(self):
        config = ConfigStore()
        config.load_from_dict({})
        name = config.option('name')
        port = config.section('server').option('port')

        obj = {'server': {'port': 4567}, 'name': 'thing'}
        config.load_from_dict(obj)
        self.assertEqual(name.get(), 'thing')

        obj = {'server': {'port': 42}}
        config.update_from_dict(obj)
        self.assertEqual(port.get(), 42)
        self.assertEqual(name.get(), 'thing')

        obj = {'server': {'port': 4567}, 'name': 'thing'}
        config.load_from_dict(obj)
        self.assertEqual(port.get(), 4567)
        self.assertEqual(name.get(), 'thing')