"""Integration tests for OpenF1 MCP Server"""
import pytest
import aiohttp
from src.openf1_client import OpenF1Client
@pytest.fixture
async def api_client():
"""Create a real API client for integration tests"""
async with OpenF1Client() as client:
yield client
@pytest.mark.asyncio
@pytest.mark.integration
async def test_real_api_drivers():
"""Test fetching real drivers from API"""
async with OpenF1Client() as client:
result = await client.get_drivers()
assert isinstance(result, list)
if result:
assert "driver_number" in result[0]
@pytest.mark.asyncio
@pytest.mark.integration
async def test_real_api_teams():
"""Test fetching real teams from API"""
async with OpenF1Client() as client:
result = await client.get_teams()
assert isinstance(result, list)
@pytest.mark.asyncio
@pytest.mark.integration
async def test_real_api_races():
"""Test fetching real races from API"""
async with OpenF1Client() as client:
result = await client.get_races()
assert isinstance(result, list)
if result:
assert "round" in result[0]
@pytest.mark.asyncio
@pytest.mark.integration
async def test_real_api_races_2024():
"""Test fetching 2024 races from API"""
async with OpenF1Client() as client:
result = await client.get_races(season=2024)
assert isinstance(result, list)
@pytest.mark.asyncio
@pytest.mark.integration
async def test_real_api_sessions():
"""Test fetching real sessions from API"""
async with OpenF1Client() as client:
result = await client.get_sessions()
assert isinstance(result, list)
@pytest.mark.asyncio
@pytest.mark.integration
async def test_real_api_sessions_2024_round_1():
"""Test fetching 2024 season round 1 sessions"""
async with OpenF1Client() as client:
result = await client.get_sessions(season=2024, round_number=1)
assert isinstance(result, list)
@pytest.mark.asyncio
@pytest.mark.integration
async def test_real_api_results():
"""Test fetching real results from API"""
async with OpenF1Client() as client:
result = await client.get_results()
assert isinstance(result, list)
@pytest.mark.asyncio
@pytest.mark.integration
async def test_real_api_with_session_key():
"""Test fetching data filtered by session key"""
async with OpenF1Client() as client:
# First get a session
sessions = await client.get_sessions(season=2024, round_number=1)
if sessions:
session_key = sessions[0].get("session_key")
# Then fetch results for that session
results = await client.get_results(session_key=session_key)
assert isinstance(results, list)
@pytest.mark.asyncio
@pytest.mark.integration
async def test_real_api_connection_reuse():
"""Test that connection can be reused across multiple calls"""
async with OpenF1Client() as client:
# Make multiple calls with same client
drivers = await client.get_drivers()
teams = await client.get_teams()
races = await client.get_races()
assert isinstance(drivers, list)
assert isinstance(teams, list)
assert isinstance(races, list)
@pytest.mark.asyncio
def test_openf1_client_base_url():
"""Test that OpenF1Client has correct base URL"""
assert OpenF1Client.BASE_URL == "https://api.openf1.org/v1"
@pytest.mark.asyncio
@pytest.mark.integration
async def test_api_error_handling():
"""Test API error handling with invalid request"""
async with OpenF1Client() as client:
try:
# Try to call with invalid driver number (very high)
result = await client.get_drivers(driver_number=99999)
# Should return empty list or valid response
assert isinstance(result, list)
except aiohttp.ClientError:
# API error is acceptable
pass