diff --git a/CHANGELOG.md b/CHANGELOG.md index d26c08d..f5d53b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/grafana_client/elements/team.py b/grafana_client/elements/team.py index cac877f..27dbfcd 100644 --- a/grafana_client/elements/team.py +++ b/grafana_client/elements/team.py @@ -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: diff --git a/test/elements/test_team.py b/test/elements/test_team.py index 20d7938..754675c 100644 --- a/test/elements/test_team.py +++ b/test/elements/test_team.py @@ -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(