From 5b7220c01bd60488f18753e525b1bc1ae58b2c66 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Mon, 31 Aug 2020 09:13:04 -0500 Subject: [PATCH] Check our own settings --- setup.py | 5 +- src/pgwui_upload/check_settings.py | 72 +++++++++++++++ tests/test_check_settings.py | 135 +++++++++++++++++++++++++++++ 3 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 src/pgwui_upload/check_settings.py create mode 100644 tests/test_check_settings.py diff --git a/setup.py b/setup.py index 32993df..8d66212 100644 --- a/setup.py +++ b/setup.py @@ -185,5 +185,8 @@ setup( # pip to create the appropriate form of executable for the target platform. # # Setup an entry point to support PGWUI autoconfigure discovery. - entry_points={'pgwui.components': '.pgwui_upload = pgwui_upload'} + entry_points={ + 'pgwui.components': '.pgwui_upload = pgwui_upload', + 'pgwui.check_settings': + '.pgwui_upload = pgwui_upload.check_settings:check_settings'} ) diff --git a/src/pgwui_upload/check_settings.py b/src/pgwui_upload/check_settings.py new file mode 100644 index 0000000..330d943 --- /dev/null +++ b/src/pgwui_upload/check_settings.py @@ -0,0 +1,72 @@ +# Copyright (C) 2020 The Meme Factory, Inc. http://www.karlpinc.com/ + +# This file is part of PGWUI_Upload. +# +# 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 + +from pgwui_common import exceptions as common_ex +from pgwui_common import checkset + + +PGWUI_COMPONENT = 'pgwui_upload' +UPLOAD_SETTINGS = ['menu_label', + 'literal_column_headings', + ] +REQUIRED_SETTINGS = [] +BOOLEAN_SETTINGS = [] + + +class UploadError(common_ex.Error): + pass + + +class BadLiteralColumnHeadingsError(UploadError): + def __init__(self, value): + super().__init__( + 'The "pgwui.literal_column_headings" PGWUI setting must be' + '"on", "off", "ask", or not present') + + +def validate_literal_column_headings(errors, settings): + '''Make sure the values are those allowed + ''' + value = settings.get('literal_column_headings') + if value is None: + return + if value not in ('on', 'off', 'ask'): + errors.append(BadLiteralColumnHeadingsError(value)) + + +def check_settings(component_config): + '''Check that all pgwui_upload specific settings are good. + This includes: + checking for unknown settings + checking for missing required settings + checking the boolean settings + checking that the values of other settings are valid + ''' + errors = [] + errors.extend(checkset.unknown_settings( + PGWUI_COMPONENT, UPLOAD_SETTINGS, component_config)) + errors.extend(checkset.require_settings( + PGWUI_COMPONENT, REQUIRED_SETTINGS, component_config)) + errors.extend(checkset.boolean_settings( + PGWUI_COMPONENT, BOOLEAN_SETTINGS, component_config)) + validate_literal_column_headings(errors, component_config) + + return errors diff --git a/tests/test_check_settings.py b/tests/test_check_settings.py new file mode 100644 index 0000000..330508c --- /dev/null +++ b/tests/test_check_settings.py @@ -0,0 +1,135 @@ +# Copyright (C) 2020 The Meme Factory, Inc. http://www.karlpinc.com/ + +# This file is part of PGWUI_Upload. +# +# 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_upload.check_settings as check_settings + +from pgwui_common import checkset +from pgwui_testing import testing + +# Activiate our pytest plugin +pytest_plugins = ("pgwui",) + + +# Module packaging test + +def test_check_setting_is_pgwui_check_settings( + pgwui_check_settings_entry_point): + '''Ensure that pgwui_upload has a pgwui.check_settings entry point + ''' + assert (pgwui_check_settings_entry_point('pgwui_upload.check_settings') + is True) + + +# Mocks + +mock_unknown_settings = testing.make_mock_fixture( + checkset, 'unknown_settings') + +mock_require_settings = testing.make_mock_fixture( + checkset, 'require_settings') + +mock_boolean_settings = testing.make_mock_fixture( + checkset, 'boolean_settings') + + +# validate_literal_column_headings() + +def test_validate_literal_column_headings_nosetting(): + '''No error is delivered when there's no setting''' + errors = [] + check_settings.validate_literal_column_headings(errors, {}) + + assert errors == [] + + +def test_validate_literal_column_headings_on(): + '''No error is delivered when the setting is "on"''' + errors = [] + check_settings.validate_literal_column_headings( + errors, {'literal_column_headings': 'on'}) + + assert errors == [] + + +def test_validate_literal_column_headings_off(): + '''No error is delivered when the setting is "off"''' + errors = [] + check_settings.validate_literal_column_headings( + errors, {'literal_column_headings': 'off'}) + + assert errors == [] + + +def test_validate_literal_column_headings_ask(): + '''No error is delivered when the setting is "ask"''' + errors = [] + check_settings.validate_literal_column_headings( + errors, {'literal_column_headings': 'ask'}) + + assert errors == [] + + +def test_validate_literal_column_headings_bad(): + '''delivers an error when given a bad value''' + errors = [] + check_settings.validate_literal_column_headings( + errors, {'literal_column_headings': 'bad'}) + + assert errors + assert isinstance( + errors[0], check_settings.BadLiteralColumnHeadingsError) + + +literal_err = 'literal column headings error' +mock_validate_literal_column_headings = testing.make_mock_fixture( + check_settings, 'validate_literal_column_headings', + wraps=lambda errors, *args: errors.append(literal_err)) + + +# check_settings() + +def test_check_settings(mock_unknown_settings, + mock_require_settings, + mock_boolean_settings, + mock_validate_literal_column_headings): + '''The setting checking functions are called once, the check_settings() + call returns all the errors from each mock. + ''' + + unknown_retval = ['unk err'] + require_retval = ['req err'] + boolean_retval = ['bool err'] + + mock_unknown_settings.return_value = unknown_retval + mock_require_settings.return_value = require_retval + mock_boolean_settings.return_value = boolean_retval + + result = check_settings.check_settings({}) + + mock_unknown_settings.assert_called_once + mock_require_settings.assert_called_once + mock_boolean_settings.assert_called_once + mock_validate_literal_column_headings.assert_called_once + + assert result.sort() == ([literal_err] + + unknown_retval + + require_retval + + boolean_retval).sort() -- 2.34.1