From 5cf252187bb3e7159fc0c167858640592e4fffa5 Mon Sep 17 00:00:00 2001 From: novirium Date: Thu, 2 Jan 2020 13:03:58 +0800 Subject: [PATCH] Add multiple specs at once and test. Closes #16 --- configspec/specification.py | 11 +++++++++++ tests/test_configspec.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/configspec/specification.py b/configspec/specification.py index b00189a..675d7b3 100644 --- a/configspec/specification.py +++ b/configspec/specification.py @@ -93,6 +93,17 @@ class DictSpec(_ValueSpecification): self.spec_dict[name] = 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): """ Checks the supplied value to confirm that it complies with this specification. diff --git a/tests/test_configspec.py b/tests/test_configspec.py index e1236c3..1756421 100644 --- a/tests/test_configspec.py +++ b/tests/test_configspec.py @@ -1,6 +1,7 @@ # pylint: disable=redefined-outer-name from copy import deepcopy import pytest +import preserve import configspec @@ -63,6 +64,38 @@ def example_values(): 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): spec = example_spec spec.validate(example_values)