Skip to content

Commit 8fa9a25

Browse files
committed
tests: conditional printing api docs urls in panel
1 parent e727a77 commit 8fa9a25

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

tests/assets/single_file_docs.py

Lines changed: 48 additions & 0 deletions
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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,84 @@ def test_run_args() -> None:
152152
assert "│ fastapi dev" in result.output
153153

154154

155+
def test_no_openapi() -> None:
156+
with changing_dir(assets_path):
157+
with patch.object(uvicorn, "run") as mock_run:
158+
result = runner.invoke(
159+
app, ["dev", "single_file_docs.py", "--app-name", "no_openapi"]
160+
)
161+
assert result.exit_code == 0, result.output
162+
assert mock_run.called
163+
164+
assert "│ API docs: http://127.0.0.1:8000/docs" not in result.output
165+
assert "│ http://127.0.0.1:8000/redoc" not in result.output
166+
167+
168+
def test_none_docs() -> None:
169+
with changing_dir(assets_path):
170+
with patch.object(uvicorn, "run") as mock_run:
171+
result = runner.invoke(
172+
app, ["dev", "single_file_docs.py", "--app-name", "none_docs"]
173+
)
174+
assert result.exit_code == 0, result.output
175+
assert mock_run.called
176+
177+
assert "│ API docs: http://127.0.0.1:8000/docs" not in result.output
178+
assert "│ http://127.0.0.1:8000/redoc" not in result.output
179+
180+
181+
def test_no_docs() -> None:
182+
with changing_dir(assets_path):
183+
with patch.object(uvicorn, "run") as mock_run:
184+
result = runner.invoke(
185+
app, ["dev", "single_file_docs.py", "--app-name", "no_docs"]
186+
)
187+
assert result.exit_code == 0, result.output
188+
assert mock_run.called
189+
190+
assert "│ API docs: http://127.0.0.1:8000/redoc" in result.output
191+
assert "│ http://127.0.0.1:8000/docs" not in result.output
192+
193+
194+
def test_no_redoc() -> None:
195+
with changing_dir(assets_path):
196+
with patch.object(uvicorn, "run") as mock_run:
197+
result = runner.invoke(
198+
app, ["dev", "single_file_docs.py", "--app-name", "no_redoc"]
199+
)
200+
assert result.exit_code == 0, result.output
201+
assert mock_run.called
202+
203+
assert "│ API docs: http://127.0.0.1:8000/docs" in result.output
204+
assert "│ http://127.0.0.1:8000/docs" not in result.output
205+
206+
207+
def test_full_docs() -> None:
208+
with changing_dir(assets_path):
209+
with patch.object(uvicorn, "run") as mock_run:
210+
result = runner.invoke(
211+
app, ["dev", "single_file_docs.py", "--app-name", "full_docs"]
212+
)
213+
assert result.exit_code == 0, result.output
214+
assert mock_run.called
215+
216+
assert "│ API docs: http://127.0.0.1:8000/docs" in result.output
217+
assert "│ http://127.0.0.1:8000/redoc" in result.output
218+
219+
220+
def test_custom_docs() -> None:
221+
with changing_dir(assets_path):
222+
with patch.object(uvicorn, "run") as mock_run:
223+
result = runner.invoke(
224+
app, ["dev", "single_file_docs.py", "--app-name", "custom_docs"]
225+
)
226+
assert result.exit_code == 0, result.output
227+
assert mock_run.called
228+
229+
assert "│ API docs: http://127.0.0.1:8000/custom-docs-url" in result.output
230+
assert "│ http://127.0.0.1:8000/custom-redoc-url" in result.output
231+
232+
155233
def test_run_error() -> None:
156234
with changing_dir(assets_path):
157235
result = runner.invoke(app, ["run", "non_existing_file.py"])

0 commit comments

Comments
 (0)