"""Smoke tests for openapi_first.app.""" import types import pytest from fastapi.testclient import TestClient from openapi_first.app import OpenAPIFirstApp from openapi_first.errors import MissingOperationHandler SPEC = { "openapi": "3.0.3", "info": {"title": "App API", "version": "1.0.0"}, "servers": [{"url": "https://api.example.com/v1"}], "paths": { "/health": { "get": { "operationId": "get_health", "responses": {"200": {"description": "OK"}}, } }, "/greet/{name}": { "get": { "operationId": "get_greet", "parameters": [ { "name": "name", "in": "path", "required": True, "schema": {"type": "string"}, } ], "responses": {"200": {"description": "OK"}}, } }, }, } def _routes_module(): module = types.ModuleType("smoke_routes") def get_health(): return {"status": "ok"} def get_greet(name: str): return {"greeting": f"Hello, {name}"} module.get_health = get_health module.get_greet = get_greet return module @pytest.fixture def spec_file(tmp_path): import json path = tmp_path / "openapi.json" path.write_text(json.dumps(SPEC), encoding="utf-8") return str(path) def test_app_routes_served(spec_file): app = OpenAPIFirstApp(openapi_path=spec_file, routes_module=_routes_module()) client = TestClient(app) response = client.get("/health") assert response.status_code == 200 assert response.json() == {"status": "ok"} def test_app_path_param_route(spec_file): app = OpenAPIFirstApp(openapi_path=spec_file, routes_module=_routes_module()) client = TestClient(app) response = client.get("/greet/Ada") assert response.status_code == 200 assert response.json() == {"greeting": "Hello, Ada"} def test_app_overrides_openapi_schema(spec_file): app = OpenAPIFirstApp(openapi_path=spec_file, routes_module=_routes_module()) openapi = app.openapi() assert openapi["info"]["title"] == "App API" assert "/health" in openapi["paths"] def test_app_missing_handler_fails_at_startup(spec_file): empty = types.ModuleType("empty_routes") with pytest.raises(MissingOperationHandler): OpenAPIFirstApp(openapi_path=spec_file, routes_module=empty)