cahnges from bot review

This commit is contained in:
Georges-Antoine Assi
2026-06-07 13:54:01 -04:00
parent 41fd1eb6f1
commit 6acea1339f
3 changed files with 65 additions and 6 deletions

View File

@@ -1,7 +1,9 @@
from typing import cast
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from adapters.services import igdb
from adapters.services.igdb import IGDBService
@@ -16,11 +18,21 @@ class TestIGDBServiceUnit:
@pytest.mark.asyncio
async def test_request_acquires_rate_limiter(self, service, monkeypatch):
"""Test that the request reserves a rate-limiter slot before sending."""
mock_session = AsyncMock()
mock_response = MagicMock()
mock_response.json = AsyncMock(return_value=[{"id": 1}])
mock_response.raise_for_status.return_value = None
mock_session.post.return_value = mock_response
# Record the order in which the rate limiter is acquired and the request is sent
call_order: list[str] = []
acquire_mock = cast(AsyncMock, igdb._rate_limiter.acquire)
acquire_mock.side_effect = lambda *a, **k: call_order.append("acquire")
async def record_post(*args, **kwargs):
call_order.append("post")
return mock_response
mock_session = AsyncMock()
mock_session.post.side_effect = record_post
mock_context = MagicMock()
mock_context.get.return_value = mock_session
@@ -29,3 +41,10 @@ class TestIGDBServiceUnit:
result = await service._request("https://api.igdb.com/v4/games")
assert result == [{"id": 1}]
# The rate-limiter slot must be reserved, and before the request is sent.
acquire_mock.assert_awaited_once()
mock_session.post.assert_awaited_once()
assert call_order == [
"acquire",
"post",
], "rate limiter must be acquired before the POST is sent"

View File

@@ -1,6 +1,7 @@
import asyncio
import http
import json
from typing import cast
from unittest.mock import AsyncMock, MagicMock, patch
import aiohttp
@@ -8,6 +9,7 @@ import pytest
import yarl
from fastapi import HTTPException, status
from adapters.services import mobygames
from adapters.services.mobygames import (
MobyGamesService,
auth_middleware,
@@ -107,11 +109,21 @@ class TestMobyGamesServiceUnit:
@pytest.mark.asyncio
async def test_request_acquires_rate_limiter(self, service, monkeypatch):
"""Test that the request reserves a rate-limiter slot before sending."""
mock_session = AsyncMock()
mock_response = MagicMock()
mock_response.json = AsyncMock(return_value={"games": []})
mock_response.raise_for_status.return_value = None
mock_session.get.return_value = mock_response
# Record the order in which the rate limiter is acquired and the request is sent
call_order: list[str] = []
acquire_mock = cast(AsyncMock, mobygames._rate_limiter.acquire)
acquire_mock.side_effect = lambda *a, **k: call_order.append("acquire")
async def record_get(*args, **kwargs):
call_order.append("get")
return mock_response
mock_session = AsyncMock()
mock_session.get.side_effect = record_get
mock_context = MagicMock()
mock_context.get.return_value = mock_session
@@ -119,6 +131,14 @@ class TestMobyGamesServiceUnit:
with patch("adapters.services.mobygames.ctx_aiohttp_session", mock_context):
await service._request("https://api.mobygames.com/v1/games")
# The rate-limiter slot must be reserved, and before the request is sent.
acquire_mock.assert_awaited_once()
mock_session.get.assert_awaited_once()
assert call_order == [
"acquire",
"get",
], "rate limiter must be acquired before the GET is sent"
@pytest.mark.asyncio
async def test_request_connection_error(self, service):
"""Test request with connection error."""

View File

@@ -1,3 +1,4 @@
from typing import cast
from unittest.mock import AsyncMock, MagicMock, patch
import aiohttp
@@ -5,6 +6,7 @@ import pytest
import yarl
from fastapi import HTTPException, status
from adapters.services import retroachievements
from adapters.services.retroachievements import (
RetroAchievementsService,
auth_middleware,
@@ -80,11 +82,21 @@ class TestRetroAchievementsServiceUnit:
@pytest.mark.asyncio
async def test_request_acquires_rate_limiter(self, service, monkeypatch):
"""Test that the request reserves a rate-limiter slot before sending."""
mock_session = AsyncMock()
mock_response = MagicMock()
mock_response.json = AsyncMock(return_value={})
mock_response.raise_for_status.return_value = None
mock_session.get.return_value = mock_response
# Record the order in which the rate limiter is acquired and the request is sent
call_order: list[str] = []
acquire_mock = cast(AsyncMock, retroachievements._rate_limiter.acquire)
acquire_mock.side_effect = lambda *a, **k: call_order.append("acquire")
async def record_get(*args, **kwargs):
call_order.append("get")
return mock_response
mock_session = AsyncMock()
mock_session.get.side_effect = record_get
mock_context = MagicMock()
mock_context.get.return_value = mock_session
@@ -94,6 +106,14 @@ class TestRetroAchievementsServiceUnit:
):
await service._request("https://retroachievements.org/API")
# The rate-limiter slot must be reserved, and before the request is sent.
acquire_mock.assert_awaited_once()
mock_session.get.assert_awaited_once()
assert call_order == [
"acquire",
"get",
], "rate limiter must be acquired before the GET is sent"
class TestRetroAchievementsServiceIntegration:
@pytest.fixture