villebro commented on a change in pull request #11131: URL: https://github.com/apache/incubator-superset/pull/11131#discussion_r499779535
########## File path: tests/dashboard_utils.py ########## @@ -0,0 +1,82 @@ +# 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. +"""Utils to provide dashboards for tests""" + +import json +from typing import Any, Dict + +from pandas import DataFrame + +from superset import ConnectorRegistry, db +from superset.connectors.sqla.models import SqlaTable +from superset.models.core import Database +from superset.models.dashboard import Dashboard +from superset.models.slice import Slice + + +def create_table_for_dashboard( + df: DataFrame, tbl_name: str, database: Database, schema: Dict[str, Any] +) -> SqlaTable: + df.to_sql( + tbl_name, + database.get_sqla_engine(), + if_exists="replace", + chunksize=500, + dtype=schema, + index=False, + method="multi", + ) + + tbl_source = ConnectorRegistry.sources["table"] + obj = db.session.query(tbl_source).filter_by(table_name=tbl_name).first() + if not obj: + obj = tbl_source(table_name=tbl_name) + obj.database = database + db.session.merge(obj) + db.session.commit() + + return obj + + +def create_slice( + title: str, viz_type: str, tbl: SqlaTable, slices_dict: Dict[str, str] Review comment: naming nit again ```suggestion title: str, viz_type: str, table: SqlaTable, slices_dict: Dict[str, str] ``` ########## File path: tests/dashboard_utils.py ########## @@ -0,0 +1,82 @@ +# 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. +"""Utils to provide dashboards for tests""" + +import json +from typing import Any, Dict + +from pandas import DataFrame + +from superset import ConnectorRegistry, db +from superset.connectors.sqla.models import SqlaTable +from superset.models.core import Database +from superset.models.dashboard import Dashboard +from superset.models.slice import Slice + + +def create_table_for_dashboard( + df: DataFrame, tbl_name: str, database: Database, schema: Dict[str, Any] +) -> SqlaTable: + df.to_sql( + tbl_name, + database.get_sqla_engine(), + if_exists="replace", + chunksize=500, + dtype=schema, + index=False, + method="multi", + ) + + tbl_source = ConnectorRegistry.sources["table"] + obj = db.session.query(tbl_source).filter_by(table_name=tbl_name).first() + if not obj: + obj = tbl_source(table_name=tbl_name) + obj.database = database + db.session.merge(obj) + db.session.commit() + + return obj + + +def create_slice( + title: str, viz_type: str, tbl: SqlaTable, slices_dict: Dict[str, str] +) -> Slice: + return Slice( + slice_name=title, + viz_type=viz_type, + datasource_type="table", + datasource_id=tbl.id, + params=json.dumps(slices_dict, indent=4, sort_keys=True), + ) + + +def create_dashboard(slug: str, title: str, position: str, slc: Slice) -> Dashboard: Review comment: nit ```suggestion def create_dashboard(slug: str, title: str, position: str, slice: Slice) -> Dashboard: ``` ########## File path: tests/fixtures/unicode_dashboard.py ########## @@ -0,0 +1,106 @@ +# 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. +import pandas as pd +import pytest +from pandas import DataFrame +from sqlalchemy import String +from sqlalchemy.engine import Engine + +from superset import db +from superset.connectors.sqla.models import SqlaTable +from superset.models.dashboard import Dashboard +from superset.models.slice import Slice +from superset.utils.core import get_example_database +from tests.dashboard_utils import ( + create_dashboard, + create_slice, + create_table_for_dashboard, +) +from tests.test_app import app + + [email protected]() +def load_unicode_dashboard_with_slice(): + tbl_name = "unicode_test" + df = _get_dataframe() + with app.app_context(): + yield _create_unicode_dashboard(df, tbl_name, "Unicode Cloud", None) + + _cleanup() + + [email protected]() +def load_unicode_dashboard_with_position(): + tbl_name = "unicode_test" + df = _get_dataframe() + position = "{}" + with app.app_context(): + yield _create_unicode_dashboard(df, tbl_name, "Unicode Cloud", position) + + _cleanup() + + +def _get_dataframe(): + data = _get_unicode_data() + return pd.DataFrame.from_dict(data) + + +def _get_unicode_data(): + return [ + {"phrase": "Под"}, + {"phrase": "řšž"}, + {"phrase": "視野無限廣"}, + {"phrase": "微風"}, + {"phrase": "中国智造"}, + {"phrase": "æøå"}, + {"phrase": "ëœéè"}, + {"phrase": "いろはにほ"}, + ] + + +def _create_unicode_dashboard( + df: DataFrame, tbl_name: str, slc_title: str, position: str Review comment: ```suggestion df: DataFrame, table_name: str, slice_title: str, position: str ``` ########## File path: tests/dashboard_utils.py ########## @@ -0,0 +1,82 @@ +# 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. +"""Utils to provide dashboards for tests""" + +import json +from typing import Any, Dict + +from pandas import DataFrame + +from superset import ConnectorRegistry, db +from superset.connectors.sqla.models import SqlaTable +from superset.models.core import Database +from superset.models.dashboard import Dashboard +from superset.models.slice import Slice + + +def create_table_for_dashboard( + df: DataFrame, tbl_name: str, database: Database, schema: Dict[str, Any] Review comment: nit: I personally find `table_name` more pythonic. ```suggestion df: DataFrame, table_name: str, database: Database, schema: Dict[str, Any] ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
