示例#1
0
文件: tests.py 项目: rmca/confix
 def test_schema_base(self):
     @register('name')
     class config:
         foo = schema(10)
     self.dict_to_file({})
     parse(self.TESTFN)
     self.assertEqual(config.foo, 10)
示例#2
0
文件: tests.py 项目: giampaolo/confix
    def test_root_only(self):
        @register()
        class root_conf:
            root_value = 1

        self.assertRaises(NotParsedError, get_parsed_conf)
        parse()
        assert get_parsed_conf() == {'root_value': 1}
示例#3
0
文件: tests.py 项目: giampaolo/confix
    def test_hidden_key(self):
        @register()
        class config:
            foo = 1
            _hidden = 2

        parse()
        assert get_parsed_conf() == {'foo': 1}
示例#4
0
文件: tests.py 项目: rmca/confix
 def test_validator_ok(self):
     @register('name')
     class config:
         foo = schema(10, validator=lambda x: isinstance(x, int))
     self.dict_to_file({
         'name': dict(foo=5)
     })
     parse(self.TESTFN)
示例#5
0
文件: tests.py 项目: giampaolo/confix
 def test_conf_file_w_unknown_ext(self):
     # Conf file with unsupported extension.
     with open(TESTFN, 'w') as f:
         f.write('foo')
     self.addCleanup(safe_remove, TESTFN)
     with self.assertRaises(ValueError) as cm:
         parse(TESTFN)
     assert "don't know how to parse" in str(cm.exception)
     assert "extension not supported" in str(cm.exception)
示例#6
0
文件: tests.py 项目: rmca/confix
 def test_empty_conf_file(self):
     @register('name')
     class config:
         foo = 1
         bar = 2
     self.write_to_file("   ")
     parse(self.TESTFN)
     self.assertEqual(config.foo, 1)
     self.assertEqual(config.bar, 2)
示例#7
0
文件: tests.py 项目: rmca/confix
 def test_schema_base_overwritten(self):
     @register('name')
     class config:
         foo = schema(10, required=True)
     self.dict_to_file({
         'name': dict(foo=5)
     })
     parse(self.TESTFN)
     self.assertEqual(config.foo, 5)
示例#8
0
文件: tests.py 项目: giampaolo/confix
    def test_parse_called_twice(self):
        @register()
        class config:
            foo = 1
            bar = 2

        parse()
        self.assertRaises(AlreadyParsedError, parse)
        self.assertRaises(AlreadyParsedError, parse_with_envvars)
示例#9
0
文件: tests.py 项目: giampaolo/confix
    def test_no_conf_file(self):
        # parse() is supposed to parse also if no conf file is passed
        @register()
        class config:
            foo = 1
            bar = schema(10)

        parse()
        assert config.foo == 1
        assert config.bar == 10
示例#10
0
文件: tests.py 项目: rmca/confix
 def test_already_configured(self):
     @register('name')
     class config:
         foo = 1
         bar = 2
     self.dict_to_file({
         'name': dict(foo=5, bar=6)
     })
     parse(self.TESTFN)
     self.assertRaises(Error, parse, self.TESTFN)
示例#11
0
文件: tests.py 项目: giampaolo/confix
    def test_sub_plus_root(self):
        @register('sub')
        class sub_conf:
            sub_value = 1

        @register()
        class root_conf:
            root_value = 1

        parse()
        assert get_parsed_conf() == {'root_value': 1, 'sub': {'sub_value': 1}}
示例#12
0
文件: tests.py 项目: rmca/confix
 def test_float(self):
     @register('name')
     class config:
         foo = 1.1
         bar = 2
     self.write_to_file(textwrap.dedent("""
         [name]
         foo = 1.3
     """))
     parse(self.TESTFN)
     self.assertEqual(config.foo, 1.3)
示例#13
0
文件: tests.py 项目: rmca/confix
 def test_conf_file_overrides_one(self):
     @register('name')
     class config:
         foo = 1
         bar = 2
     self.dict_to_file({
         'name': dict(foo=5)
     })
     parse(self.TESTFN)
     self.assertEqual(config.foo, 5)
     self.assertEqual(config.bar, 2)
