Skip to content

Commit 4a7ea66

Browse files
committed
Refactor docs_urls-related tests
1 parent 03ea860 commit 4a7ea66

2 files changed

Lines changed: 55 additions & 39 deletions

File tree

tests/assets/single_file_docs.py

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,51 @@
11
from fastapi import FastAPI
22

3-
no_openapi = FastAPI(openapi_url=None)
3+
# App 1: API documentation disabled via `openapi_url=None`
4+
# ------------------------------------------------------------------------------------
45

6+
openapi_none = FastAPI(openapi_url=None)
57

6-
@no_openapi.get("/")
7-
def no_openapi_root():
8-
return {"message": "single file no_openapi"}
98

9+
@openapi_none.get("/")
10+
def openapi_none_root():
11+
return {"message": "single file openapi_none"}
1012

11-
none_docs = FastAPI(docs_url=None, redoc_url=None)
1213

14+
# App 2: Both docs and redoc disabled via `docs_url=None` and `redoc_url=None`
15+
# ------------------------------------------------------------------------------------
1316

14-
@none_docs.get("/")
15-
def none_docs_root():
16-
return {"message": "single file none_docs"}
17+
docs_none_redoc_none = FastAPI(docs_url=None, redoc_url=None)
1718

1819

19-
no_docs = FastAPI(docs_url=None)
20+
@docs_none_redoc_none.get("/")
21+
def docs_none_redoc_none_root():
22+
return {"message": "single file docs_none_redoc_none"}
2023

2124

22-
@no_docs.get("/")
23-
def no_docs_root():
24-
return {"message": "single file no_docs"}
25+
# App 3: Only ReDoc. Swagger docs disabled via `docs_url=None`
26+
# ------------------------------------------------------------------------------------
2527

28+
only_redoc = FastAPI(docs_url=None)
2629

27-
no_redoc = FastAPI(redoc_url=None)
2830

31+
@only_redoc.get("/")
32+
def only_redoc_root():
33+
return {"message": "single file only_redoc"}
2934

30-
@no_redoc.get("/")
31-
def no_redoc_root():
32-
return {"message": "single file no_redoc"}
3335

36+
# App 4: Only Swagger docs. ReDoc disabled via `redoc_url=None`
37+
# ------------------------------------------------------------------------------------
38+
39+
only_docs = FastAPI(redoc_url=None)
40+
41+
42+
@only_docs.get("/")
43+
def only_docs_root():
44+
return {"message": "single file only_docs"}
45+
46+
47+
# App 5: Both docs and redoc enabled with default URLs
48+
# ------------------------------------------------------------------------------------
3449

3550
full_docs = FastAPI()
3651

@@ -40,6 +55,9 @@ def full_docs_root():
4055
return {"message": "single file full_docs"}
4156

4257

58+
# App 6: Swagger docs with custom URL. ReDoc with default URL.
59+
# ------------------------------------------------------------------------------------
60+
4361
custom_docs = FastAPI(docs_url="/custom-docs-url")
4462

4563

@@ -48,6 +66,9 @@ def custom_docs_root():
4866
return {"message": "single file custom_docs"}
4967

5068

69+
# App 7: ReDoc with custom URL. Swagger docs with default URL.
70+
# ------------------------------------------------------------------------------------
71+
5172
custom_redoc = FastAPI(docs_url=None, redoc_url="/custom-redoc-url")
5273

5374

tests/test_cli.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import Path
44
from unittest.mock import patch
55

6+
import pytest
67
import uvicorn
78
from typer.testing import CliRunner
89

@@ -391,11 +392,18 @@ def test_run_env_vars_and_args() -> None:
391392
assert "Documentation at http://0.0.0.0:8080/docs" in result.output
392393

393394

394-
def test_no_openapi() -> None:
395+
@pytest.mark.parametrize(
396+
"app_name",
397+
[
398+
"openapi_none",
399+
"docs_none_redoc_none",
400+
],
401+
)
402+
def test_docs_urls_disabled(app_name: str) -> None:
395403
with changing_dir(assets_path):
396404
with patch.object(uvicorn, "run") as mock_run:
397405
result = runner.invoke(
398-
app, ["dev", "single_file_docs.py", "--app", "no_openapi"]
406+
app, ["dev", "single_file_docs.py", "--app", app_name]
399407
)
400408
assert result.exit_code == 0, result.output
401409
assert mock_run.called
@@ -404,24 +412,24 @@ def test_no_openapi() -> None:
404412
assert "http://127.0.0.1:8000/redoc" not in result.output
405413

406414

407-
def test_none_docs() -> None:
415+
def test_docs_urls_only_docs() -> None:
408416
with changing_dir(assets_path):
409417
with patch.object(uvicorn, "run") as mock_run:
410418
result = runner.invoke(
411-
app, ["dev", "single_file_docs.py", "--app", "none_docs"]
419+
app, ["dev", "single_file_docs.py", "--app", "only_docs"]
412420
)
413421
assert result.exit_code == 0, result.output
414422
assert mock_run.called
415423

416-
assert "http://127.0.0.1:8000/docs" not in result.output
424+
assert "http://127.0.0.1:8000/docs" in result.output
417425
assert "http://127.0.0.1:8000/redoc" not in result.output
418426

419427

420-
def test_no_docs() -> None:
428+
def test_docs_urls_only_redoc() -> None:
421429
with changing_dir(assets_path):
422430
with patch.object(uvicorn, "run") as mock_run:
423431
result = runner.invoke(
424-
app, ["dev", "single_file_docs.py", "--app", "no_docs"]
432+
app, ["dev", "single_file_docs.py", "--app", "only_redoc"]
425433
)
426434
assert result.exit_code == 0, result.output
427435
assert mock_run.called
@@ -430,20 +438,7 @@ def test_no_docs() -> None:
430438
assert "http://127.0.0.1:8000/docs" not in result.output
431439

432440

433-
def test_no_redoc() -> None:
434-
with changing_dir(assets_path):
435-
with patch.object(uvicorn, "run") as mock_run:
436-
result = runner.invoke(
437-
app, ["dev", "single_file_docs.py", "--app", "no_redoc"]
438-
)
439-
assert result.exit_code == 0, result.output
440-
assert mock_run.called
441-
442-
assert "http://127.0.0.1:8000/docs" in result.output
443-
assert "http://127.0.0.1:8000/redocs" not in result.output
444-
445-
446-
def test_full_docs() -> None:
441+
def test_docs_urls_full_docs() -> None:
447442
with changing_dir(assets_path):
448443
with patch.object(uvicorn, "run") as mock_run:
449444
result = runner.invoke(
@@ -456,7 +451,7 @@ def test_full_docs() -> None:
456451
assert "http://127.0.0.1:8000/redoc" not in result.output # docs has precedence
457452

458453

459-
def test_custom_docs() -> None:
454+
def test_docs_urls_custom_docs() -> None:
460455
with changing_dir(assets_path):
461456
with patch.object(uvicorn, "run") as mock_run:
462457
result = runner.invoke(
@@ -468,7 +463,7 @@ def test_custom_docs() -> None:
468463
assert "http://127.0.0.1:8000/custom-docs-url" in result.output
469464

470465

471-
def test_custom_redoc() -> None:
466+
def test_docs_urls_custom_redoc() -> None:
472467
with changing_dir(assets_path):
473468
with patch.object(uvicorn, "run") as mock_run:
474469
result = runner.invoke(

0 commit comments

Comments
 (0)