mirror of
https://github.com/apache/superset.git
synced 2026-04-07 18:35:15 +00:00
feat: add Postgres SQL validator (#11538)
* Add Postgres SQL validator * Strip line number from message * Add unit tests * Run tests only with postgres backend * Add dep * Add dep to bashlib
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
# isort:skip_file
|
||||
# pylint: disable=invalid-name, no-self-use
|
||||
"""Unit tests for Sql Lab"""
|
||||
import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
@@ -22,10 +23,10 @@ from unittest.mock import MagicMock, patch
|
||||
import pytest
|
||||
from pyhive.exc import DatabaseError
|
||||
|
||||
import tests.test_app
|
||||
from superset import app
|
||||
from superset.sql_validators import SQLValidationAnnotation
|
||||
from superset.sql_validators.base import BaseSQLValidator
|
||||
from superset.sql_validators.postgres import PostgreSQLValidator
|
||||
from superset.sql_validators.presto_db import (
|
||||
PrestoDBSQLValidator,
|
||||
PrestoSQLValidationError,
|
||||
@@ -211,5 +212,33 @@ class TestPrestoValidator(SupersetTestCase):
|
||||
self.assertIn("no SQL validator is configured", resp["error"])
|
||||
|
||||
|
||||
class TestPostgreSQLValidator(SupersetTestCase):
|
||||
def test_valid_syntax(self):
|
||||
if get_example_database().backend != "postgresql":
|
||||
return
|
||||
|
||||
mock_database = MagicMock()
|
||||
annotations = PostgreSQLValidator.validate(
|
||||
sql='SELECT 1, "col" FROM "table"', schema="", database=mock_database
|
||||
)
|
||||
assert annotations == []
|
||||
|
||||
def test_invalid_syntax(self):
|
||||
if get_example_database().backend != "postgresql":
|
||||
return
|
||||
|
||||
mock_database = MagicMock()
|
||||
annotations = PostgreSQLValidator.validate(
|
||||
sql='SELECT 1, "col"\nFROOM "table"', schema="", database=mock_database
|
||||
)
|
||||
|
||||
assert len(annotations) == 1
|
||||
annotation = annotations[0]
|
||||
assert annotation.line_number == 2
|
||||
assert annotation.start_column is None
|
||||
assert annotation.end_column is None
|
||||
assert annotation.message == 'ERROR: syntax error at or near """'
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user