75 lines
1.9 KiB
Python
75 lines
1.9 KiB
Python
import pytest
|
|
from src.core.parser import parse_openapi_spec, load_spec_file, ParseError
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_spec(tmp_path):
|
|
spec = {
|
|
"openapi": "3.0.0",
|
|
"info": {
|
|
"title": "Test API",
|
|
"version": "1.0.0"
|
|
},
|
|
"paths": {
|
|
"/users": {
|
|
"get": {
|
|
"summary": "Get users",
|
|
"description": "Retrieve a list of users",
|
|
"tags": ["users"],
|
|
"responses": {
|
|
"200": {"description": "Successful response"}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
path = tmp_path / "test.json"
|
|
path.write_text(__import__('json').dumps(spec))
|
|
return path
|
|
|
|
|
|
def test_parse_valid_spec(sample_spec):
|
|
result = parse_openapi_spec(str(sample_spec))
|
|
assert result['valid'] == True
|
|
assert result['title'] == 'Test API'
|
|
assert result['version_num'] == '1.0.0'
|
|
|
|
|
|
def test_parse_invalid_spec(tmp_path):
|
|
invalid = tmp_path / "invalid.json"
|
|
invalid.write_text('{"invalid": "spec"}')
|
|
result = parse_openapi_spec(str(invalid))
|
|
assert result['valid'] == False
|
|
assert 'errors' in result
|
|
|
|
|
|
def test_load_json_spec(sample_spec):
|
|
spec = load_spec_file(str(sample_spec))
|
|
assert spec['info']['title'] == 'Test API'
|
|
|
|
|
|
def test_load_yaml_spec(tmp_path):
|
|
content = '''
|
|
openapi: "3.0.0"
|
|
info:
|
|
title: Test API
|
|
version: "1.0.0"
|
|
paths: {}
|
|
'''
|
|
path = tmp_path / "test.yaml"
|
|
path.write_text(content)
|
|
spec = load_spec_file(str(path))
|
|
assert spec['info']['title'] == 'Test API'
|
|
|
|
|
|
def test_parse_error_missing_file():
|
|
with pytest.raises(ParseError):
|
|
load_spec_file('/nonexistent/path.json')
|
|
|
|
|
|
def test_parse_error_unsupported_format(tmp_path):
|
|
path = tmp_path / "test.txt"
|
|
path.write_text('some content')
|
|
with pytest.raises(ParseError):
|
|
load_spec_file(str(path))
|