|
| 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:
|
@@ -377,6 +386,51 @@ def test_dev_help() -> None:
|
377 | 386 | assert "Use multiple worker processes." not in result.output
|
378 | 387 |
|
379 | 388 |
|
| 389 | +def test_schema() -> None: |
| 390 | + with changing_dir(assets_path): |
| 391 | + with open("openapi.json") as stream: |
| 392 | + expected = stream.read().strip() |
| 393 | + assert expected != "", "Failed to read expected result" |
| 394 | + result = runner.invoke(app, ["schema", "single_file_app.py"]) |
| 395 | + assert result.exit_code == 0, result.output |
| 396 | + assert expected in result.output, result.output |
| 397 | + |
| 398 | + |
| 399 | +def test_schema_file() -> None: |
| 400 | + with changing_dir(assets_path): |
| 401 | + filename = "unit-test.json" |
| 402 | + expected = read_file("openapi.json", strip=True) |
| 403 | + assert expected != "", "Failed to read expected result" |
| 404 | + result = runner.invoke( |
| 405 | + app, ["schema", "single_file_app.py", "--output", filename] |
| 406 | + ) |
| 407 | + assert os.path.isfile(filename) |
| 408 | + actual = read_file(filename, strip=True) |
| 409 | + os.remove(filename) |
| 410 | + assert result.exit_code == 0, result.output |
| 411 | + assert expected == actual |
| 412 | + |
| 413 | + |
| 414 | +def test_schema_invalid_path() -> None: |
| 415 | + with changing_dir(assets_path): |
| 416 | + result = runner.invoke(app, ["schema", "invalid/single_file_app.py"]) |
| 417 | + assert result.exit_code == 1, result.output |
| 418 | + assert isinstance(result.exception, FastAPICLIException) |
| 419 | + assert "Path does not exist invalid/single_file_app.py" in str(result.exception) |
| 420 | + |
| 421 | + |
| 422 | +# |
| 423 | +# |
| 424 | +# def test_schema_invalid_package() -> None: |
| 425 | +# with changing_dir(assets_path): |
| 426 | +# result = runner.invoke( |
| 427 | +# app, ["schema", "broken_package/mod/app.py"] |
| 428 | +# ) |
| 429 | +# assert result.exit_code == 1, result.output |
| 430 | +# assert isinstance(result.exception, ImportError) |
| 431 | +# assert "attempted relative import beyond top-level package" in str(result.exception) |
| 432 | + |
| 433 | + |
380 | 434 | def test_run_help() -> None:
|
381 | 435 | result = runner.invoke(app, ["run", "--help"])
|
382 | 436 | assert result.exit_code == 0, result.output
|
|
0 commit comments