127 lines
3.6 KiB
Python
127 lines
3.6 KiB
Python
# Standard library imports
|
|
import os
|
|
import unittest
|
|
|
|
# PyQt imports
|
|
|
|
# Third party imports
|
|
import pytest
|
|
from pytestqt.plugin import QtBot # type: ignore
|
|
|
|
# App imports
|
|
from app import playlistmodel, utilities
|
|
from app import ds, musicmuster
|
|
from classes import InsertTrack
|
|
|
|
|
|
# Custom fixture to adapt qtbot for use with unittest.TestCase
|
|
@pytest.fixture(scope="class")
|
|
def qtbot_adapter(qapp, request):
|
|
"""Adapt qtbot fixture for usefixtures and unittest.TestCase"""
|
|
request.cls.qtbot = QtBot(request)
|
|
|
|
|
|
# Wrapper to handle setup/teardown operations
|
|
def with_updown(function):
|
|
def test_wrapper(self, *args, **kwargs):
|
|
if callable(getattr(self, "up", None)):
|
|
self.up()
|
|
try:
|
|
function(self, *args, **kwargs)
|
|
finally:
|
|
if callable(getattr(self, "down", None)):
|
|
self.down()
|
|
|
|
test_wrapper.__doc__ = function.__doc__
|
|
return test_wrapper
|
|
|
|
|
|
# Apply the custom fixture to the test class
|
|
@pytest.mark.usefixtures("qtbot_adapter")
|
|
class MyTestCase(unittest.TestCase):
|
|
def up(self):
|
|
ds.db.create_all()
|
|
self.widget = musicmuster.Window()
|
|
# self.widget.show()
|
|
|
|
# Add two tracks to database
|
|
self.track1 = ds.track_create(
|
|
{
|
|
"path": "testdata/isa.mp3",
|
|
"title": "I'm so afraid",
|
|
"artist": "Fleetwood Mac",
|
|
"bitrate": 64,
|
|
"duration": 263000,
|
|
"start_gap": 60,
|
|
"fade_at": 236263,
|
|
"silence_at": 260343,
|
|
}
|
|
)
|
|
self.track2 = ds.track_create(
|
|
{
|
|
"path": "testdata/mom.mp3",
|
|
"title": "Man of Mystery",
|
|
"artist": "The Shadows",
|
|
"bitrate": 64,
|
|
"duration": 120000,
|
|
"start_gap": 70,
|
|
"fade_at": 115000,
|
|
"silence_at": 118000,
|
|
}
|
|
)
|
|
|
|
def down(self):
|
|
ds.db.drop_all()
|
|
|
|
@with_updown
|
|
def test_init(self):
|
|
"""Just check we can create a playlist_tab"""
|
|
|
|
playlist_name = "test_init playlist"
|
|
|
|
playlist = ds.playlist_create(playlist_name, template_id=0)
|
|
self.widget._open_playlist(playlist, is_template=False)
|
|
with self.qtbot.waitExposed(self.widget):
|
|
self.widget.show()
|
|
|
|
@with_updown
|
|
def test_save_and_restore(self):
|
|
"""Playlist with one track, one note, save and restore"""
|
|
|
|
note_text = "my note"
|
|
playlist_name = "test_save_and_restore playlist"
|
|
|
|
playlist = ds.playlist_create(playlist_name, template_id=0)
|
|
model = playlistmodel.PlaylistModel(playlist.playlist_id, is_template=False)
|
|
|
|
# Add a track with a note
|
|
model.insert_row_signal_handler(
|
|
InsertTrack(
|
|
playlist_id=playlist.playlist_id,
|
|
track_id=self.track1.track_id,
|
|
note=note_text,
|
|
)
|
|
)
|
|
|
|
# Retrieve playlist
|
|
all_playlists = ds.playlists_all()
|
|
assert len(all_playlists) == 1
|
|
retrieved_playlist = all_playlists[0]
|
|
playlist_rows = ds.playlistrows_by_playlist(retrieved_playlist.playlist_id)
|
|
assert len(playlist_rows) == 1
|
|
paths = [a.track.path for a in playlist_rows]
|
|
assert self.track1.path in paths
|
|
notes = [a.note for a in playlist_rows]
|
|
assert note_text in notes
|
|
|
|
@with_updown
|
|
def test_utilities(self):
|
|
"""Test check_db utility"""
|
|
|
|
from config import Config
|
|
|
|
Config.ROOT = os.path.join(os.path.dirname(__file__), "testdata")
|
|
|
|
utilities.check_db()
|
|
utilities.update_bitrates()
|