You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

conftest.py 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import asyncio
  2. from typing import Any, AsyncGenerator, Dict
  3. import pytest
  4. import pytest_asyncio
  5. import uvicorn
  6. from autogen_core import ComponentModel
  7. from fastapi import FastAPI
  8. from pydantic import BaseModel, Field
  9. class TestArgs(BaseModel):
  10. query: str = Field(description="The test query")
  11. value: int = Field(description="A test value")
  12. class TestResponse(BaseModel):
  13. result: str = Field(description="The test result")
  14. # Create a test FastAPI app
  15. app = FastAPI()
  16. @app.post("/test")
  17. async def test_endpoint(body: TestArgs) -> TestResponse:
  18. return TestResponse(result=f"Received: {body.query} with value {body.value}")
  19. @app.post("/test/{query}/{value}")
  20. async def test_path_params_endpoint(query: str, value: int) -> TestResponse:
  21. return TestResponse(result=f"Received: {query} with value {value}")
  22. @app.put("/test/{query}/{value}")
  23. async def test_path_params_and_body_endpoint(query: str, value: int, body: Dict[str, Any]) -> TestResponse:
  24. return TestResponse(result=f"Received: {query} with value {value} and extra {body.get('extra')}") # type: ignore
  25. @app.get("/test")
  26. async def test_get_endpoint(query: str, value: int) -> TestResponse:
  27. return TestResponse(result=f"Received: {query} with value {value}")
  28. @app.put("/test")
  29. async def test_put_endpoint(body: TestArgs) -> TestResponse:
  30. return TestResponse(result=f"Received: {body.query} with value {body.value}")
  31. @app.delete("/test")
  32. async def test_delete_endpoint(query: str, value: int) -> TestResponse:
  33. return TestResponse(result=f"Received: {query} with value {value}")
  34. @app.patch("/test")
  35. async def test_patch_endpoint(body: TestArgs) -> TestResponse:
  36. return TestResponse(result=f"Received: {body.query} with value {body.value}")
  37. @pytest.fixture
  38. def test_config() -> ComponentModel:
  39. return ComponentModel(
  40. provider="autogen_ext.tools.http.HttpTool",
  41. config={
  42. "name": "TestHttpTool",
  43. "description": "A test HTTP tool",
  44. "scheme": "http",
  45. "path": "/test",
  46. "host": "localhost",
  47. "port": 8000,
  48. "method": "POST",
  49. "headers": {"Content-Type": "application/json"},
  50. "json_schema": {
  51. "type": "object",
  52. "properties": {
  53. "query": {"type": "string", "description": "The test query"},
  54. "value": {"type": "integer", "description": "A test value"},
  55. },
  56. "required": ["query", "value"],
  57. },
  58. },
  59. )
  60. @pytest_asyncio.fixture(scope="function") # type: ignore
  61. async def test_server() -> AsyncGenerator[None, None]:
  62. # Start the test server
  63. config = uvicorn.Config(app, host="127.0.0.1", port=8000, log_level="error")
  64. server = uvicorn.Server(config)
  65. # Create a task for the server
  66. server_task = asyncio.create_task(server.serve())
  67. # Wait a bit for server to start
  68. await asyncio.sleep(0.5) # Increased sleep time to ensure server is ready
  69. yield
  70. # Cleanup
  71. server.should_exit = True
  72. await server_task