async def test_fetch_tiles(session): """ Test fetching map tiles. """ tiles = [ Tile('http://a.b.c/1', 'o', 'image', None), Tile('http://a.b.c/2', 'o', 'image', None), Tile('http://a.b.c/3', 'o', None, 'error'), Tile('http://a.b.c/4', 'o', 'image', None), ] tasks = [asyncio.Future() for t in tiles] for task, tile in zip(tasks, tiles): task.set_result(tile) ctx_ac = mock.patch.object(asyncio, 'as_completed') ctx_ft = mock.patch.object(geotiler.tile.io, 'fetch_tile') with ctx_ac as mock_as_completed, ctx_ft: mock_as_completed.return_value = tasks tiles = [t async for t in fetch_tiles(tiles, 2)] img = [tile.img for tile in tiles] assert ['image', 'image', None, 'image'] == img, tiles error = [tile.error for tile in tiles] assert [None, None, 'error', None] == error, tiles
def test_redis_downloader_and_cache(): """ Test Redis downloader and cache functions. """ async def images(tiles, num_workers): tile = partial(Tile, error=None, offset=None) for t in tiles: yield tile(url=t.url, img='img') async def as_list(tiles): return [t async for t in tiles] client = mock.MagicMock() client.get.side_effect = ['c-img1', None, 'c-img3'] downloader = redis_downloader(client, downloader=images, timeout=10) assert caching_downloader == downloader.func urls = ['url1', 'url2', 'url3'] tiles = [Tile(url, None, None, None) for url in urls] loop = asyncio.get_event_loop() tiles = downloader(tiles, 2) result = loop.run_until_complete(as_list(tiles)) args = [v[0][0] for v in client.get.call_args_list] assert ['url1', 'url2', 'url3'] == args args = sorted(v[0] for v in client.setex.call_args_list) assert 3 == len(args) assert ('url1', 'c-img1', 10) == args[0] assert ('url2', 'img', 10) == args[1] assert ('url3', 'c-img3', 10) == args[2]
async def test_fetch_tile_error(session): """ Test fetching a map tile with an error. """ # assign something to `img` and `error` to see if they are overriden # properly in case of an error tile = Tile('http://a.b.c', 'a', 'b', 'c') with mock_url_open(session, 'image', error_msg='some error'): tile = await fetch_tile(session, tile) assert tile.img is None error = 'Unable to download http://a.b.c (error: some error)' assert error == str(tile.error)
async def test_fetch_tile(session): """ Test fetching a map tile. """ tile = Tile('http://a.b.c', None, None, None) with mock_url_open(session, 'image'): result = await fetch_tile(session, tile) assert result is not tile # copy of tile is returned assert tile.url == result.url assert 'image' == result.img assert result.error is None
async def _tile_generator(offsets, data): """ Create asynchronous generator of map tiles. """ for o, i in zip(offsets, data): yield Tile(None, o, i, None)