aboutsummaryrefslogtreecommitdiff
path: root/test/test_config/test_sections.py
blob: 39b41a793890b9dce0511064270bbabc50d26200 (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
# Copyright (C) 2010  Leonard Thomas
#
# This file is part of Dodai.
#
# Dodai is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Dodai is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Dodai.  If not, see <http://www.gnu.org/licenses/>.

import sys
import os
import ConfigParser
import unittest
path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..'))
sys.path.append(path)
from dodai.tools.himo import Himo
from dodai.tools.himo import String2Himo
from dodai.config.sections import ConfigSections


class TestSections(unittest.TestCase):

    def setUp(self):
        path = os.path.dirname(__file__)
        filepath = os.path.join(path, 'config.cfg')
        self.parser = ConfigParser.ConfigParser()
        self.parser.readfp(open(filepath))
        self.sections = ConfigSections(String2Himo())

    def test_call(self):
        self.sections(self.parser)
        count = len(self.parser.sections())
        self.assertEqual(count, len(self.sections))

    def test_iterator(self):
        result = True
        for section in self.sections:
            if not section.get_title() in self.parser.sections():
                result = False
        self.assertTrue(result==True)

    def test_get_section(self):
        self.sections(self.parser)
        self.sections(self.parser)
        check = self.sections.get_section('test_db')
        self.assertEqual(check.get_title(), 'test_db')

    def test_build_string_object(self):
        sections = ConfigSections()
        sections(self.parser)
        count = len(self.parser.sections())
        self.assertEqual(count, len(sections))

    def test_getitem(self):
        self.sections(self.parser)
        title = self.sections['test_db'].get_title()
        self.assertEqual(title, 'test_db')

    def test_getattr(self):
        self.sections(self.parser)
        title = self.sections.test_db.get_title()
        self.assertEqual(title, 'test_db')

    def test_get_keys(self):
        self.sections(self.parser)
        keys = self.sections.keys()
        self.assertTrue(len(keys))

    def test_section_object_one(self):
        self.sections(self.parser)
        keys = self.sections.test_db.keys()
        self.assertTrue(len(keys))

    def test_section_object_two(self):
        self.sections(self.parser)
        keys = self.sections.test_db.___options___.keys()
        self.assertTrue(len(keys))

    def test_section_object_three(self):
        self.sections(self.parser)
        self.sections.test_db.___blah___ = 'test'
        val = self.sections.test_db.__getattr__('___blah___')
        self.assertTrue(val == 'test')

    def test_section_object_four(self):
        self.sections(self.parser)
        self.sections.test_db.foo = 'bar'
        val = self.sections.test_db.__getattr__('foo')
        self.assertTrue(val == 'bar')

    def test_section_object_five(self):
        self.sections(self.parser)
        keys = []
        for key in self.sections.test_db:
            keys.append(key)
        self.assertTrue(keys)

    def test_section_object_six(self):
        self.sections(self.parser)
        self.sections.test_db.foo = 'bar'
        val = self.sections.test_db['foo']
        self.assertTrue(val == 'bar')

    def test_section_object_seven(self):
        self.sections(self.parser, 'unicode_escape')
        self.assertEqual(self.sections.extra.name, u'\u8c61')


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