From 1b2f4a0deb72954e364a8b85814c458fd5d66b9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Eide?= Date: Thu, 4 Jun 2020 14:37:10 +0200 Subject: [PATCH] Use deduced_required value for config.push --- configsuite/config.py | 2 ++ tests/data/__init__.py | 1 + tests/data/required.py | 31 +++++++++++++++++++++ tests/test_push_deduced_required.py | 42 +++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 tests/data/required.py create mode 100644 tests/test_push_deduced_required.py diff --git a/configsuite/config.py b/configsuite/config.py index eb5a1a4e..834c68ac 100644 --- a/configsuite/config.py +++ b/configsuite/config.py @@ -89,6 +89,7 @@ def __init__( self._valid = True self._errors = () self._snapshot = None + self._deduce_required = deduce_required self._cached_merged_config = self._build_merged_config() if self._readable: @@ -158,6 +159,7 @@ def push(self, raw_config): layers=self._layers, extract_validation_context=self._extract_validation_context, extract_transformation_context=self._extract_transformation_context, + deduce_required=self._deduce_required, ) @property diff --git a/tests/data/__init__.py b/tests/data/__init__.py index 9e614ab4..97d2309c 100644 --- a/tests/data/__init__.py +++ b/tests/data/__init__.py @@ -28,3 +28,4 @@ from . import numbers from . import special_numbers from . import car +from . import required diff --git a/tests/data/required.py b/tests/data/required.py new file mode 100644 index 00000000..2d34f42e --- /dev/null +++ b/tests/data/required.py @@ -0,0 +1,31 @@ +"""Copyright 2019 Equinor ASA and The Netherlands Organisation for +Applied Scientific Research TNO. + +Licensed under the MIT license. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the conditions stated in the LICENSE file in the project root for +details. + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. +""" + + +from configsuite import MetaKeys as MK +from configsuite import types + + +def build_schema(): + return { + MK.Type: types.NamedDict, + MK.Content: { + "not_required": {MK.Type: types.Integer, MK.AllowNone: True}, + "a_required_string": {MK.Type: types.String,}, + }, + } diff --git a/tests/test_push_deduced_required.py b/tests/test_push_deduced_required.py new file mode 100644 index 00000000..d0d59617 --- /dev/null +++ b/tests/test_push_deduced_required.py @@ -0,0 +1,42 @@ +"""Copyright 2019 Equinor ASA and The Netherlands Organisation for +Applied Scientific Research TNO. + +Licensed under the MIT license. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the conditions stated in the LICENSE file in the project root for +details. + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. +""" + +import unittest + +import configsuite + +from . import data + + +class TestPush(unittest.TestCase): + def assertEqualSnapshots(self, first, second): + self.assertEqual(first.heroes, second.heroes) + self.assertEqual(sorted(first.villains), sorted(second.villains)) + + def test_push_deduced_required(self): + schema = data.required.build_schema() + + heroes = {"a_required_string": "a_string"} + + basic_config = configsuite.ConfigSuite(heroes, schema, deduce_required=True) + self.assertTrue(basic_config.valid) + + basic_config = basic_config.push({"not_required": 10}) + self.assertTrue(basic_config.valid) + + self.assertTrue(basic_config._deduce_required)