Skip to content

Commit ee27df3

Browse files
committed
Format code with "black" and "isort"
1 parent 4a99b1e commit ee27df3

File tree

5 files changed

+70
-95
lines changed

5 files changed

+70
-95
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
format:
2+
.venv/bin/pip --quiet install black isort
23
black grafana_client test
34
isort grafana_client test

grafana_client/elements/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from .base import Base
44
from .dashboard import Dashboard
55
from .datasource import Datasource
6-
from .health import Health
76
from .folder import Folder
7+
from .health import Health
88
from .notifications import Notifications
99
from .organization import Organization, Organizations
1010
from .search import Search

grafana_client/elements/datasource.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,9 @@ def delete_datasource_by_name(self, datasource_name):
9696
r = self.client.DELETE(delete_datasource)
9797
return r
9898

99-
def get_datasource_proxy_data(self, datasource_id
100-
, query_type='query'
101-
, version='v1'
102-
, expr=None
103-
, time=None
104-
, start=None
105-
, end=None
106-
, step=None
107-
):
99+
def get_datasource_proxy_data(
100+
self, datasource_id, query_type="query", version="v1", expr=None, time=None, start=None, end=None, step=None
101+
):
108102
"""
109103
110104
:param datasource_id:
@@ -114,12 +108,12 @@ def get_datasource_proxy_data(self, datasource_id
114108
115109
:return: r (dict)
116110
"""
117-
get_datasource_path = "/datasources/proxy/{0}" \
118-
'/api/{1}/{2}?query={3}'.format( datasource_id, version, query_type, expr)
119-
if query_type == 'query_range':
120-
get_datasource_path = get_datasource_path + '&start={0}&end={1}&step={2}'.format(
121-
start, end, step)
111+
get_datasource_path = "/datasources/proxy/{0}" "/api/{1}/{2}?query={3}".format(
112+
datasource_id, version, query_type, expr
113+
)
114+
if query_type == "query_range":
115+
get_datasource_path = get_datasource_path + "&start={0}&end={1}&step={2}".format(start, end, step)
122116
else:
123-
get_datasource_path = get_datasource_path + '&time={}'.format(time)
117+
get_datasource_path = get_datasource_path + "&time={}".format(time)
124118
r = self.client.GET(get_datasource_path)
125-
return r
119+
return r

test/elements/test_datasource.py

Lines changed: 56 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,128 +16,114 @@ class DatasourceTestCase(unittest.TestCase):
1616
def setUp(self):
1717
self.grafana = GrafanaApi(("admin", "admin"), host="localhost", url_path_prefix="", protocol="http")
1818

19-
2019
@requests_mock.Mocker()
2120
def test_find_datasource(self, m):
2221
m.get(
2322
"http://localhost/api/datasources/name/Prometheus",
2423
json={
25-
"id":1,
26-
"uid":"h8KkCLt7z",
27-
"orgId":1,
28-
"name":"Prometheus",
29-
"type":"prometheus",
30-
"typeName":"Prometheus",
31-
"typeLogoUrl":"public/app/plugins/datasource/prometheus/img/prometheus_logo.svg",
32-
"access":"proxy",
33-
"url":"http://localhost:9090",
34-
"password":"",
35-
"user":"",
36-
"database":"",
24+
"id": 1,
25+
"uid": "h8KkCLt7z",
26+
"orgId": 1,
27+
"name": "Prometheus",
28+
"type": "prometheus",
29+
"typeName": "Prometheus",
30+
"typeLogoUrl": "public/app/plugins/datasource/prometheus/img/prometheus_logo.svg",
31+
"access": "proxy",
32+
"url": "http://localhost:9090",
33+
"password": "",
34+
"user": "",
35+
"database": "",
3736
"basicAuth": False,
3837
"isDefault": True,
39-
"jsonData":{
40-
"httpMethod":"POST"
41-
},
42-
"readOnly": False
43-
}
38+
"jsonData": {"httpMethod": "POST"},
39+
"readOnly": False,
40+
},
4441
)
4542

46-
result = self.grafana.datasource.find_datasource('Prometheus')
47-
self.assertEqual(result["type"], 'prometheus')
43+
result = self.grafana.datasource.find_datasource("Prometheus")
44+
self.assertEqual(result["type"], "prometheus")
4845

4946
@requests_mock.Mocker()
5047
def test_find_datasource_not_existing(self, m):
5148
m.get(
5249
"http://localhost/api/datasources/name/it_doesnot_exist",
53-
json= {"message": "Data source not found"},
54-
status_code=400
50+
json={"message": "Data source not found"},
51+
status_code=400,
5552
)
5653

5754
with self.assertRaises(GrafanaBadInputError):
58-
result = self.grafana.datasource.find_datasource('it_doesnot_exist')
59-
55+
result = self.grafana.datasource.find_datasource("it_doesnot_exist")
6056

6157
@requests_mock.Mocker()
6258
def test_get_datasource_id_by_name(self, m):
63-
m.get(
64-
"http://localhost/api/datasources/id/Prometheus",
65-
json={"id": 1}
66-
)
59+
m.get("http://localhost/api/datasources/id/Prometheus", json={"id": 1})
6760

