-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(badgerdoc_cli): add tests and GH actions CI
- Loading branch information
1 parent
599a70f
commit 9cab634
Showing
7 changed files
with
115 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: badgerdoc linters and tests | ||
on: | ||
push: | ||
paths: | ||
- lib/badgerdoc_cli/** | ||
- .github/worlflows/badgerdoc-cli.yml | ||
pull_request: | ||
paths: | ||
- lib/badgerdoc_cli/** | ||
- .github/worlflows/badgerdoc-cli.yml | ||
jobs: | ||
badgerdoc-cli-test: | ||
strategy: | ||
matrix: | ||
python-version: [ "3.8.15" ] | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
cache: 'pip' | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install ".[dev]" | ||
- name: Run linters and checkers [isort -> black -> mypy -> pylint] | ||
run: | | ||
pre-commit run --all-files | ||
- name: Run tests | ||
run: | | ||
pytest tests/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from .main import cli_handler, init_cli_app | ||
|
||
__all__ = [ | ||
cli_handler, | ||
init_cli_app, | ||
"cli_handler", | ||
"init_cli_app", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from typing import Optional | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from _pytest.capture import CaptureFixture | ||
from fastapi import FastAPI | ||
|
||
from badgerdoc_cli import main | ||
|
||
|
||
def test_init_cli_app() -> None: | ||
app = FastAPI() | ||
main.init_cli_app(app) | ||
assert main.APP is app | ||
|
||
|
||
def test_init_cli_app_invalid_app() -> None: | ||
fake_app = type("FastAPI", (), {}) | ||
with pytest.raises(TypeError, match="Invalid app type. Must be FastAPI"): | ||
main.init_cli_app(fake_app) | ||
|
||
|
||
def test_verbose_param() -> None: | ||
with patch( | ||
"badgerdoc_cli.main.generate_openapi", | ||
side_effect=ValueError("Foo!"), | ||
), pytest.raises(ValueError, match="Foo"): | ||
app = FastAPI() | ||
cli_args = ("--verbose", "openapi", "foo.json") | ||
main.init_cli_handlers(app, cli_args) | ||
|
||
|
||
@pytest.mark.parametrize(("indent", "expected_indent"), ((None, 2), ("4", 4))) | ||
def test_openapi_command(indent: Optional[str], expected_indent: int) -> None: | ||
app = FastAPI() | ||
cli_args = ["openapi", "foo.json"] | ||
if indent is not None: | ||
cli_args.append(f"--indent={indent}") | ||
|
||
with patch("badgerdoc_cli.main.generate_openapi") as gendoc_mock: | ||
main.init_cli_handlers(app, cli_args) | ||
gendoc_mock.assert_called_once_with( | ||
app=app, | ||
file_path="foo.json", | ||
indent=expected_indent, | ||
) | ||
|
||
|
||
def test_openapi_command_non_initialized_cli( | ||
capsys: CaptureFixture[str], | ||
) -> None: | ||
app = None | ||
cli_args = ("openapi", "foo.json") | ||
main.init_cli_handlers(app, cli_args) | ||
captured_stdout = capsys.readouterr().out | ||
expected = "CLI is not initialized. Add init_cli_app call to set up CLI\n" | ||
assert captured_stdout == expected | ||
|
||
|
||
def test_openapi_command_invalid_file_format( | ||
capsys: CaptureFixture[str], | ||
) -> None: | ||
app = FastAPI() | ||
cli_args = ("openapi", "foo.pdf") | ||
main.init_cli_handlers(app, cli_args) | ||
captured_stdout = capsys.readouterr().out | ||
expected = "Invalid file format. Must be .json\n" | ||
assert captured_stdout == expected |