268 lines
6.5 KiB
Python
268 lines
6.5 KiB
Python
"""Pytest configuration and fixtures for OpenAPI Mock Generator tests."""
|
|
|
|
import pytest
|
|
import tempfile
|
|
import json
|
|
from pathlib import Path
|
|
|
|
|
|
@pytest.fixture
|
|
def temp_dir():
|
|
"""Create a temporary directory for tests."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
yield Path(tmpdir)
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_openapi_yaml():
|
|
"""Sample OpenAPI specification in YAML format."""
|
|
return """
|
|
openapi: \"3.0.0\"
|
|
info:
|
|
title: Sample API
|
|
version: \"1.0.0\"
|
|
paths:
|
|
/users:
|
|
get:
|
|
summary: Get all users
|
|
parameters:
|
|
- name: page
|
|
in: query
|
|
schema:
|
|
type: integer
|
|
- name: limit
|
|
in: query
|
|
schema:
|
|
type: integer
|
|
responses:
|
|
'200':
|
|
description: Successful response
|
|
content:
|
|
application/json:
|
|
schema:
|
|
type: array
|
|
items:
|
|
type: object
|
|
properties:
|
|
id:
|
|
type: integer
|
|
name:
|
|
type: string
|
|
email:
|
|
type: string
|
|
format: email
|
|
createdAt:
|
|
type: string
|
|
format: date-time
|
|
/users/{userId}:
|
|
get:
|
|
summary: Get user by ID
|
|
parameters:
|
|
- name: userId
|
|
in: path
|
|
required: true
|
|
schema:
|
|
type: integer
|
|
responses:
|
|
'200':
|
|
description: Successful response
|
|
content:
|
|
application/json:
|
|
schema:
|
|
type: object
|
|
properties:
|
|
id:
|
|
type: integer
|
|
name:
|
|
type: string
|
|
email:
|
|
type: string
|
|
format: email
|
|
'404':
|
|
description: User not found
|
|
content:
|
|
application/json:
|
|
schema:
|
|
type: object
|
|
properties:
|
|
error:
|
|
type: string
|
|
message:
|
|
type: string
|
|
put:
|
|
summary: Update user
|
|
parameters:
|
|
- name: userId
|
|
in: path
|
|
required: true
|
|
schema:
|
|
type: integer
|
|
requestBody:
|
|
required: true
|
|
content:
|
|
application/json:
|
|
schema:
|
|
type: object
|
|
properties:
|
|
name:
|
|
type: string
|
|
email:
|
|
type: string
|
|
format: email
|
|
required:
|
|
- name
|
|
responses:
|
|
'200':
|
|
description: User updated
|
|
content:
|
|
application/json:
|
|
schema:
|
|
type: object
|
|
properties:
|
|
id:
|
|
type: integer
|
|
name:
|
|
type: string
|
|
/posts:
|
|
post:
|
|
summary: Create a post
|
|
requestBody:
|
|
required: true
|
|
content:
|
|
application/json:
|
|
schema:
|
|
type: object
|
|
properties:
|
|
title:
|
|
type: string
|
|
content:
|
|
type: string
|
|
authorId:
|
|
type: integer
|
|
responses:
|
|
'201':
|
|
description: Post created
|
|
content:
|
|
application/json:
|
|
schema:
|
|
type: object
|
|
properties:
|
|
id:
|
|
type: integer
|
|
title:
|
|
type: string
|
|
content:
|
|
type: string
|
|
authorId:
|
|
type: integer
|
|
publishedAt:
|
|
type: string
|
|
format: date-time
|
|
components:
|
|
schemas:
|
|
User:
|
|
type: object
|
|
properties:
|
|
id:
|
|
type: integer
|
|
name:
|
|
type: string
|
|
email:
|
|
type: string
|
|
format: email
|
|
createdAt:
|
|
type: string
|
|
format: date-time
|
|
Post:
|
|
type: object
|
|
properties:
|
|
id:
|
|
type: integer
|
|
title:
|
|
type: string
|
|
content:
|
|
type: string
|
|
authorId:
|
|
type: integer
|
|
publishedAt:
|
|
type: string
|
|
format: date-time
|
|
Error:
|
|
type: object
|
|
properties:
|
|
error:
|
|
type: string
|
|
message:
|
|
type: string
|
|
"""
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_openapi_json():
|
|
"""Sample OpenAPI specification in JSON format."""
|
|
return json.dumps({
|
|
"openapi": "3.0.0",
|
|
"info": {
|
|
"title": "Sample API",
|
|
"version": "1.0.0"
|
|
},
|
|
"paths": {
|
|
"/users": {
|
|
"get": {
|
|
"summary": "Get all users",
|
|
"responses": {
|
|
"200": {
|
|
"description": "Success",
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {
|
|
"type": "array",
|
|
"items": {
|
|
"type": "object",
|
|
"properties": {
|
|
"id": {"type": "integer"},
|
|
"name": {"type": "string"}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}, indent=2)
|
|
|
|
|
|
@pytest.fixture
|
|
def invalid_openapi_yaml():
|
|
"""Invalid OpenAPI specification (missing openapi version)."""
|
|
return """
|
|
info:
|
|
title: Invalid API
|
|
version: \"1.0.0\"
|
|
paths:
|
|
/users:
|
|
get:
|
|
responses:
|
|
'200':
|
|
description: Success
|
|
"""
|
|
|
|
|
|
@pytest.fixture
|
|
def temp_spec_file(sample_openapi_yaml, tmp_path):
|
|
"""Create a temporary OpenAPI spec file."""
|
|
spec_file = tmp_path / "openapi.yaml"
|
|
spec_file.write_text(sample_openapi_yaml)
|
|
return str(spec_file)
|
|
|
|
|
|
@pytest.fixture
|
|
def temp_json_spec_file(sample_openapi_json, tmp_path):
|
|
"""Create a temporary OpenAPI spec file in JSON format."""
|
|
spec_file = tmp_path / "openapi.json"
|
|
spec_file.write_text(sample_openapi_json)
|
|
return str(spec_file)
|