File: py_config_file_test_case.py

package info (click to toggle)
python-apptools 4.5.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,668 kB
  • sloc: python: 16,754; makefile: 79
file content (178 lines) | stat: -rw-r--r-- 6,985 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
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
""" Tests for Python-esque '.ini' files. """


# Standard library imports.
import os, tempfile, unittest
from os.path import join

# Major package imports.
from pkg_resources import resource_filename

# Enthought library imports.
from .py_config_file import PyConfigFile


# This module's package.
PKG = 'apptools.preferences.tests'


class PyConfigFileTestCase(unittest.TestCase):
    """ Tests for Python-esque '.ini' files. """

    ###########################################################################
    # 'TestCase' interface.
    ###########################################################################

    def setUp(self):
        """ Prepares the test fixture before each test method is called. """

        # The filenames of the example preferences files.
        self.example = resource_filename(PKG, 'py_config_example.ini')
        self.example_2 = resource_filename(PKG, 'py_config_example_2.ini')

        return

    def tearDown(self):
        """ Called immediately after each test method has been called. """

        return

    ###########################################################################
    # Tests.
    ###########################################################################

    def test_load_from_filename(self):
        """ load from filename """

        config = PyConfigFile(self.example)

        self.assertEqual('blue', config['acme.ui']['bgcolor'])
        self.assertEqual(50, config['acme.ui']['width'])
        self.assertEqual(1.0, config['acme.ui']['ratio'])
        self.assertEqual(True, config['acme.ui']['visible'])
        self.assertEqual({'a' : 1, 'b' : 2}, config['acme.ui']['foo'])
        self.assertEqual([1, 2, 3, 4], config['acme.ui']['bar'])
        self.assertEqual((1, 'a', 6, 4), config['acme.ui']['baz'])
        self.assertEqual('red', config['acme.ui.other']['fred'])
        self.assertEqual(100, config['acme.ui.other']['wilma'])
        self.assertEqual(90, config['tds.foogle']['joe'])
        self.assertEqual("meerkat", config['simples']['animal'])

        return

    def test_load_from_file(self):
        """ load from file """

        config = PyConfigFile(open(self.example))

        self.assertEqual('blue', config['acme.ui']['bgcolor'])
        self.assertEqual(50, config['acme.ui']['width'])
        self.assertEqual(1.0, config['acme.ui']['ratio'])
        self.assertEqual(True, config['acme.ui']['visible'])
        self.assertEqual({'a' : 1, 'b' : 2}, config['acme.ui']['foo'])
        self.assertEqual([1, 2, 3, 4], config['acme.ui']['bar'])
        self.assertEqual((1, 'a', 6, 4), config['acme.ui']['baz'])
        self.assertEqual('red', config['acme.ui.other']['fred'])
        self.assertEqual(100, config['acme.ui.other']['wilma'])
        self.assertEqual(90, config['tds.foogle']['joe'])
        self.assertEqual("meerkat", config['simples']['animal'])

        return

    def test_save(self):
        """ save """

        config = PyConfigFile(open(self.example))

        self.assertEqual('blue', config['acme.ui']['bgcolor'])
        self.assertEqual(50, config['acme.ui']['width'])
        self.assertEqual(1.0, config['acme.ui']['ratio'])
        self.assertEqual(True, config['acme.ui']['visible'])
        self.assertEqual({'a' : 1, 'b' : 2}, config['acme.ui']['foo'])
        self.assertEqual([1, 2, 3, 4], config['acme.ui']['bar'])
        self.assertEqual('red', config['acme.ui.other']['fred'])
        self.assertEqual(100, config['acme.ui.other']['wilma'])
        self.assertEqual(90, config['tds.foogle']['joe'])
        self.assertEqual("meerkat", config['simples']['animal'])

        # Save the config to another file.
        tmpdir = tempfile.mkdtemp()
        tmp    = join(tmpdir, 'tmp.ini')

        config.save(tmp)
        try:
            self.assert_(os.path.exists(tmp))

            # Make sure we can read the file back in and that we get the same
            # values!
            config = PyConfigFile(open(tmp))

            self.assertEqual('blue', config['acme.ui']['bgcolor'])
            self.assertEqual(50, config['acme.ui']['width'])
            self.assertEqual(1.0, config['acme.ui']['ratio'])
            self.assertEqual(True, config['acme.ui']['visible'])
            self.assertEqual({'a' : 1, 'b' : 2}, config['acme.ui']['foo'])
            self.assertEqual([1, 2, 3, 4], config['acme.ui']['bar'])
            self.assertEqual((1, 'a', 6, 4), config['acme.ui']['baz'])
            self.assertEqual('red', config['acme.ui.other']['fred'])
            self.assertEqual(100, config['acme.ui.other']['wilma'])
            self.assertEqual(90, config['tds.foogle']['joe'])
            self.assertEqual("meerkat", config['simples']['animal'])

        finally:
            # Clean up!
            os.remove(tmp)
            os.rmdir(tmpdir)

        return

    def test_load_multiple_files(self):
        """ load multiple files """

        config = PyConfigFile(self.example)

        self.assertEqual('blue', config['acme.ui']['bgcolor'])
        self.assertEqual(50, config['acme.ui']['width'])
        self.assertEqual(1.0, config['acme.ui']['ratio'])
        self.assertEqual(True, config['acme.ui']['visible'])
        self.assertEqual({'a' : 1, 'b' : 2}, config['acme.ui']['foo'])
        self.assertEqual([1, 2, 3, 4], config['acme.ui']['bar'])
        self.assertEqual((1, 'a', 6, 4), config['acme.ui']['baz'])
        self.assertEqual('red', config['acme.ui.other']['fred'])
        self.assertEqual(100, config['acme.ui.other']['wilma'])
        self.assertEqual(90, config['tds.foogle']['joe'])
        self.assertEqual("meerkat", config['simples']['animal'])

        # Load another file.
        config.load(self.example_2)

        # Make sure we still have the unchanged values...
        self.assertEqual('red', config['acme.ui']['bgcolor'])
        self.assertEqual(50, config['acme.ui']['width'])
        self.assertEqual(1.0, config['acme.ui']['ratio'])
        self.assertEqual(True, config['acme.ui']['visible'])
        self.assertEqual({'a' : 1, 'b' : 2}, config['acme.ui']['foo'])
        self.assertEqual([1, 2, 3, 4], config['acme.ui']['bar'])
        self.assertEqual((1, 'a', 6, 4), config['acme.ui']['baz'])
        self.assertEqual('red', config['acme.ui.other']['fred'])
        self.assertEqual(100, config['acme.ui.other']['wilma'])
        self.assertEqual(90, config['tds.foogle']['joe'])
        self.assertEqual("meerkat", config['simples']['animal'])

        # ... and the values that were overwritten...
        self.assertEqual('red', config['acme.ui']['bgcolor'])

        # ... and that we have the new ones.
        self.assertEqual(42, config['acme.ui']['bazzle'])

        # ... and that the new ones can refer to the old ones!
        self.assertEqual(180, config['acme.ui']['blimey'])

        return


# Entry point for stand-alone testing.
if __name__ == '__main__':
    unittest.main()

#### EOF ######################################################################