8000 :technologist: Add conditional printing API docs URLs in panel by FlavienRx · Pull Request #119 · fastapi/fastapi-cli · GitHub
[go: up one dir, main page]

Skip to content

🧑‍💻 Add conditional printing API docs URLs in panel #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tests: conditional printing api docs urls
  • Loading branch information
FlavienRx committed Dec 16, 2024
commit 9f95bd6964a792010fd8d05701de58f216773d02
48 changes: 48 additions & 0 deletions tests/assets/single_file_docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from fastapi import FastAPI

no_openapi = FastAPI(openapi_url=None)


@no_openapi.get("/")
def no_openapi_root():
return {"message": "single file no_openapi"}


none_docs = FastAPI(docs_url=None, redoc_url=None)


@none_docs.get("/")
def none_docs_root():
return {"message": "single file none_docs"}


no_docs = FastAPI(docs_url=None)


@no_docs.get("/")
def no_docs_root():
return {"message": "single file no_docs"}


no_redoc = FastAPI(redoc_url=None)


@no_redoc.get("/")
def no_redoc_root():
return {"message": "single file no_redoc"}


full_docs = FastAPI()


@full_docs.get("/")
def full_docs_root():
return {"message": "single file full_docs"}


custom_docs = FastAPI(docs_url="/custom-docs-url", redoc_url="/custom-redoc-url")


@custom_docs.get("/")
def custom_docs_root():
return {"message": "single file custom_docs"}
78 changes: 78 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,84 @@ def test_run_args() -> None:
)


def test_no_openapi() -> None:
with changing_dir(assets_path):
with patch.object(uvicorn, "run") as mock_run:
result = runner.invoke(
app, ["dev", "single_file_docs.py", "--app", "no_openapi"]
)
assert result.exit_code == 0, result.output
assert mock_run.called

assert "http://127.0.0.1:8000/docs" not in result.output
assert "http://127.0.0.1:8000/redoc" not in result.output


def test_none_docs() -> None:
with changing_dir(assets_path):
with patch.object(uvicorn, "run") as mock_run:
result = runner.invoke(
app, ["dev", "single_file_docs.py", "--app", "none_docs"]
)
assert result.exit_code == 0, result.output
assert mock_run.called

assert "http://127.0.0.1:8000/docs" not in result.output
assert "http://127.0.0.1:8000/redoc" not in result.output


def test_no_docs() -> None:
with changing_dir(assets_path):
with patch.object(uvicorn, "run") as mock_run:
result = runner.invoke(
app, ["dev", "single_file_docs.py", "--app", "no_docs"]
)
assert result.exit_code == 0, result.output
assert mock_run.called

assert "http://127.0.0.1:8000/redoc" in result.output
assert "http://127.0.0.1:8000/docs" not in result.output


def test_no_redoc() -> None:
with changing_dir(assets_path):
with patch.object(uvicorn, "run") as mock_run:
result = runner.invoke(
app, ["dev", "single_file_docs.py", "--app", "no_redoc"]
)
assert result.exit_code == 0, result.output
assert mock_run.called

assert "http://127.0.0.1:8000/docs" in result.output
assert "http://127.0.0.1:8000/redocs" not in result.output


def test_full_docs() -> None:
with changing_dir(assets_path):
with patch.object(uvicorn, "run") as mock_run:
result = runner.invoke(
app, ["dev", "single_file_docs.py", "--app", "full_docs"]
)
assert result.exit_code == 0, result.output
assert mock_run.called

assert "http://127.0.0.1:8000/docs" in result.output
assert "http://127.0.0.1:8000/redoc" in result.output


def test_custom_docs() -> None:
with changing_dir(assets_path):
with patch.object(uvicorn, "run") as mock_run:
result = runner.invoke(
app, ["dev", "single_file_docs.py", "--app", "custom_docs"]
)
assert result.exit_code == 0, result.output
assert mock_run.called

assert "http://127.0.0.1:8000/custom-docs-url" in result.output
assert "http://127.0.0.1:8000/custom-redoc-url" in result.output


def test_run_error() -> None:
with changing_dir(assets_path):
result = runner.invoke(app, ["run", "non_existing_file.py"])
Expand Down
Loading
0