From deaf743e67d610f0ca19b0811e85759b05ebda5b Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sat, 31 Jan 2026 17:14:31 +0000 Subject: [PATCH] Add unit tests --- tests/unit/test_models.py | 134 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 tests/unit/test_models.py diff --git a/tests/unit/test_models.py b/tests/unit/test_models.py new file mode 100644 index 0000000..aa20028 --- /dev/null +++ b/tests/unit/test_models.py @@ -0,0 +1,134 @@ +"""Tests for docgen models.""" + +import pytest +from pathlib import Path +from docgen.models import Endpoint, Parameter, Response, HTTPMethod, DocConfig, OutputFormat, ParameterIn + + +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"