Add multiple specs at once and test. Closes #16

master
Tom Wilson 6 years ago
parent ddf9821259
commit 5cf252187b

@ -93,6 +93,17 @@ class DictSpec(_ValueSpecification):
self.spec_dict[name] = newspec self.spec_dict[name] = newspec
return newspec return newspec
def add_specs(self, spec_list):
"""
Convenience method to pass a series of specifications to 'add_spec()'
Args:
spec_list: A list of tuples, where each tuple contains a `name` and `newspec` value,
to be passed in the same form as a call to `add_spec`
"""
for spec in spec_list:
self.add_spec(*spec)
def validate(self, value_dict): def validate(self, value_dict):
""" """
Checks the supplied value to confirm that it complies with this specification. Checks the supplied value to confirm that it complies with this specification.

@ -1,6 +1,7 @@
# pylint: disable=redefined-outer-name # pylint: disable=redefined-outer-name
from copy import deepcopy from copy import deepcopy
import pytest import pytest
import preserve
import configspec import configspec
@ -63,6 +64,38 @@ def example_values():
return vals return vals
def test_addspec(example_spec):
confspec = configspec.ConfigSpecification()
confspec.add_specs([
("bool_a", configspec.BoolSpec(False, False, "Bool A")),
("boollist_a", configspec.BoolListSpec(False, False, "Bool A")),
("int_a", configspec.IntSpec(0, -4, 4, False, "Int A")),
("intlist_a", configspec.IntListSpec(0, -4, 4, False, "Int A")),
("string_a", configspec.StringSpec("ThisIsAString", 4, 15, False, "String A")),
("stringlist_a", configspec.StringListSpec("ThisIsAString", 4, 15, False, "String A"))
])
confdictspec = confspec.add_spec("dict_a", configspec.DictSpec(None, False, "Dict A"))
confdictspec.add_specs([
("bool_b", configspec.BoolSpec(False, False, "Bool B")),
("int_b", configspec.IntSpec(0, -4, 4, False, "Int B")),
("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_specs([
("bool_c", configspec.BoolSpec(False, False, "Bool C")),
("int_c", configspec.IntSpec(0, -4, 4, False, "Int C")),
("string_c", configspec.StringSpec("ThisIsAString", 4, 15, False, "String C"))
])
# Preserve to be able to directly compare resulting confspec structure
assert preserve.preserve(example_spec) == preserve.preserve(confspec)
def test_config_validate(example_spec, example_values): def test_config_validate(example_spec, example_values):
spec = example_spec spec = example_spec
spec.validate(example_values) spec.validate(example_values)

Loading…
Cancel
Save