Files
openapi-mock-generator/tests/test_parser.py
7000pctAUTO 05fdab9657
Some checks failed
CI / test (push) Has been cancelled
Add test files: conftest, parser tests, generator tests
2026-02-02 19:58:15 +00:00

145 lines
4.3 KiB
Python

"""Tests for OpenAPI Spec Parser."""
import pytest
from pathlib import Path
from src.core.parser import (
OpenAPIParser,
OpenAPIParserError,
InvalidOpenAPIFormat,
UnsupportedOpenAPIVersion,
)
class TestOpenAPIParser:
"""Tests for OpenAPIParser class."""
def test_load_valid_yaml_spec(self, temp_spec_file):
"""Test loading a valid YAML OpenAPI specification."""
parser = OpenAPIParser(str(temp_spec_file))
spec = parser.load()
assert spec is not None
assert spec["openapi"] == "3.0.0"
assert spec["info"]["title"] == "Sample API"
assert spec["info"]["version"] == "1.0.0"
def test_load_valid_json_spec(self, temp_json_spec_file):
"""Test loading a valid JSON OpenAPI specification."""
parser = OpenAPIParser(str(temp_json_spec_file))
spec = parser.load()
assert spec is not None
assert spec["openapi"] == "3.0.0"
def test_file_not_found(self):
"""Test handling of non-existent spec file."""
parser = OpenAPIParser("/nonexistent/path/openapi.yaml")
with pytest.raises(FileNotFoundError):
parser.load()
def test_get_paths(self, temp_spec_file):
"""Test extracting paths from specification."""
parser = OpenAPIParser(str(temp_spec_file))
parser.load()
paths = parser.get_paths()
assert "/users" in paths
assert "/users/{userId}" in paths
assert "/posts" in paths
def test_get_response_schema(self, temp_spec_file):
"""Test extracting response schema for an operation."""
parser = OpenAPIParser(str(temp_spec_file))
parser.load()
schema = parser.get_response_schema("/users", "get", "200")
assert schema is not None
def test_get_response_schema_not_found(self, temp_spec_file):
"""Test response schema not found for undefined status code."""
parser = OpenAPIParser(str(temp_spec_file))
parser.load()
schema = parser.get_response_schema("/users", "get", "500")
assert schema is None
def test_version_extraction(self, temp_spec_file):
"""Test OpenAPI version extraction."""
parser = OpenAPIParser(str(temp_spec_file))
parser.load()
assert parser.version == "3.0.0"
def test_get_servers(self, temp_spec_file):
"""Test extracting servers from specification."""
parser = OpenAPIParser(str(temp_spec_file))
parser.load()
servers = parser.get_servers()
assert isinstance(servers, list)
def test_get_base_path(self, temp_spec_file):
"""Test extracting base path from specification."""
parser = OpenAPIParser(str(temp_spec_file))
parser.load()
base_path = parser.get_base_path()
assert isinstance(base_path, str)
class TestOpenAPIParserErrors:
"""Tests for parser error handling."""
def test_invalid_yaml_format(self, tmp_path):
"""Test handling of invalid YAML format."""
invalid_file = tmp_path / "invalid.yaml"
invalid_file.write_text("not: valid: yaml: content: [")
parser = OpenAPIParser(str(invalid_file))
with pytest.raises(InvalidOpenAPIFormat):
parser.load()
def test_missing_openapi_version(self, tmp_path):
"""Test handling of missing OpenAPI version."""
invalid_file = tmp_path / "missing_version.yaml"
invalid_file.write_text("""
info:
title: Test API
version: \"1.0.0\"
paths: {}
""")
parser = OpenAPIParser(str(invalid_file))
with pytest.raises(InvalidOpenAPIFormat):
parser.load()
def test_unsupported_version(self, tmp_path):
"""Test handling of unsupported OpenAPI version."""
invalid_file = tmp_path / "unsupported.yaml"
invalid_file.write_text("""
openapi: \"2.0.0\"
info:
title: Test API
version: \"1.0.0\"
paths: {}
""")
parser = OpenAPIParser(str(invalid_file))
with pytest.raises(UnsupportedOpenAPIVersion):
parser.load()
def test_invalid_json_format(self, tmp_path):
"""Test handling of invalid JSON format."""
invalid_file = tmp_path / "invalid.json"
invalid_file.write_text("{invalid json}")
parser = OpenAPIParser(str(invalid_file))
with pytest.raises(InvalidOpenAPIFormat):
parser.load()