|
| 1 | +import os |
1 | 2 | import subprocess
|
2 | 3 | import sys
|
3 | 4 | from pathlib import Path
|
|
7 | 8 | from typer.testing import CliRunner
|
8 | 9 |
|
9 | 10 | from fastapi_cli.cli import app
|
| 11 | +from fastapi_cli.exceptions import FastAPICLIException |
10 | 12 | from fastapi_cli.utils.cli import get_uvicorn_log_config
|
11 | 13 | from tests.utils import changing_dir
|
12 | 14 |
|
|
15 | 17 | assets_path = Path(__file__).parent / "assets"
|
16 | 18 |
|
17 | 19 |
|
| 20 | +def read_file(filename: str, strip: bool = True) -> str: |
| 21 | + """Read file and return content as string""" |
| 22 | + with open("openapi.json") as stream: |
| 23 | + data = stream.read() |
| 24 | + return data.strip() if data and strip else data |
| 25 | + |
| 26 | + |
18 | 27 | def test_dev() -> None:
|
19 | 28 | with changing_dir(assets_path):
|
20 | 29 | with patch.object(uvicorn, "run") as mock_run:
|
@@ -251,6 +260,51 @@ def test_dev_help() -> None:
|
251 | 260 | assert "Use multiple worker processes." not in result.output
|
252 | 261 |
|
253 | 262 |
|
| 263 | +def test_schema() -> None: |
| 264 | + with changing_dir(assets_path): |
| 265 | + with open("openapi.json") as stream: |
| 266 | + expected = stream.read().strip() |
| 267 | + assert expected != "", "Failed to read expected result" |
| 268 | + result = runner.invoke(app, ["schema", "single_file_app.py"]) |
| 269 | + assert result.exit_code == 0, result.output |
| 270 | + assert expected in result.output, result.output |
| 271 | + |
| 272 | + |
| 273 | +def test_schema_file() -> None: |
| 274 | + with changing_dir(assets_path): |
| 275 | + filename = "unit-test.json" |
| 276 | + expected = read_file("openapi.json", strip=True) |
| 277 | + assert expected != "", "Failed to read expected result" |
| 278 | + result = runner.invoke( |
| 279 | + app, ["schema", "single_file_app.py", "--output", filename] |
| 280 | + ) |
| 281 | + assert os.path.isfile(filename) |
| 282 | + actual = read_file(filename, strip=True) |
| 283 | + os.remove(filename) |
| 284 | + assert result.exit_code == 0, result.output |
| 285 | + assert expected == actual |
| 286 | + |
| 287 | + |
| 288 | +def test_schema_invalid_path() -> None: |
| 289 | + with changing_dir(assets_path): |
| 290 | + result = runner.invoke(app, ["schema", "invalid/single_file_app.py"]) |
| 291 | + assert result.exit_code == 1, result.output |
| 292 | + assert isinstance(result.exception, FastAPICLIException) |
| 293 | + assert "Path does not exist invalid/single_file_app.py" in str(result.exception) |
| 294 | + |
| 295 | + |
| 296 | +# |
| 297 | +# |
| 298 | +# def test_schema_invalid_package() -> None: |
| 299 | +# with changing_dir(assets_path): |
| 300 | +# result = runner.invoke( |
| 301 | +# app, ["schema", "broken_package/mod/app.py"] |
| 302 | +# ) |
| 303 | +# assert result.exit_code == 1, result.output |
| 304 | +# assert isinstance(result.exception, ImportError) |
| 305 | +# assert "attempted relative import beyond top-level package" in str(result.exception) |
| 306 | + |
| 307 | + |
254 | 308 | def test_run_help() -> None:
|
255 | 309 | result = runner.invoke(app, ["run", "--help"])
|
256 | 310 | assert result.exit_code == 0, result.output
|
|
0 commit comments