summaryrefslogtreecommitdiff
path: root/functionaltests/test_validate_errors.py
blob: 4517817d157a012b2a44529e5a105d726a684847 (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
import os
try:
    import unittest2 as unittest
except ImportError:
    import unittest

from configobj import ConfigObj, get_extra_values
from validate import Validator

thisdir = os.path.dirname(os.path.join(os.getcwd(), __file__))
inipath = os.path.join(thisdir, 'conf.ini')
specpath = os.path.join(thisdir, 'conf.spec')


class TestValidateErrors(unittest.TestCase):
    
    def test_validate_no_valid_entries(self):
        conf = ConfigObj(inipath, configspec=specpath)
        
        validator = Validator()
        result = conf.validate(validator)
        self.assertFalse(result)
    
    
    def test_validate_preserve_errors(self):
        conf = ConfigObj(inipath, configspec=specpath)
        
        validator = Validator()
        result = conf.validate(validator, preserve_errors=True)
        
        self.assertFalse(result['value'])
        self.assertFalse(result['missing-section'])
        
        section = result['section']
        self.assertFalse(section['value'])
        self.assertFalse(section['sub-section']['value'])
        self.assertFalse(section['missing-subsection'])
        
    
    def test_validate_extra_values(self):
        conf = ConfigObj(inipath, configspec=specpath)
        conf.validate(Validator(), preserve_errors=True)
        
        self.assertEqual(conf.extra_values, ['extra', 'extra-section'])
        
        self.assertEqual(conf['section'].extra_values, ['extra-sub-section'])
        self.assertEqual(conf['section']['sub-section'].extra_values,
                         ['extra'])
        
    
    def test_get_extra_values(self):
        conf = ConfigObj(inipath, configspec=specpath)
        
        conf.validate(Validator(), preserve_errors=True)
        extra_values = get_extra_values(conf)
        
        expected = sorted([
            ((), 'extra'),
            ((), 'extra-section'),
            (('section', 'sub-section'), 'extra'),
            (('section',), 'extra-sub-section'),
        ])
        self.assertEqual(sorted(extra_values), expected)

if __name__ == '__main__':
    unittest.main()