Add basic tests. Closes #3
parent
7989a06140
commit
07a817141c
@ -0,0 +1,103 @@
|
||||
import pytest
|
||||
import configspec
|
||||
|
||||
# Test each def type, both pass and fail
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def example_spec():
|
||||
confspec = configspec.ConfigSpecification()
|
||||
confspec.add_spec("bool_a",
|
||||
configspec.BoolSpec(False, False, "Bool A"))
|
||||
confspec.add_spec("boollist_a",
|
||||
configspec.BoolListSpec(False, False, "Bool A"))
|
||||
confspec.add_spec("int_a",
|
||||
configspec.IntSpec(0, -4, 4, False, "Int A"))
|
||||
confspec.add_spec("intlist_a",
|
||||
configspec.IntListSpec(0, -4, 4, False, "Int A"))
|
||||
confspec.add_spec("string_a",
|
||||
configspec.StringSpec("ThisIsAString", 4, 15, False, "String A"))
|
||||
confspec.add_spec("stringlist_a",
|
||||
configspec.StringListSpec("ThisIsAString", 4, 15, False, "String A"))
|
||||
|
||||
confdictspec = confspec.add_spec("dict_a",
|
||||
configspec.DictSpec(None, False, "Dict A"))
|
||||
confdictspec.add_spec("bool_b",
|
||||
configspec.BoolSpec(False, False, "Bool B"))
|
||||
confdictspec.add_spec("int_b",
|
||||
configspec.IntSpec(0, -4, 4, False, "Int B"))
|
||||
confdictspec.add_spec("string_b",
|
||||
configspec.StringSpec("ThisIsAString", 4, 15, False, "String B"))
|
||||
|
||||
confdictlist = confspec.add_spec("dictlist_a",
|
||||
configspec.DictListSpec(None, False, "List of Dicts B"))
|
||||
confdictlist.add_spec("bool_c",
|
||||
configspec.BoolSpec(False, False, "Bool C"))
|
||||
confdictlist.add_spec("int_c",
|
||||
configspec.IntSpec(0, -4, 4, False, "Int C"))
|
||||
confdictlist.add_spec("string_c",
|
||||
configspec.StringSpec("ThisIsAString", 4, 15, False, "String C"))
|
||||
|
||||
return confspec
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def example_values():
|
||||
vals = {'bool_a': True,
|
||||
'boollist_a': [False, True],
|
||||
'int_a': 1,
|
||||
'intlist_a': [1, 2, 3, 4],
|
||||
'string_a': 'sometext',
|
||||
'stringlist_a': ['somemoretext', 'yetmoretext'],
|
||||
'dict_a': {
|
||||
'bool_b': False,
|
||||
'int_b': 0,
|
||||
'string_b': 'texttesttext'},
|
||||
'dictlist_a': [{'bool_c': False,
|
||||
'int_c': 0,
|
||||
'string_c': 'string1'},
|
||||
{'bool_c': False,
|
||||
'int_c': 0,
|
||||
'string_c': 'string2'}]}
|
||||
|
||||
return vals
|
||||
|
||||
|
||||
def test_config_validate(example_spec, example_values):
|
||||
spec = example_spec
|
||||
spec.validate(example_values)
|
||||
|
||||
|
||||
def test_invalid_bool():
|
||||
spec = configspec.BoolSpec()
|
||||
with pytest.raises(configspec.InvalidConfigError, match="value must be a boolean"):
|
||||
spec.validate("false")
|
||||
with pytest.raises(configspec.InvalidConfigError, match="value must be a boolean"):
|
||||
spec.validate(None)
|
||||
|
||||
|
||||
def test_invalid_int():
|
||||
spec = configspec.IntSpec(minval=-4, maxval=4)
|
||||
with pytest.raises(configspec.InvalidConfigError, match="value must be >="):
|
||||
spec.validate(-5)
|
||||
with pytest.raises(configspec.InvalidConfigError, match="value must be <="):
|
||||
spec.validate(5)
|
||||
with pytest.raises(configspec.InvalidConfigError, match="value must be an integer"):
|
||||
spec.validate("5")
|
||||
with pytest.raises(configspec.InvalidConfigError, match="value must be an integer"):
|
||||
spec.validate(None)
|
||||
|
||||
|
||||
def test_invalid_string():
|
||||
spec = configspec.StringSpec(minlength=4, maxlength=15)
|
||||
with pytest.raises(configspec.InvalidConfigError, match="string length must be >="):
|
||||
spec.validate("123")
|
||||
with pytest.raises(configspec.InvalidConfigError, match="string length must be <="):
|
||||
spec.validate("1234567890abcdef")
|
||||
with pytest.raises(configspec.InvalidConfigError, match="value must be a string"):
|
||||
spec.validate(1)
|
||||
with pytest.raises(configspec.InvalidConfigError, match="value must be a string"):
|
||||
spec.validate(None)
|
||||
|
||||
|
||||
# Test manager, load, overlay, freeze
|
||||
Loading…
Reference in new issue