Exemple #1
0
 def test_schema_base(self):
     @register('name')
     class config:
         foo = schema(10)
     self.dict_to_file({})
     parse(self.TESTFN)
     self.assertEqual(config.foo, 10)
Exemple #2
0
    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}
Exemple #3
0
    def test_hidden_key(self):
        @register()
        class config:
            foo = 1
            _hidden = 2

        parse()
        assert get_parsed_conf() == {'foo': 1}
Exemple #4
0
 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)
Exemple #5
0
 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)
Exemple #6
0
 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)
Exemple #7
0
 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)
Exemple #8
0
    def test_parse_called_twice(self):
        @register()
        class config:
            foo = 1
            bar = 2

        parse()
        self.assertRaises(AlreadyParsedError, parse)
        self.assertRaises(AlreadyParsedError, parse_with_envvars)
Exemple #9
0
    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
Exemple #10
0
 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)
Exemple #11
0
    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}}
Exemple #12
0
 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)
Exemple #13
0
 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)
Exemple #14
0
 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)
Exemple #15
0
 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')
Exemple #16
0
 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')
Exemple #17
0
 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()
Exemple #18
0
    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
Exemple #19
0
    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')
Exemple #20
0
    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: {})
Exemple #21
0
    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}
Exemple #22
0
 def parse(self, *args, **kwargs):
     parse(*args, **kwargs)