fix: catch exception when create connection (#16692)

* fix: catch exception when create connection

* fix lint

* added UT

(cherry picked from commit 21f98ddc21)
This commit is contained in:
Yongjie Zhao
2021-09-16 08:55:57 +01:00
committed by Ville Brofeldt
parent 523c97b04f
commit b95e697ef9
3 changed files with 27 additions and 2 deletions

View File

@@ -18,6 +18,8 @@
import textwrap
import unittest
from unittest import mock
from superset.exceptions import SupersetException
from tests.integration_tests.fixtures.birth_names_dashboard import (
load_birth_names_dashboard_with_slices,
)
@@ -337,6 +339,18 @@ class TestDatabaseModel(SupersetTestCase):
df = main_db.get_df("USE superset; SELECT ';';", None)
self.assertEqual(df.iat[0, 0], ";")
@mock.patch("superset.models.core.create_engine")
def test_get_sqla_engine(self, mocked_create_engine):
model = Database(
database_name="test_database", sqlalchemy_uri="mysql://root@localhost",
)
model.db_engine_spec.get_dbapi_exception_mapping = mock.Mock(
return_value={Exception: SupersetException}
)
mocked_create_engine.side_effect = Exception()
with self.assertRaises(SupersetException):
model.get_sqla_engine()
class TestSqlaTableModel(SupersetTestCase):
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")