|
1 | 1 | # """Test TiTiler Tiler Factories.""" |
2 | 2 |
|
3 | | -from rio_tiler.io import COGReader |
| 3 | +import os |
| 4 | +import tempfile |
| 5 | +from contextlib import contextmanager |
4 | 6 |
|
| 7 | +from cogeo_mosaic.backends import FileBackend |
| 8 | +from cogeo_mosaic.mosaic import MosaicJSON |
5 | 9 |
|
6 | | -def test_TilerFactory(set_env): |
| 10 | +from titiler.dependencies import TMSParams, WebMercatorTMSParams |
| 11 | +from titiler.endpoints import factory |
| 12 | +from titiler.resources.enums import OptionalHeaders |
| 13 | + |
| 14 | +from .conftest import DATA_DIR |
| 15 | + |
| 16 | +from fastapi import FastAPI |
| 17 | + |
| 18 | +from starlette.testclient import TestClient |
| 19 | + |
| 20 | +assets = [os.path.join(DATA_DIR, asset) for asset in ["cog1.tif", "cog2.tif"]] |
| 21 | + |
| 22 | + |
| 23 | +def test_TilerFactory(): |
7 | 24 | """Test TilerFactory class.""" |
8 | | - from titiler.dependencies import TMSParams |
9 | | - from titiler.endpoints import factory |
| 25 | + cog = factory.TilerFactory() |
| 26 | + assert len(cog.router.routes) == 21 |
| 27 | + assert cog.tms_dependency == TMSParams |
| 28 | + |
| 29 | + cog = factory.TilerFactory(add_preview=False, add_part=False) |
| 30 | + assert len(cog.router.routes) == 17 |
| 31 | + |
| 32 | + app = FastAPI() |
| 33 | + cog = factory.TilerFactory(optional_headers=[OptionalHeaders.server_timing]) |
| 34 | + app.include_router(cog.router) |
| 35 | + client = TestClient(app) |
10 | 36 |
|
11 | | - app = factory.TilerFactory(reader=COGReader) |
12 | | - assert len(app.router.routes) == 21 |
13 | | - assert app.tms_dependency == TMSParams |
| 37 | + response = client.get(f"/tiles/8/87/48?url={DATA_DIR}/cog.tif&rescale=0,1000") |
| 38 | + assert response.status_code == 200 |
| 39 | + assert response.headers["content-type"] == "image/jpeg" |
| 40 | + timing = response.headers["server-timing"] |
| 41 | + assert "dataread;dur" in timing |
| 42 | + assert "postprocess;dur" in timing |
| 43 | + assert "format;dur" in timing |
14 | 44 |
|
15 | | - app = factory.TilerFactory(reader=COGReader, add_preview=False, add_part=False) |
16 | | - assert len(app.router.routes) == 17 |
| 45 | + response = client.get( |
| 46 | + f"/preview?url={DATA_DIR}/cog.tif&rescale=0,1000&max_size=256" |
| 47 | + ) |
| 48 | + assert response.status_code == 200 |
| 49 | + assert response.headers["content-type"] == "image/jpeg" |
| 50 | + timing = response.headers["server-timing"] |
| 51 | + assert "dataread;dur" in timing |
| 52 | + assert "postprocess;dur" in timing |
| 53 | + assert "format;dur" in timing |
17 | 54 |
|
| 55 | + response = client.get( |
| 56 | + f"/crop/-56.228,72.715,-54.547,73.188.png?url={DATA_DIR}/cog.tif&rescale=0,1000&max_size=256" |
| 57 | + ) |
| 58 | + assert response.status_code == 200 |
| 59 | + assert response.headers["content-type"] == "image/png" |
| 60 | + timing = response.headers["server-timing"] |
| 61 | + assert "dataread;dur" in timing |
| 62 | + assert "postprocess;dur" in timing |
| 63 | + assert "format;dur" in timing |
18 | 64 |
|
19 | | -def test_MosaicTilerFactory(set_env): |
| 65 | + response = client.get(f"/point/-56.228,72.715?url={DATA_DIR}/cog.tif") |
| 66 | + assert response.status_code == 200 |
| 67 | + assert response.headers["content-type"] == "application/json" |
| 68 | + timing = response.headers["server-timing"] |
| 69 | + assert "dataread;dur" in timing |
| 70 | + |
| 71 | + |
| 72 | +@contextmanager |
| 73 | +def tmpmosaic(): |
| 74 | + """Create a Temporary MosaicJSON file.""" |
| 75 | + fileobj = tempfile.NamedTemporaryFile(suffix=".json.gz", delete=False) |
| 76 | + fileobj.close() |
| 77 | + mosaic_def = MosaicJSON.from_urls(assets) |
| 78 | + with FileBackend(fileobj.name, mosaic_def=mosaic_def) as mosaic: |
| 79 | + mosaic.write(overwrite=True) |
| 80 | + |
| 81 | + try: |
| 82 | + yield fileobj.name |
| 83 | + finally: |
| 84 | + os.remove(fileobj.name) |
| 85 | + |
| 86 | + |
| 87 | +def test_MosaicTilerFactory(): |
20 | 88 | """Test MosaicTilerFactory class.""" |
21 | | - from titiler.dependencies import WebMercatorTMSParams |
22 | | - from titiler.endpoints import factory |
| 89 | + mosaic = factory.MosaicTilerFactory( |
| 90 | + optional_headers=[OptionalHeaders.server_timing, OptionalHeaders.x_assets], |
| 91 | + router_prefix="mosaic", |
| 92 | + ) |
| 93 | + assert len(mosaic.router.routes) == 19 |
| 94 | + assert mosaic.tms_dependency == WebMercatorTMSParams |
| 95 | + |
| 96 | + app = FastAPI() |
| 97 | + app.include_router(mosaic.router, prefix="/mosaic") |
| 98 | + client = TestClient(app) |
| 99 | + |
| 100 | + with tmpmosaic() as mosaic_file: |
| 101 | + response = client.get( |
| 102 | + "/mosaic/point/-74.53125,45.9956935", params={"url": mosaic_file}, |
| 103 | + ) |
| 104 | + assert response.status_code == 200 |
| 105 | + timing = response.headers["server-timing"] |
| 106 | + assert "mosaicread;dur" in timing |
| 107 | + assert "dataread;dur" in timing |
| 108 | + |
| 109 | + response = client.get("/mosaic/tiles/7/37/45", params={"url": mosaic_file}) |
| 110 | + assert response.status_code == 200 |
23 | 111 |
|
24 | | - app = factory.MosaicTilerFactory() |
25 | | - assert len(app.router.routes) == 19 |
26 | | - assert app.tms_dependency == WebMercatorTMSParams |
| 112 | + assert response.headers["X-Assets"] |
| 113 | + timing = response.headers["server-timing"] |
| 114 | + assert "mosaicread;dur" in timing |
| 115 | + assert "dataread;dur" in timing |
| 116 | + assert "postprocess;dur" in timing |
| 117 | + assert "format;dur" in timing |
0 commit comments