Skip to content

Commit 20db9de

Browse files
committed
Tests: Make test case for explore datasources use _two_ data sources
1 parent fe736e5 commit 20db9de

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ in progress
1515
all resources provisioned to Grafana.
1616
- Tests: Improve test quality, specifically for ``explore dashboards`` on
1717
Grafana 6 vs. Grafana >= 7
18+
- Tests: Make test case for `explore datasources` use _two_ data sources
1819

1920

2021
2022-02-03 0.13.1

tests/conftest.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,15 +313,22 @@ def grafana_version(docker_grafana):
313313
return grafana_version
314314

315315

316-
def mkdashboard(title: str, datasource: str):
316+
def mkdashboard(title: str, datasources: List[str]):
317317
"""
318-
Build dashboard with single panel.
318+
Build dashboard with multiple panels, each with a different data source.
319319
"""
320320
# datasource = grafanalib.core.DataSourceInput(name="foo", label="foo", pluginId="foo", pluginName="foo")
321-
panel_gl = grafanalib.core.Panel(dataSource=datasource, gridPos={"h": 1, "w": 24, "x": 0, "y": 0})
322-
dashboard_gl = grafanalib.core.Dashboard(title=title, panels=[panel_gl.panel_json(overrides={})])
321+
322+
# Build dashboard object model.
323+
panels = []
324+
for datasource in datasources:
325+
panel = grafanalib.core.Panel(dataSource=datasource, gridPos={"h": 1, "w": 24, "x": 0, "y": 0})
326+
panels.append(panel.panel_json(overrides={}))
327+
dashboard = grafanalib.core.Dashboard(title=title, panels=panels)
328+
329+
# Render dashboard to JSON.
323330
dashboard_json = StringIO()
324-
write_dashboard(dashboard_gl, dashboard_json)
331+
write_dashboard(dashboard, dashboard_json)
325332
dashboard_json.seek(0)
326333
dashboard = json.loads(dashboard_json.read())
327334
return dashboard

tests/test_commands.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,26 +244,30 @@ def test_log_tabular_success(ldi_resources, capsys, caplog):
244244

245245
def test_explore_datasources_used(create_datasource, create_dashboard, capsys, caplog):
246246

247-
# Create a datasource and a dashboard which uses it.
247+
# Create two data sources and a dashboard which uses them.
248248
create_datasource(name="foo")
249-
create_dashboard(mkdashboard(title="baz", datasource="foo"))
249+
create_datasource(name="bar")
250+
create_dashboard(mkdashboard(title="baz", datasources=["foo", "bar"]))
250251

251252
# Compute breakdown.
252253
set_command("explore datasources", "--format=yaml")
253254

254255
# Proof the output is correct.
255256
with caplog.at_level(logging.DEBUG):
256257
grafana_wtf.commands.run()
257-
assert "Found 1 data source(s)" in caplog.messages
258+
assert "Found 2 data source(s)" in caplog.messages
258259

259260
captured = capsys.readouterr()
260261
data = yaml.safe_load(captured.out)
261262

262-
assert len(data["used"]) == 1
263+
assert len(data["used"]) == 2
263264
assert len(data["unused"]) == 0
264265

265-
assert data["used"][0]["datasource"]["name"] == "foo"
266+
# Results will be sorted by name, so `bar` comes first.
267+
assert data["used"][0]["datasource"]["name"] == "bar"
266268
assert data["used"][0]["datasource"]["type"] == "testdata"
269+
assert data["used"][1]["datasource"]["name"] == "foo"
270+
assert data["used"][1]["datasource"]["type"] == "testdata"
267271

268272

269273
def test_explore_datasources_unused(create_datasource, capsys, caplog):

0 commit comments

Comments
 (0)