Ejemplo n.º 1
0
    def run_fetch_tile(self, status, *urls):
        """
        Mock aiohttp request and response for `fetch_tile` coroutine and
        run the coroutine.

        The `status` parameter is HTTP error code, but can be exception as
        well. If exception, then it is raised during request.

        :param status: HTTP error code (i.e. 200) or an exception.
        :param urls: Collection of urls.
        """

        @asyncio.coroutine
        def mock_read_and_close(*args, **kwargs):
            return "image"

        @asyncio.coroutine
        def mock_request(*args, **kwargs):
            if isinstance(status, Exception):
                raise status

            response = mock.MagicMock()
            response.status = status
            response.read_and_close = mock_read_and_close
            return response

        with mock.patch.object(aiohttp, "request", mock_request) as request:
            coros = (fetch_tile(u) for u in urls)
            task = asyncio.gather(*coros, return_exceptions=True)
            loop = asyncio.get_event_loop()
            result = loop.run_until_complete(task)
            yield result
Ejemplo n.º 2
0
    def run_fetch_tile(self, status, *urls):
        """
        Mock `urllib.request.urlopen` call and run `fetch_tile` function.

        The `status` parameter is HTTP error code, but can be exception as
        well. If exception, then it is raised during request.

        :param status: HTTP error code (i.e. 200) or an exception.
        :param urls: Collection of urls.
        """
        response = mock.MagicMock()
        response.read.return_value = "image"
        response.status = status

        with mock.patch.object(urllib.request, "Request") as _, mock.patch.object(urllib.request, "urlopen") as f:

            if isinstance(status, Exception):
                f.side_effect = status
            else:
                f.return_value = response
            result = [fetch_tile(u) for u in urls]
            yield result