forked from giampaolo/confix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
403 lines (340 loc) · 11.1 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# TODO:
# -test parse()'s 'format' parameter
import errno
import json
import os
import sys
import textwrap
try:
import configparser # py3
except ImportError:
import ConfigParser as configparser
if sys.version_info >= (2, 7):
import unittest
else:
import unittest2 as unittest # https://pypi.python.org/pypi/unittest2
try:
import toml
except ImportError:
toml = None
try:
import yaml
except ImportError:
yaml = None
from confix import register, parse, discard, schema
from confix import Error, InvalidKeyError, TypesMismatchError, RequiredKeyError
from confix import ValidationError
THIS_MODULE = os.path.splitext(os.path.basename(__file__))[0]
TESTFN = '$testfile'
PY3 = sys.version_info >= (3, )
if PY3:
import io
StringIO = io.StringIO
else:
from StringIO import StringIO
def unlink(path):
try:
os.remove(path)
except OSError as err:
if err.errno != errno.ENOENT:
raise
# ===================================================================
# base class
# ===================================================================
class TestBase(object):
"""Base class from which mixin classes are derived."""
TESTFN = None
def tearDown(self):
discard()
unlink(self.TESTFN)
@classmethod
def tearDownClass(cls):
unlink(TESTFN)
def dict_to_file(self, dct):
raise NotImplementedError('must be implemented in subclass')
def write_to_file(self, content, fname=None):
if fname is None:
fname = self.TESTFN
with open(fname, 'w') as f:
f.write(content)
def test_empty_conf_file(self):
@register('name')
class config:
foo = 1
bar = 2
self.write_to_file(" ")
parse(self.TESTFN)
self.assertEqual(config.foo, 1)
self.assertEqual(config.bar, 2)
def test_unknown_format(self):
with open(TESTFN, 'w') as f:
f.write('foo')
self.addCleanup(unlink, TESTFN)
self.assertRaises(ValueError, parse, TESTFN)
def test_conf_file_overrides_one(self):
@register('name')
class config:
foo = 1
bar = 2
self.dict_to_file({
'name': dict(foo=5)
})
parse(self.TESTFN)
self.assertEqual(config.foo, 5)
self.assertEqual(config.bar, 2)
def test_conf_file_overrides_both(self):
@register('name')
class config:
foo = 1
bar = 2
self.dict_to_file({
'name': dict(foo=5, bar=6)
})
parse(self.TESTFN)
self.assertEqual(config.foo, 5)
self.assertEqual(config.bar, 6)
def test_invalid_field(self):
@register('name')
class config:
foo = 1
bar = 2
self.dict_to_file({
'name': dict(foo=5, apple=6)
})
with self.assertRaises(InvalidKeyError) as cm:
parse(self.TESTFN)
self.assertEqual(cm.exception.section, 'name')
self.assertEqual(cm.exception.key, 'apple')
# TODO: make this reliable across different languages
# def test_types_mismatch(self):
# @register('name')#
# class config:
# foo = 1
# bar = 2
# self.dict_to_file({
# THIS_MODULE: dict(foo=5, bar='6')
# })
# with self.assertRaises(TypesMismatchError) as cm:
# parse(self.TESTFN)
# self.assertEqual(cm.exception.section, 'name')
# self.assertEqual(cm.exception.key, 'bar')
# self.assertEqual(cm.exception.default_value, 2)
# self.assertEqual(cm.exception.new_value, '6')
# def test_invalid_yaml_file(self):
# self.dict_to_file('?!?')
# with self.assertRaises(Error) as cm:
# parse(self.TESTFN)
def test_already_configured(self):
@register('name')
class config:
foo = 1
bar = 2
self.dict_to_file({
'name': dict(foo=5, bar=6)
})
parse(self.TESTFN)
self.assertRaises(Error, parse, self.TESTFN)
def test_schema_base(self):
@register('name')
class config:
foo = schema(10)
self.dict_to_file({})
parse(self.TESTFN)
self.assertEqual(config.foo, 10)
def test_schema_base_required(self):
@register('name')
class config:
foo = schema(10, required=True)
bar = 2
self.dict_to_file({
'name': dict(bar=2)
})
with self.assertRaises(RequiredKeyError) as cm:
parse(self.TESTFN)
self.assertEqual(cm.exception.section, 'name')
self.assertEqual(cm.exception.key, 'foo')
def test_schema_base_overwritten(self):
@register('name')
class config:
foo = schema(10, required=True)
self.dict_to_file({
'name': dict(foo=5)
})
parse(self.TESTFN)
self.assertEqual(config.foo, 5)
def test_schema_errors(self):
# no default nor required=True
self.assertRaises(TypeError, schema)
# not callable validator
self.assertRaises(ValueError, schema, 10, False, 'foo')
def test_validator_ok(self):
@register('name')
class config:
foo = schema(10, validator=lambda x: isinstance(x, int))
self.dict_to_file({
'name': dict(foo=5)
})
parse(self.TESTFN)
def test_validator_ko(self):
@register('name')
class config:
foo = schema(10, validator=lambda x: isinstance(x, str))
self.dict_to_file({
'name': dict(foo=5)
})
with self.assertRaises(ValidationError) as cm:
parse(self.TESTFN)
self.assertEqual(cm.exception.section, 'name')
self.assertEqual(cm.exception.key, 'foo')
self.assertEqual(cm.exception.value, 5)
def test_validator_ko_custom_exc_w_message(self):
def validator(value):
raise ValidationError('message')
@register('name')
class config:
foo = schema(10, validator=validator)
self.dict_to_file({
'name': dict(foo=5)
})
with self.assertRaises(ValidationError) as cm:
parse(self.TESTFN)
self.assertEqual(cm.exception.section, 'name')
self.assertEqual(cm.exception.key, 'foo')
self.assertEqual(cm.exception.value, 5)
self.assertEqual(cm.exception.msg, 'message')
def test_validator_ko_custom_exc_w_no_message(self):
def validator(value):
raise ValidationError
@register('name')
class config:
foo = schema(10, validator=validator)
self.dict_to_file({
'name': dict(foo=5)
})
with self.assertRaises(ValidationError) as cm:
parse(self.TESTFN)
self.assertEqual(cm.exception.section, 'name')
self.assertEqual(cm.exception.key, 'foo')
self.assertEqual(cm.exception.value, 5)
self.assertEqual(cm.exception.msg, None)
self.assertIn('(got 5)', str(cm.exception))
# ===================================================================
# mixin tests
# ===================================================================
class TestJsonMixin(TestBase, unittest.TestCase):
TESTFN = TESTFN + 'testfile.json'
def dict_to_file(self, dct):
self.write_to_file(json.dumps(dct))
class TestIniMixin(TestBase, unittest.TestCase):
TESTFN = TESTFN + 'testfile.ini'
def dict_to_file(self, dct):
config = configparser.RawConfigParser()
for section, values in dct.items():
assert isinstance(section, str)
config.add_section(section)
for key, value in values.items():
config.set(section, key, value)
fl = StringIO()
config.write(fl)
fl.seek(0)
content = fl.read()
self.write_to_file(content)
@unittest.skipUnless(toml is not None, "toml module is not installed")
class TestTomlMixin(TestBase, unittest.TestCase):
TESTFN = TESTFN + 'testfile.toml'
def dict_to_file(self, dct):
# TODO: make this more reliable
content = ""
for section, values in dct.items():
content += "[%s]\n" % section
for key, value in values.items():
content += "%s = %s\n" % (key, value)
self.write_to_file(content)
@unittest.skipUnless(yaml is not None, "yaml module is not installed")
class TestYamlMixin(TestBase, unittest.TestCase):
TESTFN = 'testfile.yaml'
def dict_to_file(self, dct):
content = ""
for section, values in dct.items():
content += "%s:\n" % section
for key, value in values.items():
content += " %s: %r\n" % (key, value)
self.write_to_file(content)
# ===================================================================
# tests for a specific format
# ===================================================================
class TestIni(unittest.TestCase):
TESTFN = TESTFN + '.ini'
def tearDown(self):
discard()
unlink(self.TESTFN)
def write_to_file(self, content):
with open(self.TESTFN, 'w') as f:
f.write(content)
# XXX: should this test be common to all formats?
def test_int_ok(self):
@register('name')
class config:
foo = 1
bar = 2
self.write_to_file(textwrap.dedent("""
[name]
foo = 9
"""))
parse(self.TESTFN)
self.assertEqual(config.foo, 9)
# XXX: should this test be common to all formats?
def test_int_ko(self):
@register('name')
class config:
foo = 1
bar = 2
self.write_to_file(textwrap.dedent("""
[name]
foo = '9'
"""))
self.assertRaises(TypesMismatchError, parse, self.TESTFN)
def test_float(self):
@register('name')
class config:
foo = 1.1
bar = 2
self.write_to_file(textwrap.dedent("""
[name]
foo = 1.3
"""))
parse(self.TESTFN)
self.assertEqual(config.foo, 1.3)
def test_true(self):
@register('name')
class config:
foo = None
bar = 2
true_values = ("1", "yes", "true", "on")
for value in true_values:
self.write_to_file(textwrap.dedent("""
[name]
foo = %s
""" % (value)))
parse(self.TESTFN)
self.assertEqual(config.foo, True)
discard()
def test_false(self):
@register('name')
class config:
foo = None
bar = 2
true_values = ("0", "no", "false", "off")
for value in true_values:
self.write_to_file(textwrap.dedent("""
[name]
foo = %s
""" % (value)))
parse(self.TESTFN)
self.assertEqual(config.foo, False)
discard()
def test_main():
verbosity = os.environ.get('TOX') and 1 or 2
unittest.main(verbosity=verbosity)
if __name__ == '__main__':
test_main()