Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle changes from Databricks Python SDK 0.37.0 #320

Merged
merged 6 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading