diff options
Diffstat (limited to 'test/test_config/test_files.py')
-rw-r--r-- | test/test_config/test_files.py | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/test/test_config/test_files.py b/test/test_config/test_files.py new file mode 100644 index 0000000..7d32a07 --- /dev/null +++ b/test/test_config/test_files.py | |||
@@ -0,0 +1,135 @@ | |||
1 | # Copyright (C) 2010 Leonard Thomas | ||
2 | # | ||
3 | # This file is part of Dodai. | ||
4 | # | ||
5 | # Dodai is free software: you can redistribute it and/or modify | ||
6 | # it under the terms of the GNU General Public License as published by | ||
7 | # the Free Software Foundation, either version 3 of the License, or | ||
8 | # (at your option) any later version. | ||
9 | # | ||
10 | # Dodai is distributed in the hope that it will be useful, | ||
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | # GNU General Public License for more details. | ||
14 | # | ||
15 | # You should have received a copy of the GNU General Public License | ||
16 | # along with Dodai. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | import sys | ||
19 | import os | ||
20 | import unittest | ||
21 | path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..')) | ||
22 | sys.path.append(path) | ||
23 | from ConfigParser import ConfigParser | ||
24 | from ConfigParser import MissingSectionHeaderError | ||
25 | from dodai.config.files import ConfigFiles | ||
26 | from dodai.tools.odict import OrderedDict | ||
27 | from dodai.tools.himo import String2Himo | ||
28 | from dodai.tools import Section | ||
29 | from dodai.exception import InvalidConfigParser | ||
30 | from dodai.exception import FileDoesNotExist | ||
31 | from dodai.exception import FileIsDirectory | ||
32 | |||
33 | |||
34 | class GoodParser(object): | ||
35 | |||
36 | DATA = OrderedDict() | ||
37 | DATA['sec_a'] = OrderedDict() | ||
38 | DATA['sec_a']['opt_a'] = 'foo' | ||
39 | DATA['sec_a']['opt_b'] = 'bar' | ||
40 | DATA['sec_a']['opt_c'] = '1' | ||
41 | DATA['sec_a']['opt_d'] = '22' | ||
42 | |||
43 | DATA['sec_b'] = OrderedDict() | ||
44 | DATA['sec_b']['opt_a'] = 'lt' | ||
45 | DATA['sec_b']['opt_b'] = 'el' | ||
46 | DATA['sec_b']['opt_c'] = 'mg' | ||
47 | DATA['sec_b']['opt_d'] = 'ds' | ||
48 | |||
49 | def sections(self): | ||
50 | return self.DATA.keys() | ||
51 | |||
52 | def options(self, section): | ||
53 | return self.DATA[section].keys() | ||
54 | |||
55 | def get(self, section, option): | ||
56 | return self.DATA[section][option] | ||
57 | |||
58 | def read(self, filename): | ||
59 | return [filename] | ||
60 | |||
61 | class BadParser(object): | ||
62 | |||
63 | DATA = OrderedDict() | ||
64 | DATA['sec_a'] = OrderedDict() | ||
65 | DATA['sec_a']['opt_a'] = 'foo' | ||
66 | DATA['sec_a']['opt_b'] = 'bar' | ||
67 | DATA['sec_a']['opt_c'] = '1' | ||
68 | DATA['sec_a']['opt_d'] = '22' | ||
69 | |||
70 | DATA['sec_b'] = OrderedDict() | ||
71 | DATA['sec_b']['opt_a'] = 'lt' | ||
72 | DATA['sec_b']['opt_b'] = 'el' | ||
73 | DATA['sec_b']['opt_c'] = 'mg' | ||
74 | DATA['sec_b']['opt_d'] = 'ds' | ||
75 | |||
76 | def sections(self): | ||
77 | return self.DATA.keys() | ||
78 | |||
79 | def get(self, section, option): | ||
80 | return self.DATA[section][option] | ||
81 | |||
82 | def read(self, filename): | ||
83 | return [filename] | ||
84 | |||
85 | class TestFiles(unittest.TestCase): | ||
86 | |||
87 | def setUp(self): | ||
88 | s2h = String2Himo() | ||
89 | self.obj = ConfigFiles(OrderedDict, Section, s2h) | ||
90 | |||
91 | def test_good_parser(self): | ||
92 | self.obj.register_parser_object(GoodParser) | ||
93 | |||
94 | def test_bad_parser(self): | ||
95 | self.failUnlessRaises(InvalidConfigParser, | ||
96 | self.obj.register_parser_object, | ||
97 | BadParser) | ||
98 | |||
99 | def test_add(self): | ||
100 | filename = os.path.realpath(__file__) | ||
101 | self.obj.add(filename) | ||
102 | self.assertTrue(filename in self.obj.files) | ||
103 | |||
104 | def test_add_bad_file(self): | ||
105 | filename = os.path.dirname(os.path.realpath(__file__)) | ||
106 | filename = os.path.join(filename, 'no_way_jose.234dasf2345') | ||
107 | self.failUnlessRaises(FileDoesNotExist, | ||
108 | self.obj.add, | ||
109 | filename) | ||
110 | |||
111 | def test_add_directory(self): | ||
112 | filename = os.path.dirname(os.path.realpath(__file__)) | ||
113 | self.failUnlessRaises(FileIsDirectory, | ||
114 | self.obj.add, | ||
115 | filename) | ||
116 | |||
117 | def file_test_read(self): | ||
118 | filename = os.path.realpath(__file__) | ||
119 | self.obj.register_parser_object(GoodParser) | ||
120 | self.obj.add(filename) | ||
121 | sec = self.obj.load() | ||
122 | |||
123 | def test_bad_read(self): | ||
124 | self.obj.register_parser_object(ConfigParser) | ||
125 | filename = os.path.realpath(__file__) | ||
126 | self.obj.add(filename) | ||
127 | self.failUnlessRaises(MissingSectionHeaderError, self.obj.load) | ||
128 | |||
129 | def test_good_read_with_multiple_parsers(self): | ||
130 | self.obj.register_parser_object(ConfigParser) | ||
131 | self.obj.register_parser_object(GoodParser) | ||
132 | filename = os.path.realpath(__file__) | ||
133 | self.obj.add(filename) | ||
134 | sec = self.obj.load() | ||
135 | |||