From bbd123965939044dcaee6df490c0968316238cab Mon Sep 17 00:00:00 2001 From: Kasia Kucharczyk Date: Thu, 1 Oct 2020 20:03:20 +0200 Subject: [PATCH 01/13] Removed depemdency to unicode example in tests config. --- tests/conftest.py | 2 -- tests/dashboard_utils.py | 0 2 files changed, 2 deletions(-) create mode 100644 tests/dashboard_utils.py diff --git a/tests/conftest.py b/tests/conftest.py index e922315785ea..4f7f988ab830 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -45,7 +45,6 @@ def setup_sample_data() -> Any: examples.load_energy(sample=True) examples.load_world_bank_health_n_pop(sample=True) examples.load_birth_names(sample=True) - examples.load_unicode_test_data(sample=True) yield @@ -54,7 +53,6 @@ def setup_sample_data() -> Any: engine.execute("DROP TABLE energy_usage") engine.execute("DROP TABLE wb_health_population") engine.execute("DROP TABLE birth_names") - engine.execute("DROP TABLE unicode_test") # drop sqlachemy tables diff --git a/tests/dashboard_utils.py b/tests/dashboard_utils.py new file mode 100644 index 000000000000..e69de29bb2d1 From 37ecef0fad3d3e02cf0a7fc271c0cba9076bd137 Mon Sep 17 00:00:00 2001 From: Kasia Kucharczyk Date: Thu, 1 Oct 2020 20:24:31 +0200 Subject: [PATCH 02/13] Added common methods for creating dashboards for tests. --- tests/dashboard_utils.py | 90 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/tests/dashboard_utils.py b/tests/dashboard_utils.py index e69de29bb2d1..498d81288fbe 100644 --- a/tests/dashboard_utils.py +++ b/tests/dashboard_utils.py @@ -0,0 +1,90 @@ +# 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 +"""Utils to provide dashboards for tests""" + +import datetime +import json + +import random +from pandas import DataFrame +from typing import Dict, Any + +from superset import db, ConnectorRegistry +from superset.models.dashboard import Dashboard +from superset.models.slice import Slice + + +def add_datetime_value_to_data(data): + data_dict = {} + for d in data: + data_dict["phrase"] = d + data_dict["dttm"] = datetime.datetime.now().date() + data_dict["value"] = [random.randint(1, 100) for _ in range(len(data))] + return data_dict + + +def create_table_for_dashboard( + df: DataFrame, tbl_name: str, database, schema: Dict[str, Any] +): + 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.main_dttm_col = "dttm" + obj.database = database + db.session.merge(obj) + db.session.commit() + + return obj + + +def create_slice(tbl, slices_dict: Dict[str, str]): + return Slice( + slice_name="Unicode Cloud", + viz_type="word_cloud", + 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): + dash = db.session.query(Dashboard).filter_by(slug=slug).first() + + if not dash: + dash = Dashboard() + dash.dashboard_title = title + if position is not None: + js = position + pos = json.loads(js) + dash.position_json = json.dumps(pos, indent=4) + dash.slug = slug + if slc is not None: + dash.slices = [slc] + db.session.merge(dash) + db.session.commit() From 173ea9abd932de1a75391104a6d66fbed350ade6 Mon Sep 17 00:00:00 2001 From: Kasia Kucharczyk Date: Thu, 1 Oct 2020 20:39:25 +0200 Subject: [PATCH 03/13] Added fixtures to all tests which were using unicode example. --- tests/databases/api_tests.py | 93 ++++++++++++++++++++++++++++++- tests/security_tests.py | 99 ++++++++++++++++++++++++++++++++- tests/strategy_tests.py | 103 ++++++++++++++++++++++++++++++++++- 3 files changed, 291 insertions(+), 4 deletions(-) diff --git a/tests/databases/api_tests.py b/tests/databases/api_tests.py index 0e965d74eb54..e00d32e3a100 100644 --- a/tests/databases/api_tests.py +++ b/tests/databases/api_tests.py @@ -16,16 +16,26 @@ # under the License. # isort:skip_file """Unit tests for Superset""" + import json +import pandas as pd import prison +import pytest + +from sqlalchemy import String, Date, Float from sqlalchemy.sql import func -from superset import db, security_manager +from superset import db, security_manager, ConnectorRegistry from superset.connectors.sqla.models import SqlaTable from superset.models.core import Database from superset.utils.core import get_example_database, get_main_database from tests.base_tests import SupersetTestCase +from tests.dashboard_utils import ( + add_datetime_value_to_data, + create_table_for_dashboard, + create_dashboard, +) from tests.fixtures.certificates import ssl_certificate from tests.test_app import app @@ -758,6 +768,45 @@ def test_test_connection_unsafe_uri(self): } self.assertEqual(response, expected_response) + @pytest.fixture() + def load_unicode_dashboard(self): + data = [ + "Под", + "řšž", + "視野無限廣", + "微風", + "中国智造", + "æøå", + "ëœéè", + "いろはにほ", + "다람쥐", + "Чешће", + "ŕľšťýď", + "žšč", + "éúüñóá", + "كۆچەج", + ] + tbl_name = "unicode_test" + + # generate date/numeric data + unicode_data_dict = add_datetime_value_to_data(data) + df = pd.DataFrame.from_dict(unicode_data_dict) + + with self.create_app().app_context(): + database = get_example_database() + schema = { + "phrase": String(500), + "dttm": Date(), + "value": Float(), + } + obj = create_table_for_dashboard(df, tbl_name, database, schema) + obj.fetch_metadata() + + db.session.commit() + position = _get_position() + create_dashboard("unicode-test", "Unicode Test", position, None) + + @pytest.mark.usefixtures("load_unicode_dashboard") def test_get_database_related_objects(self): """ Database API: Test get chart and dashboard count related to a database @@ -789,3 +838,45 @@ def test_get_database_related_objects_not_found(self): uri = f"api/v1/database/{database.id}/related_objects/" rv = self.client.get(uri) self.assertEqual(rv.status_code, 404) + + +def _get_position(): + return """{ + "CHART-Hkx6154FEm": { + "children": [], + "id": "CHART-Hkx6154FEm", + "meta": { + "chartId": 2225, + "height": 30, + "sliceName": "slice 1", + "width": 4 + }, + "type": "CHART" + }, + "GRID_ID": { + "children": [ + "ROW-SyT19EFEQ" + ], + "id": "GRID_ID", + "type": "GRID" + }, + "ROOT_ID": { + "children": [ + "GRID_ID" + ], + "id": "ROOT_ID", + "type": "ROOT" + }, + "ROW-SyT19EFEQ": { + "children": [ + "CHART-Hkx6154FEm" + ], + "id": "ROW-SyT19EFEQ", + "meta": { + "background": "BACKGROUND_TRANSPARENT" + }, + "type": "ROW" + }, + "DASHBOARD_VERSION_KEY": "v2" + } + """ diff --git a/tests/security_tests.py b/tests/security_tests.py index 33136e822678..872866eff897 100644 --- a/tests/security_tests.py +++ b/tests/security_tests.py @@ -20,11 +20,14 @@ import unittest from unittest.mock import Mock, patch +import pandas as pd import prison +import pytest + from flask import current_app, g +from sqlalchemy import Float, Date, String -import tests.test_app -from superset import app, appbuilder, db, security_manager, viz +from superset import app, appbuilder, db, security_manager, viz, ConnectorRegistry from superset.connectors.druid.models import DruidCluster, DruidDatasource from superset.connectors.sqla.models import RowLevelSecurityFilter, SqlaTable from superset.errors import ErrorLevel, SupersetError, SupersetErrorType @@ -35,6 +38,12 @@ from superset.utils.core import get_example_database from .base_tests import SupersetTestCase +from .dashboard_utils import ( + create_table_for_dashboard, + create_slice, + create_dashboard, + add_datetime_value_to_data, +) def get_perm_tuples(role_name): @@ -1122,6 +1131,50 @@ def test_rls_filter_doesnt_alter_energy_query(self): assert tbl.get_extra_cache_keys(self.query_obj) == [] assert "value > 1" not in sql + @pytest.fixture() + def load_unicode_dashboard(self): + data = [ + "Под", + "řšž", + "視野無限廣", + "微風", + "中国智造", + "æøå", + "ëœéè", + "いろはにほ", + "다람쥐", + "Чешће", + "ŕľšťýď", + "žšč", + "éúüñóá", + "كۆچەج", + ] + tbl_name = "unicode_test" + + # generate date/numeric data + unicode_data_dict = add_datetime_value_to_data(data) + df = pd.DataFrame.from_dict(unicode_data_dict) + + with self.create_app().app_context(): + database = get_example_database() + schema = { + "phrase": String(500), + "dttm": Date(), + "value": Float(), + } + obj = create_table_for_dashboard(df, tbl_name, database, schema) + obj.fetch_metadata() + + tbl = obj + slc = create_slice(tbl, None) + o = db.session.query(Slice).filter_by(slice_name=slc.slice_name).first() + if o: + db.session.delete(o) + db.session.add(slc) + db.session.commit() + create_dashboard("unicode-test", "Unicode Test", None, slc) + + @pytest.mark.usefixtures("load_unicode_dashboard") def test_multiple_table_filter_alters_another_tables_query(self): g.user = self.get_user( username="alpha" @@ -1165,3 +1218,45 @@ def test_rls_filter_doesnt_alter_admin_birth_names_query(self): assert not self.NAMES_B_REGEX.search(sql) assert not self.NAMES_Q_REGEX.search(sql) assert not self.BASE_FILTER_REGEX.search(sql) + + +def _get_position(): + return """{ + "CHART-Hkx6154FEm": { + "children": [], + "id": "CHART-Hkx6154FEm", + "meta": { + "chartId": 2225, + "height": 30, + "sliceName": "slice 1", + "width": 4 + }, + "type": "CHART" + }, + "GRID_ID": { + "children": [ + "ROW-SyT19EFEQ" + ], + "id": "GRID_ID", + "type": "GRID" + }, + "ROOT_ID": { + "children": [ + "GRID_ID" + ], + "id": "ROOT_ID", + "type": "ROOT" + }, + "ROW-SyT19EFEQ": { + "children": [ + "CHART-Hkx6154FEm" + ], + "id": "ROW-SyT19EFEQ", + "meta": { + "background": "BACKGROUND_TRANSPARENT" + }, + "type": "ROW" + }, + "DASHBOARD_VERSION_KEY": "v2" + } + """ diff --git a/tests/strategy_tests.py b/tests/strategy_tests.py index f8cea8be31a8..56d1e573b2bb 100644 --- a/tests/strategy_tests.py +++ b/tests/strategy_tests.py @@ -16,11 +16,19 @@ # under the License. # isort:skip_file """Unit tests for Superset cache warmup""" + import json from unittest.mock import MagicMock +from sqlalchemy import String, Date, Float + +import pytest +import pandas as pd + +from superset.models.slice import Slice +from superset.utils.core import get_example_database -import tests.test_app from superset import db + from superset.models.core import Log from superset.models.tags import get_tag, ObjectTypes, TaggedObject, TagTypes from superset.tasks.cache import ( @@ -30,6 +38,12 @@ ) from .base_tests import SupersetTestCase +from .dashboard_utils import ( + create_dashboard, + create_slice, + create_table_for_dashboard, + add_datetime_value_to_data, +) URL_PREFIX = "http://0.0.0.0:8081" @@ -193,6 +207,51 @@ def reset_tag(self, tag): db.session.delete(o) db.session.commit() + @pytest.fixture() + def load_unicode_dashboard(self): + data = [ + "Под", + "řšž", + "視野無限廣", + "微風", + "中国智造", + "æøå", + "ëœéè", + "いろはにほ", + "다람쥐", + "Чешће", + "ŕľšťýď", + "žšč", + "éúüñóá", + "كۆچەج", + ] + tbl_name = "unicode_test" + + # generate date/numeric data + unicode_data_dict = add_datetime_value_to_data(data) + df = pd.DataFrame.from_dict(unicode_data_dict) + + with self.create_app().app_context(): + database = get_example_database() + schema = { + "phrase": String(500), + "dttm": Date(), + "value": Float(), + } + obj = create_table_for_dashboard(df, tbl_name, database, schema) + obj.fetch_metadata() + + tbl = obj + slc = create_slice(tbl, None) + o = db.session.query(Slice).filter_by(slice_name=slc.slice_name).first() + if o: + db.session.delete(o) + db.session.add(slc) + + db.session.commit() + create_dashboard("unicode-test", "Unicode Test", None, slc) + + @pytest.mark.usefixtures("load_unicode_dashboard") def test_dashboard_tags(self): tag1 = get_tag("tag1", db.session, TagTypes.custom) # delete first to make test idempotent @@ -242,3 +301,45 @@ def test_dashboard_tags(self): result = sorted(strategy.get_urls()) expected = sorted(tag1_urls + tag2_urls) self.assertEqual(result, expected) + + +def _get_position(): + return """{ + "CHART-Hkx6154FEm": { + "children": [], + "id": "CHART-Hkx6154FEm", + "meta": { + "chartId": 2225, + "height": 30, + "sliceName": "slice 1", + "width": 4 + }, + "type": "CHART" + }, + "GRID_ID": { + "children": [ + "ROW-SyT19EFEQ" + ], + "id": "GRID_ID", + "type": "GRID" + }, + "ROOT_ID": { + "children": [ + "GRID_ID" + ], + "id": "ROOT_ID", + "type": "ROOT" + }, + "ROW-SyT19EFEQ": { + "children": [ + "CHART-Hkx6154FEm" + ], + "id": "ROW-SyT19EFEQ", + "meta": { + "background": "BACKGROUND_TRANSPARENT" + }, + "type": "ROW" + }, + "DASHBOARD_VERSION_KEY": "v2" + } + """ From 5ac088fae91a2d4cde0950944114bd2791a5cc7a Mon Sep 17 00:00:00 2001 From: Kasia Kucharczyk Date: Thu, 1 Oct 2020 20:41:36 +0200 Subject: [PATCH 04/13] Added cleanup for unicode_test table --- tests/conftest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/conftest.py b/tests/conftest.py index 4f7f988ab830..90044bb6c143 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -53,6 +53,7 @@ def setup_sample_data() -> Any: engine.execute("DROP TABLE energy_usage") engine.execute("DROP TABLE wb_health_population") engine.execute("DROP TABLE birth_names") + engine.execute("DROP TABLE IF EXISTS unicode_test") # drop sqlachemy tables From 8002941ca0d735bddaa10e5b3462d5965098cc25 Mon Sep 17 00:00:00 2001 From: Kasia Kucharczyk Date: Fri, 2 Oct 2020 14:59:59 +0200 Subject: [PATCH 05/13] Removed unnecessary fixture parts of unicode dashboard tests --- tests/dashboard_utils.py | 12 ----- tests/databases/api_tests.py | 73 +++++-------------------------- tests/security_tests.py | 72 +++++------------------------- tests/strategy_tests.py | 85 ++++++------------------------------ 4 files changed, 37 insertions(+), 205 deletions(-) diff --git a/tests/dashboard_utils.py b/tests/dashboard_utils.py index 498d81288fbe..d8bec4da649f 100644 --- a/tests/dashboard_utils.py +++ b/tests/dashboard_utils.py @@ -17,10 +17,8 @@ # isort:skip_file """Utils to provide dashboards for tests""" -import datetime import json -import random from pandas import DataFrame from typing import Dict, Any @@ -29,15 +27,6 @@ from superset.models.slice import Slice -def add_datetime_value_to_data(data): - data_dict = {} - for d in data: - data_dict["phrase"] = d - data_dict["dttm"] = datetime.datetime.now().date() - data_dict["value"] = [random.randint(1, 100) for _ in range(len(data))] - return data_dict - - def create_table_for_dashboard( df: DataFrame, tbl_name: str, database, schema: Dict[str, Any] ): @@ -55,7 +44,6 @@ def create_table_for_dashboard( obj = db.session.query(tbl_source).filter_by(table_name=tbl_name).first() if not obj: obj = tbl_source(table_name=tbl_name) - obj.main_dttm_col = "dttm" obj.database = database db.session.merge(obj) db.session.commit() diff --git a/tests/databases/api_tests.py b/tests/databases/api_tests.py index e00d32e3a100..082da1003fa5 100644 --- a/tests/databases/api_tests.py +++ b/tests/databases/api_tests.py @@ -16,12 +16,13 @@ # under the License. # isort:skip_file """Unit tests for Superset""" - +import datetime import json import pandas as pd import prison import pytest +import random from sqlalchemy import String, Date, Float from sqlalchemy.sql import func @@ -32,7 +33,6 @@ from superset.utils.core import get_example_database, get_main_database from tests.base_tests import SupersetTestCase from tests.dashboard_utils import ( - add_datetime_value_to_data, create_table_for_dashboard, create_dashboard, ) @@ -771,26 +771,19 @@ def test_test_connection_unsafe_uri(self): @pytest.fixture() def load_unicode_dashboard(self): data = [ - "Под", - "řšž", - "視野無限廣", - "微風", - "中国智造", - "æøå", - "ëœéè", - "いろはにほ", - "다람쥐", - "Чешће", - "ŕľšťýď", - "žšč", - "éúüñóá", - "كۆچەج", + {"phrase": "Под"}, + {"phrase": "řšž"}, + {"phrase": "視野無限廣"}, + {"phrase": "微風"}, + {"phrase": "中国智造"}, + {"phrase": "æøå"}, + {"phrase": "ëœéè"}, + {"phrase": "いろはにほ"}, ] tbl_name = "unicode_test" # generate date/numeric data - unicode_data_dict = add_datetime_value_to_data(data) - df = pd.DataFrame.from_dict(unicode_data_dict) + df = pd.DataFrame.from_dict(data) with self.create_app().app_context(): database = get_example_database() @@ -803,7 +796,7 @@ def load_unicode_dashboard(self): obj.fetch_metadata() db.session.commit() - position = _get_position() + position = "{}" create_dashboard("unicode-test", "Unicode Test", position, None) @pytest.mark.usefixtures("load_unicode_dashboard") @@ -838,45 +831,3 @@ def test_get_database_related_objects_not_found(self): uri = f"api/v1/database/{database.id}/related_objects/" rv = self.client.get(uri) self.assertEqual(rv.status_code, 404) - - -def _get_position(): - return """{ - "CHART-Hkx6154FEm": { - "children": [], - "id": "CHART-Hkx6154FEm", - "meta": { - "chartId": 2225, - "height": 30, - "sliceName": "slice 1", - "width": 4 - }, - "type": "CHART" - }, - "GRID_ID": { - "children": [ - "ROW-SyT19EFEQ" - ], - "id": "GRID_ID", - "type": "GRID" - }, - "ROOT_ID": { - "children": [ - "GRID_ID" - ], - "id": "ROOT_ID", - "type": "ROOT" - }, - "ROW-SyT19EFEQ": { - "children": [ - "CHART-Hkx6154FEm" - ], - "id": "ROW-SyT19EFEQ", - "meta": { - "background": "BACKGROUND_TRANSPARENT" - }, - "type": "ROW" - }, - "DASHBOARD_VERSION_KEY": "v2" - } - """ diff --git a/tests/security_tests.py b/tests/security_tests.py index 872866eff897..345a5047d684 100644 --- a/tests/security_tests.py +++ b/tests/security_tests.py @@ -15,6 +15,7 @@ # specific language governing permissions and limitations # under the License. # isort:skip_file +import datetime import inspect import re import unittest @@ -23,6 +24,7 @@ import pandas as pd import prison import pytest +import random from flask import current_app, g from sqlalchemy import Float, Date, String @@ -42,7 +44,6 @@ create_table_for_dashboard, create_slice, create_dashboard, - add_datetime_value_to_data, ) @@ -1134,33 +1135,24 @@ def test_rls_filter_doesnt_alter_energy_query(self): @pytest.fixture() def load_unicode_dashboard(self): data = [ - "Под", - "řšž", - "視野無限廣", - "微風", - "中国智造", - "æøå", - "ëœéè", - "いろはにほ", - "다람쥐", - "Чешће", - "ŕľšťýď", - "žšč", - "éúüñóá", - "كۆچەج", + {"phrase": "Под"}, + {"phrase": "řšž"}, + {"phrase": "視野無限廣"}, + {"phrase": "微風"}, + {"phrase": "中国智造"}, + {"phrase": "æøå"}, + {"phrase": "ëœéè"}, + {"phrase": "いろはにほ"}, ] tbl_name = "unicode_test" # generate date/numeric data - unicode_data_dict = add_datetime_value_to_data(data) - df = pd.DataFrame.from_dict(unicode_data_dict) + df = pd.DataFrame.from_dict(data) with self.create_app().app_context(): database = get_example_database() schema = { "phrase": String(500), - "dttm": Date(), - "value": Float(), } obj = create_table_for_dashboard(df, tbl_name, database, schema) obj.fetch_metadata() @@ -1218,45 +1210,3 @@ def test_rls_filter_doesnt_alter_admin_birth_names_query(self): assert not self.NAMES_B_REGEX.search(sql) assert not self.NAMES_Q_REGEX.search(sql) assert not self.BASE_FILTER_REGEX.search(sql) - - -def _get_position(): - return """{ - "CHART-Hkx6154FEm": { - "children": [], - "id": "CHART-Hkx6154FEm", - "meta": { - "chartId": 2225, - "height": 30, - "sliceName": "slice 1", - "width": 4 - }, - "type": "CHART" - }, - "GRID_ID": { - "children": [ - "ROW-SyT19EFEQ" - ], - "id": "GRID_ID", - "type": "GRID" - }, - "ROOT_ID": { - "children": [ - "GRID_ID" - ], - "id": "ROOT_ID", - "type": "ROOT" - }, - "ROW-SyT19EFEQ": { - "children": [ - "CHART-Hkx6154FEm" - ], - "id": "ROW-SyT19EFEQ", - "meta": { - "background": "BACKGROUND_TRANSPARENT" - }, - "type": "ROW" - }, - "DASHBOARD_VERSION_KEY": "v2" - } - """ diff --git a/tests/strategy_tests.py b/tests/strategy_tests.py index 56d1e573b2bb..62384d031e3c 100644 --- a/tests/strategy_tests.py +++ b/tests/strategy_tests.py @@ -16,9 +16,11 @@ # under the License. # isort:skip_file """Unit tests for Superset cache warmup""" - +import datetime import json from unittest.mock import MagicMock + +import random from sqlalchemy import String, Date, Float import pytest @@ -38,12 +40,7 @@ ) from .base_tests import SupersetTestCase -from .dashboard_utils import ( - create_dashboard, - create_slice, - create_table_for_dashboard, - add_datetime_value_to_data, -) +from .dashboard_utils import create_dashboard, create_slice, create_table_for_dashboard URL_PREFIX = "http://0.0.0.0:8081" @@ -210,34 +207,22 @@ def reset_tag(self, tag): @pytest.fixture() def load_unicode_dashboard(self): data = [ - "Под", - "řšž", - "視野無限廣", - "微風", - "中国智造", - "æøå", - "ëœéè", - "いろはにほ", - "다람쥐", - "Чешће", - "ŕľšťýď", - "žšč", - "éúüñóá", - "كۆچەج", + {"phrase": "Под"}, + {"phrase": "řšž"}, + {"phrase": "視野無限廣"}, + {"phrase": "微風"}, + {"phrase": "中国智造"}, + {"phrase": "æøå"}, + {"phrase": "ëœéè"}, + {"phrase": "いろはにほ"}, ] tbl_name = "unicode_test" - # generate date/numeric data - unicode_data_dict = add_datetime_value_to_data(data) - df = pd.DataFrame.from_dict(unicode_data_dict) + df = pd.DataFrame.from_dict(data) with self.create_app().app_context(): database = get_example_database() - schema = { - "phrase": String(500), - "dttm": Date(), - "value": Float(), - } + schema = {"phrase": String(500)} obj = create_table_for_dashboard(df, tbl_name, database, schema) obj.fetch_metadata() @@ -301,45 +286,3 @@ def test_dashboard_tags(self): result = sorted(strategy.get_urls()) expected = sorted(tag1_urls + tag2_urls) self.assertEqual(result, expected) - - -def _get_position(): - return """{ - "CHART-Hkx6154FEm": { - "children": [], - "id": "CHART-Hkx6154FEm", - "meta": { - "chartId": 2225, - "height": 30, - "sliceName": "slice 1", - "width": 4 - }, - "type": "CHART" - }, - "GRID_ID": { - "children": [ - "ROW-SyT19EFEQ" - ], - "id": "GRID_ID", - "type": "GRID" - }, - "ROOT_ID": { - "children": [ - "GRID_ID" - ], - "id": "ROOT_ID", - "type": "ROOT" - }, - "ROW-SyT19EFEQ": { - "children": [ - "CHART-Hkx6154FEm" - ], - "id": "ROW-SyT19EFEQ", - "meta": { - "background": "BACKGROUND_TRANSPARENT" - }, - "type": "ROW" - }, - "DASHBOARD_VERSION_KEY": "v2" - } - """ From 773898bf3842037e21414d3f5cca68268e6b45e0 Mon Sep 17 00:00:00 2001 From: Kasia Kucharczyk Date: Fri, 2 Oct 2020 17:30:30 +0200 Subject: [PATCH 06/13] Parametrized creating slice for tests --- tests/dashboard_utils.py | 6 +++--- tests/security_tests.py | 2 +- tests/strategy_tests.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/dashboard_utils.py b/tests/dashboard_utils.py index d8bec4da649f..e896b300b30a 100644 --- a/tests/dashboard_utils.py +++ b/tests/dashboard_utils.py @@ -51,10 +51,10 @@ def create_table_for_dashboard( return obj -def create_slice(tbl, slices_dict: Dict[str, str]): +def create_slice(title, viz_type, tbl, slices_dict: Dict[str, str]): return Slice( - slice_name="Unicode Cloud", - viz_type="word_cloud", + slice_name=title, + viz_type=viz_type, datasource_type="table", datasource_id=tbl.id, params=json.dumps(slices_dict, indent=4, sort_keys=True), diff --git a/tests/security_tests.py b/tests/security_tests.py index 345a5047d684..27d51defbc85 100644 --- a/tests/security_tests.py +++ b/tests/security_tests.py @@ -1158,7 +1158,7 @@ def load_unicode_dashboard(self): obj.fetch_metadata() tbl = obj - slc = create_slice(tbl, None) + slc = create_slice("Unicode Cloud", "word_cloud", tbl, None) o = db.session.query(Slice).filter_by(slice_name=slc.slice_name).first() if o: db.session.delete(o) diff --git a/tests/strategy_tests.py b/tests/strategy_tests.py index 62384d031e3c..7fcffbbbdb7f 100644 --- a/tests/strategy_tests.py +++ b/tests/strategy_tests.py @@ -227,7 +227,7 @@ def load_unicode_dashboard(self): obj.fetch_metadata() tbl = obj - slc = create_slice(tbl, None) + slc = create_slice("Unicode Cloud", "word_cloud", tbl, None) o = db.session.query(Slice).filter_by(slice_name=slc.slice_name).first() if o: db.session.delete(o) From 21e5fe51549b94cddc70380a7bcb154c0d376012 Mon Sep 17 00:00:00 2001 From: Kasia Kucharczyk Date: Mon, 5 Oct 2020 18:49:17 +0200 Subject: [PATCH 07/13] Moved fixtures for unicode test to separate file and refactored to several methods. Added param types and return types. --- tests/conftest.py | 1 - tests/dashboard_utils.py | 18 +++-- tests/databases/api_tests.py | 34 +-------- tests/fixtures/unicode_dashboard.py | 106 ++++++++++++++++++++++++++++ tests/security_tests.py | 37 +--------- tests/strategy_tests.py | 36 +--------- 6 files changed, 123 insertions(+), 109 deletions(-) create mode 100644 tests/fixtures/unicode_dashboard.py diff --git a/tests/conftest.py b/tests/conftest.py index 90044bb6c143..4f7f988ab830 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -53,7 +53,6 @@ def setup_sample_data() -> Any: engine.execute("DROP TABLE energy_usage") engine.execute("DROP TABLE wb_health_population") engine.execute("DROP TABLE birth_names") - engine.execute("DROP TABLE IF EXISTS unicode_test") # drop sqlachemy tables diff --git a/tests/dashboard_utils.py b/tests/dashboard_utils.py index e896b300b30a..fc4f38bbcb92 100644 --- a/tests/dashboard_utils.py +++ b/tests/dashboard_utils.py @@ -14,22 +14,23 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -# isort:skip_file """Utils to provide dashboards for tests""" import json +from typing import Any, Dict from pandas import DataFrame -from typing import Dict, Any -from superset import db, ConnectorRegistry +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, schema: Dict[str, Any] -): + df: DataFrame, tbl_name: str, database: "Database", schema: Dict[str, Any] +) -> "SqlaTable": df.to_sql( tbl_name, database.get_sqla_engine(), @@ -51,7 +52,9 @@ def create_table_for_dashboard( return obj -def create_slice(title, viz_type, tbl, slices_dict: Dict[str, str]): +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, @@ -61,7 +64,7 @@ def create_slice(title, viz_type, tbl, slices_dict: Dict[str, str]): ) -def create_dashboard(slug: str, title: str, position: str, slc: Slice): +def create_dashboard(slug: str, title: str, position: str, slc: Slice) -> Dashboard: dash = db.session.query(Dashboard).filter_by(slug=slug).first() if not dash: @@ -76,3 +79,4 @@ def create_dashboard(slug: str, title: str, position: str, slc: Slice): dash.slices = [slc] db.session.merge(dash) db.session.commit() + return dash diff --git a/tests/databases/api_tests.py b/tests/databases/api_tests.py index 082da1003fa5..b0f853b6d288 100644 --- a/tests/databases/api_tests.py +++ b/tests/databases/api_tests.py @@ -37,6 +37,7 @@ create_dashboard, ) from tests.fixtures.certificates import ssl_certificate +from tests.fixtures.unicode_dashboard import load_unicode_dashboard_with_position from tests.test_app import app @@ -768,38 +769,7 @@ def test_test_connection_unsafe_uri(self): } self.assertEqual(response, expected_response) - @pytest.fixture() - def load_unicode_dashboard(self): - data = [ - {"phrase": "Под"}, - {"phrase": "řšž"}, - {"phrase": "視野無限廣"}, - {"phrase": "微風"}, - {"phrase": "中国智造"}, - {"phrase": "æøå"}, - {"phrase": "ëœéè"}, - {"phrase": "いろはにほ"}, - ] - tbl_name = "unicode_test" - - # generate date/numeric data - df = pd.DataFrame.from_dict(data) - - with self.create_app().app_context(): - database = get_example_database() - schema = { - "phrase": String(500), - "dttm": Date(), - "value": Float(), - } - obj = create_table_for_dashboard(df, tbl_name, database, schema) - obj.fetch_metadata() - - db.session.commit() - position = "{}" - create_dashboard("unicode-test", "Unicode Test", position, None) - - @pytest.mark.usefixtures("load_unicode_dashboard") + @pytest.mark.usefixtures("load_unicode_dashboard_with_position") def test_get_database_related_objects(self): """ Database API: Test get chart and dashboard count related to a database diff --git a/tests/fixtures/unicode_dashboard.py b/tests/fixtures/unicode_dashboard.py new file mode 100644 index 000000000000..bd8c4ed1f799 --- /dev/null +++ b/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 + + +@pytest.fixture() +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() + + +@pytest.fixture() +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 +) -> Dashboard: + database = get_example_database() + schema = { + "phrase": String(500), + } + obj = create_table_for_dashboard(df, tbl_name, database, schema) + obj.fetch_metadata() + + tbl = obj + + if slc_title: + slc = _create_and_commit_unicode_slice(tbl, slc_title) + + return create_dashboard("unicode-test", "Unicode Test", position, slc) + + +def _create_and_commit_unicode_slice(tbl: SqlaTable, title: str): + slc = create_slice(title, "word_cloud", tbl, {}) + o = db.session.query(Slice).filter_by(slice_name=slc.slice_name).first() + if o: + db.session.delete(o) + db.session.add(slc) + db.session.commit() + return slc + + +def _cleanup() -> None: + engine = get_example_database().get_sqla_engine() + engine.execute("DROP TABLE IF EXISTS unicode_test") + db.session.commit() diff --git a/tests/security_tests.py b/tests/security_tests.py index 27d51defbc85..c8e3eeacb550 100644 --- a/tests/security_tests.py +++ b/tests/security_tests.py @@ -45,6 +45,7 @@ create_slice, create_dashboard, ) +from .fixtures.unicode_dashboard import load_unicode_dashboard_with_slice def get_perm_tuples(role_name): @@ -1132,41 +1133,7 @@ def test_rls_filter_doesnt_alter_energy_query(self): assert tbl.get_extra_cache_keys(self.query_obj) == [] assert "value > 1" not in sql - @pytest.fixture() - def load_unicode_dashboard(self): - data = [ - {"phrase": "Под"}, - {"phrase": "řšž"}, - {"phrase": "視野無限廣"}, - {"phrase": "微風"}, - {"phrase": "中国智造"}, - {"phrase": "æøå"}, - {"phrase": "ëœéè"}, - {"phrase": "いろはにほ"}, - ] - tbl_name = "unicode_test" - - # generate date/numeric data - df = pd.DataFrame.from_dict(data) - - with self.create_app().app_context(): - database = get_example_database() - schema = { - "phrase": String(500), - } - obj = create_table_for_dashboard(df, tbl_name, database, schema) - obj.fetch_metadata() - - tbl = obj - slc = create_slice("Unicode Cloud", "word_cloud", tbl, None) - o = db.session.query(Slice).filter_by(slice_name=slc.slice_name).first() - if o: - db.session.delete(o) - db.session.add(slc) - db.session.commit() - create_dashboard("unicode-test", "Unicode Test", None, slc) - - @pytest.mark.usefixtures("load_unicode_dashboard") + @pytest.mark.usefixtures("load_unicode_dashboard_with_slice") def test_multiple_table_filter_alters_another_tables_query(self): g.user = self.get_user( username="alpha" diff --git a/tests/strategy_tests.py b/tests/strategy_tests.py index 7fcffbbbdb7f..29f736c98974 100644 --- a/tests/strategy_tests.py +++ b/tests/strategy_tests.py @@ -20,7 +20,6 @@ import json from unittest.mock import MagicMock -import random from sqlalchemy import String, Date, Float import pytest @@ -41,6 +40,7 @@ from .base_tests import SupersetTestCase from .dashboard_utils import create_dashboard, create_slice, create_table_for_dashboard +from .fixtures.unicode_dashboard import load_unicode_dashboard_with_slice URL_PREFIX = "http://0.0.0.0:8081" @@ -204,39 +204,7 @@ def reset_tag(self, tag): db.session.delete(o) db.session.commit() - @pytest.fixture() - def load_unicode_dashboard(self): - data = [ - {"phrase": "Под"}, - {"phrase": "řšž"}, - {"phrase": "視野無限廣"}, - {"phrase": "微風"}, - {"phrase": "中国智造"}, - {"phrase": "æøå"}, - {"phrase": "ëœéè"}, - {"phrase": "いろはにほ"}, - ] - tbl_name = "unicode_test" - - df = pd.DataFrame.from_dict(data) - - with self.create_app().app_context(): - database = get_example_database() - schema = {"phrase": String(500)} - obj = create_table_for_dashboard(df, tbl_name, database, schema) - obj.fetch_metadata() - - tbl = obj - slc = create_slice("Unicode Cloud", "word_cloud", tbl, None) - o = db.session.query(Slice).filter_by(slice_name=slc.slice_name).first() - if o: - db.session.delete(o) - db.session.add(slc) - - db.session.commit() - create_dashboard("unicode-test", "Unicode Test", None, slc) - - @pytest.mark.usefixtures("load_unicode_dashboard") + @pytest.mark.usefixtures("load_unicode_dashboard_with_slice") def test_dashboard_tags(self): tag1 = get_tag("tag1", db.session, TagTypes.custom) # delete first to make test idempotent From 2f9e21a06fccc3348dee1818303bcf01440957aa Mon Sep 17 00:00:00 2001 From: Kasia Kucharczyk Date: Mon, 5 Oct 2020 18:56:50 +0200 Subject: [PATCH 08/13] Cleandup after fix --- tests/dashboard_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/dashboard_utils.py b/tests/dashboard_utils.py index fc4f38bbcb92..fa10967bd545 100644 --- a/tests/dashboard_utils.py +++ b/tests/dashboard_utils.py @@ -29,8 +29,8 @@ def create_table_for_dashboard( - df: DataFrame, tbl_name: str, database: "Database", schema: Dict[str, Any] -) -> "SqlaTable": + df: DataFrame, tbl_name: str, database: Database, schema: Dict[str, Any] +) -> SqlaTable: df.to_sql( tbl_name, database.get_sqla_engine(), From c04ce35ecc4c08726bc968f273500c569d3cc3bd Mon Sep 17 00:00:00 2001 From: Kasia Kucharczyk Date: Tue, 6 Oct 2020 13:09:19 +0200 Subject: [PATCH 09/13] Changed variable names to more readable --- tests/dashboard_utils.py | 28 ++++++++++++------------- tests/fixtures/unicode_dashboard.py | 32 ++++++++++++++--------------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/tests/dashboard_utils.py b/tests/dashboard_utils.py index fa10967bd545..dd961dbd981a 100644 --- a/tests/dashboard_utils.py +++ b/tests/dashboard_utils.py @@ -29,10 +29,10 @@ def create_table_for_dashboard( - df: DataFrame, tbl_name: str, database: Database, schema: Dict[str, Any] + df: DataFrame, table_name: str, database: Database, schema: Dict[str, Any] ) -> SqlaTable: df.to_sql( - tbl_name, + table_name, database.get_sqla_engine(), if_exists="replace", chunksize=500, @@ -41,30 +41,30 @@ def create_table_for_dashboard( 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) + table_source = ConnectorRegistry.sources["table"] + table = db.session.query(table_source).filter_by(table_name=table_name).first() + if not table: + table = table_source(table_name=table_name) + table.database = database + db.session.merge(table) db.session.commit() - return obj + return table def create_slice( - title: str, viz_type: str, tbl: SqlaTable, slices_dict: Dict[str, str] + title: str, viz_type: str, table: SqlaTable, slices_dict: Dict[str, str] ) -> Slice: return Slice( slice_name=title, viz_type=viz_type, datasource_type="table", - datasource_id=tbl.id, + datasource_id=table.id, params=json.dumps(slices_dict, indent=4, sort_keys=True), ) -def create_dashboard(slug: str, title: str, position: str, slc: Slice) -> Dashboard: +def create_dashboard(slug: str, title: str, position: str, slice: Slice) -> Dashboard: dash = db.session.query(Dashboard).filter_by(slug=slug).first() if not dash: @@ -75,8 +75,8 @@ def create_dashboard(slug: str, title: str, position: str, slc: Slice) -> Dashbo pos = json.loads(js) dash.position_json = json.dumps(pos, indent=4) dash.slug = slug - if slc is not None: - dash.slices = [slc] + if slice is not None: + dash.slices = [slice] db.session.merge(dash) db.session.commit() return dash diff --git a/tests/fixtures/unicode_dashboard.py b/tests/fixtures/unicode_dashboard.py index bd8c4ed1f799..2382a3f91957 100644 --- a/tests/fixtures/unicode_dashboard.py +++ b/tests/fixtures/unicode_dashboard.py @@ -35,21 +35,21 @@ @pytest.fixture() def load_unicode_dashboard_with_slice(): - tbl_name = "unicode_test" + table_name = "unicode_test" df = _get_dataframe() with app.app_context(): - yield _create_unicode_dashboard(df, tbl_name, "Unicode Cloud", None) + yield _create_unicode_dashboard(df, table_name, "Unicode Cloud", None) _cleanup() @pytest.fixture() def load_unicode_dashboard_with_position(): - tbl_name = "unicode_test" + table_name = "unicode_test" df = _get_dataframe() position = "{}" with app.app_context(): - yield _create_unicode_dashboard(df, tbl_name, "Unicode Cloud", position) + yield _create_unicode_dashboard(df, table_name, "Unicode Cloud", position) _cleanup() @@ -73,31 +73,29 @@ def _get_unicode_data(): def _create_unicode_dashboard( - df: DataFrame, tbl_name: str, slc_title: str, position: str + df: DataFrame, table_name: str, slice_title: str, position: str ) -> Dashboard: database = get_example_database() schema = { "phrase": String(500), } - obj = create_table_for_dashboard(df, tbl_name, database, schema) - obj.fetch_metadata() + table = create_table_for_dashboard(df, table_name, database, schema) + table.fetch_metadata() - tbl = obj + if slice_title: + slice = _create_and_commit_unicode_slice(table, slice_title) - if slc_title: - slc = _create_and_commit_unicode_slice(tbl, slc_title) + return create_dashboard("unicode-test", "Unicode Test", position, slice) - return create_dashboard("unicode-test", "Unicode Test", position, slc) - -def _create_and_commit_unicode_slice(tbl: SqlaTable, title: str): - slc = create_slice(title, "word_cloud", tbl, {}) - o = db.session.query(Slice).filter_by(slice_name=slc.slice_name).first() +def _create_and_commit_unicode_slice(table: SqlaTable, title: str): + slice = create_slice(title, "word_cloud", table, {}) + o = db.session.query(Slice).filter_by(slice_name=slice.slice_name).first() if o: db.session.delete(o) - db.session.add(slc) + db.session.add(slice) db.session.commit() - return slc + return slice def _cleanup() -> None: From 7c0790745164896f2b997ef8857e7b9bbbf29c1f Mon Sep 17 00:00:00 2001 From: Kasia Kucharczyk Date: Tue, 6 Oct 2020 19:24:37 +0200 Subject: [PATCH 10/13] Added cleanup for dashboards and slices --- tests/dashboard_utils.py | 3 ++- tests/fixtures/unicode_dashboard.py | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/tests/dashboard_utils.py b/tests/dashboard_utils.py index dd961dbd981a..de9ffbb282af 100644 --- a/tests/dashboard_utils.py +++ b/tests/dashboard_utils.py @@ -77,6 +77,7 @@ def create_dashboard(slug: str, title: str, position: str, slice: Slice) -> Dash dash.slug = slug if slice is not None: dash.slices = [slice] - db.session.merge(dash) + db.session.merge(dash) db.session.commit() + return dash diff --git a/tests/fixtures/unicode_dashboard.py b/tests/fixtures/unicode_dashboard.py index 2382a3f91957..b0495117577c 100644 --- a/tests/fixtures/unicode_dashboard.py +++ b/tests/fixtures/unicode_dashboard.py @@ -18,7 +18,6 @@ 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 @@ -36,22 +35,25 @@ @pytest.fixture() def load_unicode_dashboard_with_slice(): table_name = "unicode_test" + slice_name = "Unicode Cloud" df = _get_dataframe() with app.app_context(): - yield _create_unicode_dashboard(df, table_name, "Unicode Cloud", None) + dash = _create_unicode_dashboard(df, table_name, slice_name, None) + yield - _cleanup() + _cleanup(dash, slice_name) @pytest.fixture() def load_unicode_dashboard_with_position(): table_name = "unicode_test" + slice_name = "Unicode Cloud" df = _get_dataframe() position = "{}" with app.app_context(): - yield _create_unicode_dashboard(df, table_name, "Unicode Cloud", position) - - _cleanup() + dash = _create_unicode_dashboard(df, table_name, slice_name, position) + yield + _cleanup(dash, slice_name) def _get_dataframe(): @@ -98,7 +100,11 @@ def _create_and_commit_unicode_slice(table: SqlaTable, title: str): return slice -def _cleanup() -> None: +def _cleanup(dash: Dashboard, slice_name: str) -> None: engine = get_example_database().get_sqla_engine() engine.execute("DROP TABLE IF EXISTS unicode_test") + db.session.delete(dash) + if slice_name: + slice = db.session.query(Slice).filter_by(slice_name=slice_name).first() + db.session.delete(slice) db.session.commit() From 8f5ec978779f9b33e10e481a7043bd5d150ce895 Mon Sep 17 00:00:00 2001 From: Kasia Kucharczyk Date: Wed, 7 Oct 2020 10:33:40 +0200 Subject: [PATCH 11/13] Applied unicode fixture to charts api tests --- tests/charts/api_tests.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/charts/api_tests.py b/tests/charts/api_tests.py index 9e6dca780504..35fe93942a8c 100644 --- a/tests/charts/api_tests.py +++ b/tests/charts/api_tests.py @@ -28,6 +28,7 @@ from sqlalchemy.sql import func from superset.utils.core import get_example_database +from tests.fixtures.unicode_dashboard import load_unicode_dashboard_with_slice from tests.test_app import app from superset.connectors.connector_registry import ConnectorRegistry from superset.extensions import db, security_manager @@ -567,6 +568,7 @@ def test_get_chart_no_data_access(self): rv = self.client.get(uri) self.assertEqual(rv.status_code, 404) + @pytest.mark.usefixtures("load_unicode_dashboard_with_slice") def test_get_charts(self): """ Chart API: Test get charts @@ -733,6 +735,7 @@ def test_get_charts_favorite_filter(self): assert rv.status_code == 200 assert len(expected_models) == data["count"] + @pytest.mark.usefixtures("load_unicode_dashboard_with_slice") def test_get_charts_page(self): """ Chart API: Test get charts filter From c4537ff755b63845a5324b1108195d1663b925a6 Mon Sep 17 00:00:00 2001 From: Kasia Kucharczyk <2536609+kkucharc@users.noreply.github.com> Date: Wed, 7 Oct 2020 15:44:06 +0200 Subject: [PATCH 12/13] Update schema variable to dtype in dashboard utils Co-authored-by: Ville Brofeldt <33317356+villebro@users.noreply.github.com> --- tests/dashboard_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/dashboard_utils.py b/tests/dashboard_utils.py index de9ffbb282af..147e20b4624a 100644 --- a/tests/dashboard_utils.py +++ b/tests/dashboard_utils.py @@ -29,7 +29,7 @@ def create_table_for_dashboard( - df: DataFrame, table_name: str, database: Database, schema: Dict[str, Any] + df: DataFrame, table_name: str, database: Database, dtype: Dict[str, Any] ) -> SqlaTable: df.to_sql( table_name, From 7c78f7a900a6dd8b700eb757c5fd5e724665a409 Mon Sep 17 00:00:00 2001 From: Kasia Kucharczyk Date: Wed, 7 Oct 2020 16:29:06 +0200 Subject: [PATCH 13/13] Changed variable schema to dtype in dashboards. Replaced accessing first element with one_or_none --- tests/dashboard_utils.py | 8 +++++--- tests/fixtures/unicode_dashboard.py | 8 ++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/dashboard_utils.py b/tests/dashboard_utils.py index 147e20b4624a..d94c5d4003fc 100644 --- a/tests/dashboard_utils.py +++ b/tests/dashboard_utils.py @@ -36,13 +36,15 @@ def create_table_for_dashboard( database.get_sqla_engine(), if_exists="replace", chunksize=500, - dtype=schema, + dtype=dtype, index=False, method="multi", ) table_source = ConnectorRegistry.sources["table"] - table = db.session.query(table_source).filter_by(table_name=table_name).first() + table = ( + db.session.query(table_source).filter_by(table_name=table_name).one_or_none() + ) if not table: table = table_source(table_name=table_name) table.database = database @@ -65,7 +67,7 @@ def create_slice( def create_dashboard(slug: str, title: str, position: str, slice: Slice) -> Dashboard: - dash = db.session.query(Dashboard).filter_by(slug=slug).first() + dash = db.session.query(Dashboard).filter_by(slug=slug).one_or_none() if not dash: dash = Dashboard() diff --git a/tests/fixtures/unicode_dashboard.py b/tests/fixtures/unicode_dashboard.py index b0495117577c..13937578649e 100644 --- a/tests/fixtures/unicode_dashboard.py +++ b/tests/fixtures/unicode_dashboard.py @@ -78,10 +78,10 @@ def _create_unicode_dashboard( df: DataFrame, table_name: str, slice_title: str, position: str ) -> Dashboard: database = get_example_database() - schema = { + dtype = { "phrase": String(500), } - table = create_table_for_dashboard(df, table_name, database, schema) + table = create_table_for_dashboard(df, table_name, database, dtype) table.fetch_metadata() if slice_title: @@ -92,7 +92,7 @@ def _create_unicode_dashboard( def _create_and_commit_unicode_slice(table: SqlaTable, title: str): slice = create_slice(title, "word_cloud", table, {}) - o = db.session.query(Slice).filter_by(slice_name=slice.slice_name).first() + o = db.session.query(Slice).filter_by(slice_name=slice.slice_name).one_or_none() if o: db.session.delete(o) db.session.add(slice) @@ -105,6 +105,6 @@ def _cleanup(dash: Dashboard, slice_name: str) -> None: engine.execute("DROP TABLE IF EXISTS unicode_test") db.session.delete(dash) if slice_name: - slice = db.session.query(Slice).filter_by(slice_name=slice_name).first() + slice = db.session.query(Slice).filter_by(slice_name=slice_name).one_or_none() db.session.delete(slice) db.session.commit()