beatsaber-playlist-tool/tests/test_beatleader.py

106 lines
3.5 KiB
Python

# tests/test_beatleader.py
import pytest
from unittest.mock import patch, Mock
from saberlist.beatleader import BeatLeaderAPI
@pytest.fixture
def mock_response():
mock = Mock()
mock.json.return_value = {
"metadata": {"itemsPerPage": 100, "page": 1, "total": 2},
"data": [
{
"leaderboard": {
"song": {
"id": "song1",
"name": "Test Song 1",
"author": "Test Author 1",
"mapper": "Test Mapper 1",
"hash": "testhash1",
"bpm": 120,
"duration": 180
}
}
},
{
"leaderboard": {
"song": {
"id": "song2",
"name": "Test Song 2",
"author": "Test Author 2",
"mapper": "Test Mapper 2",
"hash": "testhash2",
"bpm": 140,
"duration": 200
}
}
}
]
}
return mock
@pytest.fixture
def beat_leader_api():
return BeatLeaderAPI()
@patch('requests.Session.get')
def test_get_player_scores(mock_get, beat_leader_api, mock_response):
mock_get.return_value = mock_response
player_id = '123456'
result = beat_leader_api.get_player_scores(player_id)
assert result == mock_response.json.return_value
mock_get.assert_called_once_with(
f"{BeatLeaderAPI.BASE_URL}/player/{player_id}/scores",
params={"page": 1, "count": 100}
)
@patch('requests.Session.get')
def test_get_player_songs(mock_get, beat_leader_api, mock_response):
mock_get.return_value = mock_response
player_id = '123456'
result = beat_leader_api.get_player_songs(player_id)
expected_songs = [
{
'id': 'song1',
'name': 'Test Song 1',
'author': 'Test Author 1',
'mapper': 'Test Mapper 1',
'hash': 'testhash1',
'bpm': 120,
'duration': 180
},
{
'id': 'song2',
'name': 'Test Song 2',
'author': 'Test Author 2',
'mapper': 'Test Mapper 2',
'hash': 'testhash2',
'bpm': 140,
'duration': 200
}
]
assert result == expected_songs
mock_get.assert_called_once_with(
f"{BeatLeaderAPI.BASE_URL}/player/{player_id}/scores",
params={"page": 1, "count": 100}
)
def test_get_player_songs_unique(beat_leader_api):
with patch.object(beat_leader_api, 'get_player_scores') as mock_get_scores:
mock_get_scores.return_value = {
"data": [
{"leaderboard": {"song": {"id": "song1", "name": "Song 1", "author": "Author 1", "mapper": "Mapper 1", "hash": "hash1", "bpm": 120, "duration": 180}}},
{"leaderboard": {"song": {"id": "song1", "name": "Song 1", "author": "Author 1", "mapper": "Mapper 1", "hash": "hash1", "bpm": 120, "duration": 180}}},
{"leaderboard": {"song": {"id": "song2", "name": "Song 2", "author": "Author 2", "mapper": "Mapper 2", "hash": "hash2", "bpm": 140, "duration": 200}}}
]
}
result = beat_leader_api.get_player_songs('123456')
assert len(result) == 2
assert result[0]['id'] == 'song1'
assert result[1]['id'] == 'song2'