aboutsummaryrefslogtreecommitdiff
path: root/test/test_config/test_databases.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_config/test_databases.py')
-rw-r--r--test/test_config/test_databases.py341
1 files changed, 341 insertions, 0 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
19import sys
20import os
21import unittest
22import ConfigParser
23from dingus import Dingus
24path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..'))
25sys.path.append(path)
26from dodai.config.databases import ConfigDatabases
27from dodai.config.databases import DatabaseConnectionValidator
28from dodai.exception import DatabaseEmptyOptionException
29from dodai.exception import DatabasePortException
30from dodai.exception import DatabaseHostnameException
31from dodai.exception import DatabaseProtocolException
32from dodai.exception import DatabaseConnectionException
33from dodai.exception import UnknownDatabaseConnectionException
34from dodai.tools import list_to_english
35from dodai.tools.himo import String2Himo
36from dodai.config.sections import ConfigSections
37
38
39class 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')