lib: Return trackids in Tag.__getstate__()

And provide a function for replacing the trackid with an actual track
during startup.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-06-23 11:08:35 -04:00
parent 51fff44746
commit e0c8f6dac6
2 changed files with 23 additions and 5 deletions

View File

@ -11,7 +11,7 @@ class Tag:
def __getstate__(self):
with self.lock:
return { "name" : self.name,
"tracks" : self.tracks }
"tracks" : [ t.trackid for t in self.tracks ] }
def __len__(self):
with self.lock:
@ -33,6 +33,14 @@ class Tag:
with self.lock:
self.tracks.append(track)
def init_track(self, track):
with self.lock:
try:
i = self.tracks.index(track.trackid)
self.tracks[i] = track
except Exception as e:
pass
def remove_track(self, track):
with self.lock:
self.tracks.remove(track)

View File

@ -3,6 +3,11 @@ from . import tag
import threading
import unittest
class FakeTrack:
def __init__(self, trackid):
self.trackid = trackid
class TestTag(unittest.TestCase):
def test_tag_init(self):
t = tag.Tag("test")
@ -16,7 +21,7 @@ class TestTag(unittest.TestCase):
def test_tag_len(self):
t = tag.Tag("Test")
self.assertEqual(len(t), 0)
t.tracks = [ 1, 2, 3, 4, 5 ]
t.tracks = [ FakeTrack(i) for i in range(5) ]
self.assertEqual(len(t), 5)
def test_tag_lt(self):
@ -29,20 +34,25 @@ class TestTag(unittest.TestCase):
def test_tag_state(self):
t = tag.Tag("test")
t.tracks = [ 1, 2, 3, 4, 5 ]
tracks = [ FakeTrack(i) for i in range(5) ]
t.tracks = tracks
state = t.__getstate__()
self.assertEqual(set(state.keys()), set([ "name", "tracks" ]))
self.assertEqual(state["name"], "test")
self.assertEqual(state["tracks"], [ 1, 2, 3, 4, 5 ])
self.assertEqual(state["tracks"], [ 0, 1, 2, 3, 4 ])
t.__dict__.clear()
t.__setstate__(state)
self.assertEqual(t.name, "test")
self.assertEqual(t.tracks, [ 1, 2, 3, 4, 5 ])
self.assertEqual(t.tracks, [ 0, 1, 2, 3, 4 ])
self.assertEqual(t.widgets, None)
self.assertIsInstance(t.lock, type(threading.Lock()))
for track in tracks:
t.init_track(track)
self.assertEqual(t.tracks, tracks)
def test_tag_add_track(self):
t = tag.Tag("test")
t.add_track(1)