Commit 649764f1 authored by Kostas Chartsias's avatar Kostas Chartsias
Browse files

refactor: converted unittest modules to pytest function-style tests

parent 0f2a12a8
Loading
Loading
Loading
Loading
Loading
+22 −22
Original line number Diff line number Diff line
import asyncio
import importlib.util
import pathlib
import sys
import unittest
from unittest.mock import AsyncMock, patch

import httpx
@@ -18,37 +18,37 @@ spec.loader.exec_module(ai_agent_app)
app = ai_agent_app.app


class AiAgentErrorCodeTests(unittest.IsolatedAsyncioTestCase):
    async def asyncSetUp(self) -> None:
async def _make_request(method: str, path: str, **kwargs: object) -> httpx.Response:
    transport = httpx.ASGITransport(app=app)
        self.client = httpx.AsyncClient(transport=transport, base_url="http://test")
    async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
        return await client.request(method, path, **kwargs)

    async def asyncTearDown(self) -> None:
        await self.client.aclose()

    async def test_groq_query_missing_query_returns_400(self) -> None:
        response = await self.client.post("/groq-mcp", json={})
def test_groq_query_missing_query_returns_400() -> None:
    response = asyncio.run(_make_request("POST", "/groq-mcp", json={}))

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json(), {"error": "No query provided"})
    assert response.status_code == 400
    assert response.json() == {"error": "No query provided"}

    async def test_groq_stream_missing_query_returns_400(self) -> None:
        response = await self.client.get("/groq-mcp/stream")

        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json(), {"error": "No query provided"})
def test_groq_stream_missing_query_returns_400() -> None:
    response = asyncio.run(_make_request("GET", "/groq-mcp/stream"))

    async def test_groq_query_agent_failure_returns_500(self) -> None:
    assert response.status_code == 400
    assert response.json() == {"error": "No query provided"}


def test_groq_query_agent_failure_returns_500() -> None:
    async def _run() -> httpx.Response:
        agent = AsyncMock()
        agent.run.side_effect = RuntimeError("backend failure")

        with patch("ai_agent.routes.groq.create_groq_agent", new=AsyncMock(return_value=agent)):
            response = await self.client.post("/groq-mcp", json={"query": "hello"})
            response = await _make_request("POST", "/groq-mcp", json={"query": "hello"})

        self.assertEqual(response.status_code, 500)
        self.assertEqual(response.json(), {"error": "backend failure"})
        agent.close.assert_awaited_once()
        return response


if __name__ == "__main__":
    unittest.main()
    response = asyncio.run(_run())
    assert response.status_code == 500
    assert response.json() == {"error": "backend failure"}
+11 −17
Original line number Diff line number Diff line
import asyncio
import importlib.util
import pathlib
import sys
import unittest

import httpx

@@ -17,24 +17,18 @@ spec.loader.exec_module(mcp_module_app)
app = mcp_module_app.app


class McpModuleErrorCodeTests(unittest.IsolatedAsyncioTestCase):
    async def asyncSetUp(self) -> None:
async def _make_request(method: str, path: str, **kwargs: object) -> httpx.Response:
    transport = httpx.ASGITransport(app=app)
        self.client = httpx.AsyncClient(transport=transport, base_url="http://test")
    async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
        return await client.request(method, path, **kwargs)

    async def asyncTearDown(self) -> None:
        await self.client.aclose()

    async def test_unknown_route_returns_404(self) -> None:
        response = await self.client.get("/does-not-exist")
def test_unknown_route_returns_404() -> None:
    response = asyncio.run(_make_request("GET", "/does-not-exist"))

        self.assertEqual(response.status_code, 404)
    assert response.status_code == 404

    async def test_health_trailing_slash_returns_404(self) -> None:
        response = await self.client.get("/health/")

        self.assertEqual(response.status_code, 404)


if __name__ == "__main__":
    unittest.main()
def test_health_trailing_slash_returns_404() -> None:
    response = asyncio.run(_make_request("GET", "/health/"))
    assert response.status_code == 404