diff options
| author | Eddy Pedroni <epedroni@pm.me> | 2025-02-25 13:04:44 +0100 | 
|---|---|---|
| committer | Eddy Pedroni <epedroni@pm.me> | 2025-02-25 13:07:02 +0100 | 
| commit | 876071a7a41ee4a1f579739b242a00058bdc160d (patch) | |
| tree | 886b6a40c5d4c1e7b84fb08ec9451cb7eb94bfb3 /solo-tool-project/src/solo_tool | |
| parent | 5f6d352aaa4c1273d95a740768c0079ae02005c5 (diff) | |
Refactor song selection tests, change behaviour so first added song is automatically selected
Diffstat (limited to 'solo-tool-project/src/solo_tool')
| -rw-r--r-- | solo-tool-project/src/solo_tool/solo_tool.py | 2 | ||||
| -rw-r--r-- | solo-tool-project/src/solo_tool/solo_tool_songs_integrationtest.py | 112 | 
2 files changed, 114 insertions, 0 deletions
| diff --git a/solo-tool-project/src/solo_tool/solo_tool.py b/solo-tool-project/src/solo_tool/solo_tool.py index 199e2ad..f5b05c0 100644 --- a/solo-tool-project/src/solo_tool/solo_tool.py +++ b/solo-tool-project/src/solo_tool/solo_tool.py @@ -33,6 +33,8 @@ class SoloTool:              raise FileNotFoundError(path)          self._songs.append(path)          self._keyPoints.append([]) +        if self.song is None: +            self.song = 0      @property      def song(self) -> int: diff --git a/solo-tool-project/src/solo_tool/solo_tool_songs_integrationtest.py b/solo-tool-project/src/solo_tool/solo_tool_songs_integrationtest.py new file mode 100644 index 0000000..0023d81 --- /dev/null +++ b/solo-tool-project/src/solo_tool/solo_tool_songs_integrationtest.py @@ -0,0 +1,112 @@ +import pathlib +import shutil +import pytest + +from solo_tool.solo_tool import SoloTool +from player_mock import Player as MockPlayer + +@pytest.fixture +def mockPlayer(): +    return MockPlayer() + +@pytest.fixture +def uut(mockPlayer): +    return SoloTool(player=mockPlayer) + +@pytest.fixture +def testSongs(): +    return [ +        "test.flac", +        "test.mp3" +    ] + +def test_songSelectionFlow(uut, mockPlayer, testSongs): +    # Initially, song list is empty and no song is selected +    assert uut.song is None +    assert mockPlayer.currentSong == None +    assert uut.songs == [] + +    # When the first song is added, it is selected automatically +    uut.addSong(testSongs[0]) +    assert uut.song == 0 +    assert mockPlayer.currentSong == testSongs[0] +    assert uut.songs == testSongs[0:1] + +    # Subsequently added songs are not selected automatically +    # Song list order is addition order +    for i, song in enumerate(testSongs[1:]): +        uut.addSong(testSongs[1]) +        assert uut.song == 0 +        assert mockPlayer.currentSong == testSongs[0] +        assert uut.songs == testSongs[0:i + 2] + +    # Songs are selected by index +    for i, s in enumerate(uut.songs): +        uut.song = i +        assert uut.song == i +        assert mockPlayer.currentSong == uut.songs[i] + +def test_songSelectionSanitization(uut, mockPlayer, testSongs): +    for song in testSongs: +        uut.addSong(song) + +    # Modifying the song list directly has no effect +    uut.songs.append("something") +    assert uut.songs == testSongs +    assert mockPlayer.currentSong == testSongs[0] + +    # The current song cannot be de-selected +    uut.song = None +    assert uut.song == 0 +    assert mockPlayer.currentSong == testSongs[0] + +    # Non-existent songs cannot be selected +    uut.song = -1 +    assert uut.song == 0 +    assert mockPlayer.currentSong == testSongs[0] + +    uut.song = len(testSongs) +    assert uut.song == 0 +    assert mockPlayer.currentSong == testSongs[0] + +def test_addInexistentSong(uut, mockPlayer): +    # Songs must exist in the filesystem +    song = "not/a/real/file" + +    with pytest.raises(FileNotFoundError): +        uut.addSong(song) + +    assert uut.songs == [] +    assert uut.song is None + +def test_songSelectionNotification(uut, testSongs): +    called = False +    receivedValue = None +    def callback(value): +        nonlocal called, receivedValue +        called = True +        receivedValue = value + +    uut.registerSongSelectionCallback(callback) +    assert not called + +    # Adding the first song triggers because the song is automatically selected +    uut.addSong(songs[0]) +    assert called +    assert receivedValue == 0 +    called = False + +    # Adding more songs does not trigger since the selection doesn't change +    for song in testSongs: +        uut.addSong(song) +        assert not called + +    # Selecting another song triggers +    uut.song = 1 +    assert called +    assert receivedValue == 1 +    called = False + +    # Selecting the currently selected song does not trigger +    uut.song = 1 +    assert not called | 
