87 lines
3.5 KiB
Python
87 lines
3.5 KiB
Python
"""
|
|
Tests demonstrating hypermodern typeguard usage.
|
|
No decorators needed - validation happens automatically.
|
|
"""
|
|
|
|
import pytest
|
|
from backend import PlaylistManager, calculate_playlist_duration, process_user_data
|
|
|
|
|
|
class TestHypermodernTypeguard:
|
|
"""Tests showing automatic type validation without decorators."""
|
|
|
|
def test_process_user_data_valid_types(self) -> None:
|
|
"""Test with correct types - should work normally."""
|
|
result = process_user_data(123, "John Doe", "JOHN@EXAMPLE.COM")
|
|
|
|
expected = {
|
|
"id": 123,
|
|
"name": "John Doe",
|
|
"email": "john@example.com",
|
|
"display_name": "John Doe (john@example.com)",
|
|
}
|
|
assert result == expected
|
|
|
|
def test_process_user_data_invalid_user_id(self) -> None:
|
|
"""Test with wrong user_id type - typeguard catches automatically."""
|
|
with pytest.raises(TypeError):
|
|
# This will fail because "123" is str, not int
|
|
process_user_data("123", "John", "john@example.com") # type: ignore[arg-type]
|
|
|
|
def test_process_user_data_invalid_name(self) -> None:
|
|
"""Test with wrong name type - typeguard catches automatically."""
|
|
with pytest.raises(TypeError):
|
|
# This will fail because 123 is int, not str
|
|
process_user_data(123, 123, "john@example.com") # type: ignore[arg-type]
|
|
|
|
def test_calculate_duration_valid(self) -> None:
|
|
"""Test duration calculation with valid types."""
|
|
durations = [3.5, 4.2, 2.8, 5.1]
|
|
total = calculate_playlist_duration(durations)
|
|
|
|
assert abs(total - 15.6) < 0.001 # Float comparison
|
|
|
|
def test_calculate_duration_invalid_list_type(self) -> None:
|
|
"""Test with wrong parameter type - not a list."""
|
|
with pytest.raises(TypeError):
|
|
# This will fail because "not a list" is str, not list[float]
|
|
calculate_playlist_duration("not a list") # type: ignore[arg-type]
|
|
|
|
def test_calculate_duration_invalid_element_types(self) -> None:
|
|
"""Test with wrong element types in list."""
|
|
with pytest.raises(TypeError):
|
|
# This will fail because list contains strings, not floats
|
|
calculate_playlist_duration(["3.5", "4.2"]) # type: ignore[list-item]
|
|
|
|
def test_playlist_manager_creation(self) -> None:
|
|
"""Test playlist manager creation with valid types."""
|
|
manager = PlaylistManager("My Playlist", 500)
|
|
|
|
assert manager.name == "My Playlist"
|
|
assert manager.max_size == 500
|
|
assert manager.get_track_count() == 0
|
|
|
|
def test_playlist_manager_invalid_name_type(self) -> None:
|
|
"""Test playlist manager creation with invalid name type."""
|
|
with pytest.raises(TypeError):
|
|
# This will fail because 123 is int, not str
|
|
PlaylistManager(123) # type: ignore[arg-type]
|
|
|
|
def test_playlist_manager_add_track_valid(self) -> None:
|
|
"""Test adding tracks with valid types."""
|
|
manager = PlaylistManager("Test Playlist", 2)
|
|
|
|
assert manager.add_track("track_1") is True
|
|
assert manager.add_track("track_2") is True
|
|
assert manager.add_track("track_3") is False # Exceeds max_size
|
|
|
|
assert manager.get_track_count() == 2
|
|
|
|
def test_playlist_manager_add_track_invalid_type(self) -> None:
|
|
"""Test adding track with invalid type."""
|
|
manager = PlaylistManager("Test Playlist")
|
|
|
|
with pytest.raises(TypeError):
|
|
# This will fail because 123 is int, not str
|
|
manager.add_track(123) # type: ignore[arg-type]
|