emmental/trackdb/test_stack.py

112 lines
3.2 KiB
Python
Raw Normal View History

# Copyright 2021 (c) Anna Schumaker.
from lib import publisher
from lib import tag
from . import stack
from . import tags
import unittest
class FakeTrack:
def __init__(self, n):
self.n = n
self.length = n
def add_to_playlist(self, name):
tags.User.add(name, self)
def remove_from_playlist(self, name):
tags.User[name].remove_track(self)
class TestTagStack(unittest.TestCase):
def setUp(self):
self.pushpop = None
def tearDown(self):
tags.reset()
def on_push_pop(self, prev, new):
self.pushpop = (prev, new)
def test_tag_stack_init(self):
s = stack.TagStack()
self.assertIsInstance(s.PushPop, publisher.Publisher)
self.assertEqual(s.tags, [ ])
def test_tag_stack_next(self):
s = stack.TagStack()
t = tag.Tag("Test")
s.push(t)
t.tracks = [ FakeTrack(1), FakeTrack(2), FakeTrack(3) ]
tags.User["Collection"].tracks = [ FakeTrack(4), FakeTrack(5) ]
self.assertEqual(s.next().n, 1)
self.assertEqual(s.tags, [ t ])
self.assertEqual(s.next().n, 2)
self.assertEqual(s.tags, [ t ])
self.assertEqual(s.next().n, 3)
self.assertEqual(s.tags, [ t ])
self.assertEqual(s.next().n, 4)
self.assertEqual(s.tags, [ ])
self.assertEqual(s.next().n, 5)
self.assertEqual([ t.n for t in tags.User["Previous"].tracks ],
[ 5, 4, 3, 2, 1 ])
def test_tag_stack_pop(self):
s = stack.TagStack()
t1 = tag.Tag("Test")
t2 = tag.Tag("Test Two")
s.tags = [ t1, t2 ]
s.PushPop.register(self.on_push_pop)
s.pop()
self.assertEqual(s.tags, [ t2 ])
self.assertEqual(self.pushpop, (t1, t2))
s.pop()
self.assertEqual(s.tags, [ ])
self.assertEqual(self.pushpop, (t2, tags.User["Collection"]))
def test_tag_stack_previous(self):
s = stack.TagStack()
for i in [ 1, 2, 3 ]:
tags.User["Previous"].add_track(i)
self.assertEqual(s.previous(), 2)
self.assertEqual(s.previous(), 1)
self.assertIsNone(s.previous())
def test_tag_stack_push(self):
s = stack.TagStack()
t1 = tag.Tag("Test")
t2 = tag.Tag("Test Two")
t1.current = 3
s.PushPop.register(self.on_push_pop)
s.push(t1)
self.assertEqual(s.tags, [ t1 ])
self.assertEqual(t1.current, -1)
self.assertEqual(self.pushpop, (tags.User["Collection"], t1))
s.push(t2)
self.assertEqual(s.tags, [ t2, t1 ])
self.assertEqual(self.pushpop, (t1, t2))
s.push(t1)
self.assertEqual(s.tags, [ t1, t2 ])
self.assertEqual(self.pushpop, (t2, t1))
s.push(tags.User["Previous"])
self.assertEqual(s.tags, [ t1, t2 ])
s.push(tags.User["Collection"])
self.assertEqual(s.tags, [ ])
self.assertEqual(self.pushpop, (t1, tags.User["Collection"]))
def test_tag_stack_queue(self):
s = stack.TagStack()
s.queue(FakeTrack(1))
self.assertEqual(s.tags, [ tags.User["Up Next"] ])
self.assertEqual(s.next().n, 1)
self.assertEqual(tags.User["Up Next"].tracks, [ ])