95 lines
2.9 KiB
Python
95 lines
2.9 KiB
Python
import pytest
|
|
from click.testing import CliRunner
|
|
|
|
from src.cli import generate, main, search, serve, validate
|
|
|
|
runner = CliRunner()
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_spec_path(tmp_path):
|
|
spec = {
|
|
"openapi": "3.0.3",
|
|
"info": {"title": "Test API", "version": "1.0.0"},
|
|
"paths": {
|
|
"/users": {
|
|
"get": {
|
|
"summary": "List users",
|
|
"description": "Get all users",
|
|
"tags": ["Users"],
|
|
"responses": {"200": {"description": "Success"}}
|
|
}
|
|
},
|
|
"/users/{id}": {
|
|
"get": {
|
|
"summary": "Get user",
|
|
"description": "Get a user by ID",
|
|
"tags": ["Users"],
|
|
"parameters": [
|
|
{"name": "id", "in": "path", "required": True, "schema": {"type": "string"}}
|
|
],
|
|
"responses": {"200": {"description": "Success"}}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
path = tmp_path / "openapi.json"
|
|
import json
|
|
path.write_text(json.dumps(spec))
|
|
return str(path)
|
|
|
|
|
|
class TestCLI:
|
|
def test_main_help(self):
|
|
result = runner.invoke(main, ["--help"])
|
|
assert result.exit_code == 0
|
|
assert "LocalAPI Docs" in result.output
|
|
|
|
def test_serve_help(self):
|
|
result = runner.invoke(serve, ["--help"])
|
|
assert result.exit_code == 0
|
|
assert "serve" in result.output
|
|
|
|
def test_generate_help(self):
|
|
result = runner.invoke(generate, ["--help"])
|
|
assert result.exit_code == 0
|
|
assert "generate" in result.output
|
|
|
|
def test_validate_help(self):
|
|
result = runner.invoke(validate, ["--help"])
|
|
assert result.exit_code == 0
|
|
assert "validate" in result.output
|
|
|
|
def test_search_help(self):
|
|
result = runner.invoke(search, ["--help"])
|
|
assert result.exit_code == 0
|
|
assert "search" in result.output
|
|
|
|
|
|
class TestValidateCommand:
|
|
def test_validate_valid_spec(self, sample_spec_path):
|
|
result = runner.invoke(validate, [sample_spec_path])
|
|
assert result.exit_code == 0
|
|
assert "Valid OpenAPI spec" in result.output
|
|
assert "Test API" in result.output
|
|
|
|
def test_validate_nonexistent_file(self):
|
|
result = runner.invoke(validate, ["/nonexistent/path.json"])
|
|
assert result.exit_code != 0
|
|
|
|
|
|
class TestSearchCommand:
|
|
def test_search_query(self, sample_spec_path):
|
|
result = runner.invoke(search, [sample_spec_path, "users"])
|
|
assert result.exit_code == 0
|
|
assert "users" in result.output.lower() or "found" in result.output.lower()
|
|
|
|
def test_search_no_query(self, sample_spec_path):
|
|
result = runner.invoke(search, [sample_spec_path])
|
|
assert result.exit_code == 0
|
|
assert "query" in result.output.lower()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pytest.main([__file__, "-v"])
|