summaryrefslogtreecommitdiff
path: root/src/tests/test_validate.py
blob: b775364c4acdd49c2cbaa3e7bf0e577fb705b2e9 (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# *- coding: utf-8 -*-
# pylint: disable=wildcard-import, missing-docstring, no-self-use, bad-continuation
# pylint: disable=invalid-name, redefined-outer-name, too-few-public-methods
"""Validator tests"""

import pytest

from configobj import ConfigObj
from configobj.validate import *


class TestBasic(object):
    def test_values_too_small(self, val):
        config = '''
        test1=40
        test2=hello
        test3=3
        test4=5.0
        [section]
            test1=40
            test2=hello
            test3=3
            test4=5.0
            [[sub section]]
                test1=40
                test2=hello
                test3=3
                test4=5.0
        '''.splitlines()
        configspec = '''
        test1= integer(30,50)
        test2= string
        test3=integer
        test4=float(6.0)
        [section ]
            test1=integer(30,50)
            test2=string
            test3=integer
            test4=float(6.0)
            [[sub section]]
                test1=integer(30,50)
                test2=string
                test3=integer
                test4=float(6.0)
        '''.splitlines()
        c1 = ConfigObj(config, configspec=configspec)
        test = c1.validate(val)
        assert test == {
                'test1': True,
                'test2': True,
                'test3': True,
                'test4': False,
                'section': {
                    'test1': True,
                    'test2': True,
                    'test3': True,
                    'test4': False,
                    'sub section': {
                        'test1': True,
                        'test2': True,
                        'test3': True,
                        'test4': False,
                    },
                },
            }

        with pytest.raises(VdtValueTooSmallError) as excinfo:
            val.check(c1.configspec['test4'], c1['test4'])
        assert str(excinfo.value) == 'the value "5.0" is too small.'

    def test_values(self, val):
        val_test_config = '''
            key = 0
            key2 = 1.1
            [section]
            key = some text
            key2 = 1.1, 3.0, 17, 6.8
                [[sub-section]]
                key = option1
                key2 = True'''.splitlines()
        val_test_configspec = '''
            key = integer
            key2 = float
            [section]
            key = string
            key2 = float_list(4)
               [[sub-section]]
               key = option(option1, option2)
               key2 = boolean'''.splitlines()
        val_test = ConfigObj(val_test_config, configspec=val_test_configspec)
        assert val_test.validate(val)
        val_test['key'] = 'text not a digit'
        val_res = val_test.validate(val)
        assert val_res == {'key2': True, 'section': True, 'key': False}

    def test_defaults(self, val):
        configspec = '''
            test1=integer(30,50, default=40)
            test2=string(default="hello")
            test3=integer(default=3)
            test4=float(6.0, default=6.0)
            [section ]
                test1=integer(30,50, default=40)
                test2=string(default="hello")
                test3=integer(default=3)
                test4=float(6.0, default=6.0)
                [[sub section]]
                    test1=integer(30,50, default=40)
                    test2=string(default="hello")
                    test3=integer(default=3)
                    test4=float(6.0, default=6.0)
            '''.splitlines()
        default_test = ConfigObj(['test1=30'], configspec=configspec)
        assert repr(default_test) == "ConfigObj({'test1': '30'})"
        assert default_test.defaults == []
        assert default_test.default_values == {}
        assert default_test.validate(val)
        assert default_test == {
            'test1': 30,
            'test2': 'hello',
            'test3': 3,
            'test4': 6.0,
            'section': {
                'test1': 40,
                'test2': 'hello',
                'test3': 3,
                'test4': 6.0,
                'sub section': {
                    'test1': 40,
                    'test3': 3,
                    'test2': 'hello',
                    'test4': 6.0,
                },
            },
        }

        assert default_test.defaults == ['test2', 'test3', 'test4']
        assert default_test.default_values == {
            'test1': 40, 'test2': 'hello',
            'test3': 3, 'test4': 6.0
        }
        assert default_test.restore_default('test1') == 40
        assert default_test['test1'] == 40
        assert 'test1' in default_test.defaults  # pylint: disable=unsupported-membership-test

        def change(section, key):
            section[key] = 3
        default_test.walk(change)
        assert default_test['section']['sub section']['test4'] == 3

        default_test.restore_defaults()
        assert default_test == {
            'test1': 40,
            'test2': "hello",
            'test3': 3,
            'test4': 6.0,
            'section': {
                'test1': 40,
                'test2': "hello",
                'test3': 3,
                'test4': 6.0,
                'sub section': {
                    'test1': 40,
                    'test2': "hello",
                    'test3': 3,
                    'test4': 6.0
        }}}


class TestStringChecks(object):

    def test_required_string_cannot_be_none(self, val):  # issue #93
        val.check('string(min=1)', None)
        with pytest.raises(VdtMissingValue):
            val.check('string(min=1)', None, missing=True)

    @pytest.mark.parametrize('text', ("text = ", ""))
    def test_required_string_cannot_be_empty_or_missing(self, val, text):  # issue #93
        config = [text]
        configspec = ["text = string(min=1)"]
        configobj = ConfigObj(config, configspec=configspec)
        assert configobj.validate(val, preserve_errors=True) is not True, "Validation should've failed"


class TestListChecks(object):

    TYPESPECS = (
        "'integer', 'string', 'boolean', 'float'",
        "'int', 'str', 'bool', 'float'",
        "int, str, bool, float",
    )

    def test_force_list(self, val):
        config = '''
            scalar = 1
            '''.splitlines()
        configspec = '''
            scalar = force_list
            '''.splitlines()
        configobj = ConfigObj(config, configspec=configspec)
        assert configobj.validate(val, preserve_errors=True) is True, "Validation failed unexpectedly"
        assert configobj['scalar'] == ['1']

    @pytest.mark.parametrize('typespec', TYPESPECS)
    def test_mixed_list(self, val, typespec):
        config = '''
            mixed = 1, 2, yes, 3.1415
            '''.splitlines()
        configspec = '''
            mixed = mixed_list({})
            '''.format(typespec).splitlines()
        configobj = ConfigObj(config, configspec=configspec)
        assert configobj.validate(val, preserve_errors=True) is True, "Validation failed unexpectedly"
        assert configobj['mixed'] == [1, '2', True, 3.1415]


class TestDottedQuadToNum(object):

    def test_stripped(self):
        assert dottedQuadToNum('192.0.2.0') == 3221225984
        assert dottedQuadToNum('192.0.2.1 ') == 3221225985
        assert dottedQuadToNum(' 192.0.2.2') == 3221225986
        assert dottedQuadToNum('\t\t192.0.2.3\n') == 3221225987
        with pytest.raises(ValueError) as excinfo:
            dottedQuadToNum('192. 0. 2. 4')
        assert str(excinfo.value) == 'Not a good dotted-quad IP: 192. 0. 2. 4'

    def test_boundaries(self):
        assert dottedQuadToNum('0.0.0.0') == 0
        assert dottedQuadToNum('255.255.255.255') == 4294967295
        with pytest.raises(ValueError) as excinfo:
            dottedQuadToNum('255.255.255.256')
        assert str(excinfo.value) == (
            'Not a good dotted-quad IP: 255.255.255.256')
        with pytest.raises(ValueError) as excinfo:
            dottedQuadToNum('-1')
        assert str(excinfo.value) == 'Not a good dotted-quad IP: -1'