# Karl O. Pinc <kop@karlpinc.com>
from pgwui_common import checkset
-from . import exceptions as upload_core_ex
UPLOAD_SETTINGS = ['menu_label',
]
REQUIRED_SETTINGS = []
BOOLEAN_SETTINGS = []
-BOOLEAN_CHOICE_SETTINGS = ['trim']
-
-
-def validate_literal_column_headings(component, 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(
- upload_core_ex.BadLiteralColumnHeadingsError(component, value))
+BOOLEAN_CHOICE_SETTINGS = ['literal_column_headings',
+ 'trim']
def check_settings(
component, boolean_setngs, component_config))
errors.extend(checkset.boolean_choice(
component, boolean_choice_setngs, component_config))
- validate_literal_column_headings(component, errors, component_config)
return errors
from pgwui_core import exceptions as core_ex
-# PGWUI setting related exceptions
+# Upload related exceptions
class UploadError(core_ex.Error):
pass
-class BadLiteralColumnHeadingsError(UploadError):
- def __init__(self, component, value):
- super().__init__(
- f'The "pgwui.{component}.literal_column_headings" PGWUI setting '
- f' is ({value}), it must be "on", "off", "ask", or the'
- ' entire setting be omitted')
-
-
-# Upload related exceptions
-
class NoTableError(UploadError):
'''No table uploaded'''
def __init__(self, e, descr='', detail=''):
from pgwui_common.path import asset_abspath
auth_base_mak = asset_abspath('pgwui_common:templates/auth_base.mak')
+
+ def show_choice(pgwui, setting):
+ val = pgwui['upload_settings'][setting]
+ return (val == 'choice-yes' or val == 'choice-no')
%>
<%inherit file="${auth_base_mak}" />
<%block name="title">${pgwui['upload_settings']['menu_label']}</%block>
<%def name="trim_row(tab_index)">
- <% trim_setting = pgwui['upload_settings']['trim'] %>
- % if (trim_setting == 'choice-yes' or trim_setting == 'choice-no'):
+ % if show_choice(pgwui, 'trim'):
<tr>
<td class="label">
<label for="trim_upload_id">Trim Leading/Trailing Spaces:</label>
<%def name="append_elements(form_elements)">
<% form_elements.append(trim_row) %>
- % if ask_about_literal_cols:
+ % if show_choice(pgwui, 'literal_column_headings'):
<%def name="literal_row(tab_index)">
<tr>
<td class="label">
upload_settings = settings['pgwui'][self.component]
self.trim_upload = (
upload_settings['trim'] == 'choice-yes')
- self.literal_col_headings = (
- upload_settings['literal_column_headings'] == 'yes')
+ lch = upload_settings['literal_column_headings']
+ self.literal_col_headings = (lch == 'yes-always', 'choice-yes')
return self
'''Return boolean -- whether to take column names literally
'''
quoter_setting = settings.get('literal_column_headings')
- if quoter_setting == 'on':
- return True
- elif quoter_setting == 'ask':
- return self.uf['literal_col_headings']
- else:
- return False
+ return (quoter_setting == 'yes-always'
+ or quoter_setting == 'choice-yes')
def validate_table(self, qualified_table):
'''Return schema and table names, or raise an exception
settings = request.registry.settings
upload_settings = settings['pgwui'][component]
- quoter_setting = upload_settings.get('literal_column_headings')
- response['ask_about_literal_cols'] = quoter_setting == 'ask'
-
response.setdefault('pgwui', dict())
response['pgwui']['upload_settings'] = upload_settings
from pgwui_common import checkset
from pgwui_testing import testing
-from pgwui_upload_core import exceptions as upload_ex
# Activiate the PGWUI pytest plugins
pytest_plugins = ("pgwui",)
checkset, 'boolean_choice')
-# validate_literal_column_headings()
-
-@pytest.mark.parametrize(
- ('settings', 'error_class'), [
- ({}, None),
- ({'literal_column_headings': 'on'}, None),
- ({'literal_column_headings': 'off'}, None),
- ({'literal_column_headings': 'ask'}, None),
- ({'literal_column_headings': 'bad'},
- upload_ex.BadLiteralColumnHeadingsError)])
-@pytest.mark.unittest
-def test_validate_literal_column_headings(settings, error_class):
- '''No error is delivered when there's no setting'''
- errors = []
- check_settings.validate_literal_column_headings(None, errors, settings)
-
- if error_class:
- assert len(errors) == 1
- assert isinstance(
- errors[0], error_class)
- else:
- assert errors == []
-
-
-literal_err = 'literal column headings error'
-mock_validate_literal_column_headings = testing.make_mock_fixture(
- check_settings, 'validate_literal_column_headings',
- wraps=lambda component, errors, *args: errors.append(literal_err))
-
-
# check_settings()
@pytest.mark.unittest
def test_check_settings(mock_unknown_settings,
mock_require_settings,
mock_boolean_settings,
- mock_boolean_choice,
- mock_validate_literal_column_headings):
+ mock_boolean_choice):
'''The setting checking functions are called once, the check_settings()
call returns all the errors from each mock.
'''
mock_require_settings.assert_called_once
mock_boolean_settings.assert_called_once
mock_boolean_choice.assert_called_once
- mock_validate_literal_column_headings.assert_called_once
- assert result.sort() == ([literal_err]
- + unknown_retval
+ assert result.sort() == (unknown_retval
+ require_retval
+ boolean_retval
+ boolean_choice_retval).sort()
return run
-def test_tableuploadhandler_quote_columns_on(get_quote_columns):
+@pytest.mark.parametrize(
+ ('val', 'expected'), [
+ ('yes-always', True),
+ ('choice-yes', True),
+ ('choice-no', False),
+ ('no-never', False)])
+def test_tableuploadhandler_quote_columns(get_quote_columns, val, expected):
'''When the settings ask for literal_column_headings = on return
True
'''
result = get_quote_columns(UPLOAD_FORM_W_LIT_CHECKED,
- {'literal_column_headings': 'on'})
- assert result is True
-
-
-def test_tableuploadhandler_quote_columns_off(get_quote_columns):
- '''When the settings ask for literal_column_headings = off return
- False
- '''
- result = get_quote_columns(UPLOAD_FORM_W_LIT_CHECKED,
- {'literal_column_headings': 'off'})
- assert result is False
-
-
-def test_tableuploadhandler_quote_columns_default(get_quote_columns):
- '''When the settings literal_column_headings is not present return
- False (as default)
- '''
- result = get_quote_columns(UPLOAD_FORM_W_LIT_CHECKED, {})
- assert result is False
-
-
-def test_tableuploadhandler_quote_columns_ask_on(get_quote_columns):
- '''When the form asks for literal column headings return True
- '''
- result = get_quote_columns(UPLOAD_FORM_W_LIT_CHECKED,
- {'literal_column_headings': 'ask'})
- assert result is True
-
-
-def test_tableuploadhandler_quote_columns_ask_off(get_quote_columns):
- '''When the form does not ask for literal column headings return False
- '''
- result = get_quote_columns({'literal_col_headings': False},
- {'literal_column_headings': 'ask'})
- assert result is False
+ {'literal_column_headings': val})
+ assert result is expected
# BaseTableUploadHandler.validate_table()