From 29ddc305c9a5edd1dc98081a56d65b6329d25d8d Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Sat, 29 Aug 2020 18:19:45 -0500 Subject: [PATCH] Move setting checker helpers from pgwui_server --- src/pgwui_common/checkset.py | 60 +++++++++++++++++++ tests/test_checkset.py | 112 +++++++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 src/pgwui_common/checkset.py create mode 100644 tests/test_checkset.py diff --git a/src/pgwui_common/checkset.py b/src/pgwui_common/checkset.py new file mode 100644 index 0000000..97b2e23 --- /dev/null +++ b/src/pgwui_common/checkset.py @@ -0,0 +1,60 @@ +# Copyright (C) 2020 The Meme Factory, Inc. http://www.karlpinc.com/ + +# This file is part of PGWUI_Server. +# +# This program is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with this program. If not, see +# . +# + +# Karl O. Pinc + +'''Helper routines for checking a PGWUI component's settings +''' + +from ast import literal_eval + +from . import exceptions + + +def require_settings(component, required_settings, conf): + errors = [] + for setting in required_settings: + if setting not in conf: + errors.append(exceptions.MissingSettingError( + '{}.{}'.format(component, setting))) + return errors + + +def unknown_settings(component, settings, conf): + errors = [] + for setting in conf: + if setting not in settings: + errors.append(exceptions.UnknownSettingKeyError( + '{}.{}'.format(component, setting))) + return errors + + +def boolean_settings(component, booleans, conf): + errors = [] + for setting in booleans: + if setting in conf: + try: + val = literal_eval(conf[setting]) + except ValueError: + val = None + if (val is not True + and val is not False): + errors.append(exceptions.NotBooleanSettingError( + '{}.{}'.format(component, setting), conf[setting])) + return errors diff --git a/tests/test_checkset.py b/tests/test_checkset.py new file mode 100644 index 0000000..6994ef1 --- /dev/null +++ b/tests/test_checkset.py @@ -0,0 +1,112 @@ +# Copyright (C) 2018, 2019, 2020 The Meme Factory, Inc. +# http://www.karlpinc.com/ + +# This file is part of PGWUI_Server. +# +# This program is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with this program. If not, see +# . +# + +# Karl O. Pinc + +import pgwui_common.exceptions as ex +import pgwui_common.checkset as checkset + + +# require_settings() + +def test_require_settings_good(): + '''No errors when the required settings are in the config + ''' + required = ['settinga', 'settingb'] + settings = {'settinga': 'a', 'settingb': 'b'} + + result = checkset.require_settings('testcomp', required, settings) + + assert result == [] + + +def test_require_settings_bad(): + '''Errors when the required settings are not in the config + ''' + required = ['settinga', 'settingb'] + settings = {} + + result = checkset.require_settings('testcomp', required, settings) + + assert len(result) == len(required) + for error in result: + assert isinstance(error, ex.MissingSettingError) + + +# unknown_settings() + +def test_unknown_settings_good(): + '''There are no errors when all settings are known + ''' + settings = ['settinga', 'settingb'] + conf = {'settinga': 'a', 'settingb': 'b'} + + result = checkset.unknown_settings('testcomp', settings, conf) + + assert result == [] + + +def test_unknown_settings_bad(): + '''Errors when settings are not known + ''' + conf = {'settinga': 'a', 'settingb': 'b'} + + result = checkset.unknown_settings('testcomp', [], conf) + + assert len(result) == len(conf) + for error in result: + assert isinstance(error, ex.UnknownSettingKeyError) + + +# boolean_settings() + +def test_boolean_settings_good(): + '''No errors when boolean settings are boolean + ''' + conf = {'settinga': 'a', 'settingb': 'True', 'settingc': 'False'} + bools = ['settingc', 'settingb'] + + result = checkset.boolean_settings('testcomp', bools, conf) + + assert result == [] + + +def test_boolean_settings_bad(): + '''Errors when boolean settings are not boolean + ''' + conf = {'settinga': 'a', 'settingb': 'True', 'settingc': 'c'} + bools = ['settinga', 'settingb'] + + result = checkset.boolean_settings('testcomp', bools, conf) + + assert len(result) == 1 + for error in result: + assert isinstance(error, ex.NotBooleanSettingError) + + +def test_boolean_settings_missing(): + '''No errors when the boolean setting is missing from the config + ''' + conf = {} + bools = ['settinga'] + + result = checkset.boolean_settings('testcomp', bools, conf) + + assert result == [] -- 2.34.1