Skip to content

Commit 5e6e428

Browse files
committed
tests: conditional printing api docs urls
1 parent a23816f commit 5e6e428

File tree

3 files changed

+132
-4
lines changed

3 files changed

+132
-4
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
@@ -349,6 +349,84 @@ def test_run_env_vars_and_args() -> None:
349349
assert "Documentation at http://0.0.0.0:8080/docs" in result.output
350350

351351

352+
def test_no_openapi() -> None:
353+
with changing_dir(assets_path):
354+
with patch.object(uvicorn, "run") as mock_run:
355+
result = runner.invoke(
356+
app, ["dev", "single_file_docs.py", "--app", "no_openapi"]
357+
)
358+
assert result.exit_code == 0, result.output
359+
assert mock_run.called
360+
361+
assert "http://127.0.0.1:8000/docs" not in result.output
362+
assert "http://127.0.0.1:8000/redoc" not in result.output
363+
364+
365+
def test_none_docs() -> None:
366+
with changing_dir(assets_path):
367+
with patch.object(uvicorn, "run") as mock_run:
368+
result = runner.invoke(
369+
app, ["dev", "single_file_docs.py", "--app", "none_docs"]
370+
)
371+
assert result.exit_code == 0, result.output
372+
assert mock_run.called
373+
374+
assert "http://127.0.0.1:8000/docs" not in result.output
375+
assert "http://127.0.0.1:8000/redoc" not in result.output
376+
377+
378+
def test_no_docs() -> None:
379+
with changing_dir(assets_path):
380+
with patch.object(uvicorn, "run") as mock_run:
381+
result = runner.invoke(
382+
app, ["dev", "single_file_docs.py", "--app", "no_docs"]
383+
)
384+
assert result.exit_code == 0, result.output
385+
assert mock_run.called
386+
387+
assert "http://127.0.0.1:8000/redoc" in result.output
388+
assert "http://127.0.0.1:8000/docs" not in result.output
389+
390+
391+
def test_no_redoc() -> None:
392+
with changing_dir(assets_path):
393+
with patch.object(uvicorn, "run") as mock_run:
394+
result = runner.invoke(
395+
app, ["dev", "single_file_docs.py", "--app", "no_redoc"]
396+
)
397+
assert result.exit_code == 0, result.output
398+
assert mock_run.called
399+
400+
assert "http://127.0.0.1:8000/docs" in result.output
401+
assert "http://127.0.0.1:8000/redocs" not in result.output
402+
403+
404+
def test_full_docs() -> None:
405+
with changing_dir(assets_path):
406+
with patch.object(uvicorn, "run") as mock_run:
407+
result = runner.invoke(
408+
app, ["dev", "single_file_docs.py", "--app", "full_docs"]
409+
)
410+
assert result.exit_code == 0, result.output
411+
assert mock_run.called
412+
413+
assert "http://127.0.0.1:8000/docs" in result.output
414+
assert "http://127.0.0.1:8000/redoc" in result.output
415+
416+
417+
def test_custom_docs() -> None:
418+
with changing_dir(assets_path):
419+
with patch.object(uvicorn, "run") as mock_run:
420+
result = runner.invoke(
421+
app, ["dev", "single_file_docs.py", "--app", "custom_docs"]
422+
)
423+
assert result.exit_code == 0, result.output
424+
assert mock_run.called
425+
426+
assert "http://127.0.0.1:8000/custom-docs-url" in result.output
427+
assert "http://127.0.0.1:8000/custom-redoc-url" in result.output
428+
429+
352430
def test_run_error() -> None:
353431
with changing_dir(assets_path):
354432
result = runner.invoke(app, ["run", "non_existing_file.py"])

tests/test_discover.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77
get_import_data_from_import_string,
88
)
99
from fastapi_cli.exceptions import FastAPICLIException
10+
from tests.utils import changing_dir
1011

1112
assets_path = Path(__file__).parent / "assets"
1213

1314

1415
def test_get_import_data_from_import_string_valid() -> None:
15-
result = get_import_data_from_import_string("module.submodule:app")
16+
with changing_dir(assets_path):
17+
result = get_import_data_from_import_string("package.mod.app:app")
1618

1719
assert isinstance(result, ImportData)
1820
assert result.app_name == "app"
19-
assert result.import_string == "module.submodule:app"
20-
assert result.module_data.module_import_str == "module.submodule"
21-
assert result.module_data.extra_sys_path == Path(".").resolve()
21+
assert result.import_string == "package.mod.app:app"
22+
assert result.module_data.module_import_str == "package.mod.app"
23+
assert result.module_data.extra_sys_path == Path(assets_path).resolve()
2224
assert result.module_data.module_paths == []
2325

2426

0 commit comments

Comments
 (0)