-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
327 lines (253 loc) · 9.7 KB
/
tests.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env python
# encoding: utf-8
from __future__ import unicode_literals
import figs
from unittest import TestCase
from ConfigParser import NoOptionError, MissingSectionHeaderError
from StringIO import StringIO
import tempfile as tmp
import os
from textwrap import dedent
class TestLoad(TestCase):
def setUp(self):
tmp_file = tmp.NamedTemporaryFile(prefix='figs-test-',
delete=False)
tmp_file.write(dedent('''\
[default]
key = val
'''))
tmp_file.close()
self.target_filename = tmp_file.name
def tearDown(self):
os.remove(self.target_filename)
def test_filename(self):
figs.load(self.target_filename)
def test_file(self):
f = open(self.target_filename)
figs.load(f)
self.assertFalse(f.closed)
f.close()
def test_string(self):
conf = figs.loads('[default]\na = 1\n')
self.assertEqual(conf.default.a, '1')
def test_indented_string(self):
conf = figs.loads('''\
[default]
a = 1
''')
self.assertEqual(conf.default.a, '1')
def test_as_dict(self):
conf = figs.loads('''\
[default]
a = 1
''', as_dict=True)
self.assertIsInstance(conf, dict)
self.assertEqual(conf.viewkeys(), {'default'})
self.assertEqual(conf['default'].viewkeys(), {'a'})
def test_no_sections(self):
conf = figs.loads('key1 = val1\nkey2 = val2\n')
self.assertEqual(conf.key1, 'val1')
self.assertEqual(conf.key2, 'val2')
def test_no_first_section(self):
self.assertRaises(MissingSectionHeaderError, lambda: figs.loads('''\
key = val
[section1]
name = me
'''))
def test_no_sections_as_dict(self):
conf = figs.loads('key1 = val1\nkey2 = val2\n', as_dict=True)
self.assertIsInstance(conf, dict)
self.assertEqual(conf.viewkeys(), {'key1', 'key2'})
class TestDump(TestCase):
def setUp(self):
self.conf_content = '[default]\nname = myself\n\n'
self.conf = figs.loads(self.conf_content)
tmp_file = tmp.NamedTemporaryFile(prefix='figs-test-',
delete=False)
tmp_file.close()
self.target_filename = tmp_file.name
def tearDown(self):
os.remove(self.target_filename)
def test_filename(self):
figs.dump(self.conf, self.target_filename)
with open(self.target_filename) as f:
self.assertEqual(f.read(), self.conf_content)
def test_file(self):
sio = StringIO()
figs.dump(self.conf, sio)
self.assertEqual(sio.getvalue(), self.conf_content)
self.assertFalse(sio.closed)
def test_string(self):
self.assertEqual(figs.dumps(self.conf), self.conf_content)
def test_section(self):
self.assertEqual(figs.dumps(self.conf.default),
self.conf_content.split(None, 1)[-1])
def test_no_sections(self):
content = 'key1 = val1\nkey2 = val2\n\n'
conf = figs.loads(content)
self.assertEqual(figs.dumps(conf), content)
def test_section_filename(self):
figs.dump(self.conf.default, self.target_filename)
with open(self.target_filename) as f:
self.assertEqual(f.read(), self.conf_content.split(None, 1)[-1])
class TestAccesses(TestCase):
def setUp(self):
self.conf = figs.loads('''\
[default]
name = myself
count = 1
keys = 10
[onemore]
key = val
''')
def test_access(self):
# The following also checks that accessing a section/option multiple
# times gives us the *same* object.
# Existing section
self.assertIs(self.conf['default'], self.conf.default)
# Non-existent section
self.assertIs(self.conf['defaulty'], self.conf.defaulty)
# Existing option
self.assertIs(self.conf.default['name'], self.conf.default.name)
self.assertIs(self.conf.default['keys'], self.conf.default.keys)
# Non-existing option
self.assertRaises(NoOptionError, lambda: self.conf.default.namey)
def test_contains(self):
self.assertIn('default', self.conf)
self.assertNotIn('defaulty', self.conf)
self.assertIn('name', self.conf.default)
self.assertNotIn('namey', self.conf.default)
def test_section_list(self):
sections = ['default', 'onemore']
# Should be able to iterate any number of times.
for sname, section in self.conf:
self.assertIn(sname, sections)
self.assertEqual(sname, section._name)
self.assertEqual(list(s[0] for s in self.conf), sections)
def test_section_dict(self):
sections = dict(self.conf)
self.assertEqual(sections.viewkeys(), {'default', 'onemore'})
self.assertEqual(sections['default'], self.conf.default)
def test_option_list(self):
options = ['name', 'count', 'keys']
for key, value in self.conf.default:
self.assertIn(key, options)
self.assertEqual(list(o[0] for o in self.conf.default), options)
def test_option_dict(self):
options = dict(self.conf.default)
self.assertEqual(options.viewkeys(), {'name', 'count', 'keys'})
self.assertEqual(options['name'], self.conf.default.name)
class TestChanges(TestCase):
def setUp(self):
self.conf = figs.loads('''\
[default]
name = myself
count = 1
[onemore]
key = val
''')
def test_set_option(self):
self.conf.default.count = 10
self.assertEqual(self.conf.default.count.as_int, 10)
# Accessing it puts it in the cache.
self.conf.default.count
# Test if change updates cache.
self.conf.default.count = 100
self.assertEqual(self.conf.default.count.as_int, 100)
self.conf.default.new = 1
self.assertEqual(self.conf.default.new.as_int, 1)
self.assertIn('new', self.conf.default)
def test_set_option_as_item(self):
self.conf.default['count'] = 10
self.assertEqual(self.conf.default.count.as_int, 10)
def test_new_section(self):
self.assertIs(self.conf.universe, self.conf['universe'])
self.conf.universe.answer = 42
self.assertEqual(self.conf.universe.answer.as_int, 42)
self.assertIn('universe', self.conf)
def test_del_option(self):
del self.conf.default.name
self.assertNotIn('name', self.conf.default)
def test_del_section(self):
del self.conf.default
self.assertNotIn('default', self.conf)
class TestTypability(TestCase):
def test_unknown(self):
unknowns = figs.loads('''\
[unknowns]
o1 = hohoho
''').unknowns
self.assertRaises(ValueError, lambda: unknowns.o1.as_bool)
self.assertRaises(ValueError, lambda: unknowns.o1.as_int)
self.assertRaises(ValueError, lambda: unknowns.o1.as_float)
def test_trues(self):
conf = figs.loads('''\
[trues]
o1 = 1
o2 = on
o3 = yes
o4 = true
o5 = ON
o6 = Yes
o7 = True
''')
map(lambda (key, val): self.assertIs(val, True,
msg=key + ' is not True'),
((o[0], o[1].as_bool) for o in conf.trues))
def test_falses(self):
conf = figs.loads('''\
[falses]
o1 = 0
o2 = off
o3 = no
o4 = false
o5 = OFF
o6 = No
o7 = False
''')
map(lambda (key, val): self.assertIs(val, False,
msg=key + ' is not False'),
((o[0], o[1].as_bool) for o in conf.falses))
def test_ints(self):
ints = figs.loads('''\
[ints]
o1 = 1
o2 = 10.1
o3 = 10.8
o4 = 1e2
o5 = 1.2e2
''').ints
# All values should be of type `int`.
map(lambda (key, val): self.assertIsInstance(val, int,
msg=key + ' is not an int (' + repr(val) + ')'),
((o[0], o[1].as_int) for o in ints))
self.assertEqual(ints.o1.as_int, 1)
self.assertEqual(ints.o2.as_int, 10)
self.assertEqual(ints.o3.as_int, 11)
self.assertEqual(ints.o4.as_int, 100)
self.assertEqual(ints.o5.as_int, 120)
def test_floats(self):
floats = figs.loads('''\
[floats]
o1 = 1
o2 = 1.
o3 = 1.0
o4 = 1.1e1
''').floats
# All values should be of type `int`.
map(lambda (key, val): self.assertIsInstance(val, float,
msg=key + ' is not an int (' + repr(val) + ')'),
((o[0], o[1].as_float) for o in floats))
self.assertEqual(floats.o1.as_float, 1)
self.assertEqual(floats.o2.as_float, 1)
self.assertEqual(floats.o3.as_float, 1)
self.assertEqual(floats.o4.as_float, 11)
class TestErrors(TestCase):
def test_databox_attr_access(self):
db = figs.Databox()
self.assertRaises(AttributeError, lambda: db._non_existent)
self.assertRaisesRegexp(AttributeError, r'^Databox ',
lambda: db._non_existent)
def test_unknown_args_to_load(self):
self.assertRaises(TypeError, figs.load, None,
nonexistent_keyword_argument=1)