示例#14
0
文件: tests.py 项目: rmca/confix
 def test_validator_ko(self):
     @register('name')
     class config:
         foo = schema(10, validator=lambda x: isinstance(x, str))
     self.dict_to_file({
         'name': dict(foo=5)
     })
     with self.assertRaises(ValidationError) as cm:
         parse(self.TESTFN)
     self.assertEqual(cm.exception.section, 'name')
     self.assertEqual(cm.exception.key, 'foo')
     self.assertEqual(cm.exception.value, 5)
示例#15
0
文件: tests.py 项目: rmca/confix
 def test_schema_base_required(self):
     @register('name')
     class config:
         foo = schema(10, required=True)
         bar = 2
     self.dict_to_file({
         'name': dict(bar=2)
     })
     with self.assertRaises(RequiredKeyError) as cm:
         parse(self.TESTFN)
     self.assertEqual(cm.exception.section, 'name')
     self.assertEqual(cm.exception.key, 'foo')
示例#16
0
文件: tests.py 项目: rmca/confix
 def test_invalid_field(self):
     @register('name')
     class config:
         foo = 1
         bar = 2
     self.dict_to_file({
         'name': dict(foo=5, apple=6)
     })
     with self.assertRaises(InvalidKeyError) as cm:
         parse(self.TESTFN)
     self.assertEqual(cm.exception.section, 'name')
     self.assertEqual(cm.exception.key, 'apple')
示例#17
0
文件: tests.py 项目: rmca/confix
 def test_false(self):
     @register('name')
     class config:
         foo = None
         bar = 2
     true_values = ("0", "no", "false", "off")
     for value in true_values:
         self.write_to_file(textwrap.dedent("""
             [name]
             foo = %s
         """ % (value)))
         parse(self.TESTFN)
         self.assertEqual(config.foo, False)
         discard()
示例#18
0
文件: tests.py 项目: giampaolo/confix
    def test_dictify_and_method(self):
        @register()
        class config:
            foo = 1
            bar = 2
            _hidden = 3

            @classmethod
            def some_method(cls):
                return 1

        assert dict(config) == {'foo': 1, 'bar': 2}
        assert config.some_method() == 1
        parse()
        assert dict(config) == {'foo': 1, 'bar': 2}
        assert config.some_method() == 1
示例#19
0
文件: tests.py 项目: rmca/confix
    def test_validator_ko_custom_exc_w_message(self):
        def validator(value):
            raise ValidationError('message')

        @register('name')
        class config:
            foo = schema(10, validator=validator)
        self.dict_to_file({
            'name': dict(foo=5)
        })
        with self.assertRaises(ValidationError) as cm:
            parse(self.TESTFN)
        self.assertEqual(cm.exception.section, 'name')
        self.assertEqual(cm.exception.key, 'foo')
        self.assertEqual(cm.exception.value, 5)
        self.assertEqual(cm.exception.msg, 'message')
示例#20
0
文件: tests.py 项目: giampaolo/confix
    def test_file_like(self):
        @register()
        class foo:
            foo = 1

        file = io.StringIO()
        with self.assertRaises(Error) as cm:
            parse(file)
        assert str(cm.exception) == \
            "can't determine file format from a file object with no 'name' " \
            "attribute"

        assert str(cm.exception) == \
            "can't determine file format from a file object with no 'name' " \
            "attribute"

        file = io.StringIO()
        parse(file, file_parser=lambda x: {})
示例#21
0
文件: tests.py 项目: giampaolo/confix
    def test_register_after_parse(self):
        @register()
        class config:
            foo = 1

        parse()

        with warnings.catch_warnings(record=True) as ws:
            @register(section="unparsed")
            class unparsed_config:
                bar = 1

        assert len(ws) == 1
        assert 'configuration class defined after parse' in \
            str(ws[0].message)
        assert ws[0].category is UserWarning
        # global conf will not include this
        assert get_parsed_conf() == {'foo': 1}
        # but it's still a magic object
        assert dict(unparsed_config) == {'bar': 1}
示例#22
0
文件: tests.py 项目: giampaolo/confix
 def parse(self, *args, **kwargs):
     parse(*args, **kwargs)