File: test_validation.py

package info (click to toggle)
python-confuse 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 468 kB
  • sloc: python: 2,823; makefile: 112; sh: 8
file content (151 lines) | stat: -rw-r--r-- 4,901 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import confuse
import enum
import os
import unittest
from . import _root


class TypeCheckTest(unittest.TestCase):
    def test_str_type_correct(self):
        config = _root({'foo': 'bar'})
        value = config['foo'].get(str)
        self.assertEqual(value, 'bar')

    def test_str_type_incorrect(self):
        config = _root({'foo': 2})
        with self.assertRaises(confuse.ConfigTypeError):
            config['foo'].get(str)

    def test_int_type_correct(self):
        config = _root({'foo': 2})
        value = config['foo'].get(int)
        self.assertEqual(value, 2)

    def test_int_type_incorrect(self):
        config = _root({'foo': 'bar'})
        with self.assertRaises(confuse.ConfigTypeError):
            config['foo'].get(int)


class BuiltInValidatorTest(unittest.TestCase):
    def test_as_filename_with_non_file_source(self):
        config = _root({'foo': 'foo/bar'})
        value = config['foo'].as_filename()
        self.assertEqual(value, os.path.join(os.getcwd(), 'foo', 'bar'))

    def test_as_filename_with_file_source(self):
        source = confuse.ConfigSource({'foo': 'foo/bar'},
                                      filename='/baz/config.yaml')
        config = _root(source)
        config.config_dir = lambda: '/config/path'
        value = config['foo'].as_filename()
        self.assertEqual(value, os.path.realpath('/config/path/foo/bar'))

    def test_as_filename_with_default_source(self):
        source = confuse.ConfigSource({'foo': 'foo/bar'},
                                      filename='/baz/config.yaml',
                                      default=True)
        config = _root(source)
        config.config_dir = lambda: '/config/path'
        value = config['foo'].as_filename()
        self.assertEqual(value, os.path.realpath('/config/path/foo/bar'))

    def test_as_filename_wrong_type(self):
        config = _root({'foo': None})
        with self.assertRaises(confuse.ConfigTypeError):
            config['foo'].as_filename()

    def test_as_path(self):
        config = _root({'foo': 'foo/bar'})
        path = os.path.join(os.getcwd(), 'foo', 'bar')
        try:
            import pathlib
        except ImportError:
            with self.assertRaises(ImportError):
                value = config['foo'].as_path()
        else:
            value = config['foo'].as_path()
            path = pathlib.Path(path)
            self.assertEqual(value, path)

    def test_as_choice_correct(self):
        config = _root({'foo': 'bar'})
        value = config['foo'].as_choice(['foo', 'bar', 'baz'])
        self.assertEqual(value, 'bar')

    def test_as_choice_error(self):
        config = _root({'foo': 'bar'})
        with self.assertRaises(confuse.ConfigValueError):
            config['foo'].as_choice(['foo', 'baz'])

    def test_as_choice_with_dict(self):
        config = _root({'foo': 'bar'})
        res = config['foo'].as_choice({
            'bar': 'baz',
            'x': 'y',
        })
        self.assertEqual(res, 'baz')

    def test_as_choice_with_enum(self):
        class Foobar(enum.Enum):
            Foo = 'bar'

        config = _root({'foo': Foobar.Foo.value})
        res = config['foo'].as_choice(Foobar)
        self.assertEqual(res, Foobar.Foo)

    def test_as_choice_with_enum_error(self):
        class Foobar(enum.Enum):
            Foo = 'bar'

        config = _root({'foo': 'foo'})
        with self.assertRaises(confuse.ConfigValueError):
            config['foo'].as_choice(Foobar)

    def test_as_number_float(self):
        config = _root({'f': 1.0})
        config['f'].as_number()

    def test_as_number_int(self):
        config = _root({'i': 2})
        config['i'].as_number()

    def test_as_number_string(self):
        config = _root({'s': 'a'})
        with self.assertRaises(confuse.ConfigTypeError):
            config['s'].as_number()

    def test_as_str_seq_str(self):
        config = _root({'k': 'a b c'})
        self.assertEqual(
            config['k'].as_str_seq(),
            ['a', 'b', 'c']
        )

    def test_as_str_seq_list(self):
        config = _root({'k': ['a b', 'c']})
        self.assertEqual(
            config['k'].as_str_seq(),
            ['a b', 'c']
        )

    def test_as_str(self):
        config = _root({'s': 'foo'})
        config['s'].as_str()

    def test_as_str_non_string(self):
        config = _root({'f': 1.0})
        with self.assertRaises(confuse.ConfigTypeError):
            config['f'].as_str()

    def test_as_str_expanded(self):
        config = _root({'s': '${CONFUSE_TEST_VAR}/bar'})
        os.environ["CONFUSE_TEST_VAR"] = 'foo'
        self.assertEqual(config['s'].as_str_expanded(), 'foo/bar')

    def test_as_pairs(self):
        config = _root({'k': [{'a': 'A'}, 'b', ['c', 'C']]})
        self.assertEqual(
            [('a', 'A'), ('b', None), ('c', 'C')],
            config['k'].as_pairs()
        )