80 lines
3.5 KiB
Python
80 lines
3.5 KiB
Python
# This test hits the live api, and serves as a sanity check for the BeatLeaderAPI class.
|
|
# Run this sparingly! Ideally you should only need it just before tagging a release version.
|
|
|
|
import pytest
|
|
import os
|
|
import logging
|
|
from saberlist.beatleaderAPI import BeatLeaderAPI
|
|
|
|
# Set up logging
|
|
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
# Test player ID (you may want to use a known player ID for consistent testing)
|
|
TEST_PLAYER_ID = '76561199407393962'
|
|
|
|
@pytest.fixture(scope="module")
|
|
def api():
|
|
return BeatLeaderAPI(cache_expiry_days=0) # Set to 0 to always get fresh data during tests
|
|
|
|
def test_cache_directory(api):
|
|
cache_dir = api.get_cache_dir()
|
|
assert os.path.exists(cache_dir), f"Cache directory {cache_dir} does not exist"
|
|
assert cache_dir.endswith('saberlist'), f"Cache directory {cache_dir} does not end with 'saberlist'"
|
|
|
|
def test_get_player_scores(api):
|
|
scores = api.get_player_scores(TEST_PLAYER_ID, use_cache=False)
|
|
assert isinstance(scores, dict), "get_player_scores should return a dictionary"
|
|
assert 'data' in scores, "Scores response should contain 'data' key"
|
|
assert len(scores['data']) > 0, "Player should have at least one score"
|
|
|
|
def test_get_player_songs(api):
|
|
songs = api.get_player_songs(TEST_PLAYER_ID, use_cache=False)
|
|
assert isinstance(songs, list), "get_player_songs should return a list"
|
|
assert len(songs) > 0, "Player should have at least one song"
|
|
|
|
# Check if all expected keys are present in the first song
|
|
expected_keys = ['id', 'name', 'author', 'mapper', 'hash', 'bpm', 'duration']
|
|
assert all(key in songs[0] for key in expected_keys), f"Song is missing some expected keys. Got: {songs[0].keys()}"
|
|
|
|
def test_get_player_songs_minimal(api):
|
|
songs = api.get_player_songs_minimal(TEST_PLAYER_ID, use_cache=False)
|
|
assert isinstance(songs, list), "get_player_songs_minimal should return a list"
|
|
assert len(songs) > 0, "Player should have at least one song"
|
|
|
|
# Check if only the minimal keys are present in the first song
|
|
expected_keys = ['id', 'name', 'author', 'mapper', 'hash', 'bpm', 'duration']
|
|
assert set(songs[0].keys()) == set(expected_keys), f"Minimal song data has unexpected keys. Got: {songs[0].keys()}"
|
|
|
|
def test_caching(api):
|
|
# First call should hit the API
|
|
songs1 = api.get_player_songs(TEST_PLAYER_ID, use_cache=True)
|
|
|
|
# Second call should use cache
|
|
songs2 = api.get_player_songs(TEST_PLAYER_ID, use_cache=True)
|
|
|
|
assert songs1 == songs2, "Cached result should be the same as the initial API call"
|
|
|
|
# Force a fresh API call
|
|
songs3 = api.get_player_songs(TEST_PLAYER_ID, use_cache=False)
|
|
|
|
# The results might be different if the player has new scores, but the structure should be the same
|
|
assert type(songs1) == type(songs3), "Fresh API call should return the same data structure"
|
|
|
|
def test_clear_cache(api):
|
|
# Ensure there's something in the cache
|
|
api.get_player_songs(TEST_PLAYER_ID, use_cache=True)
|
|
|
|
# Clear cache for the test player
|
|
api.clear_cache(TEST_PLAYER_ID)
|
|
|
|
cache_file = api._get_cache_filename(TEST_PLAYER_ID)
|
|
assert not os.path.exists(cache_file), f"Cache file for player {TEST_PLAYER_ID} should not exist after clearing"
|
|
|
|
# Clear all cache
|
|
api.get_player_songs(TEST_PLAYER_ID, use_cache=True) # Recreate some cache
|
|
api.clear_cache()
|
|
|
|
assert len(os.listdir(api.get_cache_dir())) == 0, "Cache directory should be empty after clearing all cache"
|
|
|
|
if __name__ == "__main__":
|
|
pytest.main([__file__, "-v"]) |