-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
787c513
commit 595e41d
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# 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. | ||
|
||
# pylint: disable=import-outside-toplevel | ||
|
||
from typing import List, Optional | ||
|
||
from pytest_mock import MockFixture | ||
from sqlalchemy.engine.reflection import Inspector | ||
|
||
|
||
def test_get_metrics(mocker: MockFixture) -> None: | ||
""" | ||
Tests for ``get_metrics``. | ||
""" | ||
from superset.db_engine_specs.base import MetricType | ||
from superset.db_engine_specs.sqlite import SqliteEngineSpec | ||
from superset.models.core import Database | ||
|
||
database = Database(database_name="my_database", sqlalchemy_uri="sqlite://") | ||
assert database.get_metrics("table") == [ | ||
{ | ||
"expression": "COUNT(*)", | ||
"metric_name": "count", | ||
"metric_type": "count", | ||
"verbose_name": "COUNT(*)", | ||
} | ||
] | ||
|
||
class CustomSqliteEngineSpec(SqliteEngineSpec): | ||
@classmethod | ||
def get_metrics( | ||
cls, | ||
database: Database, | ||
inspector: Inspector, | ||
table_name: str, | ||
schema: Optional[str], | ||
) -> List[MetricType]: | ||
return [ | ||
{ | ||
"expression": "COUNT(DISTINCT user_id)", | ||
"metric_name": "count_distinct_user_id", | ||
"metric_type": "count_distinct", | ||
"verbose_name": "COUNT(DISTINCT user_id)", | ||
}, | ||
] | ||
|
||
database.get_db_engine_spec_for_backend = mocker.MagicMock( # type: ignore | ||
return_value=CustomSqliteEngineSpec | ||
) | ||
assert database.get_metrics("table") == [ | ||
{ | ||
"expression": "COUNT(DISTINCT user_id)", | ||
"metric_name": "count_distinct_user_id", | ||
"metric_type": "count_distinct", | ||
"verbose_name": "COUNT(DISTINCT user_id)", | ||
}, | ||
] |