Exemplo n.º 1
0
    def test_get_ordered_playlist_and_pop_first_song(self):
        with db_operations.session_scope() as session:
            playlist_manager = stream_playlist.PlaylistManager(session)
            playlist_manager.add_song_by_id(1)
            playlist_manager.add_song_by_id(2, True)
            playlist_manager.add_song_by_id(3)
            playlist_manager.add_song_by_id(4, True)
            ordered_playlist = playlist_manager.get_ordered_playlist()
            ordered_playlist_as_list = playlist_manager.get_ordered_playlist(
                as_list=True)
            assert type(list()) == type(ordered_playlist_as_list)
        expected_playlist = {}
        expected_song_ids = [2, 4, 1, 3]
        expected_requested_state = [True, True, False, False]
        for ii in range(len(expected_song_ids)):
            expected_playlist[unicode(ii)] = {
                'song_id': expected_song_ids[ii],
                'user_requested': expected_requested_state[ii]
            }
        assert expected_playlist == ordered_playlist

        with db_operations.session_scope() as session:
            playlist_manager = stream_playlist.PlaylistManager(session)
            playlist_manager.pop_first_song()
            ordered_playlist = playlist_manager.get_ordered_playlist()
        expected_playlist = {}
        expected_song_ids.pop(0)
        expected_requested_state.pop(0)
        for ii in range(len(expected_song_ids)):
            expected_playlist[unicode(ii)] = {
                'song_id': expected_song_ids[ii],
                'user_requested': expected_requested_state[ii]
            }
        assert expected_playlist == ordered_playlist
Exemplo n.º 2
0
 def test_add_random_songs_to_playlist_until_max_length(self):
     with db_operations.session_scope() as session:
         playlist_manager = stream_playlist.PlaylistManager(session)
         assert 0 == playlist_manager.get_playlist_length()
         self._stream_controller.add_random_songs_to_playlist_until_max_length(
         )
         playlist_size = config.stream_options['playlist_size']
         assert playlist_size == playlist_manager.get_playlist_length()
Exemplo n.º 3
0
 def test_get_playlist_length(self):
     with db_operations.session_scope() as session:
         playlist_manager = stream_playlist.PlaylistManager(session)
         songs_to_add = 5
         for ii in range(songs_to_add):
             assert ii == playlist_manager.get_playlist_length()
             playlist_manager.add_song_by_id(ii)
         assert songs_to_add == playlist_manager.get_playlist_length()
Exemplo n.º 4
0
 def add_song_with_user_request_to_playlist(self,
                                            song_id,
                                            user_requested=False):
     with db_operations.session_scope() as session:
         playlist_maanger = stream_playlist.PlaylistManager(session)
         playlist_maanger.add_song_by_id(song_id, user_requested)
     if user_requested:
         self._song_cache.set_song_request_block_with_ttl(
             song_id, config.stream_options['song_request_cooldown'])
Exemplo n.º 5
0
 def test_set_first_song_as_currently_playing(self):
     with db_operations.session_scope() as session:
         playlist_manager = stream_playlist.PlaylistManager(session)
         playlist_manager.add_song_by_id(1)
         playlist_manager.set_first_song_as_currently_playing()
     with db_operations.session_scope() as session:
         first_song = session.query(
             stream_playlist.Playlist).filter_by(id=1).one()
         assert True == first_song.currently_playing
Exemplo n.º 6
0
 def test_transition_to_next_song(self, mock_add, mock_crop):
     with db_operations.session_scope() as session:
         playlist_manager = stream_playlist.PlaylistManager(session)
         prev_playlist = playlist_manager.get_ordered_playlist()
         self._stream_controller.transition_to_next_song()
         after_playlist = playlist_manager.get_ordered_playlist()
         assert prev_playlist != after_playlist
         self._stream_controller.transition_to_next_song()
     assert True == mock_add.called
     assert True == mock_crop.called
Exemplo n.º 7
0
 def get(self):
     if song_cache.get_playlist():
         current_playlist = json.loads(song_cache.get_playlist())
     else:
         with db_operations.session_scope() as session:
             playlist_manager = stream_playlist.PlaylistManager(session)
             current_playlist = playlist_manager.get_ordered_playlist()
             song_manager = song.SongManager(session)
             for playlist_order, playlist_data in current_playlist.iteritems():
                 self._add_song_data_to_playlist_data(playlist_data, song_manager)
         song_cache.set_playlist(json.dumps(current_playlist))
     self.write(current_playlist)
Exemplo n.º 8
0
 def transition_to_next_song(self):
     with db_operations.session_scope() as session:
         playlist_manager = stream_playlist.PlaylistManager(session)
         song_manager = song.SongManager(session)
         playlist_manager.pop_first_song()
         playlist_manager.set_first_song_as_currently_playing()
         ordered_playlist = playlist_manager.get_ordered_playlist(
             as_list=True)
         next_two_songs = ordered_playlist[1:3]
         self._stream_player.crop()
         for coming_song in next_two_songs:
             song_data = song_manager.get_song_by_id(coming_song['song_id'])
             self._stream_player.add(song_data['file_path'])
     self.add_random_songs_to_playlist_until_max_length()
Exemplo n.º 9
0
    def test_add_song_by_id(self):
        with db_operations.session_scope() as session:
            playlist_manager = stream_playlist.PlaylistManager(session)
            playlist_manager.add_song_by_id(1)
            playlist_manager.add_song_by_id(2, True)
        expected_row_1 = {
            'id': 1,
            'song_id': 1,
            'user_requested': False,
            'currently_playing': False
        }
        expected_row_2 = {
            'id': 2,
            'song_id': 2,
            'user_requested': True,
            'currently_playing': False
        }
        with db_operations.session_scope() as session:
            playlist_manager = stream_playlist.PlaylistManager(session)
            playlist_song_1 = session.query(
                stream_playlist.Playlist).filter_by(id=1).first()
            playlist_song_2 = session.query(
                stream_playlist.Playlist).filter_by(id=2).first()
            playlist_song_1_contents = \
                playlist_manager.format_query_rows_to_dict(playlist_song_1)
            playlist_song_2_contents = \
                playlist_manager.format_query_rows_to_dict(playlist_song_2)
            playlist_song_1_time = playlist_song_1_contents.pop(
                'datetime_added')
            playlist_song_2_time = playlist_song_2_contents.pop(
                'datetime_added')

            assert expected_row_1 == playlist_song_1_contents
            assert type(datetime.datetime.now()) == type(playlist_song_1_time)
            assert expected_row_2 == playlist_song_2_contents
            assert type(datetime.datetime.now()) == type(playlist_song_2_time)
Exemplo n.º 10
0
 def add_random_songs_to_playlist_until_max_length(self):
     with db_operations.session_scope() as session:
         playlist_manager = stream_playlist.PlaylistManager(session)
         max_playlist_size = config.stream_options['playlist_size']
         while playlist_manager.get_playlist_length() < max_playlist_size:
             self.add_random_song_with_user_request_to_playlist()