mirror of
https://github.com/apache/superset.git
synced 2026-04-23 18:14:56 +00:00
feat: add endpoint to fetch available DBs (#14208)
* feat: add endpoint to fetch available DBs * Fix lint
This commit is contained in:
125
tests/databases/schema_tests.py
Normal file
125
tests/databases/schema_tests.py
Normal file
@@ -0,0 +1,125 @@
|
||||
# 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.
|
||||
|
||||
from unittest import mock
|
||||
|
||||
from marshmallow import fields, Schema, ValidationError
|
||||
|
||||
from superset.databases.schemas import DatabaseParametersSchemaMixin
|
||||
from superset.db_engine_specs.base import BaseParametersMixin
|
||||
|
||||
|
||||
class DummySchema(Schema, DatabaseParametersSchemaMixin):
|
||||
sqlalchemy_uri = fields.String()
|
||||
|
||||
|
||||
class DummyEngine(BaseParametersMixin):
|
||||
drivername = "dummy"
|
||||
|
||||
|
||||
class InvalidEngine:
|
||||
pass
|
||||
|
||||
|
||||
@mock.patch("superset.databases.schemas.get_engine_specs")
|
||||
def test_database_parameters_schema_mixin(get_engine_specs):
|
||||
get_engine_specs.return_value = {"dummy_engine": DummyEngine}
|
||||
payload = {
|
||||
"parameters": {
|
||||
"engine": "dummy_engine",
|
||||
"username": "username",
|
||||
"password": "password",
|
||||
"host": "localhost",
|
||||
"port": 12345,
|
||||
"database": "dbname",
|
||||
}
|
||||
}
|
||||
schema = DummySchema()
|
||||
result = schema.load(payload)
|
||||
assert result == {
|
||||
"sqlalchemy_uri": "dummy://username:password@localhost:12345/dbname"
|
||||
}
|
||||
|
||||
|
||||
def test_database_parameters_schema_mixin_no_engine():
|
||||
payload = {
|
||||
"parameters": {
|
||||
"username": "username",
|
||||
"password": "password",
|
||||
"host": "localhost",
|
||||
"port": 12345,
|
||||
"dbname": "dbname",
|
||||
}
|
||||
}
|
||||
schema = DummySchema()
|
||||
try:
|
||||
schema.load(payload)
|
||||
except ValidationError as err:
|
||||
assert err.messages == {
|
||||
"_schema": [
|
||||
"An engine must be specified when passing individual parameters to a database."
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
@mock.patch("superset.databases.schemas.get_engine_specs")
|
||||
def test_database_parameters_schema_mixin_invalid_engine(get_engine_specs):
|
||||
get_engine_specs.return_value = {}
|
||||
payload = {
|
||||
"parameters": {
|
||||
"engine": "dummy_engine",
|
||||
"username": "username",
|
||||
"password": "password",
|
||||
"host": "localhost",
|
||||
"port": 12345,
|
||||
"dbname": "dbname",
|
||||
}
|
||||
}
|
||||
schema = DummySchema()
|
||||
try:
|
||||
schema.load(payload)
|
||||
except ValidationError as err:
|
||||
assert err.messages == {
|
||||
"_schema": ['Engine "dummy_engine" is not a valid engine.']
|
||||
}
|
||||
|
||||
|
||||
@mock.patch("superset.databases.schemas.get_engine_specs")
|
||||
def test_database_parameters_schema_no_mixin(get_engine_specs):
|
||||
get_engine_specs.return_value = {"invalid_engine": InvalidEngine}
|
||||
payload = {
|
||||
"parameters": {
|
||||
"engine": "invalid_engine",
|
||||
"username": "username",
|
||||
"password": "password",
|
||||
"host": "localhost",
|
||||
"port": 12345,
|
||||
"database": "dbname",
|
||||
}
|
||||
}
|
||||
schema = DummySchema()
|
||||
try:
|
||||
schema.load(payload)
|
||||
except ValidationError as err:
|
||||
assert err.messages == {
|
||||
"_schema": [
|
||||
(
|
||||
'Engine spec "InvalidEngine" does not support '
|
||||
"being configured via individual parameters."
|
||||
)
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user