aboutsummaryrefslogtreecommitdiff
path: root/test/test_config/test_databases.py
blob: 9fc6f68ab6d27f637462bec71a65827e87e35e8f (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# Copyright (C) 2010  Leonard Thomas
#
# This file is part of Dodai.
#
# Dodai is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Dodai is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Dodai.  If not, see <http://www.gnu.org/licenses/>.


import sys
import os
import unittest
import ConfigParser
from dingus import Dingus
path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..'))
sys.path.append(path)
from dodai.config.databases import ConfigDatabases
from dodai.config.databases import DatabaseConnectionValidator
from dodai.exception import DatabaseEmptyOptionException
from dodai.exception import DatabasePortException
from dodai.exception import DatabaseHostnameException
from dodai.exception import DatabaseProtocolException
from dodai.exception import DatabaseConnectionException
from dodai.exception import UnknownDatabaseConnectionException
from dodai.tools import list_to_english
from dodai.tools.himo import String2Himo
from dodai.config.sections import ConfigSections


class TestConfigDatabases(unittest.TestCase):

    SECTIONS = {}

    # valid server connection
    SECTIONS['db_1'] = {}
    SECTIONS['db_1']['protocol'] = 'postgresql'
    SECTIONS['db_1']['hostname'] = '127.0.0.1'
    SECTIONS['db_1']['username'] = 'test'
    SECTIONS['db_1']['password'] = 'test'
    SECTIONS['db_1']['port'] = '12345'
    SECTIONS['db_1']['database'] = 'testing'
    SECTIONS['db_1']['schema'] = 'foo'

    # valid file connection
    SECTIONS['db_2'] = {}
    SECTIONS['db_2']['protocol'] = 'sqlite'
    SECTIONS['db_2']['filename'] = '/tmp/test'

    # missing protocol server
    SECTIONS['db_3'] = {}
    SECTIONS['db_3']['hostname'] = '127.0.0.1'
    SECTIONS['db_3']['username'] = 'test'
    SECTIONS['db_3']['password'] = 'test'
    SECTIONS['db_3']['port'] = '12345'
    SECTIONS['db_3']['database'] = 'testing'

    # missing protocol file
    SECTIONS['db_4'] = {}
    SECTIONS['db_4']['filename'] = '/tmp/test'

    # empty protocol server
    SECTIONS['db_5'] = {}
    SECTIONS['db_5']['protocol'] = ''
    SECTIONS['db_5']['hostname'] = '127.0.0.1'
    SECTIONS['db_5']['username'] = 'test'
    SECTIONS['db_5']['password'] = 'test'
    SECTIONS['db_5']['port'] = '12345'
    SECTIONS['db_5']['database'] = 'testing'

    # empty protocol file
    SECTIONS['db_6'] = {}
    SECTIONS['db_6']['protocol'] = None
    SECTIONS['db_6']['filename'] ='/tmp/test'

    # Missing port
    SECTIONS['db_7'] = {}
    SECTIONS['db_7']['protocol'] = 'postgresql'
    SECTIONS['db_7']['protocol_extra'] = 'psycopg2'
    SECTIONS['db_7']['hostname'] = '127.0.0.1'
    SECTIONS['db_7']['username'] = 'test'
    SECTIONS['db_7']['password'] = 'test'
    SECTIONS['db_7']['database'] = 'testing'

    # Empty port
    SECTIONS['db_8'] = {}
    SECTIONS['db_8']['protocol'] ='postgresql'
    SECTIONS['db_8']['hostname'] ='127.0.0.1'
    SECTIONS['db_8']['username'] ='test'
    SECTIONS['db_8']['password'] ='test'
    SECTIONS['db_8']['port'] =''
    SECTIONS['db_8']['database'] ='testing'

    # Invalid port because it's a string
    SECTIONS['db_9'] = {}
    SECTIONS['db_9']['protocol'] ='postgresql'
    SECTIONS['db_9']['hostname'] ='127.0.0.1'
    SECTIONS['db_9']['username'] ='test'
    SECTIONS['db_9']['password'] ='test'
    SECTIONS['db_9']['port'] ='blah'
    SECTIONS['db_9']['database'] ='testing'

    # Invalid port low
    SECTIONS['db_10'] = {}
    SECTIONS['db_10']['protocol'] ='postgresql'
    SECTIONS['db_10']['hostname'] ='127.0.0.1'
    SECTIONS['db_10']['username'] ='test'
    SECTIONS['db_10']['password'] ='test'
    SECTIONS['db_10']['port'] ='0'
    SECTIONS['db_10']['database'] ='testing'

    # Invalid port high
    SECTIONS['db_11'] = {}
    SECTIONS['db_11']['protocol'] ='postgresql'
    SECTIONS['db_11']['hostname'] ='127.0.0.1'
    SECTIONS['db_11']['username'] ='test'
    SECTIONS['db_11']['password'] ='test'
    SECTIONS['db_11']['port'] ='655555'
    SECTIONS['db_11']['database'] ='testing'

    # missing hostname
    SECTIONS['db_12'] = {}
    SECTIONS['db_12']['protocol'] ='postgresql'
    SECTIONS['db_12']['username'] ='test'
    SECTIONS['db_12']['password'] ='test'
    SECTIONS['db_12']['port'] ='655'
    SECTIONS['db_12']['database'] ='testing'

    # empty hostname
    SECTIONS['db_13'] = {}
    SECTIONS['db_13']['protocol'] ='postgresql'
    SECTIONS['db_13']['hostname'] =''
    SECTIONS['db_13']['username'] ='test'
    SECTIONS['db_13']['password'] ='test'
    SECTIONS['db_13']['port'] ='655'
    SECTIONS['db_13']['database'] ='testing'

    # missing username
    SECTIONS['db_14'] = {}
    SECTIONS['db_14']['protocol'] = 'postgresql'
    SECTIONS['db_14']['hostname'] = '127.0.0.1'
    SECTIONS['db_14']['password'] = 'test'
    SECTIONS['db_14']['port'] = '12345'
    SECTIONS['db_14']['database'] = 'testing'
    SECTIONS['db_14']['schema'] = 'foo'

    # empty username
    SECTIONS['db_15'] = {}
    SECTIONS['db_15']['protocol'] = 'postgresql'
    SECTIONS['db_15']['hostname'] = '127.0.0.1'
    SECTIONS['db_15']['username'] = ''
    SECTIONS['db_15']['password'] = 'test'
    SECTIONS['db_15']['port'] = '12345'
    SECTIONS['db_15']['database'] = 'testing'

    # missing password
    SECTIONS['db_16'] = {}
    SECTIONS['db_16']['protocol'] = 'postgresql'
    SECTIONS['db_16']['hostname'] = '127.0.0.1'
    SECTIONS['db_16']['username'] = 'test'
    SECTIONS['db_16']['port'] = '12345'
    SECTIONS['db_16']['database'] = 'testing'

    # empty password
    SECTIONS['db_17'] = {}
    SECTIONS['db_17']['protocol'] = 'postgresql'
    SECTIONS['db_17']['hostname'] = '127.0.0.1'
    SECTIONS['db_17']['username'] = 'test'
    SECTIONS['db_17']['password'] = ''
    SECTIONS['db_17']['port'] = '12345'
    SECTIONS['db_17']['database'] = 'testing'

    # missing database
    SECTIONS['db_18'] = {}
    SECTIONS['db_18']['protocol'] = 'postgresql'
    SECTIONS['db_18']['hostname'] = '127.0.0.1'
    SECTIONS['db_18']['username'] = 'test'
    SECTIONS['db_18']['password'] = 'test'
    SECTIONS['db_18']['port'] = '12345'

    # empty database
    SECTIONS['db_19'] = {}
    SECTIONS['db_19']['protocol'] = 'postgresql'
    SECTIONS['db_19']['hostname'] = '127.0.0.1'
    SECTIONS['db_19']['username'] = 'test'
    SECTIONS['db_19']['password'] = 'test'
    SECTIONS['db_19']['port'] = '12345'
    SECTIONS['db_19']['database'] = ''

    # missing filename
    SECTIONS['db_20'] = {}
    SECTIONS['db_20']['protocol'] = 'sqlite'

    # empty filename
    SECTIONS['db_21'] = {}
    SECTIONS['db_21']['protocol'] = 'sqlite'
    SECTIONS['db_21']['filename'] = ''

    # not a database
    SECTIONS['db_22'] = {}
    SECTIONS['db_22']['foo'] = 'bar'

    # valid server test handler
    SECTIONS['db_23'] = {}
    SECTIONS['db_23']['handler'] = 'test_handler'
    SECTIONS['db_23']['protocol'] = 'postgresql'
    SECTIONS['db_23']['hostname'] = '127.0.0.1'
    SECTIONS['db_23']['username'] = 'test'
    SECTIONS['db_23']['password'] = 'test'
    SECTIONS['db_23']['port'] = '12345'
    SECTIONS['db_23']['database'] = 'testing'

    # invalid protocol
    SECTIONS['db_24'] = {}
    SECTIONS['db_24']['protocol'] = 'foo'
    SECTIONS['db_24']['hostname'] = '127.0.0.1'
    SECTIONS['db_24']['username'] = 'test'
    SECTIONS['db_24']['password'] = 'test'
    SECTIONS['db_24']['port'] = '12345'
    SECTIONS['db_24']['database'] = 'testing'

    def setUp(self):
        sa = Dingus('sa')
        sa.name = 'test'
        self.db = ConfigDatabases(sa, 'sa')
        self.db.add(self.SECTIONS)

    def test_default_handler(self):
        self.assertTrue('sa' in self.db._handlers)
        val = self.db._handlers['sa']
        self.assertTrue('test' == val.name)

    def test_sections(self):
        self.assertTrue(len(self.db.connections) == len(self.SECTIONS))

    def test_load_server(self):
        obj = self.db.load('db_1')

    def test_load_file(self):
        obj = self.db.load('db_2')

    def test_unable_to_load(self):
        self.failUnlessRaises(UnknownDatabaseConnectionException,
                              self.db.load, 'db_twenty_seven')

    def test_missing_protocol_server(self):
        self.failUnlessRaises(DatabaseEmptyOptionException,
                              self.db.load, 'db_3')

    def test_missing_protocol_file(self):
        self.failUnlessRaises(DatabaseEmptyOptionException,
                              self.db.load, 'db_4')

    def test_empty_protocol_server(self):
        self.failUnlessRaises(DatabaseEmptyOptionException,
                              self.db.load, 'db_5')

    def test_empty_protocol_file(self):
        self.failUnlessRaises(DatabaseEmptyOptionException,
                              self.db.load, 'db_6')

    def test_missing_port(self):
        self.failUnlessRaises(DatabaseEmptyOptionException,
                              self.db.load, 'db_7')

    def test_empty_port(self):
        self.failUnlessRaises(DatabaseEmptyOptionException,
                              self.db.load, 'db_8')

    def test_invalid_port_because_its_a_string(self):
        self.failUnlessRaises(DatabasePortException,
                              self.db.load, 'db_9')

    def test_invalid_port_because_its_to_low(self):
        self.failUnlessRaises(DatabasePortException,
                              self.db.load, 'db_10')

    def test_invalid_port_because_its_to_high(self):
        self.failUnlessRaises(DatabasePortException,
                              self.db.load, 'db_11')

    def test_missing_hostname(self):
        self.failUnlessRaises(DatabaseEmptyOptionException,
                              self.db.load, 'db_12')

    def test_empty_hostname(self):
        self.failUnlessRaises(DatabaseEmptyOptionException,
                              self.db.load, 'db_13')

    def test_missing_username(self):
        self.failUnlessRaises(DatabaseEmptyOptionException,
                              self.db.load, 'db_14')

    def test_empty_username(self):
        self.failUnlessRaises(DatabaseEmptyOptionException,
                              self.db.load, 'db_15')

    def test_missing_password(self):
        self.failUnlessRaises(DatabaseEmptyOptionException,
                              self.db.load, 'db_16')

    def test_empty_password(self):
        self.failUnlessRaises(DatabaseEmptyOptionException,
                              self.db.load, 'db_17')

    def test_missing_database(self):
        self.failUnlessRaises(DatabaseEmptyOptionException,
                              self.db.load, 'db_18')

    def test_empty_database(self):
        self.failUnlessRaises(DatabaseEmptyOptionException,
                              self.db.load, 'db_19')

    def test_missing_filename(self):
        self.failUnlessRaises(DatabaseEmptyOptionException,
                              self.db.load, 'db_20')

    def test_empty_filename(self):
        self.failUnlessRaises(DatabaseEmptyOptionException,
                              self.db.load, 'db_21')

    def test_not_a_connection(self):
        self.failUnlessRaises(DatabaseConnectionException,
                              self.db.load, 'db_22')

    def test_non_default_handler(self):
        test_handler = Dingus('test_handler')
        self.db.add_handler('test_handler', test_handler)
        self.db.load('db_23')

    def test_invalid_protocol(self):
        self.failUnlessRaises(DatabaseProtocolException,
                              self.db.load, 'db_24')