Have literal_column_headings values be those of boolean_choice
authorKarl O. Pinc <kop@karlpinc.com>
Sat, 23 Jan 2021 20:24:42 +0000 (14:24 -0600)
committerKarl O. Pinc <kop@karlpinc.com>
Sat, 23 Jan 2021 20:58:59 +0000 (14:58 -0600)
src/pgwui_upload_core/check_settings.py
src/pgwui_upload_core/exceptions.py
src/pgwui_upload_core/templates/upload.mak
src/pgwui_upload_core/views/upload.py
tests/test_check_settings.py
tests/views/test_upload.py

index 8c438367eb6049fc4c472cbdb8b1061811a77236..778770925f5b427f8538509f646b6e085b9c0b07 100644 (file)
@@ -20,7 +20,6 @@
 # Karl O. Pinc <kop@karlpinc.com>
 
 from pgwui_common import checkset
-from . import exceptions as upload_core_ex
 
 
 UPLOAD_SETTINGS = ['menu_label',
@@ -29,18 +28,8 @@ 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(
@@ -62,6 +51,5 @@ 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
index da590d796c8c69413e3c62ef21c08bc113348d86..662b7f634015d7fde366d9dce5929e22250cf8fc 100644 (file)
 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=''):
index b49d52e906c47a8394c232a05aa42285d4ad35c0..b0855464ee7fa7aeb0c5b3bc413765eb98fc65ff 100644 (file)
     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}" />
@@ -44,8 +48,7 @@
 <%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>
@@ -64,7 +67,7 @@
 
 <%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">
index 2aa9d36805fcf58070d51adce02fbcfca1f1546e..da146467fa5bc534d80c3c5456930e80895bf2dc 100644 (file)
@@ -61,8 +61,8 @@ class UploadCoreInitialPost(UploadNullFileInitialPost):
         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
 
 
@@ -175,12 +175,8 @@ class BaseTableUploadHandler(TabularFileUploadHandler):
         '''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
@@ -286,8 +282,5 @@ def set_upload_response(component, request, response):
     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
index 78898c7c0928897b5001a8648a4608334b08ce4c..18456b88bb2a7a723a150dc0fb4ab1f072ba0bbb 100644 (file)
@@ -25,7 +25,6 @@ import pgwui_upload_core.check_settings as check_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",)
@@ -46,44 +45,13 @@ mock_boolean_choice = testing.make_mock_fixture(
     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.
     '''
@@ -104,10 +72,8 @@ def test_check_settings(mock_unknown_settings,
     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()
index a14430129da5b53ebf04033e935f61a9d1de10f8..f376b395553601d3a96e94f49c88cde3e3c2d167 100644 (file)
@@ -157,46 +157,19 @@ def get_quote_columns(neuter_tableuploadhandler):
     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()