From 06919a7862c21d566fe566e3ee4d146b83701d85 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Fri, 22 Jan 2021 17:56:49 -0600 Subject: [PATCH] Support validation of a "boolean_choice" data type --- src/pgwui_common/checkset.py | 11 +++++++++++ src/pgwui_common/exceptions.py | 8 ++++++++ tests/test_checkset.py | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/src/pgwui_common/checkset.py b/src/pgwui_common/checkset.py index 2be79ab..17d5ba1 100644 --- a/src/pgwui_common/checkset.py +++ b/src/pgwui_common/checkset.py @@ -58,3 +58,14 @@ def boolean_settings(component, booleans, conf): errors.append(exceptions.NotBooleanSettingError( '{}:{}'.format(component, setting), conf[setting])) return errors + + +def boolean_choice(component, settings, conf): + errors = [] + for setting in settings: + if setting in conf: + if conf[setting] not in ( + 'yes-always', 'choice-yes', 'choice-no', 'no-never'): + errors.append(exceptions.NotBooleanChoiceSettingError( + f'{component}:{setting}', conf[setting])) + return errors diff --git a/src/pgwui_common/exceptions.py b/src/pgwui_common/exceptions.py index 31c3f79..6b77bef 100644 --- a/src/pgwui_common/exceptions.py +++ b/src/pgwui_common/exceptions.py @@ -126,6 +126,14 @@ class NotBooleanSettingError(Error): .format(key)) +class NotBooleanChoiceSettingError(Error): + def __init__(self, key, value): + super().__init__( + f'Bad value ({value}) for the {key} setting', + (f'The "{key}" PGWUI setting must be one of: ' + '"yes-always", "choice-yes", "choice-no", "no-never"')) + + class BadAssetOverrideError(Error): def __init__(self, asset, new_asset, exp): super().__init__( diff --git a/tests/test_checkset.py b/tests/test_checkset.py index 54cafef..7d7d685 100644 --- a/tests/test_checkset.py +++ b/tests/test_checkset.py @@ -119,3 +119,21 @@ def test_boolean_settings_missing(): result = checkset.boolean_settings('testcomp', bools, conf) assert result == [] + + +# boolean_choice() + +@pytest.mark.parametrize( + ('name', 'config', 'error_count'), [ + ('settingname', {'settingname': 'yes-always'}, 0), + ('settingname', {'settingname': 'choice-yes'}, 0), + ('settingname', {'settingname': 'choice-no'}, 0), + ('settingname', {'settingname': 'no-never'}, 0), + ('settingname', {'settingname': 'unrecognized'}, 1), + ('settingname', {}, 0)]) +@pytest.mark.unittest +def test_boolean_choice(name, config, error_count): + '''The right number of errors are returned + ''' + result = checkset.boolean_choice('testcomp', [name], config) + assert len(result) == error_count -- 2.34.1