Skip to content

Commit

Permalink
Handle changes from Databricks Python SDK 0.37.0 (#320)
Browse files Browse the repository at this point in the history
Handle changes from Databricks Python SDK 0.37.0: LakeviewAPI now works
with a Dashboard object
  • Loading branch information
JCZuurmond authored Nov 15, 2024
1 parent 48c287e commit 69c6e97
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
21 changes: 9 additions & 12 deletions src/databricks/labs/lsql/dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.as_dict()) # type: ignore
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.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)
Expand Down
10 changes: 6 additions & 4 deletions tests/integration/test_dashboards.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dataclasses
import datetime as dt
import json
import logging
Expand Down Expand Up @@ -52,12 +53,12 @@ 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:
display_name = f"{display_name} ({make_random()})"
dashboard = ws.lakeview.create(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)
Expand Down Expand Up @@ -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.as_dict())
lakeview_dashboard = Dashboard.from_dict(json.loads(dashboard_content))
new_dashboard = dashboards.get_dashboard(sdk_dashboard.path)

Expand Down
32 changes: 18 additions & 14 deletions tests/unit/test_dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -1464,37 +1464,41 @@ 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")

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()


Expand Down

0 comments on commit 69c6e97

Please sign in to comment.