From 5050ad04f4d5a8ba51755d24dee0a1cc8eab7a01 Mon Sep 17 00:00:00 2001 From: Cor Zuurmond Date: Fri, 15 Nov 2024 10:09:21 +0100 Subject: [PATCH 1/6] Make create parameters positional --- tests/integration/test_dashboards.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_dashboards.py b/tests/integration/test_dashboards.py index b873f8d1..975d803a 100644 --- a/tests/integration/test_dashboards.py +++ b/tests/integration/test_dashboards.py @@ -52,7 +52,7 @@ def inner(**kwargs): def make_dashboard(ws, make_random): """Clean the lakeview dashboard""" - def create(display_name: str = "") -> SDKDashboard: + def create(*, display_name: str = "") -> SDKDashboard: if len(display_name) == 0: display_name = f"created_by_lsql_{make_random()}" else: From b2a97c6141d2e001cb2c15d51a90bb360db5ef0c Mon Sep 17 00:00:00 2001 From: Cor Zuurmond Date: Fri, 15 Nov 2024 10:11:52 +0100 Subject: [PATCH 2/6] Create dashboard using Dashboard object --- tests/integration/test_dashboards.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/test_dashboards.py b/tests/integration/test_dashboards.py index 975d803a..1ae760ff 100644 --- a/tests/integration/test_dashboards.py +++ b/tests/integration/test_dashboards.py @@ -57,7 +57,7 @@ def create(*, display_name: str = "") -> SDKDashboard: display_name = f"created_by_lsql_{make_random()}" else: display_name = f"{display_name} ({make_random()})" - dashboard = ws.lakeview.create(display_name) + dashboard = ws.lakeview.create(dashboard=SDKDashboard(display_name=display_name)) if is_in_debug(): dashboard_url = f"{ws.config.host}/sql/dashboardsv3/{dashboard.dashboard_id}" webbrowser.open(dashboard_url) From 656faac712059c1722a6c0665e331f33db822582 Mon Sep 17 00:00:00 2001 From: Cor Zuurmond Date: Fri, 15 Nov 2024 10:19:27 +0100 Subject: [PATCH 3/6] Fix test using Lakeview API --- tests/integration/test_dashboards.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/integration/test_dashboards.py b/tests/integration/test_dashboards.py index 1ae760ff..4fbdf867 100644 --- a/tests/integration/test_dashboards.py +++ b/tests/integration/test_dashboards.py @@ -1,3 +1,4 @@ +import dataclasses import datetime as dt import json import logging @@ -110,12 +111,13 @@ def tmp_path(tmp_path, make_random): return folder -def test_dashboards_creates_exported_dashboard_definition(ws, make_dashboard): +def test_dashboards_creates_exported_dashboard_definition(ws, make_dashboard) -> None: dashboards = Dashboards(ws) sdk_dashboard = make_dashboard() dashboard_content = (Path(__file__).parent / "dashboards" / "dashboard.lvdash.json").read_text() - ws.lakeview.update(sdk_dashboard.dashboard_id, serialized_dashboard=dashboard_content) + dashboard_to_create = dataclasses.replace(sdk_dashboard, serialized_dashboard=dashboard_content) + ws.lakeview.update(sdk_dashboard.dashboard_id, dashboard=dashboard_to_create) lakeview_dashboard = Dashboard.from_dict(json.loads(dashboard_content)) new_dashboard = dashboards.get_dashboard(sdk_dashboard.path) From a1a50ad80b6e256d321fc3edef6ade3086f1b687 Mon Sep 17 00:00:00 2001 From: Cor Zuurmond Date: Fri, 15 Nov 2024 10:20:07 +0100 Subject: [PATCH 4/6] Use dashboard object when working with Lakeview API --- src/databricks/labs/lsql/dashboards.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/databricks/labs/lsql/dashboards.py b/src/databricks/labs/lsql/dashboards.py index c31754c9..3222ed5b 100644 --- a/src/databricks/labs/lsql/dashboards.py +++ b/src/databricks/labs/lsql/dashboards.py @@ -1125,20 +1125,17 @@ def create_dashboard( """ dashboard_metadata.validate() serialized_dashboard = json.dumps(dashboard_metadata.as_lakeview().as_dict()) + dashboard_to_create = SDKDashboard( + dashboard_id=dashboard_id, + display_name=dashboard_metadata.display_name, + parent_path=parent_path, + serialized_dashboard=serialized_dashboard, + warehouse_id=warehouse_id, + ) if dashboard_id is not None: - sdk_dashboard = self._ws.lakeview.update( - dashboard_id, - display_name=dashboard_metadata.display_name, - serialized_dashboard=serialized_dashboard, - warehouse_id=warehouse_id, - ) + sdk_dashboard = self._ws.lakeview.update(dashboard_id, dashboard=dashboard_to_create) else: - sdk_dashboard = self._ws.lakeview.create( - dashboard_metadata.display_name, - parent_path=parent_path, - serialized_dashboard=serialized_dashboard, - warehouse_id=warehouse_id, - ) + sdk_dashboard = self._ws.lakeview.create(dashboard=dashboard_to_create) if publish: assert sdk_dashboard.dashboard_id is not None self._ws.lakeview.publish(sdk_dashboard.dashboard_id, warehouse_id=warehouse_id) From ffbf0e36ac2d20a3d012331bf2446175be68a45c Mon Sep 17 00:00:00 2001 From: Cor Zuurmond Date: Fri, 15 Nov 2024 11:13:53 +0100 Subject: [PATCH 5/6] Convert Dashboard to dicts before passing them to Lakeview API TODO https://github.com/databrickslabs/lsql/issues/321 --- src/databricks/labs/lsql/dashboards.py | 4 ++-- tests/integration/test_dashboards.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/databricks/labs/lsql/dashboards.py b/src/databricks/labs/lsql/dashboards.py index 3222ed5b..091f00bf 100644 --- a/src/databricks/labs/lsql/dashboards.py +++ b/src/databricks/labs/lsql/dashboards.py @@ -1133,9 +1133,9 @@ def create_dashboard( warehouse_id=warehouse_id, ) if dashboard_id is not None: - sdk_dashboard = self._ws.lakeview.update(dashboard_id, dashboard=dashboard_to_create) + sdk_dashboard = self._ws.lakeview.update(dashboard_id, dashboard=dashboard_to_create.as_dict()) # type: ignore else: - sdk_dashboard = self._ws.lakeview.create(dashboard=dashboard_to_create) + sdk_dashboard = self._ws.lakeview.create(dashboard=dashboard_to_create.as_dict()) # type: ignore if publish: assert sdk_dashboard.dashboard_id is not None self._ws.lakeview.publish(sdk_dashboard.dashboard_id, warehouse_id=warehouse_id) diff --git a/tests/integration/test_dashboards.py b/tests/integration/test_dashboards.py index 4fbdf867..f2eae100 100644 --- a/tests/integration/test_dashboards.py +++ b/tests/integration/test_dashboards.py @@ -58,7 +58,7 @@ def create(*, display_name: str = "") -> SDKDashboard: display_name = f"created_by_lsql_{make_random()}" else: display_name = f"{display_name} ({make_random()})" - dashboard = ws.lakeview.create(dashboard=SDKDashboard(display_name=display_name)) + dashboard = ws.lakeview.create(dashboard=SDKDashboard(display_name=display_name).as_dict()) if is_in_debug(): dashboard_url = f"{ws.config.host}/sql/dashboardsv3/{dashboard.dashboard_id}" webbrowser.open(dashboard_url) @@ -117,7 +117,7 @@ def test_dashboards_creates_exported_dashboard_definition(ws, make_dashboard) -> dashboard_content = (Path(__file__).parent / "dashboards" / "dashboard.lvdash.json").read_text() dashboard_to_create = dataclasses.replace(sdk_dashboard, serialized_dashboard=dashboard_content) - ws.lakeview.update(sdk_dashboard.dashboard_id, dashboard=dashboard_to_create) + ws.lakeview.update(sdk_dashboard.dashboard_id, dashboard=dashboard_to_create.as_dict()) lakeview_dashboard = Dashboard.from_dict(json.loads(dashboard_content)) new_dashboard = dashboards.get_dashboard(sdk_dashboard.path) From 7efd31ae29c4505ac50b718cf947fc78f4256f0d Mon Sep 17 00:00:00 2001 From: Cor Zuurmond Date: Fri, 15 Nov 2024 11:18:17 +0100 Subject: [PATCH 6/6] Update Lakeview API calls tests --- tests/unit/test_dashboards.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/tests/unit/test_dashboards.py b/tests/unit/test_dashboards.py index 22e55326..4a1064ec 100644 --- a/tests/unit/test_dashboards.py +++ b/tests/unit/test_dashboards.py @@ -1464,24 +1464,33 @@ def test_dashboards_saves_markdown_files_to_folder(tmp_path): ws.assert_not_called() -def test_dashboards_calls_create_without_dashboard_id(): +def test_dashboards_calls_create_without_dashboard_id() -> None: + sdk_dashboard = SDKDashboard( + dashboard_id=None, + display_name="test", + parent_path="/non/existing/path", + serialized_dashboard=json.dumps({"pages": [{"displayName": "test", "name": "test"}]}), + warehouse_id="warehouse", + ) ws = create_autospec(WorkspaceClient) dashboards = Dashboards(ws) dashboard_metadata = DashboardMetadata("test") dashboards.create_dashboard(dashboard_metadata, parent_path="/non/existing/path", warehouse_id="warehouse") - ws.lakeview.create.assert_called_with( - "test", - parent_path="/non/existing/path", - serialized_dashboard=json.dumps({"pages": [{"displayName": "test", "name": "test"}]}), - warehouse_id="warehouse", - ) + ws.lakeview.create.assert_called_with(dashboard=sdk_dashboard.as_dict()) ws.lakeview.update.assert_not_called() ws.lakeview.publish.assert_not_called() -def test_dashboards_calls_update_with_dashboard_id(): +def test_dashboards_calls_update_with_dashboard_id() -> None: + sdk_dashboard = SDKDashboard( + dashboard_id="id", + display_name="test", + parent_path=None, + serialized_dashboard=json.dumps({"pages": [{"displayName": "test", "name": "test"}]}), + warehouse_id="warehouse", + ) ws = create_autospec(WorkspaceClient) dashboards = Dashboards(ws) dashboard_metadata = DashboardMetadata("test") @@ -1489,12 +1498,7 @@ def test_dashboards_calls_update_with_dashboard_id(): dashboards.create_dashboard(dashboard_metadata, dashboard_id="id", warehouse_id="warehouse") ws.lakeview.create.assert_not_called() - ws.lakeview.update.assert_called_with( - "id", - display_name="test", - serialized_dashboard=json.dumps({"pages": [{"displayName": "test", "name": "test"}]}), - warehouse_id="warehouse", - ) + ws.lakeview.update.assert_called_with("id", dashboard=sdk_dashboard.as_dict()) ws.lakeview.publish.assert_not_called()