347 lines
11 KiB
Python
347 lines
11 KiB
Python
# Standard library imports
|
|
import unittest
|
|
|
|
# PyQt imports
|
|
from PyQt6.QtCore import Qt, QModelIndex
|
|
|
|
# Third party imports
|
|
|
|
# App imports
|
|
from app.helpers import get_all_track_metadata
|
|
from app import ds, playlistmodel
|
|
from dbmanager import db
|
|
from classes import (
|
|
InsertTrack,
|
|
TrackAndPlaylist,
|
|
)
|
|
|
|
|
|
class TestMMMiscTracks(unittest.TestCase):
|
|
def setUp(self):
|
|
PLAYLIST_NAME = "tracks playlist"
|
|
self.test_tracks = [
|
|
"testdata/isa.mp3",
|
|
"testdata/isa_with_gap.mp3",
|
|
"testdata/loser.mp3",
|
|
"testdata/lovecats-10seconds.mp3",
|
|
"testdata/lovecats.mp3",
|
|
"testdata/mom.mp3",
|
|
"testdata/sitting.mp3",
|
|
"testdata/wrb.flac",
|
|
]
|
|
|
|
db.create_all()
|
|
|
|
# Create a playlist and model
|
|
self.playlist = ds.playlist_create(PLAYLIST_NAME, template_id=0)
|
|
self.model = playlistmodel.PlaylistModel(
|
|
self.playlist.playlist_id, is_template=False
|
|
)
|
|
|
|
for row in range(len(self.test_tracks)):
|
|
track_path = self.test_tracks[row % len(self.test_tracks)]
|
|
metadata = get_all_track_metadata(track_path)
|
|
track = ds.track_create(metadata)
|
|
self.model.insert_row_signal_handler(
|
|
InsertTrack(
|
|
playlist_id=self.playlist.playlist_id,
|
|
track_id=track.track_id,
|
|
note=f"{row=}",
|
|
)
|
|
)
|
|
|
|
def tearDown(self):
|
|
db.drop_all()
|
|
|
|
def test_8_row_playlist(self):
|
|
# Test auto-created playlist
|
|
|
|
assert self.model.rowCount() == 8
|
|
assert max(self.model.playlist_rows.keys()) == 7
|
|
for row in range(self.model.rowCount()):
|
|
assert row in self.model.playlist_rows
|
|
assert self.model.playlist_rows[row].row_number == row
|
|
|
|
def test_timing_one_track(self):
|
|
START_ROW = 0
|
|
END_ROW = 2
|
|
|
|
# Fake selected row in model
|
|
self.model.selected_rows = [self.model.playlist_rows[START_ROW]]
|
|
self.model.insert_row_signal_handler(
|
|
InsertTrack(
|
|
playlist_id=self.playlist.playlist_id, track_id=None, note="start+"
|
|
)
|
|
)
|
|
self.model.selected_rows = [self.model.playlist_rows[END_ROW]]
|
|
self.model.insert_row_signal_handler(
|
|
InsertTrack(playlist_id=self.playlist.playlist_id, track_id=None, note="-+")
|
|
)
|
|
|
|
prd = self.model.playlist_rows[START_ROW]
|
|
qv_value = self.model._display_role(
|
|
START_ROW, playlistmodel.HEADER_NOTES_COLUMN, prd
|
|
)
|
|
assert qv_value == "start [1 tracks, 4:23 unplayed]"
|
|
|
|
|
|
class TestMMMiscNoPlaylist(unittest.TestCase):
|
|
PLAYLIST_NAME = "tracks playlist"
|
|
test_tracks = [
|
|
"testdata/isa.mp3",
|
|
"testdata/isa_with_gap.mp3",
|
|
"testdata/loser.mp3",
|
|
"testdata/lovecats-10seconds.mp3",
|
|
"testdata/lovecats.mp3",
|
|
"testdata/mom.mp3",
|
|
"testdata/sitting.mp3",
|
|
]
|
|
|
|
def setUp(self):
|
|
db.create_all()
|
|
|
|
def tearDown(self):
|
|
db.drop_all()
|
|
|
|
def test_insert_track_new_playlist(self):
|
|
# insert a track into a new playlist
|
|
playlist = ds.playlist_create(self.PLAYLIST_NAME, template_id=0)
|
|
# Create a model
|
|
model = playlistmodel.PlaylistModel(playlist.playlist_id, is_template=False)
|
|
# test repr
|
|
_ = str(model)
|
|
|
|
track_path = self.test_tracks[0]
|
|
metadata = get_all_track_metadata(track_path)
|
|
track = ds.track_create(metadata)
|
|
model.insert_row_signal_handler(
|
|
InsertTrack(
|
|
playlist_id=playlist.playlist_id,
|
|
track_id=track.track_id,
|
|
note="",
|
|
)
|
|
)
|
|
|
|
prd = model.playlist_rows[model.rowCount() - 1]
|
|
# test repr
|
|
_ = str(prd)
|
|
|
|
assert (
|
|
model._edit_role(model.rowCount() - 1, playlistmodel.Col.TITLE.value, prd)
|
|
== metadata["title"]
|
|
)
|
|
|
|
|
|
class TestMMMiscRowMove(unittest.TestCase):
|
|
PLAYLIST_NAME = "rowmove playlist"
|
|
ROWS_TO_CREATE = 11
|
|
|
|
def setUp(self):
|
|
db.create_all()
|
|
|
|
self.playlist = ds.playlist_create(self.PLAYLIST_NAME, template_id=0)
|
|
self.model = playlistmodel.PlaylistModel(
|
|
self.playlist.playlist_id, is_template=False
|
|
)
|
|
for row in range(self.ROWS_TO_CREATE):
|
|
self.model.insert_row_signal_handler(
|
|
InsertTrack(
|
|
playlist_id=self.playlist.playlist_id,
|
|
track_id=None,
|
|
note=str(row),
|
|
)
|
|
)
|
|
|
|
def tearDown(self):
|
|
db.drop_all()
|
|
|
|
def test_insert_header_row_end(self):
|
|
# insert header row at end of playlist
|
|
|
|
note_text = "test text"
|
|
|
|
assert self.model.rowCount() == self.ROWS_TO_CREATE
|
|
self.model.insert_row_signal_handler(
|
|
InsertTrack(
|
|
playlist_id=self.playlist.playlist_id, track_id=None, note=note_text
|
|
)
|
|
)
|
|
assert self.model.rowCount() == self.ROWS_TO_CREATE + 1
|
|
prd = self.model.playlist_rows[self.model.rowCount() - 1]
|
|
# Test against edit_role because display_role for headers is
|
|
# handled differently (sets up row span)
|
|
assert (
|
|
self.model._edit_role(
|
|
self.model.rowCount() - 1, playlistmodel.Col.NOTE.value, prd
|
|
)
|
|
== note_text
|
|
)
|
|
|
|
def test_insert_header_row_middle(self):
|
|
# insert header row in middle of playlist
|
|
|
|
note_text = "test text"
|
|
insert_row = 6
|
|
|
|
# Fake selected row in model
|
|
self.model.selected_rows = [self.model.playlist_rows[insert_row]]
|
|
|
|
self.model.insert_row_signal_handler(
|
|
InsertTrack(
|
|
playlist_id=self.playlist.playlist_id, track_id=None, note=note_text
|
|
)
|
|
)
|
|
assert self.model.rowCount() == self.ROWS_TO_CREATE + 1
|
|
prd = self.model.playlist_rows[insert_row]
|
|
# Test against edit_role because display_role for headers is
|
|
# handled differently (sets up row span)
|
|
assert (
|
|
self.model._edit_role(
|
|
self.model.rowCount() - 1, playlistmodel.Col.NOTE.value, prd
|
|
)
|
|
== note_text
|
|
)
|
|
|
|
def test_add_track_to_header(self):
|
|
note_text = "test text"
|
|
insert_row = 6
|
|
|
|
self.model.insert_row_signal_handler(
|
|
InsertTrack(
|
|
playlist_id=self.playlist.playlist_id, track_id=None, note=note_text
|
|
)
|
|
)
|
|
assert self.model.rowCount() == self.ROWS_TO_CREATE + 1
|
|
|
|
# Fake selected row in model
|
|
self.model.selected_rows = [self.model.playlist_rows[insert_row]]
|
|
|
|
prd = self.model.playlist_rows[1]
|
|
self.model.signal_add_track_to_header_handler(
|
|
TrackAndPlaylist(playlist_id=self.model.playlist_id, track_id=prd.track_id)
|
|
)
|
|
|
|
def test_reverse_row_groups_one_row(self):
|
|
rows_to_move = [3]
|
|
|
|
result = self.model._reversed_contiguous_row_groups(rows_to_move)
|
|
|
|
assert len(result) == 1
|
|
assert result[0] == [3]
|
|
|
|
def test_reverse_row_groups_multiple_row(self):
|
|
rows_to_move = [2, 3, 4, 5, 7, 9, 10, 13, 17, 20, 21]
|
|
|
|
result = self.model._reversed_contiguous_row_groups(rows_to_move)
|
|
|
|
assert result == [[20, 21], [17], [13], [9, 10], [7], [2, 3, 4, 5]]
|
|
|
|
def test_move_one_row_between_playlists_to_end(self):
|
|
from_rows = [3]
|
|
to_row = self.ROWS_TO_CREATE
|
|
destination_playlist_name = "destination"
|
|
|
|
model_src = self.model
|
|
playlist_dst = ds.playlist_create(destination_playlist_name, template_id=0)
|
|
model_dst = playlistmodel.PlaylistModel(
|
|
playlist_dst.playlist_id, is_template=False
|
|
)
|
|
for row in range(self.ROWS_TO_CREATE):
|
|
model_dst.insert_row_signal_handler(
|
|
InsertTrack(
|
|
playlist_id=playlist_dst.playlist_id, track_id=None, note=str(row)
|
|
)
|
|
)
|
|
|
|
model_src.move_rows_between_playlists(
|
|
from_rows, to_row, playlist_dst.playlist_id
|
|
)
|
|
|
|
assert model_src.rowCount() == self.ROWS_TO_CREATE - len(from_rows)
|
|
assert model_dst.rowCount() == self.ROWS_TO_CREATE + len(from_rows)
|
|
assert sorted([a.row_number for a in model_src.playlist_rows.values()]) == list(
|
|
range(len(model_src.playlist_rows))
|
|
)
|
|
|
|
def test_move_one_row_between_playlists_to_middle(self):
|
|
from_rows = [3]
|
|
to_row = 2
|
|
destination_playlist_name = "destination"
|
|
|
|
model_src = self.model
|
|
playlist_dst = ds.playlist_create(destination_playlist_name, template_id=0)
|
|
model_dst = playlistmodel.PlaylistModel(
|
|
playlist_dst.playlist_id, is_template=False
|
|
)
|
|
for row in range(self.ROWS_TO_CREATE):
|
|
model_dst.insert_row_signal_handler(
|
|
InsertTrack(
|
|
playlist_id=playlist_dst.playlist_id, track_id=None, note=str(row)
|
|
)
|
|
)
|
|
|
|
model_src.move_rows_between_playlists(
|
|
from_rows, to_row, playlist_dst.playlist_id
|
|
)
|
|
|
|
# Check the rows of the destination model
|
|
row_notes = []
|
|
for row_number in range(model_dst.rowCount()):
|
|
index = model_dst.index(
|
|
row_number, playlistmodel.Col.TITLE.value, QModelIndex()
|
|
)
|
|
row_notes.append(model_dst.data(index, Qt.ItemDataRole.EditRole))
|
|
|
|
assert model_src.rowCount() == self.ROWS_TO_CREATE - len(from_rows)
|
|
assert model_dst.rowCount() == self.ROWS_TO_CREATE + len(from_rows)
|
|
assert [int(a) for a in row_notes] == [0, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
|
|
|
def test_move_multiple_rows_between_playlists_to_end(self):
|
|
from_rows = [1, 3, 4]
|
|
to_row = 2
|
|
destination_playlist_name = "destination"
|
|
|
|
model_src = self.model
|
|
|
|
playlist_dst = ds.playlist_create(destination_playlist_name, template_id=0)
|
|
model_dst = playlistmodel.PlaylistModel(
|
|
playlist_dst.playlist_id, is_template=False
|
|
)
|
|
for row in range(self.ROWS_TO_CREATE):
|
|
model_dst.insert_row_signal_handler(
|
|
InsertTrack(
|
|
playlist_id=playlist_dst.playlist_id, track_id=None, note=str(row)
|
|
)
|
|
)
|
|
|
|
model_src.move_rows_between_playlists(
|
|
from_rows, to_row, playlist_dst.playlist_id
|
|
)
|
|
|
|
# Check the rows of the destination model
|
|
row_notes = []
|
|
for row_number in range(model_dst.rowCount()):
|
|
index = model_dst.index(
|
|
row_number, playlistmodel.Col.TITLE.value, QModelIndex()
|
|
)
|
|
row_notes.append(model_dst.data(index, Qt.ItemDataRole.EditRole))
|
|
|
|
assert model_src.rowCount() == self.ROWS_TO_CREATE - len(from_rows)
|
|
assert model_dst.rowCount() == self.ROWS_TO_CREATE + len(from_rows)
|
|
assert [int(a) for a in row_notes] == [
|
|
0,
|
|
1,
|
|
1,
|
|
3,
|
|
4,
|
|
2,
|
|
3,
|
|
4,
|
|
5,
|
|
6,
|
|
7,
|
|
8,
|
|
9,
|
|
10,
|
|
]
|