|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +from src.services.watch_status import _artist_variants, music_listening_stats |
| 4 | + |
| 5 | +def test_artist_variants(): |
| 6 | + # Empty inputs |
| 7 | + assert _artist_variants(None) == [""] |
| 8 | + assert _artist_variants("") == [""] |
| 9 | + assert _artist_variants(" ") == [""] |
| 10 | + |
| 11 | + # Normal base case |
| 12 | + assert sorted(_artist_variants("Artist")) == ["artist"] |
| 13 | + |
| 14 | + # Dash folding test for every dash in _DASHES |
| 15 | + # The dashes are: "‐‑‒–—−" |
| 16 | + dashes = "‐‑‒–—−" |
| 17 | + for dash in dashes: |
| 18 | + # Each dash variant should produce a set containing the lowercase dash variant AND the folded dash variant |
| 19 | + name = f"Jay{dash}Z" |
| 20 | + variants = _artist_variants(name) |
| 21 | + assert len(variants) == 2, f"Expected 2 variants for dash '{dash}', got {variants}" |
| 22 | + assert "jay-z" in variants, f"Expected 'jay-z' in variants for dash '{dash}', got {variants}" |
| 23 | + assert name.lower() in variants, f"Expected '{name.lower()}' in variants for dash '{dash}', got {variants}" |
| 24 | + |
| 25 | +def test_music_listening_stats_early_exit(): |
| 26 | + # Test early exit conditions (returns None before DB access) |
| 27 | + |
| 28 | + # Missing user_id |
| 29 | + assert music_listening_stats(0, "Artist") is None |
| 30 | + assert music_listening_stats(None, "Artist") is None |
| 31 | + |
| 32 | + # Missing both artist and artist_mbid |
| 33 | + assert music_listening_stats(1, "") is None |
| 34 | + assert music_listening_stats(1, None) is None |
| 35 | + assert music_listening_stats(1, "", "") is None |
| 36 | + |
| 37 | + # With user_id and one of artist/mbid, it proceeds past early exit |
| 38 | + # (Since we haven't mocked DB here, if we pass valid args it will try to access DB, |
| 39 | + # but for early exit we just test the None returns). |
| 40 | + |
| 41 | +def test_music_listening_stats_exception(): |
| 42 | + # Test that DB exceptions are caught and return None |
| 43 | + import contextlib |
| 44 | + |
| 45 | + @contextlib.contextmanager |
| 46 | + def mock_get_db_session(): |
| 47 | + raise Exception("Mock DB connection failure") |
| 48 | + yield # just to make it a generator for context manager |
| 49 | + |
| 50 | + with patch("src.database.connection.get_db_session", new=mock_get_db_session): |
| 51 | + result = music_listening_stats(1, "Artist") |
| 52 | + assert result is None |
0 commit comments