"""Unit tests for the generator modules.""" import pytest import tempfile import os from pathlib import Path from api_testgen.core import SpecParser from api_testgen.generators import PytestGenerator, JestGenerator, GoGenerator from api_testgen.mocks import MockServerGenerator class TestPytestGenerator: """Tests for PytestGenerator class.""" @pytest.fixture def parser(self, sample_openapi_spec): """Create a spec parser with sample spec.""" with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: import yaml yaml.dump(sample_openapi_spec, f) parser = SpecParser(Path(f.name)) parser.load() return parser def test_generate_creates_file(self, parser, tmp_path): """Test that generate creates a test file.""" generator = PytestGenerator(parser, output_dir=str(tmp_path)) files = generator.generate() assert len(files) == 1 assert files[0].exists() assert files[0].suffix == ".py" def test_generate_content_contains_tests(self, parser, tmp_path): """Test that generated file contains test functions.""" generator = PytestGenerator(parser, output_dir=str(tmp_path)) files = generator.generate() content = files[0].read_text() assert "test_" in content assert "BASE_URL" in content assert "def test_" in content def test_generate_with_custom_output_file(self, parser, tmp_path): """Test generating to a specific file path.""" generator = PytestGenerator(parser, output_dir=str(tmp_path)) files = generator.generate(output_file=str(tmp_path / "custom_test.py")) assert len(files) == 1 assert files[0].name == "custom_test.py" def test_generate_endpoint_test(self, parser): """Test generating a single endpoint test.""" generator = PytestGenerator(parser) test_code = generator.generate_endpoint_tests("/pets", "get") assert "test_" in test_code assert "requests.get" in test_code def test_generate_test_name(self, parser): """Test test name generation.""" generator = PytestGenerator(parser) name = generator._generate_test_name({{ "path": "/pets", "method": "get", "summary": "List all pets", }}) assert name == "get_pets" or "pets" in name class TestJestGenerator: """Tests for JestGenerator class.""" @pytest.fixture def parser(self, sample_openapi_spec): """Create a spec parser with sample spec.""" with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: import yaml yaml.dump(sample_openapi_spec, f) parser = SpecParser(Path(f.name)) parser.load() return parser def test_generate_creates_file(self, parser, tmp_path): """Test that generate creates a test file.""" generator = JestGenerator(parser, output_dir=str(tmp_path)) files = generator.generate() assert len(files) == 1 assert files[0].exists() assert files[0].suffix == ".js" def test_generate_content_contains_describe(self, parser, tmp_path): """Test that generated file contains describe blocks.""" generator = JestGenerator(parser, output_dir=str(tmp_path)) files = generator.generate() content = files[0].read_text() assert "describe" in content expect_in = "expect" in content or "toContain" in content or "toBe" in content assert expect_in def test_generate_endpoint_test(self, parser): """Test generating a single endpoint test.""" generator = JestGenerator(parser) test_code = generator.generate_endpoint_tests("/pets", "get") assert "describe" in test_code or "it(" in test_code class TestGoGenerator: """Tests for GoGenerator class.""" @pytest.fixture def parser(self, sample_openapi_spec): """Create a spec parser with sample spec.""" with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: import yaml yaml.dump(sample_openapi_spec, f) parser = SpecParser(Path(f.name)) parser.load() return parser def test_generate_creates_file(self, parser, tmp_path): """Test that generate creates a test file.""" generator = GoGenerator(parser, output_dir=str(tmp_path)) files = generator.generate() assert len(files) == 1 assert files[0].exists() assert files[0].name.endswith("_test.go") def test_generate_content_contains_tests(self, parser, tmp_path): """Test that generated file contains test functions.""" generator = GoGenerator(parser, output_dir=str(tmp_path)) files = generator.generate() content = files[0].read_text() assert "func Test" in content assert "http.Client" in content or "http.NewRequest" in content def test_generate_with_custom_package(self, parser, tmp_path): """Test generating with custom package name.""" generator = GoGenerator(parser, output_dir=str(tmp_path), package_name="custompkg") files = generator.generate() content = files[0].read_text() assert "package custompkg" in content class TestMockServerGenerator: """Tests for MockServerGenerator class.""" @pytest.fixture def parser(self, sample_openapi_spec): """Create a spec parser with sample spec.""" with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: import yaml yaml.dump(sample_openapi_spec, f) parser = SpecParser(Path(f.name)) parser.load() return parser def test_generate_prism_config(self, parser, tmp_path): """Test generating Prism configuration.""" generator = MockServerGenerator(parser, output_dir=str(tmp_path)) file = generator.generate_prism_config() assert file.exists() assert file.name == "prism-config.json" content = file.read_text() import json config = json.loads(content) assert "mock" in config assert "port" in config["mock"] def test_generate_docker_compose(self, parser, tmp_path): """Test generating Docker Compose configuration.""" generator = MockServerGenerator(parser, output_dir=str(tmp_path)) file = generator.generate_docker_compose() assert file.exists() assert file.name == "docker-compose.yml" content = file.read_text() assert "services" in content assert "mock-server" in content def test_generate_dockerfile(self, parser, tmp_path): """Test generating Dockerfile.""" generator = MockServerGenerator(parser, output_dir=str(tmp_path)) file = generator.generate_dockerfile() assert file.exists() assert file.name == "Dockerfile" content = file.read_text() assert "FROM" in content assert "prism" in content.lower() def test_generate_all(self, parser, tmp_path): """Test generating all mock server files.""" generator = MockServerGenerator(parser, output_dir=str(tmp_path)) files = generator.generate() assert len(files) == 3 file_names = [f.name for f in files] assert "prism-config.json" in file_names assert "docker-compose.yml" in file_names assert "Dockerfile" in file_names