91 lines
2.6 KiB
Python
91 lines
2.6 KiB
Python
"""Integration tests for CLI."""
|
|
|
|
import os
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
import yaml
|
|
from click.testing import CliRunner
|
|
|
|
from mockapi.cli.main import cli
|
|
|
|
|
|
class TestCLIIntegration:
|
|
"""Test cases for CLI integration."""
|
|
|
|
def test_validate_command(self):
|
|
"""Test validate command with valid spec."""
|
|
spec = {
|
|
"openapi": "3.0.0",
|
|
"info": {"title": "Test API", "version": "1.0.0"},
|
|
"paths": {
|
|
"/users": {
|
|
"get": {"operationId": "getUsers", "responses": {"200": {"description": "OK"}}},
|
|
},
|
|
},
|
|
}
|
|
|
|
with tempfile.NamedTemporaryFile(
|
|
mode="w", suffix=".yaml", delete=False
|
|
) as f:
|
|
yaml.dump(spec, f)
|
|
f.flush()
|
|
|
|
runner = CliRunner()
|
|
result = runner.invoke(cli, ["validate", f.name])
|
|
|
|
assert result.exit_code == 0
|
|
assert "valid" in result.output.lower()
|
|
|
|
Path(f.name).unlink()
|
|
|
|
def test_validate_nonexistent_file(self):
|
|
"""Test validate command with non-existent file."""
|
|
runner = CliRunner()
|
|
result = runner.invoke(cli, ["validate", "/nonexistent/file.yaml"])
|
|
assert result.exit_code != 0
|
|
|
|
def test_generate_command(self):
|
|
"""Test generate command."""
|
|
spec = {
|
|
"openapi": "3.0.0",
|
|
"info": {"title": "Test API", "version": "1.0.0"},
|
|
"paths": {
|
|
"/users": {
|
|
"get": {"operationId": "getUsers", "summary": "Get all users"},
|
|
},
|
|
},
|
|
}
|
|
|
|
with tempfile.NamedTemporaryFile(
|
|
mode="w", suffix=".yaml", delete=False
|
|
) as f:
|
|
yaml.dump(spec, f)
|
|
f.flush()
|
|
|
|
runner = CliRunner()
|
|
result = runner.invoke(cli, ["generate", f.name])
|
|
|
|
assert result.exit_code == 0
|
|
assert "users" in result.output.lower()
|
|
|
|
Path(f.name).unlink()
|
|
|
|
def test_show_config_command(self):
|
|
"""Test show-config command."""
|
|
runner = CliRunner()
|
|
result = runner.invoke(cli, ["show-config"])
|
|
assert result.exit_code == 0
|
|
assert "Port" in result.output
|
|
|
|
def test_cli_version(self):
|
|
"""Test CLI version."""
|
|
runner = CliRunner()
|
|
result = runner.invoke(cli, ["--version"])
|
|
assert result.exit_code == 0
|
|
|
|
def test_cli_help(self):
|
|
"""Test CLI help."""
|
|
runner = CliRunner()
|
|
result = runner.invoke(cli, ["--help"])
|
|
assert result.exit_code == 0 |