"""Tests for OpenAPI validator.""" import pytest from mockapi.core.validator import OpenAPIValidator class TestOpenAPIValidator: """Test cases for OpenAPIValidator.""" def test_validate_valid_spec(self): """Test validating a valid OpenAPI spec.""" spec = { "openapi": "3.0.0", "info": {"title": "Test", "version": "1.0.0"}, "paths": {}, } validator = OpenAPIValidator(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.""" spec = { "openapi": "3.0.0", "info": {"title": "Test", "version": "1.0.0"}, "paths": {}, } validator = OpenAPIValidator(spec) assert validator.is_valid() is True def test_get_paths(self): """Test getting paths from spec.""" spec = { "openapi": "3.0.0", "info": {"title": "Test", "version": "1.0.0"}, "paths": { "/users": {}, "/items": {}, }, } validator = OpenAPIValidator(spec) paths = validator.get_paths() assert "/users" in paths assert "/items" in paths def test_get_operations(self): """Test getting operations for a path.""" spec = { "openapi": "3.0.0", "info": {"title": "Test", "version": "1.0.0"}, "paths": { "/users": { "get": {"operationId": "getUsers"}, "post": {"operationId": "createUser"}, }, }, } validator = OpenAPIValidator(spec) ops = validator.get_operations("/users") assert "get" in ops assert "post" in ops def test_get_operations_invalid_path(self): """Test getting operations for invalid path.""" spec = { "openapi": "3.0.0", "info": {"title": "Test", "version": "1.0.0"}, "paths": {}, } validator = OpenAPIValidator(spec) ops = validator.get_operations("/nonexistent") assert len(ops) == 0 def test_get_schema(self): """Test getting a schema by name.""" spec = { "openapi": "3.0.0", "info": {"title": "Test", "version": "1.0.0"}, "paths": {}, "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.""" spec = { "openapi": "3.0.0", "info": {"title": "Test", "version": "1.0.0"}, "paths": {}, } validator = OpenAPIValidator(spec) schema = validator.get_schema("NonExistent") assert schema is None def test_get_all_schemas(self): """Test getting all schemas.""" spec = { "openapi": "3.0.0", "info": {"title": "Test", "version": "1.0.0"}, "paths": {}, "components": { "schemas": { "User": {"type": "object"}, "Item": {"type": "object"}, }, }, } validator = OpenAPIValidator(spec) schemas = validator.get_all_schemas() assert "User" in schemas assert "Item" in schemas