async def fetch_paginated_data( self, parser, url, counter_name, limit=DEFAULT_LIMIT, *args, **kwargs ): response = await self._http_client.get(paginate_url(url=url, limit=limit), *args, **kwargs) if not response: return [] try: total = int(response.get(counter_name, 0)) except ValueError: raise UnknownBackendResponse() responses = [response] + await asyncio.gather(*[ self._http_client.get(paginate_url(url=url, limit=limit, offset=offset), *args, **kwargs) for offset in range(limit, total, limit) ]) try: return [rec for res in responses for rec in parser(res)] except Exception: logging.exception("Cannot parse data") raise UnknownBackendResponse()
async def test_bad_format( http_get, authenticated_plugin, backend_response, ): http_get.return_value = backend_response with pytest.raises(UnknownBackendResponse): await authenticated_plugin.get_owned_games() http_get.assert_called_once_with( paginate_url(GAME_LIST_URL.format(user_id="me"), DEFAULT_LIMIT))
async def test_get_owned_games(http_get, authenticated_plugin, backend_response, games, trophy_title_map, mocker): http_get.return_value = backend_response get_game_communication_id = mocker.patch( "plugin.PSNPlugin.get_game_trophies_map", new_callable=AsyncMock, return_value=trophy_title_map) assert games == await authenticated_plugin.get_owned_games() http_get.assert_called_once_with( paginate_url(GAME_LIST_URL.format(user_id="me"), DEFAULT_LIMIT)) get_game_communication_id.assert_called_once_with( [game['titleId'] for game in backend_response['titles']])
async def test_get_owned_games(http_get, authenticated_plugin, backend_response, games, mocker): http_get.return_value = backend_response get_game_communication_id = mocker.patch( "plugin.PSNPlugin.get_game_communication_ids", new_callable=AsyncMock, return_value={game.game_id: COMMUNICATION_ID for game in GAMES}) assert games == await authenticated_plugin.get_owned_games() http_get.assert_called_once_with( paginate_url(GAME_LIST_URL.format(user_id="me"), DEFAULT_LIMIT)) get_game_communication_id.assert_called_once_with( [game.game_id for game in games])
import pytest from galaxy.api.errors import AuthenticationRequired, UnknownBackendResponse from psn_client import DEFAULT_LIMIT, FRIENDS_URL from http_client import paginate_url from tests.test_data import BACKEND_USER_PROFILES, FRIEND_INFO_LIST DEFAULT_AVATAR_SIZE = "l" GET_ALL_FRIENDS_URL = paginate_url(FRIENDS_URL.format( user_id="me", avatar_size_list=DEFAULT_AVATAR_SIZE), limit=DEFAULT_LIMIT) def test_not_authenticated(psn_plugin, event_loop): with pytest.raises(AuthenticationRequired): event_loop.run_until_complete(psn_plugin.get_friends()) @pytest.mark.asyncio async def test_empty_response(http_get, authenticated_plugin): http_get.return_value = {} assert [] == await authenticated_plugin.get_friends() http_get.assert_called_once_with(GET_ALL_FRIENDS_URL) @pytest.mark.asyncio @pytest.mark.parametrize("backend_response", [{ "profiles": "bad_format" }, { "profiles": ["bad_format"] }, {