Skip to content

Commit 3d6d9c9

Browse files
changdingfangamotl
authored andcommitted
Fix endless-loop bug in the search_teams function
This improves a situation where the value of "totalCount" was inconsistent with the total length of "teams", which resulted in a loop that could not be returned. References: - m0nhawk/grafana_api#87 - #12
1 parent f86f675 commit 3d6d9c9

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Improve example programs `datasource-health-*`
1717
* Add example program `datasource-query.py`
1818
* Add example program `grafanalib-upload-dashboard.py`
19+
* Fix endless-loop bug in the `search_teams` function. Thanks, @changdingfang!
1920

2021

2122
## 2.3.0 (2022-05-26)

grafana_client/elements/team.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def search_teams(self, query=None, page=None, perpage=None):
3636
while True:
3737
teams_on_page = self.client.GET(search_teams_path % page)
3838
list_of_teams += teams_on_page["teams"]
39-
if len(list_of_teams) == teams_on_page["totalCount"]:
39+
if len(teams_on_page["teams"]) < teams_on_page["perPage"]:
4040
break
4141
page += 1
4242
else:

test/elements/test_team.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,20 @@ def test_search_teams_only_loads_requested_page(self, m):
101101
self.assertEqual(teams[0]["name"], "MyTestTeam")
102102
self.assertEqual(len(teams), 1)
103103

104+
@requests_mock.Mocker()
105+
def test_search_teams_perpage(self, m):
106+
m.get(
107+
"http://localhost/api/teams/search?query=my%20team&page=1&perpage=5",
108+
json={"page": 1, "totalCount": 7, "teams": [{"name": "FirstTeam"}, {"name": "SecondTeam"}], "perPage": 5},
109+
)
110+
m.get(
111+
"http://localhost/api/teams/search?query=my%20team&page=2&perpage=5",
112+
json={"page": 2, "totalCount": 7, "teams": [], "perPage": 5},
113+
)
114+
teams = self.grafana.teams.search_teams("my team", perpage=5)
115+
self.assertEqual(len(teams), 2)
116+
117+
104118
@requests_mock.Mocker()
105119
def test_get_team_by_name(self, m):
106120
m.get(

0 commit comments

Comments
 (0)