import pytest from json import loads import os from solo_tool.session_manager import getSessionManager, _FileSystemSessionManager, _FileBrowserSessionManager from fixtures import songPool, soloTool, mockPlayer, testSongs, sessionPath @pytest.fixture def testSessionFile(sessionPath, testSongs): contents = """[ { "path" : "test.flac", "key_points" : [] }, { "path" : "test.mp3", "key_points" : [0.1, 0.3] } ]""" sessionFile = sessionPath / "test-session.json" with open(sessionFile, "w") as f: f.write(contents) return sessionFile @pytest.fixture def sessionManager(sessionPath, songPool): return getSessionManager(str(songPool), str(sessionPath)) def test_loadSession(sessionManager, mockPlayer, testSessionFile): sessions = sessionManager.getSessions() assert sessions == [testSessionFile.stem] soloTool = sessionManager.loadSession(sessions[0], 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(sessionManager, soloTool, testSessionFile, sessionPath): soloTool.addSong("test.flac") soloTool.addSong("test.mp3") soloTool.song = 1 soloTool.keyPoints = [0.1, 0.3] sessionKey = "test_session_saved" sessionManager.saveSession(soloTool, sessionKey) with open(sessionPath / f"{sessionKey}.json", "r") as f: savedSession = loads(f.read()) with open(testSessionFile, "r") as f: testSession = loads(f.read()) assert savedSession == testSession def test_loadAndSaveEmptySession(sessionManager, sessionPath, soloTool, tmp_path): emptySession = "empty_session" sessionManager.saveSession(soloTool, emptySession) reloadedTool = sessionManager.loadSession(emptySession) assert reloadedTool.songs == [] def test_uploadSong(sessionManager, songPool, tmp_path): song = tmp_path / "song-to-be-uploaded.mp3" song.touch() expected = songPool / "song-to-be-uploaded.mp3" assert not expected.exists() with open(song, "rb") as f: sessionManager.addSong("song-to-be-uploaded.mp3", f) assert expected.exists() def test_sessionManagerFactory(): assert type(getSessionManager("", "/some_absolute_dir")) is _FileSystemSessionManager assert type(getSessionManager("", "file:///some_dir_with_protocol")) is _FileSystemSessionManager assert type(getSessionManager("", "some_relative_dir")) is _FileSystemSessionManager assert type(getSessionManager("", "http://some_server")) is _FileBrowserSessionManager assert type(getSessionManager("", "https://some_secure_server")) is _FileBrowserSessionManager