90 lines
2.7 KiB
Python
90 lines
2.7 KiB
Python
"""Tests for OpenAPI validator."""
|
|
|
|
import pytest
|
|
|
|
from mockapi.core.validator import OpenAPIValidator
|
|
|
|
|
|
VALID_SPEC = {
|
|
"openapi": "3.0.3",
|
|
"info": {"title": "Test API", "version": "1.0.0"},
|
|
"paths": {
|
|
"/users": {
|
|
"get": {
|
|
"operationId": "listUsers",
|
|
"responses": {"200": {"description": "Success"}},
|
|
}
|
|
}
|
|
},
|
|
}
|
|
|
|
|
|
class TestOpenAPIValidator:
|
|
"""Test cases for OpenAPIValidator."""
|
|
|
|
def test_validate_valid_spec(self):
|
|
"""Test validating a valid OpenAPI spec."""
|
|
validator = OpenAPIValidator(VALID_SPEC)
|
|
errors = validator.validate()
|
|
assert len(errors) == 0
|
|
|
|
def test_is_valid_returns_true_for_valid_spec(self):
|
|
"""Test is_valid returns True for valid spec."""
|
|
validator = OpenAPIValidator(VALID_SPEC)
|
|
assert validator.is_valid() is True
|
|
|
|
def test_get_paths(self):
|
|
"""Test extracting paths from spec."""
|
|
validator = OpenAPIValidator(VALID_SPEC)
|
|
paths = validator.get_paths()
|
|
assert "/users" in paths
|
|
|
|
def test_get_operations(self):
|
|
"""Test extracting operations for a path."""
|
|
validator = OpenAPIValidator(VALID_SPEC)
|
|
operations = validator.get_operations("/users")
|
|
assert "get" in operations
|
|
|
|
def test_get_operations_invalid_path(self):
|
|
"""Test getting operations for non-existent path."""
|
|
validator = OpenAPIValidator(VALID_SPEC)
|
|
operations = validator.get_operations("/nonexistent")
|
|
assert len(operations) == 0
|
|
|
|
def test_get_schema(self):
|
|
"""Test extracting schema by name."""
|
|
spec = VALID_SPEC.copy()
|
|
spec["components"] = {
|
|
"schemas": {
|
|
"User": {
|
|
"type": "object",
|
|
"properties": {"name": {"type": "string"}},
|
|
}
|
|
}
|
|
}
|
|
validator = OpenAPIValidator(spec)
|
|
schema = validator.get_schema("User")
|
|
assert schema is not None
|
|
assert schema["type"] == "object"
|
|
|
|
def test_get_schema_not_found(self):
|
|
"""Test getting non-existent schema."""
|
|
validator = OpenAPIValidator(VALID_SPEC)
|
|
schema = validator.get_schema("NonExistent")
|
|
assert schema is None
|
|
|
|
def test_get_all_schemas(self):
|
|
"""Test extracting all schemas."""
|
|
spec = VALID_SPEC.copy()
|
|
spec["components"] = {
|
|
"schemas": {
|
|
"User": {"type": "object"},
|
|
"Product": {"type": "object"},
|
|
}
|
|
}
|
|
validator = OpenAPIValidator(spec)
|
|
schemas = validator.get_all_schemas()
|
|
assert len(schemas) == 2
|
|
assert "User" in schemas
|
|
assert "Product" in schemas
|