async def list_all_users_following( self, user_id: int, chunk_size: int = 100, max_number: int = None) -> AsyncGenerator[int, None]: """Returns n asynchronous generator of the IDs of users followed by a given user. See: `List Users Followed <https://developers.symphony.com/restapi/v20.9/reference#list-users-followed>`_ :param user_id: the user ID :param chunk_size: the maximum number of followers to return. Default: 100. :param max_number: the total maximum number of elements to retrieve. :return: an async generator of the IDs of users followed by a given user. """ async def user_following_one_page(limit, after=None): result = await self.list_users_following(user_id, limit, after=after) return result.following, getattr(result.pagination.cursors, 'after', None) return cursor_based_pagination(user_following_one_page, chunk_size, max_number)
async def test_answer_none(self): mock_func = AsyncMock() mock_func.side_effect = [(None, None)] assert [x async for x in cursor_based_pagination(mock_func, CHUNK_SIZE)] == [] mock_func.assert_has_awaits([call(CHUNK_SIZE, None)])
async def test_max_number_equals_more_than_one_chunk(self): mock_func = AsyncMock() mock_func.side_effect = [(["one", "two"], AFTER), (["three", "four"], "after_two")] assert [x async for x in cursor_based_pagination(mock_func, CHUNK_SIZE, 3)] == ["one", "two", "three"] mock_func.assert_has_awaits([call(CHUNK_SIZE, None), call(CHUNK_SIZE, AFTER)])
async def test_max_number_equals_one_chunk(self): mock_func = AsyncMock() mock_func.side_effect = [(["one", "two"], AFTER)] assert [x async for x in cursor_based_pagination(mock_func, CHUNK_SIZE, 2)] == ["one", "two"] mock_func.assert_has_awaits([call(CHUNK_SIZE, None)])
async def test_zero_max_number(self): mock_func = AsyncMock() assert [x async for x in cursor_based_pagination(mock_func, CHUNK_SIZE, 0)] == [] mock_func.assert_not_awaited()
async def test_answer_two_chunks(self): mock_func = AsyncMock() mock_func.side_effect = [(["one", "two"], AFTER), (["three"], None)] assert [x async for x in cursor_based_pagination(mock_func, CHUNK_SIZE)] == ["one", "two", "three"] mock_func.assert_has_awaits([call(CHUNK_SIZE, None), call(CHUNK_SIZE, AFTER)])