Files
schema2mock/tests/test_cli.py

88 lines
2.6 KiB
Python

import json
import pytest
from click.testing import CliRunner
from api_mock_cli.cli import cli
class TestCLI:
@pytest.fixture
def runner(self):
return CliRunner()
@pytest.fixture
def sample_har_file(self, tmp_path, sample_har_data):
har_file = tmp_path / "test.har"
har_file.write_text(json.dumps(sample_har_data))
return str(har_file)
def test_cli_version(self, runner):
result = runner.invoke(cli, ["--version"])
assert result.exit_code == 0
assert "0.1.0" in result.output
def test_capture_command(self, runner, sample_har_file, tmp_path):
output = tmp_path / "captured.json"
result = runner.invoke(
cli,
["capture", sample_har_file, "--output", str(output)],
)
assert result.exit_code == 0
assert output.exists()
def test_generate_command(self, runner, sample_har_file, tmp_path):
output = tmp_path / "mock_server.py"
result = runner.invoke(
cli,
["generate", sample_har_file, "--output", str(output)],
)
assert result.exit_code == 0
assert output.exists()
content = output.read_text()
assert "from flask import Flask" in content
def test_capture_invalid_file(self, runner):
result = runner.invoke(
cli,
["capture", "/nonexistent/file.har"],
)
assert result.exit_code != 0
def test_generate_invalid_file(self, runner):
result = runner.invoke(
cli,
["generate", "/nonexistent/file.har"],
)
assert result.exit_code != 0
def test_serve_command_with_har(self, runner, sample_har_file):
result = runner.invoke(
cli,
["serve", sample_har_file, "--port", "5001"],
)
assert result.exit_code != 0
def test_run_command_file_not_found(self, runner):
result = runner.invoke(
cli,
["run", "/nonexistent/mock_server.py"],
)
assert result.exit_code != 0
def test_cli_help(self, runner):
result = runner.invoke(cli, ["--help"])
assert result.exit_code == 0
assert "Commands:" in result.output
def test_capture_help(self, runner):
result = runner.invoke(cli, ["capture", "--help"])
assert result.exit_code == 0
def test_generate_help(self, runner):
result = runner.invoke(cli, ["generate", "--help"])
assert result.exit_code == 0
def test_serve_help(self, runner):
result = runner.invoke(cli, ["serve", "--help"])
assert result.exit_code == 0