This repository was archived by the owner on Sep 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 877
/
Copy pathtest_config.py
172 lines (137 loc) · 5.25 KB
/
test_config.py
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
# -*- coding: utf-8 -*-
# from nose import tools
# from docker_registry.core import exceptions
# import docker_registry.testing as testing
import os
import unittest
import mock
from docker_registry.lib import config
fakeenv = {}
def mockget(key, opt=None):
if key in fakeenv:
print('%s key is %s' % (key, fakeenv[key]))
return fakeenv[key]
return opt
@mock.patch('os.environ.get', mockget)
class TestConfig(unittest.TestCase):
def setUp(self):
p = os.path.join(
os.path.dirname(__file__), 'fixtures', 'test_config.yaml')
self.c = config.Config(open(p, 'rb').read())
def test__init__parse_error(self):
self.assertRaises(config.exceptions.ConfigError, config.Config, '\1')
def test__init__no_arg(self):
self.c = config.Config()
assert self.c['whatevertheflush'] is None
assert self.c.whatevertheflush is None
@mock.patch('__builtin__.repr')
def test__repr(self, r):
self.c.__repr__()
r.assert_called_once_with(self.c._config)
def test__methods__(self):
self.assertEqual(self.c.__methods__, [])
def test__members__(self):
self.assertEqual(type(self.c.__members__), list)
self.assertEqual(self.c.__members__, self.c.keys())
self.assertEqual(self.c.__members__, self.c.__dir__())
def test_accessors(self):
assert self.c.booltrue == self.c['booltrue']
assert self.c.dict.one == self.c.dict['one']
assert self.c.dict.one == self.c['dict']['one']
def test_key_existence(self):
assert 'boolfalse' in self.c
assert 'whatevertheflush' not in self.c
def test_non_existent_access(self):
assert self.c['whatevertheflush'] is None
assert self.c.whatevertheflush is None
def test_simple_types(self):
conf = self.c
assert conf.booltrue is True
assert not conf.booltrue == 'True'
assert conf.boolfalse is False
assert not conf.booltrue == 'False'
assert conf.uint == 10
assert not conf.uint == '10'
assert conf.int == -10
assert not conf.int == '-10'
assert conf.float == 0.01
assert not conf.float == '0.01'
assert conf.emptystring == ''
assert conf.emptystring is not None
assert conf.isnone is None
assert conf.nonemptystring == 'nonemptystring'
assert conf.anothernonemptystring == 'nonemptystring'
assert conf.yetanothernonemptystring == 'nonemptystring'
assert conf.array[2] == 'three'
assert len(conf.array) == 3
assert conf.dict.two == 'valuetwo'
assert isinstance(conf.dict, config.Config)
def test_env_defaults(self):
global fakeenv
fakeenv = {}
conf = self.c.ENV
assert conf.booltrue is True
assert conf.boolfalse is False
assert conf.uint == 10
assert conf.int == -10
assert conf.float == 0.01
assert conf.emptystring == ''
assert conf.emptystring is not None
assert conf.isnone is None
assert conf.nonemptystring == 'nonemptystring'
assert conf.anothernonemptystring == 'nonemptystring'
assert conf.yetanothernonemptystring == 'nonemptystring'
assert conf.bugger == 'bug:me:endlessly'
assert conf.array[2] == 'three'
assert len(conf.array) == 3
assert conf.dict is None
def test_env_overrides(self):
global fakeenv
fakeenv['BOOLTRUE'] = 'False'
fakeenv['BOOLFALSE'] = 'True'
fakeenv['UINT'] = '0'
fakeenv['INT'] = '0'
fakeenv['FLOAT'] = '0'
fakeenv['EMPTYSTRING'] = 'NOTREALLY'
fakeenv['ISNONE'] = 'False'
fakeenv['NONEMPTYSTRING'] = '""'
fakeenv['BUGGER'] = '"whatever:the:flush:"'
fakeenv['ARRAY'] = '[one, again]'
fakeenv['DICT'] = '{"one": "oneagain", "two": "twoagain"}'
conf = self.c.ENV
assert conf.booltrue is False
assert conf.boolfalse is True
assert conf.uint == 0
assert conf.int == 0
assert conf.float == 0
assert conf.emptystring == 'NOTREALLY'
assert conf.isnone is False
assert conf.isnone is not None
assert conf.nonemptystring == ''
assert conf.anothernonemptystring == 'nonemptystring'
assert conf.yetanothernonemptystring == 'nonemptystring'
assert conf.bugger == 'whatever:the:flush:'
assert conf.array[1] == 'again'
assert len(conf.array) == 2
fakeenv['ISNONE'] = ''
assert conf.isnone is None
assert isinstance(conf.dict, config.Config)
assert conf.dict.one == 'oneagain'
def test_write(self):
conf = self.c
assert conf.something == 'else'
conf.something = 'or'
assert conf.something == 'or'
conf.something = None
assert conf.something is None
def test_unicode(self):
assert self.c.uni == u'ß∞'
class TestLoad(unittest.TestCase):
def setUp(self):
self._config = config._config
def tearDown(self):
config._config = self._config
@mock.patch.object(config.os.environ, 'get')
def test_config_path_exception(self, get):
config._config = None
self.assertRaises(config.exceptions.FileNotFoundError, config.load)