forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_validator_tests.py
207 lines (165 loc) · 7.05 KB
/
sql_validator_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# isort:skip_file
"""Unit tests for Sql Lab"""
import unittest
from unittest.mock import MagicMock, patch
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.presto_db import (
PrestoDBSQLValidator,
PrestoSQLValidationError,
)
from .base_tests import SupersetTestCase
PRESTO_TEST_FEATURE_FLAGS = {
"SQL_VALIDATORS_BY_ENGINE": {
"presto": "PrestoDBSQLValidator",
"sqlite": "PrestoDBSQLValidator",
"postgresql": "PrestoDBSQLValidator",
"mysql": "PrestoDBSQLValidator",
}
}
class SqlValidatorEndpointTests(SupersetTestCase):
"""Testing for Sql Lab querytext validation endpoint"""
def tearDown(self):
self.logout()
def test_validate_sql_endpoint_noconfig(self):
"""Assert that validate_sql_json errors out when no validators are
configured for any db"""
self.login("admin")
app.config["SQL_VALIDATORS_BY_ENGINE"] = {}
resp = self.validate_sql(
"SELECT * FROM birth_names", client_id="1", raise_on_error=False
)
self.assertIn("error", resp)
self.assertIn("no SQL validator is configured", resp["error"])
@patch("superset.views.core.get_validator_by_name")
@patch.dict(
"superset.extensions.feature_flag_manager._feature_flags",
PRESTO_TEST_FEATURE_FLAGS,
clear=True,
)
def test_validate_sql_endpoint_mocked(self, get_validator_by_name):
"""Assert that, with a mocked validator, annotations make it back out
from the validate_sql_json endpoint as a list of json dictionaries"""
self.login("admin")
validator = MagicMock()
get_validator_by_name.return_value = validator
validator.validate.return_value = [
SQLValidationAnnotation(
message="I don't know what I expected, but it wasn't this",
line_number=4,
start_column=12,
end_column=42,
)
]
resp = self.validate_sql(
"SELECT * FROM somewhere_over_the_rainbow",
client_id="1",
raise_on_error=False,
)
self.assertEqual(1, len(resp))
self.assertIn("expected,", resp[0]["message"])
@patch("superset.views.core.get_validator_by_name")
@patch.dict(
"superset.extensions.feature_flag_manager._feature_flags",
PRESTO_TEST_FEATURE_FLAGS,
clear=True,
)
def test_validate_sql_endpoint_failure(self, get_validator_by_name):
"""Assert that validate_sql_json errors out when the selected validator
raises an unexpected exception"""
self.login("admin")
validator = MagicMock()
get_validator_by_name.return_value = validator
validator.validate.side_effect = Exception("Kaboom!")
resp = self.validate_sql(
"SELECT * FROM birth_names", client_id="1", raise_on_error=False
)
self.assertIn("error", resp)
self.assertIn("Kaboom!", resp["error"])
class BaseValidatorTests(SupersetTestCase):
"""Testing for the base sql validator"""
def setUp(self):
self.validator = BaseSQLValidator
def test_validator_excepts(self):
with self.assertRaises(NotImplementedError):
self.validator.validate(None, None, None)
class PrestoValidatorTests(SupersetTestCase):
"""Testing for the prestodb sql validator"""
def setUp(self):
self.validator = PrestoDBSQLValidator
self.database = MagicMock()
self.database_engine = self.database.get_sqla_engine.return_value
self.database_conn = self.database_engine.raw_connection.return_value
self.database_cursor = self.database_conn.cursor.return_value
self.database_cursor.poll.return_value = None
def tearDown(self):
self.logout()
PRESTO_ERROR_TEMPLATE = {
"errorLocation": {"lineNumber": 10, "columnNumber": 20},
"message": "your query isn't how I like it",
}
@patch("superset.sql_validators.presto_db.g")
def test_validator_success(self, flask_g):
flask_g.user.username = "nobody"
sql = "SELECT 1 FROM default.notarealtable"
schema = "default"
errors = self.validator.validate(sql, schema, self.database)
self.assertEqual([], errors)
@patch("superset.sql_validators.presto_db.g")
def test_validator_db_error(self, flask_g):
flask_g.user.username = "nobody"
sql = "SELECT 1 FROM default.notarealtable"
schema = "default"
fetch_fn = self.database.db_engine_spec.fetch_data
fetch_fn.side_effect = DatabaseError("dummy db error")
with self.assertRaises(PrestoSQLValidationError):
self.validator.validate(sql, schema, self.database)
@patch("superset.sql_validators.presto_db.g")
def test_validator_unexpected_error(self, flask_g):
flask_g.user.username = "nobody"
sql = "SELECT 1 FROM default.notarealtable"
schema = "default"
fetch_fn = self.database.db_engine_spec.fetch_data
fetch_fn.side_effect = Exception("a mysterious failure")
with self.assertRaises(Exception):
self.validator.validate(sql, schema, self.database)
@patch("superset.sql_validators.presto_db.g")
def test_validator_query_error(self, flask_g):
flask_g.user.username = "nobody"
sql = "SELECT 1 FROM default.notarealtable"
schema = "default"
fetch_fn = self.database.db_engine_spec.fetch_data
fetch_fn.side_effect = DatabaseError(self.PRESTO_ERROR_TEMPLATE)
errors = self.validator.validate(sql, schema, self.database)
self.assertEqual(1, len(errors))
def test_validate_sql_endpoint(self):
self.login("admin")
# NB this is effectively an integration test -- when there's a default
# validator for sqlite, this test will fail because the validator
# will no longer error out.
resp = self.validate_sql(
"SELECT * FROM birth_names", client_id="1", raise_on_error=False
)
self.assertIn("error", resp)
self.assertIn("no SQL validator is configured", resp["error"])
if __name__ == "__main__":
unittest.main()