aboutsummaryrefslogtreecommitdiffstats
path: root/solo-tool-project/test/session_manager_unittest.py
blob: cff3b351f7cb69596ea799edc0e243cab4c39ceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pytest
from json import loads
import os

from solo_tool.session_manager import loadSession, saveSession
from fixtures import songPool, soloTool, mockPlayer, testSongs

@pytest.fixture
def testSessionFile(tmp_path, testSongs):
    contents = """[
    {
        "path" : "test.flac",
        "key_points" : []
    },
    {
        "path" : "test.mp3",
        "key_points" : [0.1, 0.3]
    }
]"""

    basePath = tmp_path / "sessions"
    sessionFile = basePath / "test-session.json"

    os.mkdir(basePath)
    with open(sessionFile, "w") as f:
        f.write(contents)

    return sessionFile

def test_loadSession(songPool, testSessionFile, mockPlayer):
    soloTool = loadSession(testSessionFile, songPool, player=mockPlayer)

    assert soloTool.songs == ["test.flac", "test.mp3"]

    soloTool.song = 0
    assert soloTool.keyPoints == []

    soloTool.song = 1
    assert soloTool.keyPoints == [0.1, 0.3]

def test_saveSession(soloTool, testSessionFile, tmp_path):
    soloTool.addSong("test.flac")
    soloTool.addSong("test.mp3")
    soloTool.song = 1
    soloTool.keyPoints = [0.1, 0.3]

    testFile = tmp_path / "test_session_saved.json"
    saveSession(soloTool, testFile)

    with open(testFile, "r") as f:
        savedSession = loads(f.read())

    with open(testSessionFile, "r") as f:
        testSession = loads(f.read())

    assert savedSession == testSession

def test_loadAndSaveEmptySession(songPool, soloTool, tmp_path):
    emptyFile = tmp_path / "empty_session.json"

    saveSession(soloTool, emptyFile)
    reloadedTool = loadSession(emptyFile, songPool)

    assert reloadedTool.songs == []