diff --git a/github_tools/github_helpers_test.py b/github_tools/github_helpers_test.py index 890f27534405f..79c6e70cda96d 100644 --- a/github_tools/github_helpers_test.py +++ b/github_tools/github_helpers_test.py @@ -117,11 +117,12 @@ def test_execute_and_paginate_one_page_count_mismatch(self): self.client.execute = mock.MagicMock( return_value=self.mock_result(["foo"], total_count=2), ) - self.assertRaises( - AssertionError, - list, - self.client.execute_and_paginate(_TEST_QUERY, _TEST_QUERY_PATH), - ) + with self.assertRaises(AssertionError): + list( + self.client.execute_and_paginate( + _TEST_QUERY, _TEST_QUERY_PATH + ) + ) self.client.execute.assert_called_once_with(_EXP_QUERY_FIRST_PAGE) def test_execute_and_paginate_two_page(self): @@ -176,6 +177,78 @@ def test_execute_and_paginate_first_page_continue(self): ) self.client.execute.assert_called_once_with(_EXP_QUERY_SECOND_PAGE) + def test_execute_and_paginate_invalid_nodes_type(self): + invalid_result = { + "top": { + "child": { + "nodes": "not-a-list", + "pageInfo": { + "hasNextPage": False, + "endCursor": None, + }, + "totalCount": 1, + } + } + } + self.client.execute = mock.MagicMock(return_value=invalid_result) + with self.assertRaises(TypeError): + list( + self.client.execute_and_paginate( + _TEST_QUERY, _TEST_QUERY_PATH + ) + ) + + def test_execute_and_paginate_api_failure(self): + self.client.execute = mock.MagicMock( + side_effect=RuntimeError("API down") + ) + with self.assertRaises(RuntimeError): + list( + self.client.execute_and_paginate( + _TEST_QUERY, _TEST_QUERY_PATH + ) + ) + + def test_execute_and_paginate_large_pagination(self): + def paging(query): + if query == _EXP_QUERY_FIRST_PAGE: + return self.mock_result( + [f"user{i}" for i in range(100)], + total_count=150, + has_next_page=True, + ) + elif query == _EXP_QUERY_SECOND_PAGE: + return self.mock_result([f"user{i}" for i in range(100, 150)]) + + self.client.execute = mock.MagicMock( + side_effect=paging + ) + result = list( + self.client.execute_and_paginate(_TEST_QUERY, _TEST_QUERY_PATH) + ) + self.assertEqual(len(result), 150) + + def test_execute_and_paginate_missing_cursor_with_has_next(self): + bad_result = { + "top": { + "child": { + "nodes": ["foo"], + "pageInfo": { + "hasNextPage": True, + "endCursor": None, + }, + "totalCount": 2, + } + } + } + self.client.execute = mock.MagicMock(return_value=bad_result) + with self.assertRaises(Exception): + list( + self.client.execute_and_paginate( + _TEST_QUERY, _TEST_QUERY_PATH + ) + ) + if __name__ == "__main__": unittest.main()