Support validation of a "boolean_choice" data type
authorKarl O. Pinc <kop@karlpinc.com>
Fri, 22 Jan 2021 23:56:49 +0000 (17:56 -0600)
committerKarl O. Pinc <kop@karlpinc.com>
Fri, 22 Jan 2021 23:56:49 +0000 (17:56 -0600)
src/pgwui_common/checkset.py
src/pgwui_common/exceptions.py
tests/test_checkset.py

index 2be79abc586379ddc9f084ae906102b857c3cc13..17d5ba1f1a83611e0563b777897fb9098d8ef321 100644 (file)
@@ -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
index 31c3f79a4d008e3ce380fd187b6127ded6f43e34..6b77bef92f323681ecd848b9a9030173d6db7751 100644 (file)
@@ -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__(
index 54cafef732bd38a0144189f9d52493a75cca0143..7d7d6857d7fe8ecde49438a2bd540144ca293877 100644 (file)
@@ -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