legal-ai-assistant/tests/integration/test_results.py

103 lines
3.5 KiB
Python

import pytest
import httpx
from unittest.mock import AsyncMock, MagicMock
from backend.tools.api.http_request_handler import http_request
class TestHappyPath:
"""
Checks the correct processing of successful
HTTP requests and the response structure.
"""
MULTIPLE_RESULTS = 123
COUNT_ID = 5
@pytest.mark.asyncio
async def test_single_result(self, mock_client, make_response) -> None:
mock_client.get.return_value = make_response({
"items": [{"id": "sud_7", "name": "Krajský súd v Bratislave"}],
"totalCount": 1
})
result = await http_request("/sud", {})
assert result["data"]["totalCount"] == 1
assert len(result["data"]["items"]) == 1
@pytest.mark.asyncio
async def test_multiple_results(self, mock_client, make_response) -> None:
mock_client.get.return_value = make_response({
"items": [{"id": f"sud_{i}"} for i in range(self.COUNT_ID)],
"totalCount": self.MULTIPLE_RESULTS
})
result = await http_request("/sud", {})
assert result["data"]["totalCount"] == self.MULTIPLE_RESULTS
assert len(result["data"]["items"]) == self.COUNT_ID
@pytest.mark.asyncio
async def test_result_contains_url(self, mock_client, make_response) -> None:
mock_client.get.return_value = make_response({
"items": [],
"totalCount": 0,
})
result = await http_request("/sud", {})
assert "url" in result
class TestNotFound:
"""
Checks for error-free
handling of empty results.
"""
@pytest.mark.asyncio
async def test_empty_items(self, mock_client, make_response) -> None:
mock_client.get.return_value = make_response({
"items": [],
"totalCount": 0
})
result = await http_request("/sudca", {"query": "NEex1stujuc1"})
assert result["data"]["totalCount"] == 0
assert result["data"]["items"] == []
@pytest.mark.asyncio
async def test_empty_result_without_error_key(self, mock_client, make_response) -> None:
mock_client.get.return_value = make_response({
"items": [],
"totalCount": 0
})
result = await http_request("/sudca", {})
assert "error" not in result
class TestHTTPErrors:
"""
Verifies correct handling of HTTP and
network errors without crashing the function.
"""
@pytest.mark.asyncio
@pytest.mark.parametrize("status", [404, 500, 503])
async def test_http_error_returns_error_key(self, status, mock_client) -> None:
mock_client.get = AsyncMock(
side_effect=httpx.HTTPStatusError(
f"HTTP {status}",
request=MagicMock(),
response=MagicMock(status_code=status),
)
)
result = await http_request("/sud", {})
assert result["error"] == "http_error"
assert "detail" in result
@pytest.mark.asyncio
async def test_connection_error_returns_arror_key(self, mock_client) -> None:
mock_client.get = AsyncMock(
side_effect=httpx.ConnectError("refused")
)
result = await http_request("/sud", {})
assert "error" in result
assert "detail" in result
@pytest.mark.asyncio
async def test_error_not_raise_except(self, mock_client) -> None:
mock_client.get = AsyncMock(side_effect=Exception("boom"))
result = await http_request("/sud", {})
assert "error" in result