##// END OF EJS Templates
test Config objects from dicts
MinRK -
Show More
@@ -1,263 +1,284 b''
1 1 # encoding: utf-8
2 2 """
3 3 Tests for IPython.config.loader
4 4
5 5 Authors:
6 6
7 7 * Brian Granger
8 8 * Fernando Perez (design help)
9 9 """
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Copyright (C) 2008-2011 The IPython Development Team
13 13 #
14 14 # Distributed under the terms of the BSD License. The full license is in
15 15 # the file COPYING, distributed as part of this software.
16 16 #-----------------------------------------------------------------------------
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Imports
20 20 #-----------------------------------------------------------------------------
21 21
22 22 import os
23 23 import sys
24 24 from tempfile import mkstemp
25 25 from unittest import TestCase
26 26
27 27 from nose import SkipTest
28 28
29 29 from IPython.testing.tools import mute_warn
30 30
31 31 from IPython.utils.traitlets import Unicode
32 32 from IPython.config.configurable import Configurable
33 33 from IPython.config.loader import (
34 34 Config,
35 35 PyFileConfigLoader,
36 36 KeyValueConfigLoader,
37 37 ArgParseConfigLoader,
38 38 KVArgParseConfigLoader,
39 39 ConfigError
40 40 )
41 41
42 42 #-----------------------------------------------------------------------------
43 43 # Actual tests
44 44 #-----------------------------------------------------------------------------
45 45
46 46
47 47 pyfile = """
48 48 c = get_config()
49 49 c.a=10
50 50 c.b=20
51 51 c.Foo.Bar.value=10
52 52 c.Foo.Bam.value=list(range(10)) # list() is just so it's the same on Python 3
53 53 c.D.C.value='hi there'
54 54 """
55 55
56 56 class TestPyFileCL(TestCase):
57 57
58 58 def test_basic(self):
59 59 fd, fname = mkstemp('.py')
60 60 f = os.fdopen(fd, 'w')
61 61 f.write(pyfile)
62 62 f.close()
63 63 # Unlink the file
64 64 cl = PyFileConfigLoader(fname)
65 65 config = cl.load_config()
66 66 self.assertEqual(config.a, 10)
67 67 self.assertEqual(config.b, 20)
68 68 self.assertEqual(config.Foo.Bar.value, 10)
69 69 self.assertEqual(config.Foo.Bam.value, range(10))
70 70 self.assertEqual(config.D.C.value, 'hi there')
71 71
72 72 class MyLoader1(ArgParseConfigLoader):
73 73 def _add_arguments(self, aliases=None, flags=None):
74 74 p = self.parser
75 75 p.add_argument('-f', '--foo', dest='Global.foo', type=str)
76 76 p.add_argument('-b', dest='MyClass.bar', type=int)
77 77 p.add_argument('-n', dest='n', action='store_true')
78 78 p.add_argument('Global.bam', type=str)
79 79
80 80 class MyLoader2(ArgParseConfigLoader):
81 81 def _add_arguments(self, aliases=None, flags=None):
82 82 subparsers = self.parser.add_subparsers(dest='subparser_name')
83 83 subparser1 = subparsers.add_parser('1')
84 84 subparser1.add_argument('-x',dest='Global.x')
85 85 subparser2 = subparsers.add_parser('2')
86 86 subparser2.add_argument('y')
87 87
88 88 class TestArgParseCL(TestCase):
89 89
90 90 def test_basic(self):
91 91 cl = MyLoader1()
92 92 config = cl.load_config('-f hi -b 10 -n wow'.split())
93 93 self.assertEqual(config.Global.foo, 'hi')
94 94 self.assertEqual(config.MyClass.bar, 10)
95 95 self.assertEqual(config.n, True)
96 96 self.assertEqual(config.Global.bam, 'wow')
97 97 config = cl.load_config(['wow'])
98 98 self.assertEqual(config.keys(), ['Global'])
99 99 self.assertEqual(config.Global.keys(), ['bam'])
100 100 self.assertEqual(config.Global.bam, 'wow')
101 101
102 102 def test_add_arguments(self):
103 103 cl = MyLoader2()
104 104 config = cl.load_config('2 frobble'.split())
105 105 self.assertEqual(config.subparser_name, '2')
106 106 self.assertEqual(config.y, 'frobble')
107 107 config = cl.load_config('1 -x frobble'.split())
108 108 self.assertEqual(config.subparser_name, '1')
109 109 self.assertEqual(config.Global.x, 'frobble')
110 110
111 111 def test_argv(self):
112 112 cl = MyLoader1(argv='-f hi -b 10 -n wow'.split())
113 113 config = cl.load_config()
114 114 self.assertEqual(config.Global.foo, 'hi')
115 115 self.assertEqual(config.MyClass.bar, 10)
116 116 self.assertEqual(config.n, True)
117 117 self.assertEqual(config.Global.bam, 'wow')
118 118
119 119
120 120 class TestKeyValueCL(TestCase):
121 121 klass = KeyValueConfigLoader
122 122
123 123 def test_basic(self):
124 124 cl = self.klass()
125 125 argv = ['--'+s.strip('c.') for s in pyfile.split('\n')[2:-1]]
126 126 with mute_warn():
127 127 config = cl.load_config(argv)
128 128 self.assertEqual(config.a, 10)
129 129 self.assertEqual(config.b, 20)
130 130 self.assertEqual(config.Foo.Bar.value, 10)
131 131 self.assertEqual(config.Foo.Bam.value, range(10))
132 132 self.assertEqual(config.D.C.value, 'hi there')
133 133
134 134 def test_expanduser(self):
135 135 cl = self.klass()
136 136 argv = ['--a=~/1/2/3', '--b=~', '--c=~/', '--d="~/"']
137 137 with mute_warn():
138 138 config = cl.load_config(argv)
139 139 self.assertEqual(config.a, os.path.expanduser('~/1/2/3'))
140 140 self.assertEqual(config.b, os.path.expanduser('~'))
141 141 self.assertEqual(config.c, os.path.expanduser('~/'))
142 142 self.assertEqual(config.d, '~/')
143 143
144 144 def test_extra_args(self):
145 145 cl = self.klass()
146 146 with mute_warn():
147 147 config = cl.load_config(['--a=5', 'b', '--c=10', 'd'])
148 148 self.assertEqual(cl.extra_args, ['b', 'd'])
149 149 self.assertEqual(config.a, 5)
150 150 self.assertEqual(config.c, 10)
151 151 with mute_warn():
152 152 config = cl.load_config(['--', '--a=5', '--c=10'])
153 153 self.assertEqual(cl.extra_args, ['--a=5', '--c=10'])
154 154
155 155 def test_unicode_args(self):
156 156 cl = self.klass()
157 157 argv = [u'--a=épsîlön']
158 158 with mute_warn():
159 159 config = cl.load_config(argv)
160 160 self.assertEqual(config.a, u'épsîlön')
161 161
162 162 def test_unicode_bytes_args(self):
163 163 uarg = u'--a=é'
164 164 try:
165 165 barg = uarg.encode(sys.stdin.encoding)
166 166 except (TypeError, UnicodeEncodeError):
167 167 raise SkipTest("sys.stdin.encoding can't handle 'é'")
168 168
169 169 cl = self.klass()
170 170 with mute_warn():
171 171 config = cl.load_config([barg])
172 172 self.assertEqual(config.a, u'é')
173 173
174 174 def test_unicode_alias(self):
175 175 cl = self.klass()
176 176 argv = [u'--a=épsîlön']
177 177 with mute_warn():
178 178 config = cl.load_config(argv, aliases=dict(a='A.a'))
179 179 self.assertEqual(config.A.a, u'épsîlön')
180 180
181 181
182 182 class TestArgParseKVCL(TestKeyValueCL):
183 183 klass = KVArgParseConfigLoader
184 184
185 185 def test_expanduser2(self):
186 186 cl = self.klass()
187 187 argv = ['-a', '~/1/2/3', '--b', "'~/1/2/3'"]
188 188 with mute_warn():
189 189 config = cl.load_config(argv, aliases=dict(a='A.a', b='A.b'))
190 190 self.assertEqual(config.A.a, os.path.expanduser('~/1/2/3'))
191 191 self.assertEqual(config.A.b, '~/1/2/3')
192 192
193 193 def test_eval(self):
194 194 cl = self.klass()
195 195 argv = ['-c', 'a=5']
196 196 with mute_warn():
197 197 config = cl.load_config(argv, aliases=dict(c='A.c'))
198 198 self.assertEqual(config.A.c, u"a=5")
199 199
200 200
201 201 class TestConfig(TestCase):
202 202
203 203 def test_setget(self):
204 204 c = Config()
205 205 c.a = 10
206 206 self.assertEqual(c.a, 10)
207 207 self.assertEqual('b' in c, False)
208 208
209 209 def test_auto_section(self):
210 210 c = Config()
211 211 self.assertEqual('A' in c, True)
212 212 self.assertEqual(c._has_section('A'), False)
213 213 A = c.A
214 214 A.foo = 'hi there'
215 215 self.assertEqual(c._has_section('A'), True)
216 216 self.assertEqual(c.A.foo, 'hi there')
217 217 del c.A
218 218 self.assertEqual(len(c.A.keys()),0)
219 219
220 220 def test_merge_doesnt_exist(self):
221 221 c1 = Config()
222 222 c2 = Config()
223 223 c2.bar = 10
224 224 c2.Foo.bar = 10
225 225 c1._merge(c2)
226 226 self.assertEqual(c1.Foo.bar, 10)
227 227 self.assertEqual(c1.bar, 10)
228 228 c2.Bar.bar = 10
229 229 c1._merge(c2)
230 230 self.assertEqual(c1.Bar.bar, 10)
231 231
232 232 def test_merge_exists(self):
233 233 c1 = Config()
234 234 c2 = Config()
235 235 c1.Foo.bar = 10
236 236 c1.Foo.bam = 30
237 237 c2.Foo.bar = 20
238 238 c2.Foo.wow = 40
239 239 c1._merge(c2)
240 240 self.assertEqual(c1.Foo.bam, 30)
241 241 self.assertEqual(c1.Foo.bar, 20)
242 242 self.assertEqual(c1.Foo.wow, 40)
243 243 c2.Foo.Bam.bam = 10
244 244 c1._merge(c2)
245 245 self.assertEqual(c1.Foo.Bam.bam, 10)
246 246
247 247 def test_deepcopy(self):
248 248 c1 = Config()
249 249 c1.Foo.bar = 10
250 250 c1.Foo.bam = 30
251 251 c1.a = 'asdf'
252 252 c1.b = range(10)
253 253 import copy
254 254 c2 = copy.deepcopy(c1)
255 255 self.assertEqual(c1, c2)
256 256 self.assertTrue(c1 is not c2)
257 257 self.assertTrue(c1.Foo is not c2.Foo)
258 258
259 259 def test_builtin(self):
260 260 c1 = Config()
261 261 exec 'foo = True' in c1
262 262 self.assertEqual(c1.foo, True)
263 263 self.assertRaises(ConfigError, setattr, c1, 'ValueError', 10)
264
265 def test_fromdict(self):
266 c1 = Config({'Foo' : {'bar' : 1}})
267 self.assertEqual(c1.Foo.__class__, Config)
268 self.assertEqual(c1.Foo.bar, 1)
269
270 def test_fromdict_merge(self):
271 c1 = Config()
272 c2 = Config({'Foo' : {'bar' : 1}})
273 c1._merge(c2)
274 self.assertEqual(c1.Foo.__class__, Config)
275 self.assertEqual(c1.Foo.bar, 1)
276
277 def test_fromdict_merge2(self):
278 c1 = Config({'Foo' : {'baz' : 2}})
279 c2 = Config({'Foo' : {'bar' : 1}})
280 c1._merge(c2)
281 self.assertEqual(c1.Foo.__class__, Config)
282 self.assertEqual(c1.Foo.bar, 1)
283 self.assertEqual(c1.Foo.baz, 2)
284 self.assertRaises(AttributeError, getattr, c2.Foo, 'baz')
General Comments 0
You need to be logged in to leave comments. Login now