Skip to content

Commit 006aca4

Browse files
committed
Add more test cases to further increase code coverage
1 parent bc6b32f commit 006aca4

File tree

2 files changed

+176
-2
lines changed

2 files changed

+176
-2
lines changed

test/elements/test_annotations.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ def setUp(self):
1919
def test_annotations(self, m):
2020
m.get(
2121
"http://localhost/api/annotations?from=1563183710618&to=1563185212275"
22-
"&alertId=11&dashboardId=111&panelId=22&tags=tags-test&limit=1",
22+
"&alertId=11&dashboardId=111&panelId=22&userId=42&type=alert&tags=tags-test&limit=1",
2323
json=[
2424
{
2525
"id": 80,
2626
"alertId": 11,
2727
"alertName": "",
2828
"dashboardId": 111,
2929
"panelId": 22,
30-
"userId": 0,
30+
"userId": 42,
31+
"type": "alert",
3132
"newState": "",
3233
"prevState": "",
3334
"created": 1563280160455,
@@ -48,13 +49,17 @@ def test_annotations(self, m):
4849
alert_id=11,
4950
dashboard_id=111,
5051
panel_id=22,
52+
user_id=42,
53+
ann_type="alert",
5154
tags=["tags-test"],
5255
limit=1,
5356
)
5457
self.assertEqual(annotations[0]["text"], "Annotation Description")
5558
self.assertEqual(annotations[0]["alertId"], 11)
5659
self.assertEqual(annotations[0]["dashboardId"], 111)
5760
self.assertEqual(annotations[0]["panelId"], 22)
61+
self.assertEqual(annotations[0]["userId"], 42)
62+
self.assertEqual(annotations[0]["type"], "alert")
5863
self.assertEqual(annotations[0]["tags"][0], "tags-test")
5964

6065
self.assertEqual(len(annotations), 1)

test/elements/test_user.py

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
import unittest
2+
3+
import requests_mock
4+
5+
from grafana_client import GrafanaApi
6+
7+
8+
class UsersTestCase(unittest.TestCase):
9+
def setUp(self):
10+
self.grafana = GrafanaApi(("admin", "admin"), host="localhost", url_path_prefix="", protocol="http")
11+
12+
@requests_mock.Mocker()
13+
def test_update_user(self, m):
14+
m.put(
15+
"http://localhost/api/users/foo",
16+
json={},
17+
)
18+
user = self.grafana.users.update_user("foo", {})
19+
self.assertEqual(user, {})
20+
21+
@requests_mock.Mocker()
22+
def test_get_user(self, m):
23+
m.get(
24+
"http://localhost/api/users/foo",
25+
json={},
26+
)
27+
user = self.grafana.users.get_user("foo")
28+
self.assertEqual(user, {})
29+
30+
@requests_mock.Mocker()
31+
def test_find_user(self, m):
32+
m.get(
33+
"http://localhost/api/users/lookup?loginOrEmail=foo",
34+
json={},
35+
)
36+
user = self.grafana.users.find_user("foo")
37+
self.assertEqual(user, {})
38+
39+
@requests_mock.Mocker()
40+
def test_search_users_default(self, m):
41+
m.get(
42+
"http://localhost/api/users?query=foo&page=1",
43+
json=[{"name": "foo"}, {"name": "bar"}],
44+
)
45+
m.get(
46+
"http://localhost/api/users?query=foo&page=2",
47+
json=[],
48+
)
49+
users = self.grafana.users.search_users("foo")
50+
self.assertEqual(len(users), 2)
51+
52+
@requests_mock.Mocker()
53+
def test_search_users_default_empty(self, m):
54+
m.get(
55+
"http://localhost/api/users?query=foo&page=1",
56+
json=[],
57+
)
58+
users = self.grafana.users.search_users("foo")
59+
self.assertEqual(len(users), 0)
60+
61+
@requests_mock.Mocker()
62+
def test_search_users_page2(self, m):
63+
m.get(
64+
"http://localhost/api/users?query=foo&page=2",
65+
json=[{}],
66+
)
67+
users = self.grafana.users.search_users("foo", page=2)
68+
self.assertEqual(users, [{}])
69+
70+
@requests_mock.Mocker()
71+
# FIXME: Runs into an endless loop.
72+
# See also https://github.com/panodata/grafana-client/issues/12.
73+
def fixme_test_search_users_perpage(self, m):
74+
m.get(
75+
"http://localhost/api/users?query=foo&perpage=5",
76+
json=[{}],
77+
)
78+
users = self.grafana.users.search_users("foo", perpage=5)
79+
self.assertEqual(users, [{}])
80+
81+
@requests_mock.Mocker()
82+
def test_search_users_perpage(self, m):
83+
m.get(
84+
"http://localhost/api/users?query=foo&page=1&perpage=1",
85+
json=[{"name": "foo"}, {"name": "bar"}],
86+
)
87+
m.get(
88+
"http://localhost/api/users?query=foo&page=2&perpage=1",
89+
json=[],
90+
)
91+
users = self.grafana.users.search_users("foo", perpage=1)
92+
self.assertEqual(len(users), 2)
93+
94+
@requests_mock.Mocker()
95+
def test_get_user_organisations(self, m):
96+
m.get(
97+
"http://localhost/api/users/foo/orgs",
98+
json=[],
99+
)
100+
users = self.grafana.users.get_user_organisations("foo")
101+
self.assertEqual(users, [])
102+
103+
104+
class UserTestCase(unittest.TestCase):
105+
def setUp(self):
106+
self.grafana = GrafanaApi(("admin", "admin"), host="localhost", url_path_prefix="", protocol="http")
107+
108+
@requests_mock.Mocker()
109+
def test_get_actual_user(self, m):
110+
m.get(
111+
"http://localhost/api/user",
112+
json={},
113+
)
114+
result = self.grafana.user.get_actual_user()
115+
self.assertEqual(result, {})
116+
117+
@requests_mock.Mocker()
118+
def test_change_actual_user_password(self, m):
119+
m.put(
120+
"http://localhost/api/user/password",
121+
json={},
122+
)
123+
result = self.grafana.user.change_actual_user_password("old", "new")
124+
self.assertEqual(result, {})
125+
126+
@requests_mock.Mocker()
127+
def test_switch_user_organisation(self, m):
128+
m.post(
129+
"http://localhost/api/users/foo/using/acme",
130+
json={},
131+
)
132+
result = self.grafana.user.switch_user_organisation("foo", "acme")
133+
self.assertEqual(result, {})
134+
135+
@requests_mock.Mocker()
136+
def test_switch_actual_user_organisation(self, m):
137+
m.post(
138+
"http://localhost/api/user/using/acme",
139+
json={},
140+
)
141+
result = self.grafana.user.switch_actual_user_organisation("acme")
142+
self.assertEqual(result, {})
143+
144+
@requests_mock.Mocker()
145+
def test_get_actual_user_organisations(self, m):
146+
m.get(
147+
"http://localhost/api/user/orgs",
148+
json=[],
149+
)
150+
result = self.grafana.user.get_actual_user_organisations()
151+
self.assertEqual(result, [])
152+
153+
@requests_mock.Mocker()
154+
def test_star_actual_user_dashboard(self, m):
155+
m.post(
156+
"http://localhost/api/user/stars/dashboard/987vb7t33",
157+
json={},
158+
)
159+
result = self.grafana.user.star_actual_user_dashboard("987vb7t33")
160+
self.assertEqual(result, {})
161+
162+
@requests_mock.Mocker()
163+
def test_unstar_actual_user_dashboard(self, m):
164+
m.delete(
165+
"http://localhost/api/user/stars/dashboard/987vb7t33",
166+
json={},
167+
)
168+
result = self.grafana.user.unstar_actual_user_dashboard("987vb7t33")
169+
self.assertEqual(result, {})

0 commit comments

Comments
 (0)