|
1 | 1 | import json
|
| 2 | +import logging |
2 | 3 | from pathlib import Path
|
3 | 4 |
|
4 | 5 | import pytest
|
| 6 | +from databricks.sdk.core import DatabricksError |
| 7 | +from databricks.sdk.service.dashboards import Dashboard as SDKDashboard |
5 | 8 |
|
6 | 9 | from databricks.labs.lsql.dashboards import Dashboards
|
7 | 10 | from databricks.labs.lsql.lakeview.model import Dashboard
|
8 | 11 |
|
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +def factory(name, create, remove): |
| 16 | + cleanup = [] |
| 17 | + |
| 18 | + def inner(**kwargs): |
| 19 | + x = create(**kwargs) |
| 20 | + logger.debug(f"added {name} fixture: {x}") |
| 21 | + cleanup.append(x) |
| 22 | + return x |
| 23 | + |
| 24 | + yield inner |
| 25 | + logger.debug(f"clearing {len(cleanup)} {name} fixtures") |
| 26 | + for x in cleanup: |
| 27 | + try: |
| 28 | + logger.debug(f"removing {name} fixture: {x}") |
| 29 | + remove(x) |
| 30 | + except DatabricksError as e: |
| 31 | + # TODO: fix on the databricks-labs-pytester level |
| 32 | + logger.debug(f"ignoring error while {name} {x} teardown: {e}") |
| 33 | + |
9 | 34 |
|
10 | 35 | @pytest.fixture
|
11 |
| -def dashboard_id(ws, make_random): |
| 36 | +def make_dashboard(ws, make_random): |
12 | 37 | """Clean the lakeview dashboard"""
|
13 | 38 |
|
14 |
| - dashboard_display_name = f"created_by_lsql_{make_random()}" |
15 |
| - dashboard = ws.lakeview.create(dashboard_display_name) |
| 39 | + def create(display_name: str = "") -> SDKDashboard: |
| 40 | + if len(display_name) == 0: |
| 41 | + display_name = f"created_by_lsql_{make_random()}" |
| 42 | + else: |
| 43 | + display_name = f"{display_name} ({make_random()})" |
| 44 | + dashboard = ws.lakeview.create(display_name) |
| 45 | + return dashboard |
| 46 | + |
| 47 | + def delete(dashboard: SDKDashboard) -> None: |
| 48 | + ws.lakeview.trash(dashboard.dashboard_id) |
16 | 49 |
|
17 |
| - yield dashboard.dashboard_id |
| 50 | + yield from factory("dashboard", create, delete) |
18 | 51 |
|
19 |
| - ws.lakeview.trash(dashboard.dashboard_id) |
20 | 52 |
|
| 53 | +def test_dashboards_deploys_exported_dashboard_definition(ws, make_dashboard): |
| 54 | + sdk_dashboard = make_dashboard() |
21 | 55 |
|
22 |
| -def test_dashboards_deploys_exported_dashboard_definition(ws, dashboard_id): |
23 | 56 | dashboard_file = Path(__file__).parent / "dashboards" / "dashboard.json"
|
24 | 57 | with dashboard_file.open("r") as f:
|
25 | 58 | lakeview_dashboard = Dashboard.from_dict(json.load(f))
|
26 | 59 |
|
27 | 60 | dashboards = Dashboards(ws)
|
28 |
| - dashboard = dashboards.deploy_dashboard(lakeview_dashboard, dashboard_id=dashboard_id) |
| 61 | + sdk_dashboard = dashboards.deploy_dashboard(lakeview_dashboard, dashboard_id=sdk_dashboard.dashboard_id) |
| 62 | + |
| 63 | + assert ws.lakeview.get(sdk_dashboard.dashboard_id) |
29 | 64 |
|
30 |
| - assert ws.lakeview.get(dashboard.dashboard_id) |
31 | 65 |
|
| 66 | +def test_dashboard_deploys_dashboard_the_same_as_created_dashboard(ws, make_dashboard, tmp_path): |
| 67 | + sdk_dashboard = make_dashboard() |
32 | 68 |
|
33 |
| -def test_dashboard_deploys_dashboard_the_same_as_created_dashboard(tmp_path, ws, dashboard_id): |
34 | 69 | with (tmp_path / "counter.sql").open("w") as f:
|
35 | 70 | f.write("SELECT 10 AS count")
|
36 | 71 | dashboards = Dashboards(ws)
|
37 |
| - dashboard = dashboards.create_dashboard(tmp_path) |
| 72 | + lakeview_dashboard = dashboards.create_dashboard(tmp_path) |
38 | 73 |
|
39 |
| - sdk_dashboard = dashboards.deploy_dashboard(dashboard, dashboard_id=dashboard_id) |
| 74 | + sdk_dashboard = dashboards.deploy_dashboard(lakeview_dashboard, dashboard_id=sdk_dashboard.dashboard_id) |
40 | 75 | new_dashboard = dashboards.get_dashboard(sdk_dashboard.path)
|
41 | 76 |
|
42 |
| - assert dashboards._with_better_names(dashboard).as_dict() == dashboards._with_better_names(new_dashboard).as_dict() |
| 77 | + assert ( |
| 78 | + dashboards._with_better_names(lakeview_dashboard).as_dict() |
| 79 | + == dashboards._with_better_names(new_dashboard).as_dict() |
| 80 | + ) |
43 | 81 |
|
44 | 82 |
|
45 |
| -def test_dashboard_deploys_dashboard_with_ten_counters(ws, dashboard_id, tmp_path): |
| 83 | +def test_dashboard_deploys_dashboard_with_ten_counters(ws, make_dashboard, tmp_path): |
| 84 | + sdk_dashboard = make_dashboard() |
| 85 | + |
46 | 86 | for i in range(10):
|
47 | 87 | with (tmp_path / f"counter_{i}.sql").open("w") as f:
|
48 | 88 | f.write(f"SELECT {i} AS count")
|
49 | 89 | dashboards = Dashboards(ws)
|
50 | 90 | lakeview_dashboard = dashboards.create_dashboard(tmp_path)
|
51 | 91 |
|
52 |
| - sdk_dashboard = dashboards.deploy_dashboard(lakeview_dashboard, dashboard_id=dashboard_id) |
| 92 | + sdk_dashboard = dashboards.deploy_dashboard(lakeview_dashboard, dashboard_id=sdk_dashboard.dashboard_id) |
53 | 93 |
|
54 | 94 | assert ws.lakeview.get(sdk_dashboard.dashboard_id)
|
55 | 95 |
|
56 | 96 |
|
57 |
| -def test_dashboard_deploys_dashboard_with_counter_variation(ws, dashboard_id, tmp_path): |
| 97 | +def test_dashboard_deploys_dashboard_with_display_name(ws, make_dashboard, tmp_path): |
| 98 | + sdk_dashboard = make_dashboard(display_name="Counter") |
| 99 | + |
| 100 | + with (tmp_path / "dashboard.yml").open("w") as f: |
| 101 | + f.write("display_name: Counter") |
| 102 | + with (tmp_path / "counter.sql").open("w") as f: |
| 103 | + f.write("SELECT 102132 AS count") |
| 104 | + |
| 105 | + dashboards = Dashboards(ws) |
| 106 | + lakeview_dashboard = dashboards.create_dashboard(tmp_path) |
| 107 | + |
| 108 | + sdk_dashboard = dashboards.deploy_dashboard(lakeview_dashboard, dashboard_id=sdk_dashboard.dashboard_id) |
| 109 | + |
| 110 | + assert ws.lakeview.get(sdk_dashboard.dashboard_id) |
| 111 | + |
| 112 | + |
| 113 | +def test_dashboard_deploys_dashboard_with_counter_variation(ws, make_dashboard, tmp_path): |
| 114 | + sdk_dashboard = make_dashboard() |
| 115 | + |
58 | 116 | with (tmp_path / "counter.sql").open("w") as f:
|
59 | 117 | f.write("SELECT 10 AS something_else_than_count")
|
60 | 118 | dashboards = Dashboards(ws)
|
61 | 119 | lakeview_dashboard = dashboards.create_dashboard(tmp_path)
|
62 | 120 |
|
63 |
| - sdk_dashboard = dashboards.deploy_dashboard(lakeview_dashboard, dashboard_id=dashboard_id) |
| 121 | + sdk_dashboard = dashboards.deploy_dashboard(lakeview_dashboard, dashboard_id=sdk_dashboard.dashboard_id) |
64 | 122 |
|
65 | 123 | assert ws.lakeview.get(sdk_dashboard.dashboard_id)
|
0 commit comments