Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions docs/source/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ Usage
Extended Description
^^^^^^^^^^^^^^^^^^^^

The :samp:`openalchemy build` command builds a reusable Python package based off of
the specification file.
The :samp:`openalchemy build` command builds a reusable Python package based off
of the specification file.

For instance, running the following command::

Expand Down Expand Up @@ -54,3 +54,29 @@ Options
+-----------------+--------------+-------------------------------------------+
| --format, -f | sdist, wheel | limit the format to either sdist or wheel |
+-----------------+--------------+-------------------------------------------+

openalchemy generate
---------------------

Description
^^^^^^^^^^^

Generate the models described in the OpenAPI specification file.

Usage
^^^^^

.. program:: openalchemy

.. option:: openalchemy generate SPECFILE OUTPUT_FILE


Extended Description
^^^^^^^^^^^^^^^^^^^^

The :samp:`openalchemy generate` command generates the models without having to
start an application.

Example::

openalchemy generate openapi.yml models.py
33 changes: 33 additions & 0 deletions open_alchemy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from open_alchemy import build_json
from open_alchemy import build_yaml
from open_alchemy import exceptions
from open_alchemy import init_json
from open_alchemy import init_yaml

# Configure the logger.
logging.basicConfig(format="%(message)s", level=logging.INFO)
Expand Down Expand Up @@ -75,6 +77,18 @@ def build_application_parser() -> argparse.Namespace:
)
build_parser.set_defaults(func=build)

# Define the parser for the "generate" subcommand.
generate_parser = subparsers.add_parser(
"generate",
description="Generate the SQLAlchemy models.",
help="regenerate models",
)
generate_parser.add_argument(
"specfile", type=str, help="specify the specification file"
)
generate_parser.add_argument("output", type=str, help="specify the output file")
generate_parser.set_defaults(func=generate)

# Return the parsed arguments for a particular command.
return parser.parse_args()

Expand Down Expand Up @@ -103,3 +117,22 @@ def build(args: argparse.Namespace) -> None:
# Build the package.
builder = builders.get(specfile.suffix.lower())
builder(args.specfile, args.name, args.output, fmt.get(args.format)) # type: ignore


def generate(args):
"""
Define the generate subcommand.

Args:
args: CLI arguments from the parser.
"""
# Check the specfile.
specfile = pathlib.Path(args.specfile)
validate_specfile(specfile)

# Select the generator method.
generators = dict(zip(VALID_EXTENSIONS, [init_json, init_yaml, init_yaml]))

# Regenerate the models.
generator = generators.get(specfile.suffix.lower())
generator(args.specfile, models_filename=args.output)
31 changes: 27 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests for the CLI."""

import argparse
import os
import pathlib
import sys
Expand Down Expand Up @@ -29,7 +30,6 @@ def test_validate_specfile_valid(tmp_path, specfile):
"""
spec = tmp_path / specfile
spec.write_text("")

cli.validate_specfile(spec)


Expand Down Expand Up @@ -64,9 +64,14 @@ def test_validate_specfile_does_not_exist(tmp_path):
"command, expected_arguments",
[
pytest.param(
["openalchemy", "build", "my_specfile", "my_package", "my_output_dir"],
["specfile='my_specfile'", "name='my_package'", "output='my_output_dir'"],
id="cli help command",
["openalchemy", "build", "specfile.yaml", "my_package", "my_output_dir"],
["specfile='specfile.yaml'", "name='my_package'", "output='my_output_dir'"],
id="cli build command",
),
pytest.param(
["openalchemy", "generate", "specfile.yaml", "models.py"],
["specfile='specfile.yaml'", "output='models.py'"],
id="cli generate command",
),
],
)
Expand Down Expand Up @@ -104,6 +109,24 @@ def test_build(mocker):
m_build_json.assert_called_once()


@pytest.mark.cli
def test_generate(tmp_path):
"""
GIVEN arguments from the parser
WHEN they are passed to the generate() function
THEN the function generate models
"""
model_file = tmp_path / "models.py"
args = argparse.Namespace(
specfile=f"{pathlib.Path.cwd() / 'examples' / 'simple' / 'example-spec.yml'}",
output=str(model_file),
)

cli.generate(args)

assert "Autogenerated SQLAlchemy models" in model_file.read_text()


@pytest.mark.parametrize(
"command, expected_file",
[
Expand Down