68-
result = self.grafana.datasource.get_datasource_id_by_name('Prometheus')
61+
result = self.grafana.datasource.get_datasource_id_by_name("Prometheus")
6962
self.assertEqual(result["id"], 1)
7063

71-
7264
@requests_mock.Mocker()
7365
def test_list_datasources(self, m):
7466
m.get(
7567
"http://localhost/api/datasources",
7668
json=[
7769
{
78-
"id":1,
79-
"uid":"h8KkCLt7z",
80-
"orgId":1,
81-
"name":"Prometheus",
82-
"type":"prometheus",
83-
"typeName":"Prometheus",
84-
"typeLogoUrl":"public/app/plugins/datasource/prometheus/img/prometheus_logo.svg",
85-
"access":"proxy",
86-
"url":"http://localhost:9090",
87-
"password":"",
88-
"user":"",
89-
"database":"",
70+
"id": 1,
71+
"uid": "h8KkCLt7z",
72+
"orgId": 1,
73+
"name": "Prometheus",
74+
"type": "prometheus",
75+
"typeName": "Prometheus",
76+
"typeLogoUrl": "public/app/plugins/datasource/prometheus/img/prometheus_logo.svg",
77+
"access": "proxy",
78+
"url": "http://localhost:9090",
79+
"password": "",
80+
"user": "",
81+
"database": "",
9082
"basicAuth": False,
9183
"isDefault": True,
92-
"jsonData":{
93-
"httpMethod":"POST"
94-
},
95-
"readOnly": False
84+
"jsonData": {"httpMethod": "POST"},
85+
"readOnly": False,
9686
}
9787
],
9888
)
9989

10090
result = self.grafana.datasource.list_datasources()
101-
self.assertEqual(result[0]["type"], 'prometheus')
91+
self.assertEqual(result[0]["type"], "prometheus")
10292
self.assertEqual(len(result), 1)
10393

10494
@requests_mock.Mocker()
10595
def test_get_datasource_proxy_data(self, m):
106-
# http://localhost:3000/api/datasources/proxy/1/api/v1/query_range?query=up%7binstance%3d%22localhost:9090%22%7d&start=1644164339&end=1644164639&step=60
96+
# http://localhost:3000/api/datasources/proxy/1/api/v1/query_range?query=up%7binstance%3d%22localhost:9090%22%7d&start=1644164339&end=1644164639&step=60
10797
m.get(
10898
"http://localhost/api/datasources/proxy/1/api/v1/query_range",
10999
json={
110100
"status": "success",
111101
"data": {
112102
"resultType": "matrix",
113-
"result":[
103+
"result": [
114104
{
115-
"metric":{
116-
"__name__":"up",
117-
"instance":"localhost:9090",
118-
"job":"prometheus"
119-
},
120-
"values":[
121-
[1644164339,"1"],
122-
[1644164399,"1"],
123-
[1644164459,"1"],
124-
[1644164519,"1"],
125-
[1644164579,"1"],
126-
[1644164639,"1"]
127-
]
105+
"metric": {"__name__": "up", "instance": "localhost:9090", "job": "prometheus"},
106+
"values": [
107+
[1644164339, "1"],
108+
[1644164399, "1"],
109+
[1644164459, "1"],
110+
[1644164519, "1"],
111+
[1644164579, "1"],
112+
[1644164639, "1"],
113+
],
128114
}
129-
]
130-
}
131-
}
115+
],
116+
},
117+
},
132118
)
133119
result = self.grafana.datasource.get_datasource_proxy_data(
134-
1, # datasource_id
135-
query_type='query_range',
136-
expr="up{instance=\"localhost:9090\"}",
120+
1, # datasource_id
121+
query_type="query_range",
122+
expr='up{instance="localhost:9090"}',
137123
start=1644164339,
138124
end=1644164639,
139125
step=60,
140126
)
141-
self.assertEqual(result["status"], 'success')
142-
self.assertEqual(result["data"]["result"][0]["metric"]["job"], 'prometheus')
127+
self.assertEqual(result["status"], "success")
128+
self.assertEqual(result["data"]["result"][0]["metric"]["job"], "prometheus")
143129
self.assertEqual(len(result["data"]["result"][0]["values"]), 6)

test/elements/test_health.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,9 @@ def setUp(self):
1919
def test_search_dashboards(self, m):
2020
m.get(
2121
"http://localhost/api/health",
22-
json=[
23-
{
24-
"commit": "6f8c1d9fe4",
25-
"database": "ok",
26-
"version": "7.5.11"
27-
}
28-
],
22+
json=[{"commit": "6f8c1d9fe4", "database": "ok", "version": "7.5.11"}],
2923
)
3024

3125
result = self.grafana.health.check()
32-
self.assertEqual(result[0]["database"], 'ok')
26+
self.assertEqual(result[0]["database"], "ok")
3327
self.assertEqual(len(result), 1)

0 commit comments

Comments
 (0)