Completed
Push — develop ( c97d07...11b882 )
by Kale
01:01
created

tests.BasicConfigTests   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 97
Duplicated Lines 0 %
Metric Value
dl 0
loc 97
rs 10
wmc 28
1
import os
2
import unittest
3
4
from ddt import ddt, unpack, data
5
from testtools import TestCase
6
7
import auxlib.configuration
8
from auxlib.configuration import make_env_key, Configuration, reverse_env_key, YamlSource
9
from auxlib.exceptions import AssignmentError, NotFoundError
10
from auxlib.type_coercion import typify
11
12
13
APP_NAME = 'test'
14
PACKAGE = auxlib.configuration.__package__
15
data_document = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'sample_config.yml')
16
17
18
yaml_source = YamlSource(location=data_document,
19
                         provides=['new_param', 'foo', 'nonetype', 'bool1', 'bool2', 'bool3'])
20
21
22
@ddt
23
class UtilityTests(TestCase):
24
    name_tests = [('firstsecond', 'FIRSTSECOND'),
25
                  ('first_second', 'FIRST_SECOND'),
26
                  ('first-second', 'FIRST_SECOND'),
27
                  ('first second', 'FIRST_SECOND'),
28
                  ('first second-THird_foURTH', 'FIRST_SECOND_THIRD_FOURTH')]
29
30
    @unpack
31
    @data(*name_tests)
32
    def test_make_env_name(self, badname, fixedname):
33
        expected_name = APP_NAME.upper() + '_' + fixedname
34
        self.assertEqual(expected_name, make_env_key(APP_NAME, badname))
35
36
    @data('only', 'simple_lowercase', 'will_work_right')
37
    def test_reverse_env_key(self, key):
38
        self.assertEqual(key, reverse_env_key(APP_NAME, make_env_key(APP_NAME, key)))
39
40
    @unpack
41
    @data(('true', True), ('Yes', True), ('yes', True), ('ON', True), ('NO', False),
42
          ('non', 'non'), ('None', None), ('none', None), ('nonea', 'nonea'), (1, 1), ('1', 1),
43
          ('11.1', 11.1), ('11.1  ', 11.1), ('  44  ', 44), (12.2, 12.2), (None, None))
44
    def test_typify_without_hint(self, value, result):
45
        self.assertEqual(result, typify(value))
46
47
    @unpack
48
    @data(('15', True, bool), ('-1', -1.0, float), ('0', False, bool), ('00.000', False, bool),
49
          ('00.001', True, bool))
50
    def test_typify_with_hint(self, value, result, hint):
51
        self.assertEqual(result, typify(value, hint))
52
53
    def test_package_name(self):
54
        assert PACKAGE == 'auxlib'
55
56
57
@ddt
58
class BasicConfigTests(unittest.TestCase):
59
60
    def setUp(self):
61
        super(BasicConfigTests, self).setUp()
62
        os.environ[make_env_key(APP_NAME, 'foobaz')] = 'no'
63
        os.environ[make_env_key(APP_NAME, 'zero')] = '0'
64
        os.environ[make_env_key(APP_NAME, 'an_int')] = '55'
65
        os.environ[make_env_key(APP_NAME, 'a_float')] = '55.555'
66
        os.environ[make_env_key(APP_NAME, 'a_none')] = 'none'
67
        os.environ[make_env_key(APP_NAME, 'just_a_string')] = 'what say you'
68
        self.config = Configuration(APP_NAME, yaml_source)
69
70
    @unpack
71
    @data(('new_param', 42), ('foo', 'bar'), ('nonetype', None), ('bool1', True),
72
          ('bool2', True), ('bool3', True))
73
    def test_load_config_file(self, config_name, value):
74
        self.assertEqual(value, self.config[config_name])
75
        self.assertEqual(type(value), type(self.config[config_name]))
76
77
    @unpack
78
    @data(('foobaz', False), ('zero', 0), ('an_int', 55), ('a_float', 55.555), ('a_none', None),
79
          ('just_a_string', 'what say you'))
80
    def test_load_env_vars(self, config_name, value):
81
        self.assertEqual(value, self.config[config_name])
82
83
    def test_get_attr(self):
84
        self.assertEqual(False, self.config.foobaz)
85
86
    def test_get_attr_not_exist(self):
87
        with self.assertRaises(NotFoundError):
88
            self.config.not_a_key
89
90
    def test_get_item_not_exist(self):
91
        with self.assertRaises(NotFoundError):
92
            self.config['not_a_key']
93
94
    def test_get_method(self):
95
        self.assertEqual(False, self.config.get('foobaz'))
96
        self.assertEqual(None, self.config.get('not_there'))
97
        self.assertEqual(22, self.config.get('not_there', 22))
98
99
    def test_set_unset_env(self):
100
        self.assertFalse('new_key' in self.config)
101
        self.config.set_env('new_key', 12345)
102
        self.assertTrue('new_key' in self.config)
103
        self.assertEqual(12345, self.config.new_key)
104
        self.config.unset_env('new_key')
105
        self.assertFalse('new_key' in self.config)
106
        self.config.set_env('new_key', '0')
107
        self.assertTrue('new_key' in self.config)
108
        self.assertEqual(False, self.config.new_key)
109
110
    def test_unset_env_and_reload(self):
111
        self.assertTrue('a_float' in self.config)
112
        self.config.unset_env('a_float')
113
        self.assertFalse('a_float' in self.config)
114
115
    def test_ensure_required_keys(self):
116
        required_keys = ('bool1', 'nonetype', )
117
        self.config = Configuration(APP_NAME, yaml_source, required_keys)
118
        required_keys += ('doesnt_exist', )
119
        self.assertRaises(EnvironmentError, Configuration(APP_NAME, yaml_source, required_keys).verify)
120
        os.environ[make_env_key(APP_NAME, 'doesnt_exist')] = 'now it does'
121
        self.config = Configuration(APP_NAME, yaml_source, required_keys)
122
        self.assertEqual('now it does', self.config.doesnt_exist)
123
124
    def test_items(self):
125
        self.assertTrue({('a_none', None), ('bool1', True)}.issubset(set(self.config.items())))
126
127
    def test_assignment_lock(self):
128
        with self.assertRaises(AssignmentError):
129
            self.config['bool2'] = 'false'
130
131
    def test_config_no_sources(self):
132
        config = Configuration(APP_NAME)
133
        assert config.foobaz is False
134
        assert config.zero == 0
135
        assert config.an_int == 55
136
        assert config.a_float == 55.555
137
        assert config.a_none is None
138
        assert config.just_a_string == 'what say you'
139
140
        config.unset_env('an_int')
141
        with self.assertRaises(NotFoundError):
142
            assert config.an_int == 55
143
144
        config.set_env('this_key', 'that value')
145
        config.set_env('that_key', '42.42')
146
147
        assert config.this_key == 'that value'
148
        assert config.that_key == 42.42
149
150
    def test_config_no_sources_required_params(self):
151
        required_parameters = ('beta', 'theta')
152
        with self.assertRaises(EnvironmentError):
153
            Configuration(APP_NAME, required_parameters=required_parameters).verify()
154
155
156
157
158
159