Completed
Push — master ( 0d56a8...9e1177 )
by Jasper
10s
created

ConfigurationTests   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 50
Duplicated Lines 0 %
Metric Value
dl 0
loc 50
rs 10
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
B createConfiguration() 0 13 5
A test_Uses_values_in_file() 0 7 3
A test_If_config_file_reads_it() 0 4 1
A setUp() 0 4 2
A test_If_no_config_file_uses_defaults() 0 5 1
A test_Can_deal_with_lists() 0 5 2
A test_Can_pass_file_location() 0 4 1
1
import unittest
2
from mock import Mock, patch
3
import os
4
5
6
class ConfigurationTests(unittest.TestCase):
7
8
    def setUp(self):
9
        self.ospath = Mock()
10
        self.parser = Mock()
11
        self.parser.get.side_effect = lambda s, k: 'nothing'
12
13
    def createConfiguration(self, configFilePath=None):
14
        from niprov.config import Configuration
15
        with patch('niprov.config.os') as osp:
16
            osp.path = self.ospath
17
            with patch('niprov.config.ConfigParser') as ConfigParser:
18
                osp.path.expanduser.side_effect = lambda p: p.replace('~',
19
                    'user-home-directory')
20
                ConfigParser.SafeConfigParser.return_value = self.parser
21
                if configFilePath:
22
                    conf = Configuration(configFilePath)
23
                else:
24
                    conf = Configuration()
25
        return conf
26
27
    def test_If_no_config_file_uses_defaults(self):
28
        self.ospath.isfile.return_value = False
29
        conf = self.createConfiguration()
30
        self.assertEqual('info', conf.verbosity)
31
        self.assertEqual(False, conf.dryrun)
32
33
    def test_If_config_file_reads_it(self):
34
        self.ospath.isfile.return_value = True
35
        conf = self.createConfiguration()
36
        self.parser.read.assert_called_with('user-home-directory/niprov.cfg')
37
38
    def test_Can_pass_file_location(self):
39
        self.ospath.isfile.return_value = True
40
        conf = self.createConfiguration('~/my/path.cfg')
41
        self.parser.read.assert_called_with('user-home-directory/my/path.cfg')
42
43
    def test_Uses_values_in_file(self):
44
        self.ospath.isfile.return_value = True
45
        self.parser.getboolean.side_effect = lambda s, k: True
46
        self.parser.get.side_effect = lambda s, k: 'mothership'
47
        conf = self.createConfiguration()
48
        self.assertEqual(True, conf.dryrun)
49
        self.assertEqual('mothership', conf.database_type)
50
51
    def test_Can_deal_with_lists(self):
52
        self.ospath.isfile.return_value = True
53
        self.parser.get.side_effect = lambda s, k: 'a1,b2, c3,d4 ,'
54
        conf = self.createConfiguration()
55
        self.assertEqual(['a1','b2','c3','d4'], conf.discover_file_extensions)
56
57