Skip to content

Commit

Permalink
tests: conditional printing api docs urls
Browse files Browse the repository at this point in the history
  • Loading branch information
FlavienRx committed Dec 16, 2024
1 parent df3fd5d commit 9f95bd6
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
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

0 comments on commit 9f95bd6

Please sign in to comment.