diff --git a/tests/integration/test_generation.py b/tests/integration/test_generation.py new file mode 100644 index 0000000..420adff --- /dev/null +++ b/tests/integration/test_generation.py @@ -0,0 +1,147 @@ +"""Integration tests for documentation generation.""" + +import pytest +import tempfile +import os +from pathlib import Path + + +class TestHTMLGeneration: + """Tests for HTML documentation generation.""" + + def test_html_generator_basic(self): + """Test basic HTML generation.""" + from docgen.models import Endpoint, HTTPMethod, DocConfig + from docgen.generators import HTMLGenerator + + endpoints = [ + Endpoint( + path="/api/users", + method=HTTPMethod.GET, + summary="Get users", + ), + Endpoint( + path="/api/users", + method=HTTPMethod.POST, + summary="Create user", + ), + ] + + config = DocConfig( + title="Test API", + description="A test API", + version="1.0.0", + ) + + with tempfile.TemporaryDirectory() as tmpdir: + generator = HTMLGenerator(config) + output_path = generator.generate(endpoints, Path(tmpdir)) + + assert output_path.exists() + assert output_path.name == "index.html" + + content = output_path.read_text() + assert "Test API" in content + assert "/api/users" in content + + def test_html_generator_grouped_endpoints(self): + """Test endpoint grouping in HTML.""" + from docgen.models import Endpoint, HTTPMethod, DocConfig + from docgen.generators import HTMLGenerator + + endpoints = [ + Endpoint( + path="/users", + method=HTTPMethod.GET, + tags=["users"], + ), + Endpoint( + path="/items", + method=HTTPMethod.GET, + tags=["items"], + ), + ] + + with tempfile.TemporaryDirectory() as tmpdir: + generator = HTMLGenerator(DocConfig()) + generator.generate(endpoints, Path(tmpdir)) + + output_static = Path(tmpdir) / "static" + assert output_static.exists() + + +class TestMarkdownGeneration: + """Tests for Markdown documentation generation.""" + + def test_markdown_generator_basic(self): + """Test basic Markdown generation.""" + from docgen.models import Endpoint, HTTPMethod, DocConfig + from docgen.generators import MarkdownGenerator + + endpoints = [ + Endpoint( + path="/api/users", + method=HTTPMethod.GET, + summary="Get all users", + ), + ] + + config = DocConfig( + title="My API", + description="API documentation", + ) + + with tempfile.TemporaryDirectory() as tmpdir: + generator = MarkdownGenerator(config) + output_path = generator.generate(endpoints, Path(tmpdir)) + + assert output_path.exists() + assert output_path.name == "README.md" + + content = output_path.read_text() + assert "# My API" in content + assert "GET" in content + assert "/api/users" in content + + +class TestOpenAPIGeneration: + """Tests for OpenAPI specification generation.""" + + def test_openapi_generator_basic(self): + """Test basic OpenAPI generation.""" + from docgen.models import Endpoint, HTTPMethod, DocConfig, Parameter, ParameterIn + from docgen.generators import OpenAPIGenerator + import json + + param = Parameter( + name="id", + type="integer", + required=True, + location=ParameterIn.PATH, + ) + + endpoints = [ + Endpoint( + path="/users/{id}", + method=HTTPMethod.GET, + summary="Get user by ID", + parameters=[param], + ), + ] + + config = DocConfig( + title="Test API", + version="1.0.0", + ) + + with tempfile.TemporaryDirectory() as tmpdir: + generator = OpenAPIGenerator(config) + output_path = generator.generate(endpoints, Path(tmpdir)) + + assert output_path.exists() + assert output_path.name == "openapi.json" + + spec = json.loads(output_path.read_text()) + assert spec["openapi"] == "3.0.3" + assert spec["info"]["title"] == "Test API" + assert "/users/{id}" in spec["paths"]