Skip to content

Commit

Permalink
Fix endless-loop bug in the search_teams function
Browse files Browse the repository at this point in the history
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
  • Loading branch information
changdingfang authored and amotl committed Jun 25, 2022
1 parent 0576969 commit 8b6872c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* Improve example programs `datasource-health-*`
* Add example program `datasource-query.py`
* Add example program `grafanalib-upload-dashboard.py`
* Fix endless-loop bug in the `search_teams` function. Thanks, @changdingfang!


## 2.3.0 (2022-05-26)
Expand Down
2 changes: 1 addition & 1 deletion grafana_client/elements/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def search_teams(self, query=None, page=None, perpage=None):
while True:
teams_on_page = self.client.GET(search_teams_path % page)
list_of_teams += teams_on_page["teams"]
if len(list_of_teams) == teams_on_page["totalCount"]:
if len(teams_on_page["teams"]) < teams_on_page["perPage"]:
break
page += 1
else:
Expand Down
14 changes: 14 additions & 0 deletions test/elements/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ def test_search_teams_only_loads_requested_page(self, m):
self.assertEqual(teams[0]["name"], "MyTestTeam")
self.assertEqual(len(teams), 1)

@requests_mock.Mocker()
def test_search_teams_perpage(self, m):
m.get(
"http://localhost/api/teams/search?query=my%20team&page=1&perpage=5",
json={"page": 1, "totalCount": 7, "teams": [{"name": "FirstTeam"}, {"name": "SecondTeam"}], "perPage": 5},
)
m.get(
"http://localhost/api/teams/search?query=my%20team&page=2&perpage=5",
json={"page": 2, "totalCount": 7, "teams": [], "perPage": 5},
)
teams = self.grafana.teams.search_teams("my team", perpage=5)
self.assertEqual(len(teams), 2)


@requests_mock.Mocker()
def test_get_team_by_name(self, m):
m.get(
Expand Down

0 comments on commit 8b6872c

Please sign in to comment.