def test_post_below_or_above_action(self): m2 = Music( music_id="b", name="b", thumbnail="https://a.com", total_duration=114, duration=114, url="https://www.a.com", source="youtube", room=self.r, ) m2.save() pt2 = PlaylistTrack(track=m2, room=self.r) pt2.save() response = self.client.post('/playlist/%s/below/%s' % (self.pt.pk, pt2.pk)) response.status_code.should.eql(status.HTTP_200_OK) list(self.r.playlist.all()).should.eql([pt2, self.pt]) response = self.client.post('/playlist/%s/above/%s' % (pt2.pk, self.pt.pk)) response.status_code.should.eql(status.HTTP_200_OK) list(self.r.playlist.all()).should.eql([pt2, self.pt])
def test_initialization(self): settings.TESTING = False # Create fake app to test ready method init = Init.create('music') init.models = { 'playlisttrack': PlaylistTrack } music = Music( room=self.r, music_id="a", name="a", total_duration=211, duration=211, thumbnail="https://a.com/a.jpg", ) music.save() PlaylistTrack(track=music, room=self.r).save() init.ready() self.r.tracks.count().should.eql(0) settings.TESTING = True
def test_play_next_with_shuffle(self): music = Music( room=self.r, music_id='a', name='a', total_duration=200, duration=200, thumbnail='https://a.com/a.jpg', ) music.save() music2 = Music( room=self.r, music_id='b', name='b', total_duration=200, duration=200, thumbnail='https://a.com/a.jpg', ) music2.save() self.r.shuffle = True self.r.save() PlaylistTrack(track=music, room=self.r).save() self.r.play_next() self.r.current_music.should.eql(music) self.r.play_next() # No music in the playlist, but should select an other. self.r.current_music.should_not.be.none
def setUp(self): super().setUp() self.m = Music( music_id="a", name="a", thumbnail="https://a.com", total_duration=114, duration=104, url="https://www.a.com", source="youtube", timer_start=10, room=self.r, ) self.m.save() self.pt = PlaylistTrack(track=self.m, room=self.r) self.pt.save()
def test_other_action(self): m2 = Music( music_id="b", name="b", thumbnail="https://a.com", total_duration=114, duration=114, url="https://www.a.com", source="youtube", room=self.r, ) m2.save() pt2 = PlaylistTrack(track=m2, room=self.r) pt2.save() # test only one action to test the endpoint. Actions themselves are already tested in OrderedModel lib response = self.client.post('/playlist/%s/top' % pt2.pk) response.status_code.should.eql(status.HTTP_200_OK) list(self.r.playlist.all()).should.eql([pt2, self.pt])
def test_get_musics_remaining(self): music = Music( room=self.r, music_id='a', name='a', total_duration=200, duration=200, thumbnail='https://a.com/a.jpg', ) music.save() PlaylistTrack(track=music, room=self.r).save() list(self.r.get_musics_remaining()).should.eql([music])
def test_playlist_serializer(self): m = Music( music_id='a', name='a', thumbnail='https://a.com', total_duration=114, duration=114, url='https://www.a.com', source='youtube', room=self.r, ) m.save() pt = PlaylistTrack(track=m, room=self.r) pt.save() expected_serialization = { 'pk': pt.pk, 'order': 0, 'music': { 'pk': m.pk, 'music_id': 'a', 'name': 'a', 'thumbnail': 'https://a.com', 'total_duration': 114, 'duration': 114, 'url': 'https://www.a.com', 'source': 'youtube', 'timer_start': 0, 'count': 0, 'last_play': None, 'one_shot': False } } dict(PlaylistSerializer(pt).data).should.eql(expected_serialization)
def test_get_remaining_time(self): # Still no music self.r.get_remaining_time().should.eql(0) music = Music(room=self.r, music_id='a', name='a', total_duration=200, duration=200, thumbnail='https://a.com/a.jpg', last_play=datetime.now()) music.save() PlaylistTrack(track=music, room=self.r).save() self.r.get_remaining_time().should.eql(200)
def test_play_next_without_shuffle(self): music = Music( room=self.r, music_id='a', name='a', total_duration=200, duration=200, thumbnail='https://a.com/a.jpg', ) music.save() PlaylistTrack(track=music, room=self.r).save() self.r.play_next() self.r.current_music.should.eql(music) self.r.play_next() # No more music in the playlist. self.r.current_music.should.be.none
class TestPlaylist(EndpointTestCase): def setUp(self): super().setUp() self.m = Music( music_id="a", name="a", thumbnail="https://a.com", total_duration=114, duration=104, url="https://www.a.com", source="youtube", timer_start=10, room=self.r, ) self.m.save() self.pt = PlaylistTrack(track=self.m, room=self.r) self.pt.save() def test_get(self): response = self.client.get('/playlist') response.status_code.should.eql(status.HTTP_200_OK) expected_result = [{ 'pk': self.pt.pk, 'order': 0, 'music': { 'pk': self.m.pk, 'music_id': 'a', 'name': 'a', 'thumbnail': 'https://a.com', 'total_duration': 114, 'duration': 104, 'url': 'https://www.a.com', 'source': 'youtube', 'timer_start': 10, 'count': 0, 'last_play': None, 'one_shot': False } }] list(response.data).should.eql(expected_result) def test_delete(self): response = self.client.delete('/playlist/%s' % self.pt.pk) response.status_code.should.eql(status.HTTP_204_NO_CONTENT) def test_delete_unexisting(self): response = self.client.delete('/playlist/1337') response.status_code.should.eql(status.HTTP_404_NOT_FOUND) def test_post_bad_pk(self): response = self.client.post('/playlist/1894/top') response.status_code.should.eql(status.HTTP_404_NOT_FOUND) response.data.should.eql("Can't find this playlistTrack.") def test_post_bad_action(self): response = self.client.post('/playlist/%s/badaction' % self.pt.pk) response.status_code.should.eql(status.HTTP_400_BAD_REQUEST) response.data.should.eql('Action can only be: "%s"' % '" or "'.join(PlaylistTrack.ACTIONS)) def test_post_above_and_below_action_without_target(self): response = self.client.post('/playlist/%s/above' % self.pt.pk) response.status_code.should.eql(status.HTTP_400_BAD_REQUEST) response.data.should.eql('"above" action needs a target parameter') response = self.client.post('/playlist/%s/below' % self.pt.pk) response.status_code.should.eql(status.HTTP_400_BAD_REQUEST) response.data.should.eql('"below" action needs a target parameter') def test_post_above_and_below_action_with_bad_target(self): response = self.client.post('/playlist/%s/above/1337' % self.pt.pk) response.status_code.should.eql(status.HTTP_404_NOT_FOUND) response.data.should.eql("Can't find this playlistTrack as target.") response = self.client.post('/playlist/%s/below/1337' % self.pt.pk) response.status_code.should.eql(status.HTTP_404_NOT_FOUND) response.data.should.eql("Can't find this playlistTrack as target.") def test_post_below_or_above_action(self): m2 = Music( music_id="b", name="b", thumbnail="https://a.com", total_duration=114, duration=114, url="https://www.a.com", source="youtube", room=self.r, ) m2.save() pt2 = PlaylistTrack(track=m2, room=self.r) pt2.save() response = self.client.post('/playlist/%s/below/%s' % (self.pt.pk, pt2.pk)) response.status_code.should.eql(status.HTTP_200_OK) list(self.r.playlist.all()).should.eql([pt2, self.pt]) response = self.client.post('/playlist/%s/above/%s' % (pt2.pk, self.pt.pk)) response.status_code.should.eql(status.HTTP_200_OK) list(self.r.playlist.all()).should.eql([pt2, self.pt]) def test_other_action(self): m2 = Music( music_id="b", name="b", thumbnail="https://a.com", total_duration=114, duration=114, url="https://www.a.com", source="youtube", room=self.r, ) m2.save() pt2 = PlaylistTrack(track=m2, room=self.r) pt2.save() # test only one action to test the endpoint. Actions themselves are already tested in OrderedModel lib response = self.client.post('/playlist/%s/top' % pt2.pk) response.status_code.should.eql(status.HTTP_200_OK) list(self.r.playlist.all()).should.eql([pt2, self.pt])