diff options
author | Six <unknown> | 2010-05-01 22:15:49 -0400 |
---|---|---|
committer | Six <unknown> | 2010-05-01 22:15:49 -0400 |
commit | 57c0e1b947d050d94013f83d4d677c8535a35350 (patch) | |
tree | bdcaac9516b86817affeed58054f58f4ae26eaf6 /test | |
parent | 9160d125524c278ca03841296c5c9cf7d1860c11 (diff) | |
download | dodai-macsupport-57c0e1b947d050d94013f83d4d677c8535a35350.tar.bz2 dodai-macsupport-57c0e1b947d050d94013f83d4d677c8535a35350.tar.xz dodai-macsupport-57c0e1b947d050d94013f83d4d677c8535a35350.zip |
changed the dodai.config.databases to work with dictionaries. Created a dodai.exceptions.
Diffstat (limited to 'test')
-rw-r--r-- | test/test_config/test_databases.py | 341 | ||||
-rw-r--r-- | test/test_config/test_sa.py | 45 |
2 files changed, 359 insertions, 27 deletions
diff --git a/test/test_config/test_databases.py b/test/test_config/test_databases.py new file mode 100644 index 0000000..9fc6f68 --- /dev/null +++ b/test/test_config/test_databases.py | |||
@@ -0,0 +1,341 @@ | |||
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 | |||
19 | import sys | ||
20 | import os | ||
21 | import unittest | ||
22 | import ConfigParser | ||
23 | from dingus import Dingus | ||
24 | path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..')) | ||
25 | sys.path.append(path) | ||
26 | from dodai.config.databases import ConfigDatabases | ||
27 | from dodai.config.databases import DatabaseConnectionValidator | ||
28 | from dodai.exception import DatabaseEmptyOptionException | ||
29 | from dodai.exception import DatabasePortException | ||
30 | from dodai.exception import DatabaseHostnameException | ||
31 | from dodai.exception import DatabaseProtocolException | ||
32 | from dodai.exception import DatabaseConnectionException | ||
33 | from dodai.exception import UnknownDatabaseConnectionException | ||
34 | from dodai.tools import list_to_english | ||
35 | from dodai.tools.himo import String2Himo | ||
36 | from dodai.config.sections import ConfigSections | ||
37 | |||
38 | |||
39 | class TestConfigDatabases(unittest.TestCase): | ||
40 | |||
41 | SECTIONS = {} | ||
42 | |||
43 | # valid server connection | ||
44 | SECTIONS['db_1'] = {} | ||
45 | SECTIONS['db_1']['protocol'] = 'postgresql' | ||
46 | SECTIONS['db_1']['hostname'] = '127.0.0.1' | ||
47 | SECTIONS['db_1']['username'] = 'test' | ||
48 | SECTIONS['db_1']['password'] = 'test' | ||
49 | SECTIONS['db_1']['port'] = '12345' | ||
50 | SECTIONS['db_1']['database'] = 'testing' | ||
51 | SECTIONS['db_1']['schema'] = 'foo' | ||
52 | |||
53 | # valid file connection | ||
54 | SECTIONS['db_2'] = {} | ||
55 | SECTIONS['db_2']['protocol'] = 'sqlite' | ||
56 | SECTIONS['db_2']['filename'] = '/tmp/test' | ||
57 | |||
58 | # missing protocol server | ||
59 | SECTIONS['db_3'] = {} | ||
60 | SECTIONS['db_3']['hostname'] = '127.0.0.1' | ||
61 | SECTIONS['db_3']['username'] = 'test' | ||
62 | SECTIONS['db_3']['password'] = 'test' | ||
63 | SECTIONS['db_3']['port'] = '12345' | ||
64 | SECTIONS['db_3']['database'] = 'testing' | ||
65 | |||
66 | # missing protocol file | ||
67 | SECTIONS['db_4'] = {} | ||
68 | SECTIONS['db_4']['filename'] = '/tmp/test' | ||
69 | |||
70 | # empty protocol server | ||
71 | SECTIONS['db_5'] = {} | ||
72 | SECTIONS['db_5']['protocol'] = '' | ||
73 | SECTIONS['db_5']['hostname'] = '127.0.0.1' | ||
74 | SECTIONS['db_5']['username'] = 'test' | ||
75 | SECTIONS['db_5']['password'] = 'test' | ||
76 | SECTIONS['db_5']['port'] = '12345' | ||
77 | SECTIONS['db_5']['database'] = 'testing' | ||
78 | |||
79 | # empty protocol file | ||
80 | SECTIONS['db_6'] = {} | ||
81 | SECTIONS['db_6']['protocol'] = None | ||
82 | SECTIONS['db_6']['filename'] ='/tmp/test' | ||
83 | |||
84 | # Missing port | ||
85 | SECTIONS['db_7'] = {} | ||
86 | SECTIONS['db_7']['protocol'] = 'postgresql' | ||
87 | SECTIONS['db_7']['protocol_extra'] = 'psycopg2' | ||
88 | SECTIONS['db_7']['hostname'] = '127.0.0.1' | ||
89 | SECTIONS['db_7']['username'] = 'test' | ||
90 | SECTIONS['db_7']['password'] = 'test' | ||
91 | SECTIONS['db_7']['database'] = 'testing' | ||
92 | |||
93 | # Empty port | ||
94 | SECTIONS['db_8'] = {} | ||
95 | SECTIONS['db_8']['protocol'] ='postgresql' | ||
96 | SECTIONS['db_8']['hostname'] ='127.0.0.1' | ||
97 | SECTIONS['db_8']['username'] ='test' | ||
98 | SECTIONS['db_8']['password'] ='test' | ||
99 | SECTIONS['db_8']['port'] ='' | ||
100 | SECTIONS['db_8']['database'] ='testing' | ||
101 | |||
102 | # Invalid port because it's a string | ||
103 | SECTIONS['db_9'] = {} | ||
104 | SECTIONS['db_9']['protocol'] ='postgresql' | ||
105 | SECTIONS['db_9']['hostname'] ='127.0.0.1' | ||
106 | SECTIONS['db_9']['username'] ='test' | ||
107 | SECTIONS['db_9']['password'] ='test' | ||
108 | SECTIONS['db_9']['port'] ='blah' | ||
109 | SECTIONS['db_9']['database'] ='testing' | ||
110 | |||
111 | # Invalid port low | ||
112 | SECTIONS['db_10'] = {} | ||
113 | SECTIONS['db_10']['protocol'] ='postgresql' | ||
114 | SECTIONS['db_10']['hostname'] ='127.0.0.1' | ||
115 | SECTIONS['db_10']['username'] ='test' | ||
116 | SECTIONS['db_10']['password'] ='test' | ||
117 | SECTIONS['db_10']['port'] ='0' | ||
118 | SECTIONS['db_10']['database'] ='testing' | ||
119 | |||
120 | # Invalid port high | ||
121 | SECTIONS['db_11'] = {} | ||
122 | SECTIONS['db_11']['protocol'] ='postgresql' | ||
123 | SECTIONS['db_11']['hostname'] ='127.0.0.1' | ||
124 | SECTIONS['db_11']['username'] ='test' | ||
125 | SECTIONS['db_11']['password'] ='test' | ||
126 | SECTIONS['db_11']['port'] ='655555' | ||
127 | SECTIONS['db_11']['database'] ='testing' | ||
128 | |||
129 | # missing hostname | ||
130 | SECTIONS['db_12'] = {} | ||
131 | SECTIONS['db_12']['protocol'] ='postgresql' | ||
132 | SECTIONS['db_12']['username'] ='test' | ||
133 | SECTIONS['db_12']['password'] ='test' | ||
134 | SECTIONS['db_12']['port'] ='655' | ||
135 | SECTIONS['db_12']['database'] ='testing' | ||
136 | |||
137 | # empty hostname | ||
138 | SECTIONS['db_13'] = {} | ||
139 | SECTIONS['db_13']['protocol'] ='postgresql' | ||
140 | SECTIONS['db_13']['hostname'] ='' | ||
141 | SECTIONS['db_13']['username'] ='test' | ||
142 | SECTIONS['db_13']['password'] ='test' | ||
143 | SECTIONS['db_13']['port'] ='655' | ||
144 | SECTIONS['db_13']['database'] ='testing' | ||
145 | |||
146 | # missing username | ||
147 | SECTIONS['db_14'] = {} | ||
148 | SECTIONS['db_14']['protocol'] = 'postgresql' | ||
149 | SECTIONS['db_14']['hostname'] = '127.0.0.1' | ||
150 | SECTIONS['db_14']['password'] = 'test' | ||
151 | SECTIONS['db_14']['port'] = '12345' | ||
152 | SECTIONS['db_14']['database'] = 'testing' | ||
153 | SECTIONS['db_14']['schema'] = 'foo' | ||
154 | |||
155 | # empty username | ||
156 | SECTIONS['db_15'] = {} | ||
157 | SECTIONS['db_15']['protocol'] = 'postgresql' | ||
158 | SECTIONS['db_15']['hostname'] = '127.0.0.1' | ||
159 | SECTIONS['db_15']['username'] = '' | ||
160 | SECTIONS['db_15']['password'] = 'test' | ||
161 | SECTIONS['db_15']['port'] = '12345' | ||
162 | SECTIONS['db_15']['database'] = 'testing' | ||
163 | |||
164 | # missing password | ||
165 | SECTIONS['db_16'] = {} | ||
166 | SECTIONS['db_16']['protocol'] = 'postgresql' | ||
167 | SECTIONS['db_16']['hostname'] = '127.0.0.1' | ||
168 | SECTIONS['db_16']['username'] = 'test' | ||
169 | SECTIONS['db_16']['port'] = '12345' | ||
170 | SECTIONS['db_16']['database'] = 'testing' | ||
171 | |||
172 | # empty password | ||
173 | SECTIONS['db_17'] = {} | ||
174 | SECTIONS['db_17']['protocol'] = 'postgresql' | ||
175 | SECTIONS['db_17']['hostname'] = '127.0.0.1' | ||
176 | SECTIONS['db_17']['username'] = 'test' | ||
177 | SECTIONS['db_17']['password'] = '' | ||
178 | SECTIONS['db_17']['port'] = '12345' | ||
179 | SECTIONS['db_17']['database'] = 'testing' | ||
180 | |||
181 | # missing database | ||
182 | SECTIONS['db_18'] = {} | ||
183 | SECTIONS['db_18']['protocol'] = 'postgresql' | ||
184 | SECTIONS['db_18']['hostname'] = '127.0.0.1' | ||
185 | SECTIONS['db_18']['username'] = 'test' | ||
186 | SECTIONS['db_18']['password'] = 'test' | ||
187 | SECTIONS['db_18']['port'] = '12345' | ||
188 | |||
189 | # empty database | ||
190 | SECTIONS['db_19'] = {} | ||
191 | SECTIONS['db_19']['protocol'] = 'postgresql' | ||
192 | SECTIONS['db_19']['hostname'] = '127.0.0.1' | ||
193 | SECTIONS['db_19']['username'] = 'test' | ||
194 | SECTIONS['db_19']['password'] = 'test' | ||
195 | SECTIONS['db_19']['port'] = '12345' | ||
196 | SECTIONS['db_19']['database'] = '' | ||
197 | |||
198 | # missing filename | ||
199 | SECTIONS['db_20'] = {} | ||
200 | SECTIONS['db_20']['protocol'] = 'sqlite' | ||
201 | |||
202 | # empty filename | ||
203 | SECTIONS['db_21'] = {} | ||
204 | SECTIONS['db_21']['protocol'] = 'sqlite' | ||
205 | SECTIONS['db_21']['filename'] = '' | ||
206 | |||
207 | # not a database | ||
208 | SECTIONS['db_22'] = {} | ||
209 | SECTIONS['db_22']['foo'] = 'bar' | ||
210 | |||
211 | # valid server test handler | ||
212 | SECTIONS['db_23'] = {} | ||
213 | SECTIONS['db_23']['handler'] = 'test_handler' | ||
214 | SECTIONS['db_23']['protocol'] = 'postgresql' | ||
215 | SECTIONS['db_23']['hostname'] = '127.0.0.1' | ||
216 | SECTIONS['db_23']['username'] = 'test' | ||
217 | SECTIONS['db_23']['password'] = 'test' | ||
218 | SECTIONS['db_23']['port'] = '12345' | ||
219 | SECTIONS['db_23']['database'] = 'testing' | ||
220 | |||
221 | # invalid protocol | ||
222 | SECTIONS['db_24'] = {} | ||
223 | SECTIONS['db_24']['protocol'] = 'foo' | ||
224 | SECTIONS['db_24']['hostname'] = '127.0.0.1' | ||
225 | SECTIONS['db_24']['username'] = 'test' | ||
226 | SECTIONS['db_24']['password'] = 'test' | ||
227 | SECTIONS['db_24']['port'] = '12345' | ||
228 | SECTIONS['db_24']['database'] = 'testing' | ||
229 | |||
230 | def setUp(self): | ||
231 | sa = Dingus('sa') | ||
232 | sa.name = 'test' | ||
233 | self.db = ConfigDatabases(sa, 'sa') | ||
234 | self.db.add(self.SECTIONS) | ||
235 | |||
236 | def test_default_handler(self): | ||
237 | self.assertTrue('sa' in self.db._handlers) | ||
238 | val = self.db._handlers['sa'] | ||
239 | self.assertTrue('test' == val.name) | ||
240 | |||
241 | def test_sections(self): | ||
242 | self.assertTrue(len(self.db.connections) == len(self.SECTIONS)) | ||
243 | |||
244 | def test_load_server(self): | ||
245 | obj = self.db.load('db_1') | ||
246 | |||
247 | def test_load_file(self): | ||
248 | obj = self.db.load('db_2') | ||
249 | |||
250 | def test_unable_to_load(self): | ||
251 | self.failUnlessRaises(UnknownDatabaseConnectionException, | ||
252 | self.db.load, 'db_twenty_seven') | ||
253 | |||
254 | def test_missing_protocol_server(self): | ||
255 | self.failUnlessRaises(DatabaseEmptyOptionException, | ||
256 | self.db.load, 'db_3') | ||
257 | |||
258 | def test_missing_protocol_file(self): | ||
259 | self.failUnlessRaises(DatabaseEmptyOptionException, | ||
260 | self.db.load, 'db_4') | ||
261 | |||
262 | def test_empty_protocol_server(self): | ||
263 | self.failUnlessRaises(DatabaseEmptyOptionException, | ||
264 | self.db.load, 'db_5') | ||
265 | |||
266 | def test_empty_protocol_file(self): | ||
267 | self.failUnlessRaises(DatabaseEmptyOptionException, | ||
268 | self.db.load, 'db_6') | ||
269 | |||
270 | def test_missing_port(self): | ||
271 | self.failUnlessRaises(DatabaseEmptyOptionException, | ||
272 | self.db.load, 'db_7') | ||
273 | |||
274 | def test_empty_port(self): | ||
275 | self.failUnlessRaises(DatabaseEmptyOptionException, | ||
276 | self.db.load, 'db_8') | ||
277 | |||
278 | def test_invalid_port_because_its_a_string(self): | ||
279 | self.failUnlessRaises(DatabasePortException, | ||
280 | self.db.load, 'db_9') | ||
281 | |||
282 | def test_invalid_port_because_its_to_low(self): | ||
283 | self.failUnlessRaises(DatabasePortException, | ||
284 | self.db.load, 'db_10') | ||
285 | |||
286 | def test_invalid_port_because_its_to_high(self): | ||
287 | self.failUnlessRaises(DatabasePortException, | ||
288 | self.db.load, 'db_11') | ||
289 | |||
290 | def test_missing_hostname(self): | ||
291 | self.failUnlessRaises(DatabaseEmptyOptionException, | ||
292 | self.db.load, 'db_12') | ||
293 | |||
294 | def test_empty_hostname(self): | ||
295 | self.failUnlessRaises(DatabaseEmptyOptionException, | ||
296 | self.db.load, 'db_13') | ||
297 | |||
298 | def test_missing_username(self): | ||
299 | self.failUnlessRaises(DatabaseEmptyOptionException, | ||
300 | self.db.load, 'db_14') | ||
301 | |||
302 | def test_empty_username(self): | ||
303 | self.failUnlessRaises(DatabaseEmptyOptionException, | ||
304 | self.db.load, 'db_15') | ||
305 | |||
306 | def test_missing_password(self): | ||
307 | self.failUnlessRaises(DatabaseEmptyOptionException, | ||
308 | self.db.load, 'db_16') | ||
309 | |||
310 | def test_empty_password(self): | ||
311 | self.failUnlessRaises(DatabaseEmptyOptionException, | ||
312 | self.db.load, 'db_17') | ||
313 | |||
314 | def test_missing_database(self): | ||
315 | self.failUnlessRaises(DatabaseEmptyOptionException, | ||
316 | self.db.load, 'db_18') | ||
317 | |||
318 | def test_empty_database(self): | ||
319 | self.failUnlessRaises(DatabaseEmptyOptionException, | ||
320 | self.db.load, 'db_19') | ||
321 | |||
322 | def test_missing_filename(self): | ||
323 | self.failUnlessRaises(DatabaseEmptyOptionException, | ||
324 | self.db.load, 'db_20') | ||
325 | |||
326 | def test_empty_filename(self): | ||
327 | self.failUnlessRaises(DatabaseEmptyOptionException, | ||
328 | self.db.load, 'db_21') | ||
329 | |||
330 | def test_not_a_connection(self): | ||
331 | self.failUnlessRaises(DatabaseConnectionException, | ||
332 | self.db.load, 'db_22') | ||
333 | |||
334 | def test_non_default_handler(self): | ||
335 | test_handler = Dingus('test_handler') | ||
336 | self.db.add_handler('test_handler', test_handler) | ||
337 | self.db.load('db_23') | ||
338 | |||
339 | def test_invalid_protocol(self): | ||
340 | self.failUnlessRaises(DatabaseProtocolException, | ||
341 | self.db.load, 'db_24') | ||
diff --git a/test/test_config/test_sa.py b/test/test_config/test_sa.py index d6ff940..57459bc 100644 --- a/test/test_config/test_sa.py +++ b/test/test_config/test_sa.py | |||
@@ -25,6 +25,20 @@ from dodai.config.databases.sa import Sa | |||
25 | 25 | ||
26 | class TestSa(unittest.TestCase): | 26 | class TestSa(unittest.TestCase): |
27 | 27 | ||
28 | DB_ONE = {} | ||
29 | DB_ONE['protocol'] ='postgresql' | ||
30 | DB_ONE['protocol_extra'] ='psycopg2' | ||
31 | DB_ONE['hostname'] ='127.0.0.1' | ||
32 | DB_ONE['username'] ='test' | ||
33 | DB_ONE['password'] ='test' | ||
34 | DB_ONE['port'] ='12345' | ||
35 | DB_ONE['database'] ='testing' | ||
36 | DB_ONE['schema'] ='foo' | ||
37 | |||
38 | DB_TWO = {} | ||
39 | DB_TWO['protocol'] ='sqlite' | ||
40 | DB_TWO['filename'] ='/tmp/test' | ||
41 | |||
28 | def setUp(self): | 42 | def setUp(self): |
29 | self.create_engine = Dingus('create_engine') | 43 | self.create_engine = Dingus('create_engine') |
30 | self.session_maker = Dingus('session_maker') | 44 | self.session_maker = Dingus('session_maker') |
@@ -32,33 +46,10 @@ class TestSa(unittest.TestCase): | |||
32 | self.obj = Sa(self.create_engine, self.session_maker, | 46 | self.obj = Sa(self.create_engine, self.session_maker, |
33 | self.result_object) | 47 | self.result_object) |
34 | 48 | ||
35 | |||
36 | def test_sa_one(self): | 49 | def test_sa_one(self): |
37 | name = 'test_db_two' | 50 | name = 'test_db_one' |
38 | obj = DbOne() | 51 | db = self.obj(self.DB_ONE, name) |
39 | db = self.obj(name, obj) | ||
40 | |||
41 | 52 | ||
42 | def test_sa_two(self): | 53 | def test_sa_two(self): |
43 | name = 'test_db_six' | 54 | name = 'test_db_two' |
44 | obj = DbTwo() | 55 | db = self.obj(self.DB_TWO, name) |
45 | db = self.obj(name, obj) | ||
46 | |||
47 | |||
48 | class DbOne(object): | ||
49 | |||
50 | def __init__(self): | ||
51 | self.protocol='postgresql' | ||
52 | self.protocol_extra='psycopg2' | ||
53 | self.hostname='127.0.0.1' | ||
54 | self.username='test' | ||
55 | self.password='test' | ||
56 | self.port='12345' | ||
57 | self.database='testing' | ||
58 | self.schema='foo' | ||
59 | |||
60 | class DbTwo(object): | ||
61 | |||
62 | def __init__(self): | ||
63 | self.protocol='sqlite' | ||
64 | self.filename='/tmp/test' | ||