<s> from __future__ import absolute_import , unicode_literals <EOL> import random <EOL> import unittest <EOL> import pykka <EOL> from mopidy import core <EOL> from mopidy . core import PlaybackState <EOL> from mopidy . internal import deprecation <EOL> from mopidy . local import actor <EOL> from mopidy . models import Playlist , Track <EOL> from tests import dummy_audio , path_to_data_dir <EOL> from tests . local import generate_song , populate_tracklist <EOL> class LocalTracklistProviderTest ( unittest . TestCase ) : <EOL> config = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : path_to_data_dir ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <NUM_LIT> <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : path_to_data_dir ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : b'<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> } <EOL> tracks = [ <EOL> Track ( uri = generate_song ( i ) , length = <NUM_LIT> ) for i in range ( <NUM_LIT:1> , <NUM_LIT:4> ) ] <EOL> def run ( self , result = None ) : <EOL> with deprecation . ignore ( '<STR_LIT>' ) : <EOL> return super ( LocalTracklistProviderTest , self ) . run ( result ) <EOL> def setUp ( self ) : <EOL> self . audio = dummy_audio . create_proxy ( ) <EOL> self . backend = actor . LocalBackend . start ( <EOL> config = self . config , audio = self . audio ) . proxy ( ) <EOL> self . core = core . Core . start ( audio = self . audio , <EOL> backends = [ self . backend ] , <EOL> config = self . config ) . proxy ( ) <EOL> self . controller = self . core . tracklist <EOL> self . playback = self . core . playback <EOL> assert len ( self . tracks ) == <NUM_LIT:3> , '<STR_LIT>' <EOL> def tearDown ( self ) : <EOL> pykka . ActorRegistry . stop_all ( ) <EOL> def assert_state_is ( self , state ) : <EOL> self . assertEqual ( self . playback . get_state ( ) . get ( ) , state ) <EOL> def assert_current_track_is ( self , track ) : <EOL> self . assertEqual ( self . playback . get_current_track ( ) . get ( ) , track ) <EOL> def test_length ( self ) : <EOL> self . assertEqual ( <NUM_LIT:0> , len ( self . controller . get_tl_tracks ( ) . get ( ) ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . controller . get_length ( ) . get ( ) ) <EOL> self . controller . add ( self . tracks ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( self . controller . get_tl_tracks ( ) . get ( ) ) ) <EOL> self . assertEqual ( <NUM_LIT:3> , self . controller . get_length ( ) . get ( ) ) <EOL> def test_add ( self ) : <EOL> for track in self . tracks : <EOL> added = self . controller . add ( [ track ] ) . get ( ) <EOL> tracks = self . controller . get_tracks ( ) . get ( ) <EOL> tl_tracks = self . controller . get_tl_tracks ( ) . get ( ) <EOL> self . assertEqual ( track , tracks [ - <NUM_LIT:1> ] ) <EOL> self . assertEqual ( added [ <NUM_LIT:0> ] , tl_tracks [ - <NUM_LIT:1> ] ) <EOL> self . assertEqual ( track , added [ <NUM_LIT:0> ] . track ) <EOL> def test_add_at_position ( self ) : <EOL> for track in self . tracks [ : - <NUM_LIT:1> ] : <EOL> added = self . controller . add ( [ track ] , <NUM_LIT:0> ) . get ( ) <EOL> tracks = self . controller . get_tracks ( ) . get ( ) <EOL> tl_tracks = self . controller . get_tl_tracks ( ) . get ( ) <EOL> self . assertEqual ( track , tracks [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( added [ <NUM_LIT:0> ] , tl_tracks [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( track , added [ <NUM_LIT:0> ] . track ) <EOL> @ populate_tracklist <EOL> def test_add_at_position_outside_of_playlist ( self ) : <EOL> for track in self . tracks : <EOL> added = self . controller . add ( [ track ] , len ( self . tracks ) + <NUM_LIT:2> ) . get ( ) <EOL> tracks = self . controller . get_tracks ( ) . get ( ) <EOL> tl_tracks = self . controller . get_tl_tracks ( ) . get ( ) <EOL> self . assertEqual ( track , tracks [ - <NUM_LIT:1> ] ) <EOL> self . assertEqual ( added [ <NUM_LIT:0> ] , tl_tracks [ - <NUM_LIT:1> ] ) <EOL> self . assertEqual ( track , added [ <NUM_LIT:0> ] . track ) <EOL> @ populate_tracklist <EOL> def test_filter_by_tlid ( self ) : <EOL> tl_track = self . controller . get_tl_tracks ( ) . get ( ) [ <NUM_LIT:1> ] <EOL> result = self . controller . filter ( { '<STR_LIT>' : [ tl_track . tlid ] } ) . get ( ) <EOL> self . assertEqual ( [ tl_track ] , result ) <EOL> @ populate_tracklist <EOL> def test_filter_by_uri ( self ) : <EOL> tl_track = self . controller . get_tl_tracks ( ) . get ( ) [ <NUM_LIT:1> ] <EOL> result = self . controller . filter ( { '<STR_LIT>' : [ tl_track . track . uri ] } ) . get ( ) <EOL> self . assertEqual ( [ tl_track ] , result ) <EOL> @ populate_tracklist <EOL> def test_filter_by_uri_returns_nothing_for_invalid_uri ( self ) : <EOL> self . assertEqual ( [ ] , self . controller . filter ( { '<STR_LIT>' : [ '<STR_LIT>' ] } ) . get ( ) ) <EOL> def test_filter_by_uri_returns_single_match ( self ) : <EOL> t = Track ( uri = '<STR_LIT:a>' ) <EOL> self . controller . add ( [ Track ( uri = '<STR_LIT:z>' ) , t , Track ( uri = '<STR_LIT:y>' ) ] ) <EOL> result = self . controller . filter ( { '<STR_LIT>' : [ '<STR_LIT:a>' ] } ) . get ( ) <EOL> self . assertEqual ( t , result [ <NUM_LIT:0> ] . track ) <EOL> def test_filter_by_uri_returns_multiple_matches ( self ) : <EOL> track = Track ( uri = '<STR_LIT:a>' ) <EOL> self . controller . add ( [ Track ( uri = '<STR_LIT:z>' ) , track , track ] ) <EOL> tl_tracks = self . controller . filter ( { '<STR_LIT>' : [ '<STR_LIT:a>' ] } ) . get ( ) <EOL> self . assertEqual ( track , tl_tracks [ <NUM_LIT:0> ] . track ) <EOL> self . assertEqual ( track , tl_tracks [ <NUM_LIT:1> ] . track ) <EOL> def test_filter_by_uri_returns_nothing_if_no_match ( self ) : <EOL> self . controller . playlist = Playlist ( <EOL> tracks = [ Track ( uri = '<STR_LIT:z>' ) , Track ( uri = '<STR_LIT:y>' ) ] ) <EOL> self . assertEqual ( [ ] , self . controller . filter ( { '<STR_LIT>' : [ '<STR_LIT:a>' ] } ) . get ( ) ) <EOL> def test_filter_by_multiple_criteria_returns_elements_matching_all ( self ) : <EOL> t1 = Track ( uri = '<STR_LIT:a>' , name = '<STR_LIT:x>' ) <EOL> t2 = Track ( uri = '<STR_LIT:b>' , name = '<STR_LIT:x>' ) <EOL> t3 = Track ( uri = '<STR_LIT:b>' , name = '<STR_LIT:y>' ) <EOL> self . controller . add ( [ t1 , t2 , t3 ] ) <EOL> result1 = self . controller . filter ( { '<STR_LIT>' : [ '<STR_LIT:a>' ] , '<STR_LIT:name>' : [ '<STR_LIT:x>' ] } ) . get ( ) <EOL> self . assertEqual ( t1 , result1 [ <NUM_LIT:0> ] . track ) <EOL> result2 = self . controller . filter ( { '<STR_LIT>' : [ '<STR_LIT:b>' ] , '<STR_LIT:name>' : [ '<STR_LIT:x>' ] } ) . get ( ) <EOL> self . assertEqual ( t2 , result2 [ <NUM_LIT:0> ] . track ) <EOL> result3 = self . controller . filter ( { '<STR_LIT>' : [ '<STR_LIT:b>' ] , '<STR_LIT:name>' : [ '<STR_LIT:y>' ] } ) . get ( ) <EOL> self . assertEqual ( t3 , result3 [ <NUM_LIT:0> ] . track ) <EOL> def test_filter_by_criteria_that_is_not_present_in_all_elements ( self ) : <EOL> track1 = Track ( ) <EOL> track2 = Track ( uri = '<STR_LIT:b>' ) <EOL> track3 = Track ( ) <EOL> self . controller . add ( [ track1 , track2 , track3 ] ) <EOL> result = self . controller . filter ( { '<STR_LIT>' : [ '<STR_LIT:b>' ] } ) . get ( ) <EOL> self . assertEqual ( track2 , result [ <NUM_LIT:0> ] . track ) <EOL> @ populate_tracklist <EOL> def test_clear ( self ) : <EOL> self . controller . clear ( ) . get ( ) <EOL> self . assertEqual ( len ( self . controller . get_tracks ( ) . get ( ) ) , <NUM_LIT:0> ) <EOL> def test_clear_empty_playlist ( self ) : <EOL> self . controller . clear ( ) . get ( ) <EOL> self . assertEqual ( len ( self . controller . get_tracks ( ) . get ( ) ) , <NUM_LIT:0> ) <EOL> @ populate_tracklist <EOL> def test_clear_when_playing ( self ) : <EOL> self . playback . play ( ) . get ( ) <EOL> self . assert_state_is ( PlaybackState . PLAYING ) <EOL> self . controller . clear ( ) . get ( ) <EOL> self . assert_state_is ( PlaybackState . STOPPED ) <EOL> def test_add_appends_to_the_tracklist ( self ) : <EOL> self . controller . add ( [ Track ( uri = '<STR_LIT:a>' ) , Track ( uri = '<STR_LIT:b>' ) ] ) <EOL> tracks = self . controller . get_tracks ( ) . get ( ) <EOL> self . assertEqual ( len ( tracks ) , <NUM_LIT:2> ) <EOL> self . controller . add ( [ Track ( uri = '<STR_LIT:c>' ) , Track ( uri = '<STR_LIT:d>' ) ] ) <EOL> tracks = self . controller . get_tracks ( ) . get ( ) <EOL> self . assertEqual ( len ( tracks ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( tracks [ <NUM_LIT:0> ] . uri , '<STR_LIT:a>' ) <EOL> self . assertEqual ( tracks [ <NUM_LIT:1> ] . uri , '<STR_LIT:b>' ) <EOL> self . assertEqual ( tracks [ <NUM_LIT:2> ] . uri , '<STR_LIT:c>' ) <EOL> self . assertEqual ( tracks [ <NUM_LIT:3> ] . uri , '<STR_LIT:d>' ) <EOL> def test_add_does_not_reset_version ( self ) : <EOL> version = self . controller . get_version ( ) . get ( ) <EOL> self . controller . add ( [ ] ) <EOL> self . assertEqual ( self . controller . get_version ( ) . get ( ) , version ) <EOL> @ populate_tracklist <EOL> def test_add_preserves_playing_state ( self ) : <EOL> self . playback . play ( ) . get ( ) <EOL> track = self . playback . get_current_track ( ) . get ( ) <EOL> tracks = self . controller . get_tracks ( ) . get ( ) <EOL> self . controller . add ( tracks [ <NUM_LIT:1> : <NUM_LIT:2> ] ) . get ( ) <EOL> self . assert_state_is ( PlaybackState . PLAYING ) <EOL> self . assert_current_track_is ( track ) <EOL> @ populate_tracklist <EOL> def test_add_preserves_stopped_state ( self ) : <EOL> tracks = self . controller . get_tracks ( ) . get ( ) <EOL> self . controller . add ( tracks [ <NUM_LIT:1> : <NUM_LIT:2> ] ) . get ( ) <EOL> self . assert_state_is ( PlaybackState . STOPPED ) <EOL> self . assert_current_track_is ( None ) <EOL> @ populate_tracklist <EOL> def test_add_returns_the_tl_tracks_that_was_added ( self ) : <EOL> tracks = self . controller . get_tracks ( ) . get ( ) <EOL> added = self . controller . add ( tracks [ <NUM_LIT:1> : <NUM_LIT:2> ] ) . get ( ) <EOL> tracks = self . controller . get_tracks ( ) . get ( ) <EOL> self . assertEqual ( added [ <NUM_LIT:0> ] . track , tracks [ <NUM_LIT:1> ] ) <EOL> @ populate_tracklist <EOL> def test_move_single ( self ) : <EOL> self . controller . move ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> tracks = self . controller . get_tracks ( ) . get ( ) <EOL> self . assertEqual ( tracks [ <NUM_LIT:2> ] , self . tracks [ <NUM_LIT:0> ] ) <EOL> @ populate_tracklist <EOL> def test_move_group ( self ) : <EOL> self . controller . move ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> tracks = self . controller . get_tracks ( ) . get ( ) <EOL> self . assertEqual ( tracks [ <NUM_LIT:1> ] , self . tracks [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( tracks [ <NUM_LIT:2> ] , self . tracks [ <NUM_LIT:1> ] ) <EOL> @ populate_tracklist <EOL> def test_moving_track_outside_of_playlist ( self ) : <EOL> num_tracks = len ( self . controller . get_tracks ( ) . get ( ) ) <EOL> with self . assertRaises ( AssertionError ) : <EOL> self . controller . move ( <NUM_LIT:0> , <NUM_LIT:0> , num_tracks + <NUM_LIT:5> ) . get ( ) <EOL> @ populate_tracklist <EOL> def test_move_group_outside_of_playlist ( self ) : <EOL> num_tracks = len ( self . controller . get_tracks ( ) . get ( ) ) <EOL> with self . assertRaises ( AssertionError ) : <EOL> self . controller . move ( <NUM_LIT:0> , <NUM_LIT:2> , num_tracks + <NUM_LIT:5> ) . get ( ) <EOL> @ populate_tracklist <EOL> def test_move_group_out_of_range ( self ) : <EOL> num_tracks = len ( self . controller . get_tracks ( ) . get ( ) ) <EOL> with self . assertRaises ( AssertionError ) : <EOL> self . controller . move ( num_tracks + <NUM_LIT:2> , num_tracks + <NUM_LIT:3> , <NUM_LIT:0> ) . get ( ) <EOL> @ populate_tracklist <EOL> def test_move_group_invalid_group ( self ) : <EOL> with self . assertRaises ( AssertionError ) : <EOL> self . controller . move ( <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:0> ) . get ( ) <EOL> def test_tracks_attribute_is_immutable ( self ) : <EOL> tracks1 = self . controller . tracks . get ( ) <EOL> tracks2 = self . controller . tracks . get ( ) <EOL> self . assertNotEqual ( id ( tracks1 ) , id ( tracks2 ) ) <EOL> @ populate_tracklist <EOL> def test_remove ( self ) : <EOL> track1 = self . controller . get_tracks ( ) . get ( ) [ <NUM_LIT:1> ] <EOL> track2 = self . controller . get_tracks ( ) . get ( ) [ <NUM_LIT:2> ] <EOL> version = self . controller . get_version ( ) . get ( ) <EOL> self . controller . remove ( { '<STR_LIT>' : [ track1 . uri ] } ) <EOL> self . assertLess ( version , self . controller . get_version ( ) . get ( ) ) <EOL> self . assertNotIn ( track1 , self . controller . get_tracks ( ) . get ( ) ) <EOL> self . assertEqual ( track2 , self . controller . get_tracks ( ) . get ( ) [ <NUM_LIT:1> ] ) <EOL> @ populate_tracklist <EOL> def test_removing_track_that_does_not_exist_does_nothing ( self ) : <EOL> self . controller . remove ( { '<STR_LIT>' : [ '<STR_LIT>' ] } ) . get ( ) <EOL> def test_removing_from_empty_playlist_does_nothing ( self ) : <EOL> self . controller . remove ( { '<STR_LIT>' : [ '<STR_LIT>' ] } ) . get ( ) <EOL> @ populate_tracklist <EOL> def test_remove_lists ( self ) : <EOL> version = self . controller . get_version ( ) . get ( ) <EOL> tracks = self . controller . get_tracks ( ) . get ( ) <EOL> track0 = tracks [ <NUM_LIT:0> ] <EOL> track1 = tracks [ <NUM_LIT:1> ] <EOL> track2 = tracks [ <NUM_LIT:2> ] <EOL> self . controller . remove ( { '<STR_LIT>' : [ track0 . uri , track2 . uri ] } ) <EOL> tracks = self . controller . get_tracks ( ) . get ( ) <EOL> self . assertLess ( version , self . controller . get_version ( ) . get ( ) ) <EOL> self . assertNotIn ( track0 , tracks ) <EOL> self . assertNotIn ( track2 , tracks ) <EOL> self . assertEqual ( track1 , tracks [ <NUM_LIT:0> ] ) <EOL> @ populate_tracklist <EOL> def test_shuffle ( self ) : <EOL> random . seed ( <NUM_LIT:1> ) <EOL> self . controller . shuffle ( ) <EOL> shuffled_tracks = self . controller . get_tracks ( ) . get ( ) <EOL> self . assertNotEqual ( self . tracks , shuffled_tracks ) <EOL> self . assertEqual ( set ( self . tracks ) , set ( shuffled_tracks ) ) <EOL> @ populate_tracklist <EOL> def test_shuffle_subset ( self ) : <EOL> random . seed ( <NUM_LIT:1> ) <EOL> self . controller . shuffle ( <NUM_LIT:1> , <NUM_LIT:3> ) <EOL> shuffled_tracks = self . controller . get_tracks ( ) . get ( ) <EOL> self . assertNotEqual ( self . tracks , shuffled_tracks ) <EOL> self . assertEqual ( self . tracks [ <NUM_LIT:0> ] , shuffled_tracks [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( set ( self . tracks ) , set ( shuffled_tracks ) ) <EOL> @ populate_tracklist <EOL> def test_shuffle_invalid_subset ( self ) : <EOL> with self . assertRaises ( AssertionError ) : <EOL> self . controller . shuffle ( <NUM_LIT:3> , <NUM_LIT:1> ) . get ( ) <EOL> @ populate_tracklist <EOL> def test_shuffle_superset ( self ) : <EOL> num_tracks = len ( self . controller . get_tracks ( ) . get ( ) ) <EOL> with self . assertRaises ( AssertionError ) : <EOL> self . controller . shuffle ( <NUM_LIT:1> , num_tracks + <NUM_LIT:5> ) . get ( ) <EOL> @ populate_tracklist <EOL> def test_shuffle_open_subset ( self ) : <EOL> random . seed ( <NUM_LIT:1> ) <EOL> self . controller . shuffle ( <NUM_LIT:1> ) <EOL> shuffled_tracks = self . controller . get_tracks ( ) . get ( ) <EOL> self . assertNotEqual ( self . tracks , shuffled_tracks ) <EOL> self . assertEqual ( self . tracks [ <NUM_LIT:0> ] , shuffled_tracks [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( set ( self . tracks ) , set ( shuffled_tracks ) ) <EOL> @ populate_tracklist <EOL> def test_slice_returns_a_subset_of_tracks ( self ) : <EOL> track_slice = self . controller . slice ( <NUM_LIT:1> , <NUM_LIT:3> ) . get ( ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( track_slice ) ) <EOL> self . assertEqual ( self . tracks [ <NUM_LIT:1> ] , track_slice [ <NUM_LIT:0> ] . track ) <EOL> self . assertEqual ( self . tracks [ <NUM_LIT:2> ] , track_slice [ <NUM_LIT:1> ] . track ) <EOL> @ populate_tracklist <EOL> def test_slice_returns_empty_list_if_indexes_outside_tracks_list ( self ) : <EOL> self . assertEqual ( <NUM_LIT:0> , len ( self . controller . slice ( <NUM_LIT:7> , <NUM_LIT:8> ) . get ( ) ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( self . controller . slice ( - <NUM_LIT:1> , <NUM_LIT:1> ) . get ( ) ) ) <EOL> def test_version_does_not_change_when_adding_nothing ( self ) : <EOL> version = self . controller . get_version ( ) . get ( ) <EOL> self . controller . add ( [ ] ) <EOL> self . assertEqual ( version , self . controller . get_version ( ) . get ( ) ) <EOL> def test_version_increases_when_adding_something ( self ) : <EOL> version = self . controller . get_version ( ) . get ( ) <EOL> self . controller . add ( [ Track ( ) ] ) <EOL> self . assertLess ( version , self . controller . get_version ( ) . get ( ) ) </s>
