- Fix corrupted docstrings (curly braces to quotes) - Sort imports according to ruff standards - Split long line in javascript.py for readability - Add module-level docstrings to test files - Add docstring to BaseGenerator.__init__ method - Fix regex pattern in RustDetector
144 lines
4.1 KiB
Python
144 lines
4.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Tests for docgen models."""
|
|
|
|
from pathlib import Path
|
|
|
|
from docgen.models import (
|
|
DocConfig,
|
|
Endpoint,
|
|
HTTPMethod,
|
|
OutputFormat,
|
|
Parameter,
|
|
ParameterIn,
|
|
Response,
|
|
)
|
|
|
|
|
|
class TestEndpoint:
|
|
"""Tests for Endpoint model."""
|
|
|
|
def test_endpoint_creation(self):
|
|
"""Test basic endpoint creation."""
|
|
endpoint = Endpoint(
|
|
path="/api/users",
|
|
method=HTTPMethod.GET,
|
|
summary="Get all users",
|
|
)
|
|
assert endpoint.path == "/api/users"
|
|
assert endpoint.method == HTTPMethod.GET
|
|
assert endpoint.summary == "Get all users"
|
|
|
|
def test_endpoint_with_parameters(self):
|
|
"""Test endpoint with parameters."""
|
|
param = Parameter(
|
|
name="id",
|
|
type="integer",
|
|
required=True,
|
|
location=ParameterIn.PATH,
|
|
)
|
|
endpoint = Endpoint(
|
|
path="/api/users/{id}",
|
|
method=HTTPMethod.GET,
|
|
parameters=[param],
|
|
)
|
|
assert len(endpoint.parameters) == 1
|
|
assert endpoint.parameters[0].name == "id"
|
|
assert endpoint.parameters[0].required is True
|
|
|
|
def test_get_full_path(self):
|
|
"""Test full path generation."""
|
|
endpoint = Endpoint(path="/users", method=HTTPMethod.GET)
|
|
full = endpoint.get_full_path("/api")
|
|
assert full == "/api/users"
|
|
|
|
def test_get_method_color(self):
|
|
"""Test method color generation."""
|
|
endpoint = Endpoint(path="/test", method=HTTPMethod.GET)
|
|
color = endpoint.get_method_color()
|
|
assert color == "#61affe"
|
|
|
|
def test_endpoint_with_responses(self):
|
|
"""Test endpoint with responses."""
|
|
response = Response(
|
|
status_code=200,
|
|
description="Success",
|
|
example={"users": []},
|
|
)
|
|
endpoint = Endpoint(
|
|
path="/api/test",
|
|
method=HTTPMethod.POST,
|
|
responses=[response],
|
|
)
|
|
assert len(endpoint.responses) == 1
|
|
assert endpoint.responses[0].status_code == 200
|
|
|
|
|
|
class TestParameter:
|
|
"""Tests for Parameter model."""
|
|
|
|
def test_parameter_creation(self):
|
|
"""Test basic parameter creation."""
|
|
param = Parameter(
|
|
name="limit",
|
|
type="integer",
|
|
default="10",
|
|
description="Max items to return",
|
|
)
|
|
assert param.name == "limit"
|
|
assert param.type == "integer"
|
|
assert param.default == "10"
|
|
assert param.required is False
|
|
|
|
def test_parameter_all_locations(self):
|
|
"""Test all parameter locations."""
|
|
for location in ParameterIn:
|
|
param = Parameter(name="test", location=location)
|
|
assert param.location == location
|
|
|
|
|
|
class TestResponse:
|
|
"""Tests for Response model."""
|
|
|
|
def test_response_creation(self):
|
|
"""Test basic response creation."""
|
|
response = Response(
|
|
status_code=201,
|
|
description="Created",
|
|
content_type="application/json",
|
|
)
|
|
assert response.status_code == 201
|
|
assert response.description == "Created"
|
|
|
|
|
|
class TestDocConfig:
|
|
"""Tests for DocConfig model."""
|
|
|
|
def test_default_config(self):
|
|
"""Test default configuration values."""
|
|
config = DocConfig()
|
|
assert config.output_dir == Path("docs")
|
|
assert config.format == OutputFormat.HTML
|
|
assert config.verbose is False
|
|
|
|
def test_custom_config(self):
|
|
"""Test custom configuration values."""
|
|
config = DocConfig(
|
|
input_dir=Path("./src"),
|
|
output_dir=Path("./api-docs"),
|
|
format=OutputFormat.MARKDOWN,
|
|
title="My API",
|
|
)
|
|
assert config.input_dir == Path("./src")
|
|
assert config.format == OutputFormat.MARKDOWN
|
|
assert config.title == "My API"
|
|
|
|
def test_config_env_override(self):
|
|
"""Test configuration with environment variables."""
|
|
config = DocConfig(
|
|
title="Test",
|
|
description="Test description",
|
|
version="2.0.0",
|
|
)
|
|
assert config.title == "Test"
|
|
assert config.version == "2.0.0"
|