Mercurial > repos > yufei-luo > s_mart
comparison commons/core/checker/test/Test_F_ConfigChecker.py @ 6:769e306b7933
Change the repository level.
author | yufei-luo |
---|---|
date | Fri, 18 Jan 2013 04:54:14 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
5:ea3082881bf8 | 6:769e306b7933 |
---|---|
1 from commons.core.checker.ConfigChecker import ConfigChecker | |
2 from commons.core.checker.ConfigChecker import ConfigRules | |
3 from commons.core.checker.ConfigValue import ConfigValue | |
4 from commons.core.checker.RepetException import RepetException | |
5 import unittest | |
6 import os | |
7 | |
8 class Test_F_ConfigChecker(unittest.TestCase): | |
9 | |
10 #TODO: AJouter test (wrong type, etc..) | |
11 def setUp(self): | |
12 self._configFileName = "test_conf_checker" | |
13 | |
14 def tearDown(self): | |
15 os.remove(self._configFileName) | |
16 | |
17 def test_run(self): | |
18 iMock = MockConfig() | |
19 iMock.write_config(self._configFileName) | |
20 | |
21 iConfigRules = ConfigRules() | |
22 iConfigRules.addRuleSection(section="dir_name", mandatory=True) | |
23 iConfigRules.addRuleOption(section="dir_name", option ="work_dir", mandatory=True) | |
24 iConfigRules.addRuleSection(section="organism", mandatory=True) | |
25 iConfigRules.addRuleOption(section="organism", option ="abbreviation", mandatory=True) | |
26 iConfigRules.addRuleOption(section="organism", option ="genus", mandatory=True) | |
27 iConfigRules.addRuleOption(section="organism", option ="species", mandatory=True) | |
28 iConfigRules.addRuleOption(section="organism", option ="common_name", mandatory=True) | |
29 iConfigRules.addRuleOption(section="organism", option ="comment") | |
30 iConfigRules.addRuleSection(section="analysis", mandatory=True, isPattern=True) | |
31 iConfigRules.addRuleOption(section="analysis", option ="name", mandatory=True) | |
32 iConfigRules.addRuleOption(section="analysis", option ="program", mandatory=True) | |
33 iConfigRules.addRuleOption(section="analysis", option ="sourcename", mandatory=True) | |
34 iConfigRules.addRuleOption(section="analysis", option ="description") | |
35 iConfigRules.addRuleOption(section="analysis", option ="gff_name") | |
36 | |
37 iConfigChecker = ConfigChecker(self._configFileName,iConfigRules) | |
38 | |
39 obsValidatedConfig = iConfigChecker.getConfig() | |
40 | |
41 expValidatedConfig = ConfigValue() | |
42 d = {"dir_name" : {"work_dir":"toto"}, | |
43 "organism" : {"abbreviation":"T.aestivum", | |
44 "genus":"triticum", | |
45 "species":"aestivum", | |
46 "common_name":"wheat", | |
47 "comment":""}, | |
48 'analysis1': {'description': '', | |
49 'gff_name': 'BLASTX.gff2', | |
50 'name': 'BLASTXWheat2', | |
51 'program': 'BLASTX2', | |
52 'programversion': '3.32', | |
53 'sourcename': 'dummyDesc_BLASTX2'} | |
54 } | |
55 expValidatedConfig.setdOptionsValues4Sections(d) | |
56 | |
57 self.assertEquals(expValidatedConfig, obsValidatedConfig) | |
58 | |
59 | |
60 def test_run_exception_section_missing(self): | |
61 iMock = MockConfig() | |
62 iMock.write_config_section_missing(self._configFileName) | |
63 | |
64 iConfigRules = ConfigRules() | |
65 iConfigRules.addRuleSection(section="dir_name", mandatory=True) | |
66 iConfigRules.addRuleOption(section="dir_name", option ="work_dir", mandatory=True) | |
67 iConfigRules.addRuleSection(section="organism", mandatory=True) | |
68 iConfigRules.addRuleOption(section="organism", option ="abbreviation", mandatory=True) | |
69 iConfigRules.addRuleOption(section="organism", option ="genus", mandatory=True) | |
70 iConfigRules.addRuleOption(section="organism", option ="species", mandatory=True) | |
71 iConfigRules.addRuleOption(section="organism", option ="common_name", mandatory=True) | |
72 iConfigRules.addRuleOption(section="organism", option ="comment") | |
73 iConfigRules.addRuleSection(section="analysis", mandatory=True, isPattern=True) | |
74 iConfigRules.addRuleOption(section="analysis", option ="name", mandatory=True) | |
75 iConfigRules.addRuleOption(section="analysis", option ="program", mandatory=True) | |
76 iConfigRules.addRuleOption(section="analysis", option ="sourcename", mandatory=True) | |
77 iConfigRules.addRuleOption(section="analysis", option ="description") | |
78 iConfigRules.addRuleOption(section="analysis", option ="gff_name") | |
79 | |
80 iConfigChecker = ConfigChecker(self._configFileName,iConfigRules) | |
81 | |
82 expMessage = "Error in configuration file %s, following sections are missing:\n - organism\n"% self._configFileName | |
83 | |
84 try : | |
85 obsValidatedConfig = iConfigChecker.getConfig() | |
86 except RepetException, e: | |
87 obsMessage = e.getMessage() | |
88 | |
89 self.assertEquals(expMessage, obsMessage) | |
90 | |
91 | |
92 def test_run_exception_section_pattern_false(self): | |
93 iMock = MockConfig() | |
94 iMock.write_config(self._configFileName) | |
95 | |
96 iConfigRules = ConfigRules() | |
97 iConfigRules.addRuleSection(section="dir_name", mandatory=True) | |
98 iConfigRules.addRuleOption(section="dir_name", option ="work_dir", mandatory=True) | |
99 iConfigRules.addRuleSection(section="organism", mandatory=True) | |
100 iConfigRules.addRuleOption(section="organism", option ="abbreviation", mandatory=True) | |
101 iConfigRules.addRuleOption(section="organism", option ="genus", mandatory=True) | |
102 iConfigRules.addRuleOption(section="organism", option ="species", mandatory=True) | |
103 iConfigRules.addRuleOption(section="organism", option ="common_name", mandatory=True) | |
104 iConfigRules.addRuleOption(section="organism", option ="comment") | |
105 iConfigRules.addRuleSection(section="analysis", mandatory=True) | |
106 iConfigRules.addRuleOption(section="analysis", option ="name", mandatory=True) | |
107 iConfigRules.addRuleOption(section="analysis", option ="program", mandatory=True) | |
108 iConfigRules.addRuleOption(section="analysis", option ="sourcename", mandatory=True) | |
109 iConfigRules.addRuleOption(section="analysis", option ="description") | |
110 iConfigRules.addRuleOption(section="analysis", option ="gff_name") | |
111 | |
112 iConfigChecker = ConfigChecker(self._configFileName,iConfigRules) | |
113 | |
114 expMessage = "Error in configuration file %s, following sections are missing:\n - analysis\n"% self._configFileName | |
115 | |
116 try : | |
117 obsValidatedConfig = iConfigChecker.getConfig() | |
118 except RepetException, e: | |
119 obsMessage = e.getMessage() | |
120 | |
121 self.assertEquals(expMessage, obsMessage) | |
122 | |
123 | |
124 def test_run_exception_option_missing(self): | |
125 iMock = MockConfig() | |
126 iMock.write_config_option_missing(self._configFileName) | |
127 | |
128 iConfigRules = ConfigRules() | |
129 iConfigRules.addRuleSection(section="dir_name", mandatory=True) | |
130 iConfigRules.addRuleOption(section="dir_name", option ="work_dir", mandatory=True) | |
131 iConfigRules.addRuleSection(section="organism", mandatory=True) | |
132 iConfigRules.addRuleOption(section="organism", option ="abbreviation", mandatory=True) | |
133 iConfigRules.addRuleOption(section="organism", option ="genus", mandatory=True) | |
134 iConfigRules.addRuleOption(section="organism", option ="species", mandatory=True) | |
135 iConfigRules.addRuleOption(section="organism", option ="common_name", mandatory=True) | |
136 iConfigRules.addRuleOption(section="organism", option ="comment") | |
137 iConfigRules.addRuleSection(section="analysis", mandatory=True, isPattern=True) | |
138 iConfigRules.addRuleOption(section="analysis", option ="name", mandatory=True) | |
139 iConfigRules.addRuleOption(section="analysis", option ="program", mandatory=True) | |
140 iConfigRules.addRuleOption(section="analysis", option ="sourcename", mandatory=True) | |
141 iConfigRules.addRuleOption(section="analysis", option ="description") | |
142 iConfigRules.addRuleOption(section="analysis", option ="gff_name") | |
143 | |
144 iConfigChecker = ConfigChecker(self._configFileName,iConfigRules) | |
145 | |
146 expMessage = "Error in configuration file %s, following options are missing: \n - [organism]: abbreviation\n"% self._configFileName | |
147 | |
148 try : | |
149 obsValidatedConfig = iConfigChecker.getConfig() | |
150 except RepetException, e: | |
151 obsMessage = e.getMessage() | |
152 | |
153 self.assertEquals(expMessage, obsMessage) | |
154 | |
155 class MockConfig (object): | |
156 | |
157 def write_config(self, configFileName): | |
158 configF = open(configFileName, "w" ) | |
159 configF.write( "[dir_name]\n") | |
160 configF.write( "work_dir : toto \n") | |
161 configF.write( "\n") | |
162 configF.write( "[organism]\n") | |
163 configF.write( "abbreviation: T.aestivum\n") | |
164 configF.write( "genus: triticum\n") | |
165 configF.write( "species: aestivum\n") | |
166 configF.write( "common_name: wheat\n") | |
167 configF.write( "comment: \n") | |
168 configF.write( "[analysis1]\n") | |
169 configF.write( "name: BLASTXWheat2\n") | |
170 configF.write( "program: BLASTX2\n") | |
171 configF.write( "programversion: 3.32\n") | |
172 configF.write( "sourcename: dummyDesc_BLASTX2\n") | |
173 configF.write( "description: \n") | |
174 configF.write( "gff_name: BLASTX.gff2\n") | |
175 configF.write( "\n") | |
176 configF.close() | |
177 | |
178 def write_config_section_missing(self, configFileName): | |
179 configF = open(configFileName, "w" ) | |
180 configF.write( "[dir_name]\n") | |
181 configF.write( "work_dir : toto \n") | |
182 configF.write( "\n") | |
183 configF.write( "[analysis1]\n") | |
184 configF.write( "name: BLASTXWheat2\n") | |
185 configF.write( "program: BLASTX2\n") | |
186 configF.write( "programversion: 3.32\n") | |
187 configF.write( "sourcename: dummyDesc_BLASTX2\n") | |
188 configF.write( "description: \n") | |
189 configF.write( "gff_name: BLASTX.gff2\n") | |
190 configF.write( "\n") | |
191 configF.close() | |
192 | |
193 def write_config_option_missing(self, configFileName): | |
194 configF = open(configFileName, "w" ) | |
195 configF.write( "[dir_name]\n") | |
196 configF.write( "work_dir : toto \n") | |
197 configF.write( "\n") | |
198 configF.write( "[organism]\n") | |
199 configF.write( "genus: triticum\n") | |
200 configF.write( "species: aestivum\n") | |
201 configF.write( "common_name: wheat\n") | |
202 configF.write( "comment: \n") | |
203 configF.write( "[analysis1]\n") | |
204 configF.write( "name: BLASTXWheat2\n") | |
205 configF.write( "program: BLASTX2\n") | |
206 configF.write( "programversion: 3.32\n") | |
207 configF.write( "sourcename: dummyDesc_BLASTX2\n") | |
208 configF.write( "description: \n") | |
209 configF.write( "gff_name: BLASTX.gff2\n") | |
210 configF.write( "\n") | |
211 configF.close() | |
212 | |
213 if __name__ == "__main__": | |
214 unittest.main() |