94 lines
2.8 KiB
Python
94 lines
2.8 KiB
Python
import pytest
|
|
from unittest.mock import AsyncMock, patch
|
|
from api_calls.draft_pick import get_one_draftpick, patch_draftpick, DraftPick
|
|
from api_calls.team import Team
|
|
from api_calls.player import Player
|
|
from exceptions import ApiException
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_one_draftpick_by_id_success():
|
|
mock_data = {
|
|
"id": 5,
|
|
"season": 2025,
|
|
"overall": 3,
|
|
"round": 1
|
|
}
|
|
|
|
with patch("api_calls.draft_pick.db_get", new_callable=AsyncMock) as mock_db_get:
|
|
mock_db_get.return_value = mock_data
|
|
result = await get_one_draftpick(pick_id=5)
|
|
|
|
assert isinstance(result, DraftPick)
|
|
assert result.id == 5
|
|
assert result.overall == 3
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_one_draftpick_by_season_and_overall_success():
|
|
mock_data = {
|
|
"count": 1,
|
|
"picks": [{
|
|
"id": 10,
|
|
"season": 2025,
|
|
"overall": 1,
|
|
"round": 1
|
|
}]
|
|
}
|
|
|
|
with patch("api_calls.draft_pick.db_get", new_callable=AsyncMock) as mock_db_get:
|
|
mock_db_get.return_value = mock_data
|
|
result = await get_one_draftpick(season=2025, overall=1)
|
|
|
|
assert result.id == 10
|
|
assert result.season == 2025
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_one_draftpick_invalid_params_raise():
|
|
with patch("api_calls.draft_pick.log_exception") as mock_log:
|
|
mock_log.side_effect = KeyError("Missing args")
|
|
with pytest.raises(KeyError, match="Missing args"):
|
|
await get_one_draftpick()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_patch_draftpick_success():
|
|
updated_pick = DraftPick(
|
|
id=5,
|
|
season=2025,
|
|
overall=2,
|
|
round=1,
|
|
origowner=Team(id=1, abbrev='AAA', sname='Alpha', lname='Alpha Squad', season=2025),
|
|
owner=Team(id=2, abbrev='BBB', sname='Beta', lname='Beta Crew', season=2025),
|
|
player=Player(id=99, name="Test Player", team=Team(id=2, abbrev='BBB', sname='Beta', lname='Beta Crew', season=2025))
|
|
)
|
|
|
|
mock_response = {
|
|
"id": 5,
|
|
"season": 2025,
|
|
"overall": 2,
|
|
"round": 1
|
|
}
|
|
|
|
with patch("api_calls.draft_pick.db_patch", new_callable=AsyncMock) as mock_patch:
|
|
mock_patch.return_value = mock_response
|
|
|
|
result = await patch_draftpick(updated_pick)
|
|
|
|
assert isinstance(result, DraftPick)
|
|
assert result.id == 5
|
|
mock_patch.assert_awaited_once()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_patch_draftpick_missing_fields_logs_and_returns_empty():
|
|
pick = DraftPick(id=5, season=2025, overall=2, round=1)
|
|
|
|
with patch("api_calls.draft_pick.log_exception") as mock_log_exception:
|
|
mock_log_exception.side_effect = ApiException("Mocked exception")
|
|
|
|
with pytest.raises(ApiException, match="Mocked exception"):
|
|
await patch_draftpick(pick)
|
|
|