aboutsummaryrefslogtreecommitdiff
path: root/test/test_config/test_file.py
blob: 1ed014ef82d9ce3e0f43a1b77bcfc48e47ace0ca (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
# 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.file import ConfigFile
from dodai.config.file import DirectoryDoesNotExistException
from dodai.config.file import DirectoryNotSetException
from dodai.config.file import InvalidDirectoryException
from dodai.config.file import FileDoesNotExistException
from dodai.config.file import NoFilesToLoadException
import ConfigParser

class TestConfigFile(unittest.TestCase):

    def setUp(self):
        self.obj = ConfigFile()
        self.path = os.path.dirname(__file__)
        self.filename = 'config'
        self.filepath = os.path.join(self.path, self.filename)
        next_file = os.path.join(self.path, 'config.cfg')
        self.paths = [self.filepath, next_file]

    def test_file_add(self):
        self.obj.add_file(self.filepath)
        paths = [self.filepath]
        files = self.obj.get_files()
        self.assertEqual(files, paths)

    def test_double_file_add(self):
        self.obj.add_file(self.filepath)
        self.obj.add_file(self.filepath)
        paths = [self.filepath]
        files = self.obj.get_files()
        self.assertEqual(files, paths)

    def test_file_does_not_exist(self):
        path = os.path.join(self.path, 'foo')
        self.failUnlessRaises(FileDoesNotExistException,
                              self.obj.add_file, path)

    def test_multiple_file_add(self):
        self.obj.add_file(self.paths)
        files = self.obj.get_files()
        self.assertEqual(files, self.paths)

    def test_empty_parser(self):
        self.failUnlessRaises(Exception, self.obj.parser)

    def test_parser(self):
        self.obj.add_file(self.filepath)
        parser = self.obj.parser()
        self.assertTrue(isinstance(parser, ConfigParser.ConfigParser))

    def test_parser_error(self):
        self.failUnlessRaises(NoFilesToLoadException, self.obj.parser)

    def test_set_directory(self):
        self.obj.set_directory(self.path)
        dir = self.obj.get_directory()
        self.assertEqual(dir, self.path)

    def test_invalid_directory(self):
        self.failUnlessRaises(InvalidDirectoryException,
                            self.obj.set_directory, self.filepath)

    def test_directory_does_not_exist(self):
        path = os.path.join(self.path, 'nowayjose')
        self.failUnlessRaises(DirectoryDoesNotExistException,
                            self.obj.set_directory, path)

    def test_load(self):
        self.obj.set_directory(self.path)
        self.obj.load(self.filename)
        check = [self.filepath]
        self.assertEqual(check, self.obj.get_files())

    def test_no_directory_set(self):
        self.failUnlessRaises(DirectoryNotSetException,
                            self.obj.load, self.filename)

    def test_reset_parser(self):
        self.obj.add_file(self.filepath)
        self.obj.parser()
        self.obj.add_file(self.paths)
        self.obj.parser()
        self.assertEqual(self.obj.files_loaded, self.obj.get_files())