Move setting checker helpers from pgwui_server
authorKarl O. Pinc <kop@karlpinc.com>
Sat, 29 Aug 2020 23:19:45 +0000 (18:19 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Sat, 29 Aug 2020 23:19:45 +0000 (18:19 -0500)
src/pgwui_common/checkset.py [new file with mode: 0644]
tests/test_checkset.py [new file with mode: 0644]

diff --git a/src/pgwui_common/checkset.py b/src/pgwui_common/checkset.py
new file mode 100644 (file)
index 0000000..97b2e23
--- /dev/null
@@ -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
+# <http://www.gnu.org/licenses/>.
+#
+
+# Karl O. Pinc <kop@karlpinc.com>
+
+'''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 (file)
index 0000000..6994ef1
--- /dev/null
@@ -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
+# <http://www.gnu.org/licenses/>.
+#
+
+# Karl O. Pinc <kop@karlpinc.com>
+
+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 == []