aboutsummaryrefslogtreecommitdiff
path: root/test/test_config/test_db.py
blob: ed46eae7cddbb5281bad69ede0d21086f78ae2b5 (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
# 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 unittest
import os
from dodai.config.db import ConfigDb
from dodai.config.db import ConfigDbFile
from dodai.config.db import BaseConfigDb
from dodai.config.db import NotConfigParserObject
from dodai.config.db import InvalidProtocolException
from dodai.config.db import InvalidPortException
from dodai.config.db import UnknownHandlerException
from dodai.config.db import UnknownConnectionException
from dodai.config.db.sa import Sa
from dodai.config.file import ConfigFile
from dodai.db import Db

class TestConfigDb(unittest.TestCase):


    def setUp(self):
        self.config_db = ConfigDb()
        config = ConfigFile()
        config.set_directory(os.path.dirname(os.path.abspath(__file__)))
        config.load('config.cfg')
        self.parser = config.parser()

    def test_setup(self):
        obj = self.config_db._handlers['sa'][0]
        self.assertTrue(obj == Sa)

    def test_register_handler(self):
        self.config_db.register_handler('foo', Exception)
        self.assertTrue('foo' in self.config_db._handlers.keys())

    def test_add_config_one(self):
        self.config_db.add_config(config_parser=self.parser)
        self.assertTrue('test_db' in self.config_db.connections.keys())

    def test_add_config_two(self):
        self.failUnlessRaises(NotConfigParserObject, self.config_db.add_config,
                              config_parser='blah')

    def test_load_one(self):
        self.config_db.add_config(config_parser=self.parser)
        obj = self.config_db.load('test_db')
        self.assertTrue(isinstance(obj, Db))

    def test_load_two(self):
        self.config_db.add_config(config_parser=self.parser)
        obj = self.config_db.load('test_db')
        obj = self.config_db.load('test_db')
        self.assertTrue(isinstance(obj, Db))

    def test_load_handler(self):
        self.failUnlessRaises(UnknownHandlerException,
                              self.config_db._load_handler, 'test')

    def test_clean_protocol_one(self):
        self.config_db.add_config(config_parser=self.parser)
        obj = self.config_db.load('test_db_two')
        self.assertTrue(isinstance(obj, Db))

    def test_clean_protocol_one(self):
        self.config_db.add_config(config_parser=self.parser)
        obj = self.config_db.load('test_db_two')
        self.assertTrue(isinstance(obj, Db))

    def test_clean_protocol_two(self):
        self.config_db.add_config(config_parser=self.parser)
        self.failUnlessRaises(InvalidProtocolException, self.config_db.load,
                             'test_db_three')

    def test_clean_port_one(self):
        obj = BaseConfigDb()
        data = obj._clean_port('ad')
        self.assertTrue(data == None)

    def test_clean_port_two(self):
        obj = BaseConfigDb()
        data = obj._clean_port(None)
        self.assertTrue(data == None)

    def test_clean_port_three(self):
        obj = BaseConfigDb()
        self.failUnlessRaises(InvalidPortException, obj._clean_port, 66666)


    def test_file_load_one(self):
        self.config_db.add_config(config_parser=self.parser)
        obj = self.config_db.load('test_db_six')
        self.assertTrue(isinstance(obj, Db))