|
1 | | -"""Test Custom APIRoute factory.""" |
2 | | - |
3 | | -from concurrent import futures |
| 1 | +"""Test route dependencies.""" |
4 | 2 |
|
5 | 3 | import httpx |
6 | | -import pytest |
7 | | -import rasterio |
8 | | -from fastapi import APIRouter, Depends, FastAPI, HTTPException, security, status |
9 | | -from rasterio._env import get_gdal_config |
| 4 | +from fastapi import Depends, FastAPI, HTTPException, security, status |
10 | 5 | from starlette.testclient import TestClient |
11 | 6 |
|
12 | | -from titiler.core.routing import add_route_dependencies, apiroute_factory |
13 | | - |
14 | | - |
15 | | -@pytest.mark.xfail |
16 | | -def test_withoutCustomRoute(monkeypatch): |
17 | | - """Create App.""" |
18 | | - monkeypatch.setenv("GDAL_DISABLE_READDIR_ON_OPEN", "something") |
19 | | - |
20 | | - app = FastAPI() |
21 | | - router = APIRouter() |
22 | | - |
23 | | - def f(r): |
24 | | - return get_gdal_config("GDAL_DISABLE_READDIR_ON_OPEN") |
25 | | - |
26 | | - @router.get("/simple") |
27 | | - def home(): |
28 | | - """Works and should return FALSE.""" |
29 | | - with rasterio.Env(GDAL_DISABLE_READDIR_ON_OPEN="FALSE"): |
30 | | - res = get_gdal_config("GDAL_DISABLE_READDIR_ON_OPEN") |
31 | | - return {"env": res} |
32 | | - |
33 | | - @router.get("/asimple") |
34 | | - async def home1(): |
35 | | - """Works and should return FALSE.""" |
36 | | - with rasterio.Env(GDAL_DISABLE_READDIR_ON_OPEN="FALSE"): |
37 | | - res = get_gdal_config("GDAL_DISABLE_READDIR_ON_OPEN") |
38 | | - return {"env": res} |
39 | | - |
40 | | - @router.get("/future") |
41 | | - def home2(): |
42 | | - """Doesn't work and should return the value from env.""" |
43 | | - with rasterio.Env(GDAL_DISABLE_READDIR_ON_OPEN="FALSE"): |
44 | | - with futures.ThreadPoolExecutor() as executor: |
45 | | - res = list(executor.map(f, range(1)))[0] |
46 | | - return {"env": res} |
47 | | - |
48 | | - @router.get("/afuture") |
49 | | - async def home3(): |
50 | | - """Works and should return FALSE.""" |
51 | | - with rasterio.Env(GDAL_DISABLE_READDIR_ON_OPEN="FALSE"): |
52 | | - with futures.ThreadPoolExecutor() as executor: |
53 | | - res = list(executor.map(f, range(1)))[0] |
54 | | - return {"env": res} |
55 | | - |
56 | | - app.include_router(router) |
57 | | - client = TestClient(app) |
58 | | - |
59 | | - response = client.get("/simple") |
60 | | - assert response.json()["env"] == "FALSE" |
61 | | - |
62 | | - response = client.get("/asimple") |
63 | | - assert response.json()["env"] == "FALSE" |
64 | | - |
65 | | - # confirm the multi threads case doesn't work |
66 | | - response = client.get("/future") |
67 | | - assert not response.json()["env"] == "FALSE" |
68 | | - |
69 | | - response = client.get("/afuture") |
70 | | - assert response.json()["env"] == "FALSE" |
71 | | - |
72 | | - |
73 | | -@pytest.mark.xfail |
74 | | -def test_withCustomRoute(monkeypatch): |
75 | | - """Create App.""" |
76 | | - monkeypatch.setenv("GDAL_DISABLE_READDIR_ON_OPEN", "something") |
77 | | - |
78 | | - app = FastAPI() |
79 | | - |
80 | | - env = {"GDAL_DISABLE_READDIR_ON_OPEN": "FALSE"} |
81 | | - with pytest.warns(DeprecationWarning): |
82 | | - route_class = apiroute_factory(env) |
83 | | - router = APIRouter(route_class=route_class) |
84 | | - |
85 | | - def f(r): |
86 | | - return get_gdal_config("GDAL_DISABLE_READDIR_ON_OPEN") |
87 | | - |
88 | | - @router.get("/simple") |
89 | | - def home(): |
90 | | - """Works and should return FALSE.""" |
91 | | - res = get_gdal_config("GDAL_DISABLE_READDIR_ON_OPEN") |
92 | | - return {"env": res} |
93 | | - |
94 | | - @router.get("/asimple") |
95 | | - async def home1(): |
96 | | - """Works and should return FALSE.""" |
97 | | - res = get_gdal_config("GDAL_DISABLE_READDIR_ON_OPEN") |
98 | | - return {"env": res} |
99 | | - |
100 | | - @router.get("/future") |
101 | | - def home2(): |
102 | | - """Doesn't work and should return the value from env.""" |
103 | | - with futures.ThreadPoolExecutor() as executor: |
104 | | - res = list(executor.map(f, range(1)))[0] |
105 | | - return {"env": res} |
106 | | - |
107 | | - @router.get("/afuture") |
108 | | - async def home3(): |
109 | | - """Works and should return FALSE.""" |
110 | | - with futures.ThreadPoolExecutor() as executor: |
111 | | - res = list(executor.map(f, range(1)))[0] |
112 | | - return {"env": res} |
113 | | - |
114 | | - app.include_router(router) |
115 | | - client = TestClient(app) |
116 | | - |
117 | | - response = client.get("/simple") |
118 | | - assert response.json()["env"] == "FALSE" |
119 | | - |
120 | | - response = client.get("/asimple") |
121 | | - assert response.json()["env"] == "FALSE" |
122 | | - |
123 | | - # confirm the Custom APIRoute class fix |
124 | | - response = client.get("/future") |
125 | | - assert response.json()["env"] == "FALSE" |
126 | | - |
127 | | - response = client.get("/afuture") |
128 | | - assert response.json()["env"] == "FALSE" |
| 7 | +from titiler.core.routing import add_route_dependencies |
129 | 8 |
|
130 | 9 |
|
131 | 10 | def test_register_deps(): |
|
0 commit comments