Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9f95bd6

Browse files
committedDec 16, 2024·
tests: conditional printing api docs urls
1 parent df3fd5d commit 9f95bd6

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
 

‎tests/assets/single_file_docs.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from fastapi import FastAPI
2+
3+
no_openapi = FastAPI(openapi_url=None)
4+
5+
6+
@no_openapi.get("/")
7+
def no_openapi_root():
8+
return {"message": "single file no_openapi"}
9+
10+
11+
none_docs = FastAPI(docs_url=None, redoc_url=None)
12+
13+
14+
@none_docs.get("/")
15+
def none_docs_root():
16+
return {"message": "single file none_docs"}
17+
18+
19+
no_docs = FastAPI(docs_url=None)
20+
21+
22+
@no_docs.get("/")
23+
def no_docs_root():
24+
return {"message": "single file no_docs"}
25+
26+
27+
no_redoc = FastAPI(redoc_url=None)
28+
29+
30+
@no_redoc.get("/")
31+
def no_redoc_root():
32+
return {"message": "single file no_redoc"}
33+
34+
35+
full_docs = FastAPI()
36+
37+
38+
@full_docs.get("/")
39+
def full_docs_root():
40+
return {"message": "single file full_docs"}
41+
42+
43+
custom_docs = FastAPI(docs_url="/custom-docs-url", redoc_url="/custom-redoc-url")
44+
45+
46+
@custom_docs.get("/")
47+
def custom_docs_root():
48+
return {"message": "single file custom_docs"}

‎tests/test_cli.py

+78
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,84 @@ def test_run_args() -> None:
192192
)
193193

194194

195+
def test_no_openapi() -> None:
196+
with changing_dir(assets_path):
197+
with patch.object(uvicorn, "run") as mock_run:
198+
result = runner.invoke(
199+
app, ["dev", "single_file_docs.py", "--app", "no_openapi"]
200+
)
201+
assert result.exit_code == 0, result.output
202+
assert mock_run.called
203+
204+
assert "http://127.0.0.1:8000/docs" not in result.output
205+
assert "http://127.0.0.1:8000/redoc" not in result.output
206+
207+
208+
def test_none_docs() -> None:
209+
with changing_dir(assets_path):
210+
with patch.object(uvicorn, "run") as mock_run:
211+
result = runner.invoke(
212+
app, ["dev", "single_file_docs.py", "--app", "none_docs"]
213+
)
214+
assert result.exit_code == 0, result.output
215+
assert mock_run.called
216+
217+
assert "http://127.0.0.1:8000/docs" not in result.output
218+
assert "http://127.0.0.1:8000/redoc" not in result.output
219+
220+
221+
def test_no_docs() -> None:
222+
with changing_dir(assets_path):
223+
with patch.object(uvicorn, "run") as mock_run:
224+
result = runner.invoke(
225+
app, ["dev", "single_file_docs.py", "--app", "no_docs"]
226+
)
227+
assert result.exit_code == 0, result.output
228+
assert mock_run.called
229+
230+
assert "http://127.0.0.1:8000/redoc" in result.output
231+
assert "http://127.0.0.1:8000/docs" not in result.output
232+
233+
234+
def test_no_redoc() -> None:
235+
with changing_dir(assets_path):
236+
with patch.object(uvicorn, "run") as mock_run:
237+
result = runner.invoke(
238+
app, ["dev", "single_file_docs.py", "--app", "no_redoc"]
239+
)
240+
assert result.exit_code == 0, result.output
241+
assert mock_run.called
242+
243+
assert "http://127.0.0.1:8000/docs" in result.output
244+
assert "http://127.0.0.1:8000/redocs" not in result.output
245+
246+
247+
def test_full_docs() -> None:
248+
with changing_dir(assets_path):
249+
with patch.object(uvicorn, "run") as mock_run:
250+
result = runner.invoke(
251+
app, ["dev", "single_file_docs.py", "--app", "full_docs"]
252+
)
253+
assert result.exit_code == 0, result.output
254+
assert mock_run.called
255+
256+
assert "http://127.0.0.1:8000/docs" in result.output
257+
assert "http://127.0.0.1:8000/redoc" in result.output
258+
259+
260+
def test_custom_docs() -> None:
261+
with changing_dir(assets_path):
262+
with patch.object(uvicorn, "run") as mock_run:
263+
result = runner.invoke(
264+
app, ["dev", "single_file_docs.py", "--app", "custom_docs"]
265+
)
266+
assert result.exit_code == 0, result.output
267+
assert mock_run.called
268+
269+
assert "http://127.0.0.1:8000/custom-docs-url" in result.output
270+
assert "http://127.0.0.1:8000/custom-redoc-url" in result.output
271+
272+
195273
def test_run_error() -> None:
196274
with changing_dir(assets_path):
197275
result = runner.invoke(app, ["run", "non_existing_file.py"])

0 commit comments

Comments
 (0)
Please sign in to comment.