162 lines
5.2 KiB
Python
162 lines
5.2 KiB
Python
"""Pytest configuration and fixtures for OpenAPI Mock Server tests."""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_openapi_spec():
|
|
"""Sample OpenAPI 3.0 specification for testing."""
|
|
return {
|
|
"openapi": "3.0.0",
|
|
"info": {
|
|
"title": "Test API",
|
|
"version": "1.0.0",
|
|
"description": "A test API for unit tests"
|
|
},
|
|
"paths": {
|
|
"/users": {
|
|
"get": {
|
|
"summary": "List users",
|
|
"operationId": "listUsers",
|
|
"responses": {
|
|
"200": {
|
|
"description": "A list of users",
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {
|
|
"type": "array",
|
|
"items": {"$ref": "#/components/schemas/User"}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"post": {
|
|
"summary": "Create user",
|
|
"operationId": "createUser",
|
|
"requestBody": {
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {"$ref": "#/components/schemas/User"}
|
|
}
|
|
}
|
|
},
|
|
"responses": {
|
|
"201": {
|
|
"description": "User created",
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {"$ref": "#/components/schemas/User"}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"/users/{userId}": {
|
|
"get": {
|
|
"summary": "Get user by ID",
|
|
"operationId": "getUser",
|
|
"parameters": [
|
|
{
|
|
"name": "userId",
|
|
"in": "path",
|
|
"required": True,
|
|
"schema": {"type": "integer"}
|
|
}
|
|
],
|
|
"responses": {
|
|
"200": {
|
|
"description": "User found",
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {"$ref": "#/components/schemas/User"}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"components": {
|
|
"schemas": {
|
|
"User": {
|
|
"type": "object",
|
|
"required": ["id", "username", "email"],
|
|
"properties": {
|
|
"id": {"type": "integer", "format": "int64"},
|
|
"username": {"type": "string", "minLength": 3, "maxLength": 50},
|
|
"email": {"type": "string", "format": "email"},
|
|
"created_at": {"type": "string", "format": "date-time"},
|
|
"active": {"type": "boolean", "default": True}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def swagger_2_spec():
|
|
"""Sample Swagger 2.0 specification for testing."""
|
|
return {
|
|
"swagger": "2.0",
|
|
"info": {
|
|
"title": "Swagger Test API",
|
|
"version": "2.0.0"
|
|
},
|
|
"paths": {
|
|
"/pets": {
|
|
"get": {
|
|
"summary": "List pets",
|
|
"operationId": "listPets",
|
|
"responses": {
|
|
"200": {
|
|
"description": "A list of pets",
|
|
"schema": {
|
|
"type": "array",
|
|
"items": {"$ref": "#/definitions/Pet"}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"definitions": {
|
|
"Pet": {
|
|
"type": "object",
|
|
"required": ["id", "name"],
|
|
"properties": {
|
|
"id": {"type": "integer"},
|
|
"name": {"type": "string"},
|
|
"tag": {"type": "string"}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def temp_spec_file(tmp_path, sample_openapi_spec):
|
|
"""Create a temporary OpenAPI spec file."""
|
|
import yaml
|
|
spec_path = tmp_path / "test-spec.yaml"
|
|
with open(spec_path, "w") as f:
|
|
yaml.dump(sample_openapi_spec, f)
|
|
return str(spec_path)
|
|
|
|
|
|
@pytest.fixture
|
|
def invalid_yaml_file(tmp_path):
|
|
"""Create an invalid YAML file."""
|
|
file_path = tmp_path / "invalid.yaml"
|
|
with open(file_path, "w") as f:
|
|
f.write("invalid: yaml: content: [")
|
|
return str(file_path)
|