Skip to content

Commit 872365b

Browse files
committed
Tests: Mimic Grafana 7/8 on datasource references within dashboards
Newer versions have objects (uid, type) instead of bare names. This improvement reveals an implementation flaw reported at #32. TypeError: '<' not supported between instances of 'dict' and 'dict'
1 parent 20db9de commit 872365b

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ in progress
1616
- Tests: Improve test quality, specifically for ``explore dashboards`` on
1717
Grafana 6 vs. Grafana >= 7
1818
- Tests: Make test case for `explore datasources` use _two_ data sources
19+
- Tests: Mimic Grafana 7/8 on datasource references within dashboards, newer
20+
versions have objects (uid, type) instead of bare names
1921

2022

2123
2022-02-03 0.13.1

tests/conftest.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import pytest
1010
from grafana_client.client import GrafanaClientError
1111
from grafanalib._gen import write_dashboard
12+
from packaging import version
1213

1314
from grafana_wtf.core import GrafanaWtf
1415

@@ -53,7 +54,7 @@ def docker_grafana(docker_services):
5354

5455

5556
@pytest.fixture
56-
def create_datasource(docker_grafana):
57+
def create_datasource(docker_grafana, grafana_version):
5758
"""
5859
Create a Grafana data source from a test case.
5960
After the test case finished, it will remove the data source again.
@@ -100,14 +101,20 @@ def create_datasource(docker_grafana):
100101
# Keep track of the datasource ids in order to delete them afterwards.
101102
datasource_ids = []
102103

104+
def mkresponse(response):
105+
if version.parse(grafana_version) < version.parse("8"):
106+
return response["name"]
107+
else:
108+
return {"uid": response["uid"], "type": response["type"]}
109+
103110
def _create_datasource(name: str, type: str = "testdata", access: str = "proxy", **kwargs):
104111

105112
# Reuse existing datasource.
106113
try:
107114
response = grafana.datasource.get_datasource_by_name(name)
108115
datasource_id = response["id"]
109116
datasource_ids.append(datasource_id)
110-
return
117+
return mkresponse(response)
111118
except GrafanaClientError as ex:
112119
if ex.status_code != 404:
113120
raise
@@ -119,6 +126,7 @@ def _create_datasource(name: str, type: str = "testdata", access: str = "proxy",
119126
response = grafana.datasource.create_datasource(datasource)
120127
datasource_id = response["datasource"]["id"]
121128
datasource_ids.append(datasource_id)
129+
return mkresponse(response["datasource"])
122130
except GrafanaClientError as ex:
123131
# TODO: Mimic the original response in order to make the removal work.
124132
# `{'datasource': {'id': 5, 'uid': 'u9wNRyEnk', 'orgId': 1, ...`.

tests/test_commands.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ def test_log_tabular_success(ldi_resources, capsys, caplog):
245245
def test_explore_datasources_used(create_datasource, create_dashboard, capsys, caplog):
246246

247247
# Create two data sources and a dashboard which uses them.
248-
create_datasource(name="foo")
249-
create_datasource(name="bar")
250-
create_dashboard(mkdashboard(title="baz", datasources=["foo", "bar"]))
248+
ds_foo = create_datasource(name="foo")
249+
ds_bar = create_datasource(name="bar")
250+
create_dashboard(mkdashboard(title="baz", datasources=[ds_foo, ds_bar]))
251251

252252
# Compute breakdown.
253253
set_command("explore datasources", "--format=yaml")

0 commit comments

Comments
 (0)