Fix reporting on bad column names
authorKarl O. Pinc <kop@karlpinc.com>
Thu, 15 Aug 2024 18:55:10 +0000 (13:55 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Thu, 15 Aug 2024 18:55:10 +0000 (13:55 -0500)
src/pgwui_upload_core/views/upload.py
tests/views/test_upload.py

index ee7f81aea6996b67b54de83659139c2dc2c2cf88..39aa3d6cf681b1b8a84d1022706efe46934ac5b6 100644 (file)
@@ -239,35 +239,6 @@ class BaseTableUploadHandler(TabularFileUploadHandler):
                     markupsafe.escape(qualified_table)))
         return (schema, table)
 
-    def report_bad_cols(self, qualified_table, bad_cols, quotecols):
-        if quotecols:
-            detail = ('<p>The following columns are not in the ({0})'
-                      ' table, or the first line of your file is not'
-                      ' in the expected format and your column names'
-                      ' have merged (all the names appear in a single'
-                      ' list item, below), or the supplied column names'
-                      ' do not match'
-                      " the character case of the table's columns,"
-                      ' or you do not have permission to access'
-                      ' the columns:</p><ul>')
-        else:
-            detail = ('<p>The following columns are not in the ({0})'
-                      ' table, or the first line of your file is not'
-                      ' in the expected format and your column names'
-                      ' have merged (all the names appear in a single'
-                      ' list item, below), '
-                      ' or the table has column names containing'
-                      ' upper case characters, or you do not have'
-                      ' permission to access the columns:</p><ul>')
-        detail = detail.format(markupsafe.escape(qualified_table))
-
-        for bad_col in bad_cols:
-            detail += '<li>{0}</li>'.format(markupsafe.escape(bad_col))
-        detail += '</ul>'
-        raise upload_ex.BadHeadersError(
-            'Header line contains unknown column names',
-            detail=detail)
-
     def get_column_quoter(self, quotecols):
         if quotecols:
             return doublequote
@@ -306,6 +277,35 @@ class InsertStmt:
     stmt = attrs.field(default=None)
     cols = attrs.field(default=None)
 
+    def report_bad_cols(self, qualified_table, bad_cols, quotecols):
+        if quotecols:
+            detail = ('<p>The following columns are not in the ({0})'
+                      ' table, or the first line of your file is not'
+                      ' in the expected format and your column names'
+                      ' have merged (all the names appear in a single'
+                      ' list item, below), or the supplied column names'
+                      ' do not match'
+                      " the character case of the table's columns,"
+                      ' or you do not have permission to access'
+                      ' the columns:</p><ul>')
+        else:
+            detail = ('<p>The following columns are not in the ({0})'
+                      ' table, or the first line of your file is not'
+                      ' in the expected format and your column names'
+                      ' have merged (all the names appear in a single'
+                      ' list item, below), '
+                      ' or the table has column names containing'
+                      ' upper case characters, or you do not have'
+                      ' permission to access the columns:</p><ul>')
+        detail = detail.format(markupsafe.escape(qualified_table))
+
+        for bad_col in bad_cols:
+            detail += '<li>{0}</li>'.format(markupsafe.escape(bad_col))
+        detail += '</ul>'
+        raise upload_ex.BadHeadersError(
+            'Header line contains unknown column names',
+            detail=detail)
+
     def build_insert_stmt(
             self, tuh, data, qualified_table, quotecols, column_quoter):
         '''tuh: A TableUploadHandler'''
index c227250e8cad49f992237e9dbf26ee28ad1855af..f25c63a6c534f60f23aa546e4b7f2b2145e2c505 100644 (file)
@@ -211,24 +211,6 @@ def test_validate_table_bad(
     assert True
 
 
-# BaseTableUploadHandler.report_bad_cols()
-
-@pytest.mark.parametrize(
-    ('quotecols',), [
-        (True,),
-        (False,)])
-def test_report_bad_cols(mock_escape, quotecols):
-    '''The expected exception is raised
-    '''
-
-    request = DummyRequest()
-    uh = upload.BaseTableUploadHandler(request)
-    with pytest.raises(upload_ex.BadHeadersError):
-        uh.report_bad_cols(None, ['col1', 'col2'], quotecols)
-
-    assert True
-
-
 # BaseTableUploadHandler.factory()
 
 @pytest.mark.unittest
@@ -245,3 +227,19 @@ def test_basetableuploadhandler_factory():
 
     assert uh.ue == ue
     assert uh.cur == sample_cursor
+
+
+# InsertStmt.report_bad_cols()
+
+@pytest.mark.parametrize(
+    ('quotecols',), [
+        (True,),
+        (False,)])
+def test_report_bad_cols(mock_escape, quotecols):
+    '''The expected exception is raised
+    '''
+    insert_stmt = upload.InsertStmt()
+    with pytest.raises(upload_ex.BadHeadersError):
+        insert_stmt.report_bad_cols(None, ['col1', 'col2'], quotecols)
+
+    assert True