diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..ddd2575 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,147 @@ +"""Unit tests for the CLI module.""" +import pytest +import tempfile +from pathlib import Path +from click.testing import CliRunner + +from api_testgen.cli.main import main + + +class TestCLI: + """Tests for CLI commands.""" + + @pytest.fixture + def sample_spec_file(self, sample_openapi_spec): + """Create a temporary OpenAPI spec file.""" + with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: + import yaml + yaml.dump(sample_openapi_spec, f) + return Path(f.name) + + def test_main_help(self): + """Test that main command shows help.""" + runner = CliRunner() + result = runner.invoke(main, ["--help"]) + + assert result.exit_code == 0 + assert "API TestGen" in result.output + assert "parse" in result.output + assert "generate" in result.output + assert "mock" in result.output + + def test_parse_without_spec_shows_error(self): + """Test that parse without spec shows error.""" + runner = CliRunner() + result = runner.invoke(main, ["parse"]) + + assert result.exit_code == 1 + assert "Error: --spec option is required" in result.output + + def test_parse_with_valid_spec(self, sample_spec_file): + """Test parsing a valid OpenAPI spec.""" + runner = CliRunner() + result = runner.invoke(main, ["--spec", str(sample_spec_file), "parse"]) + + assert result.exit_code == 0 + assert "Test Pet Store API" in result.output + assert "Endpoints: 1" in result.output + + def test_generate_without_spec_shows_error(self): + """Test that generate without spec shows error.""" + runner = CliRunner() + result = runner.invoke(main, ["generate", "pytest"]) + + assert result.exit_code == 1 + assert "Error: --spec option is required" in result.output + + def test_generate_pytest(self, sample_spec_file, tmp_path): + """Test generating pytest tests.""" + runner = CliRunner() + result = runner.invoke(main, [ + "--spec", str(sample_spec_file), + "--output", str(tmp_path), + "generate", "pytest" + ]) + + assert result.exit_code == 0 + assert "Generated" in result.output + assert "test_" in result.output + + def test_generate_jest(self, sample_spec_file, tmp_path): + """Test generating Jest tests.""" + runner = CliRunner() + result = runner.invoke(main, [ + "--spec", str(sample_spec_file), + "--output", str(tmp_path), + "generate", "jest" + ]) + + assert result.exit_code == 0 + assert "Generated" in result.output + assert ".test.js" in result.output or "describe" in result.output + + def test_generate_go(self, sample_spec_file, tmp_path): + """Test generating Go tests.""" + runner = CliRunner() + result = runner.invoke(main, [ + "--spec", str(sample_spec_file), + "--output", str(tmp_path), + "generate", "go" + ]) + + assert result.exit_code == 0 + assert "Generated" in result.output + assert "_test.go" in result.output + + def test_mock_without_spec_shows_error(self): + """Test that mock without spec shows error.""" + runner = CliRunner() + result = runner.invoke(main, ["mock"]) + + assert result.exit_code == 1 + assert "Error: --spec option is required" in result.output + + def test_mock_generate(self, sample_spec_file, tmp_path): + """Test generating mock server configuration.""" + runner = CliRunner() + result = runner.invoke(main, [ + "--spec", str(sample_spec_file), + "--output", str(tmp_path), + "mock" + ]) + + assert result.exit_code == 0 + assert "Generated" in result.output + + def test_all_command(self, sample_spec_file, tmp_path): + """Test generating all files with 'all' command.""" + runner = CliRunner() + result = runner.invoke(main, [ + "--spec", str(sample_spec_file), + "--output", str(tmp_path), + "all", "pytest" + ]) + + assert result.exit_code == 0 + assert "Generated" in result.output + assert "All files generated successfully" in result.output + + +@pytest.fixture +def sample_openapi_spec(): + """Sample OpenAPI specification for testing.""" + return {{ + "openapi": "3.0.0", + "info": {{ + "title": "Test Pet Store API", + "version": "1.0.0" + }}, + "paths": {{ + "/pets": {{ + "get": {{ + "summary": "List all pets", + "responses": {{"200": {{"description": "OK"}}}} + }} + }} + }} + }}