Skip to content

Commit

Permalink
test playlist XML consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanljones committed Oct 3, 2023
1 parent af53096 commit 012d5a3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 31 deletions.
8 changes: 0 additions & 8 deletions pyrekordbox/db6/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -1289,14 +1289,6 @@ def delete_playlist(self, playlist):
self.registry.on_delete(child_ids[1:])
self.registry.on_delete(moved)

# Check masterPlaylist6.xml
if self.playlist_xml is not None:
for plxml in self.playlist_xml.get_playlists():
if plxml["Lib_Type"] != 0:
continue
pid = int(plxml["Id"], 16)
assert self.query(tables.DjmdPlaylist).filter_by(ID=pid).count() == 1

def move_playlist(self, playlist, parent=None, seq=None):
"""Moves a playlist (folder) in the current parent folder or to a new one.
Expand Down
87 changes: 64 additions & 23 deletions tests/test_db6.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,28 @@ def test_autoincrement_local_usn(db):
assert playlist.rb_local_usn == new_usn


def _check_playlist_xml(db):
# Check that playlist is in XML and update time is correct
for pl in db.get_playlist():
plxml = db.playlist_xml.get(pl.ID)
ts = plxml["Timestamp"]
diff = pl.updated_at - ts
if abs(diff.total_seconds()) > 1:
return False
return True


def _check_playlist_xml_delete(db):
# Check that there are no items in the XML that are not in the db
for plxml in db.playlist_xml.get_playlists():
if plxml["Lib_Type"] != 0:
continue
pid = int(plxml["Id"], 16)
if db.query(tables.DjmdPlaylist).filter_by(ID=pid).count() != 1:
return False
return True


def test_add_song_to_playlist(db):
usn_old = db.get_local_usn()
mtime_old = db.get_playlist(ID=PID1).updated_at
Expand All @@ -273,6 +295,8 @@ def test_add_song_to_playlist(db):
with pytest.raises(ValueError):
db.add_to_playlist(PID1, CID2, track_no=3)

assert _check_playlist_xml(db)


def test_add_song_to_playlist_trackno_end(db):
old_usn = db.get_local_usn()
Expand All @@ -285,6 +309,8 @@ def test_add_song_to_playlist_trackno_end(db):
assert song3.TrackNo == 3
assert db.get_local_usn() == old_usn + 3

assert _check_playlist_xml(db)


def test_add_song_to_playlist_trackno_middle(db):
song1 = db.add_to_playlist(PID1, CID1)
Expand Down Expand Up @@ -319,6 +345,8 @@ def test_add_song_to_playlist_trackno_middle(db):
assert songs[2].rb_local_usn == usn_old + 2
assert db.get_local_usn() == usn_old + 2

assert _check_playlist_xml(db)


def test_remove_song_from_playlist_end(db):
# Add songs to playlist
Expand Down Expand Up @@ -347,6 +375,8 @@ def test_remove_song_from_playlist_end(db):
assert pl.updated_at == mtime_old
assert db.get_local_usn() == usn_old + 1

assert _check_playlist_xml(db)


def test_remove_song_from_playlist_middle(db):
# Add songs to playlist
Expand All @@ -373,6 +403,8 @@ def test_remove_song_from_playlist_middle(db):
assert db.get_local_usn() == usn_old + 2
assert songs[1].rb_local_usn == usn_old + 2

assert _check_playlist_xml(db)


def test_move_in_playlist_forward(db):
# Add songs to playlist
Expand Down Expand Up @@ -401,6 +433,8 @@ def test_move_in_playlist_forward(db):
assert s3.rb_local_usn == expected_usn
assert s4.rb_local_usn == usn_old

assert _check_playlist_xml(db)


def test_move_in_playlist_backward(db):
# Add songs to playlist
Expand Down Expand Up @@ -429,6 +463,8 @@ def test_move_in_playlist_backward(db):
assert s3.rb_local_usn == expected_usn
assert s4.rb_local_usn == usn_old

assert _check_playlist_xml(db)


def test_create_playlist(db):
seqs = [pl.Seq for pl in db.get_playlist()]
Expand Down Expand Up @@ -463,8 +499,7 @@ def test_create_playlist(db):
# Check if playlist was added to xml
plxml = db.playlist_xml.get(pl.ID)
assert plxml is not None
ts = plxml["Timestamp"]
assert int(pl.updated_at.timestamp() * 1000) == int(ts.timestamp() * 1000)
assert _check_playlist_xml(db)


def test_create_playlist_seq_middle(db):
Expand Down Expand Up @@ -495,6 +530,8 @@ def test_create_playlist_seq_middle(db):
assert pl1.rb_local_usn == old_usn + 1
assert pl2.rb_local_usn == old_usn + 3

assert _check_playlist_xml(db)


def test_create_playlist_folder(db):
seqs = [pl.Seq for pl in db.get_playlist()]
Expand Down Expand Up @@ -525,27 +562,7 @@ def test_create_playlist_folder(db):
pl = db.get_playlist(ID=pid)
assert len(pl.Children) == 1


def test_playlist_xml_sync(db):
# Create playlist structure
folder1 = db.create_playlist_folder("dev folder")
folder2 = db.create_playlist_folder("sub folder 1", parent=folder1.ID)
db.create_playlist("sub playlist 1", parent=folder1.ID)
db.create_playlist("sub playlist 2", parent=folder1.ID)
db.create_playlist("sub sub playlist 1", parent=folder2.ID)
db.create_playlist("sub sub playlist 2", parent=folder2.ID)
db.commit()

# Check that every playlist is synced in the masterPlaylists xml file
for pl in db.get_playlist():
if pl.Name == "Trial playlist - Cloud Library Sync":
continue
plxml = db.playlist_xml.get(pl.ID)
# Check if playlist exists
assert plxml is not None
# Check update time
ts = plxml["Timestamp"]
assert int(pl.updated_at.timestamp() * 1000) == int(ts.timestamp() * 1000)
assert _check_playlist_xml(db)


def test_delete_playlist_empty_end(db):
Expand Down Expand Up @@ -573,6 +590,9 @@ def test_delete_playlist_empty_end(db):
# Check USN is correct (+1 for deleting)
assert db.get_local_usn() == usn_old + 1

assert _check_playlist_xml(db)
assert _check_playlist_xml_delete(db)


def test_delete_playlist_empty(db):
# Create playlist structure
Expand Down Expand Up @@ -603,6 +623,9 @@ def test_delete_playlist_empty(db):
assert pl.Seq == 2
assert pl.rb_local_usn == usn_old + 1

assert _check_playlist_xml(db)
assert _check_playlist_xml_delete(db)


def test_delete_playlist_folder_empty(db):
# Create playlist structure
Expand Down Expand Up @@ -633,6 +656,9 @@ def test_delete_playlist_folder_empty(db):
assert db.get_local_usn() == usn_old + 1
assert pl.rb_local_usn == usn_old + 1

assert _check_playlist_xml(db)
assert _check_playlist_xml_delete(db)


def test_delete_playlist_non_empty(db):
# Create playlist structure
Expand Down Expand Up @@ -668,6 +694,9 @@ def test_delete_playlist_non_empty(db):
# Check if USN is correct (+1 for deleting with contents)
assert db.get_local_usn() == usn_old + 1

assert _check_playlist_xml(db)
assert _check_playlist_xml_delete(db)


def test_delete_playlist_folder_non_empty(db):
# Create playlist structure
Expand Down Expand Up @@ -710,6 +739,9 @@ def test_delete_playlist_folder_non_empty(db):
# Check if USN is correct (+1 for deleting with Seq update, +1 for children)
assert db.get_local_usn() == usn_old + 2

assert _check_playlist_xml(db)
assert _check_playlist_xml_delete(db)


def test_delete_playlist_folder_chained(db):
# Create playlist structure
Expand Down Expand Up @@ -762,6 +794,9 @@ def test_delete_playlist_folder_chained(db):
# Check if USN is correct (+1 for deleting with Seq update, +1 for children)
assert db.get_local_usn() == usn_old + 2

assert _check_playlist_xml(db)
assert _check_playlist_xml_delete(db)


def test_move_playlist_seq(db):
# Create playlist structure
Expand Down Expand Up @@ -838,6 +873,8 @@ def test_move_playlist_seq(db):
assert pl3.updated_at > mtime_old
assert f2.updated_at > mtime_old

assert _check_playlist_xml(db)


def test_move_playlist_parent(db):
# Create playlist structure
Expand Down Expand Up @@ -916,6 +953,8 @@ def test_move_playlist_parent(db):
assert subpl4.rb_local_usn == old_usn + 4
assert pl1.rb_local_usn == old_usn + 5

assert _check_playlist_xml(db)


def test_rename_playlist(db):
# Create playlist structure
Expand All @@ -939,6 +978,8 @@ def test_rename_playlist(db):
assert db.get_local_usn() == usn_old + 1
assert pl.rb_local_usn == usn_old + 1

assert _check_playlist_xml(db)


def test_get_anlz_paths():
content = DB.get_content().first()
Expand Down

0 comments on commit 012d5a3

Please sign in to comment.