<s> from __future__ import absolute_import , unicode_literals <EOL> import unittest <EOL> import mock <EOL> from mopidy import backend , core <EOL> from mopidy . internal import deprecation <EOL> from mopidy . models import TlTrack , Track <EOL> class TracklistTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> config = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> } <EOL> self . tracks = [ <EOL> Track ( uri = '<STR_LIT>' , name = '<STR_LIT:foo>' ) , <EOL> Track ( uri = '<STR_LIT>' , name = '<STR_LIT:foo>' ) , <EOL> Track ( uri = '<STR_LIT>' , name = '<STR_LIT:bar>' ) , <EOL> ] <EOL> def lookup ( uri ) : <EOL> future = mock . Mock ( ) <EOL> future . get . return_value = [ t for t in self . tracks if t . uri == uri ] <EOL> return future <EOL> self . backend = mock . Mock ( ) <EOL> self . backend . uri_schemes . get . return_value = [ '<STR_LIT>' ] <EOL> self . library = mock . Mock ( spec = backend . LibraryProvider ) <EOL> self . library . lookup . side_effect = lookup <EOL> self . backend . library = self . library <EOL> self . core = core . Core ( config , mixer = None , backends = [ self . backend ] ) <EOL> self . tl_tracks = self . core . tracklist . add ( uris = [ <EOL> t . uri for t in self . tracks ] ) <EOL> def test_add_by_uri_looks_up_uri_in_library ( self ) : <EOL> self . library . lookup . reset_mock ( ) <EOL> self . core . tracklist . clear ( ) <EOL> with deprecation . ignore ( '<STR_LIT>' ) : <EOL> tl_tracks = self . core . tracklist . add ( uris = [ '<STR_LIT>' ] ) <EOL> self . library . lookup . assert_called_once_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( tl_tracks ) ) <EOL> self . assertEqual ( self . tracks [ <NUM_LIT:0> ] , tl_tracks [ <NUM_LIT:0> ] . track ) <EOL> self . assertEqual ( tl_tracks , self . core . tracklist . tl_tracks [ - <NUM_LIT:1> : ] ) <EOL> def test_add_by_uris_looks_up_uris_in_library ( self ) : <EOL> self . library . lookup . reset_mock ( ) <EOL> self . core . tracklist . clear ( ) <EOL> tl_tracks = self . core . tracklist . add ( uris = [ t . uri for t in self . tracks ] ) <EOL> self . library . lookup . assert_has_calls ( [ <EOL> mock . call ( '<STR_LIT>' ) , <EOL> mock . call ( '<STR_LIT>' ) , <EOL> mock . call ( '<STR_LIT>' ) , <EOL> ] ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( tl_tracks ) ) <EOL> self . assertEqual ( self . tracks [ <NUM_LIT:0> ] , tl_tracks [ <NUM_LIT:0> ] . track ) <EOL> self . assertEqual ( self . tracks [ <NUM_LIT:1> ] , tl_tracks [ <NUM_LIT:1> ] . track ) <EOL> self . assertEqual ( self . tracks [ <NUM_LIT:2> ] , tl_tracks [ <NUM_LIT:2> ] . track ) <EOL> self . assertEqual ( <EOL> tl_tracks , self . core . tracklist . tl_tracks [ - len ( tl_tracks ) : ] ) <EOL> def test_remove_removes_tl_tracks_matching_query ( self ) : <EOL> tl_tracks = self . core . tracklist . remove ( { '<STR_LIT:name>' : [ '<STR_LIT:foo>' ] } ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( tl_tracks ) ) <EOL> self . assertListEqual ( self . tl_tracks [ : <NUM_LIT:2> ] , tl_tracks ) <EOL> self . assertEqual ( <NUM_LIT:1> , self . core . tracklist . length ) <EOL> self . assertListEqual ( self . tl_tracks [ <NUM_LIT:2> : ] , self . core . tracklist . tl_tracks ) <EOL> def test_remove_works_with_dict_instead_of_kwargs ( self ) : <EOL> tl_tracks = self . core . tracklist . remove ( { '<STR_LIT:name>' : [ '<STR_LIT:foo>' ] } ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( tl_tracks ) ) <EOL> self . assertListEqual ( self . tl_tracks [ : <NUM_LIT:2> ] , tl_tracks ) <EOL> self . assertEqual ( <NUM_LIT:1> , self . core . tracklist . length ) <EOL> self . assertListEqual ( self . tl_tracks [ <NUM_LIT:2> : ] , self . core . tracklist . tl_tracks ) <EOL> def test_filter_returns_tl_tracks_matching_query ( self ) : <EOL> tl_tracks = self . core . tracklist . filter ( { '<STR_LIT:name>' : [ '<STR_LIT:foo>' ] } ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( tl_tracks ) ) <EOL> self . assertListEqual ( self . tl_tracks [ : <NUM_LIT:2> ] , tl_tracks ) <EOL> def test_filter_works_with_dict_instead_of_kwargs ( self ) : <EOL> tl_tracks = self . core . tracklist . filter ( { '<STR_LIT:name>' : [ '<STR_LIT:foo>' ] } ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( tl_tracks ) ) <EOL> self . assertListEqual ( self . tl_tracks [ : <NUM_LIT:2> ] , tl_tracks ) <EOL> def test_filter_fails_if_values_isnt_iterable ( self ) : <EOL> with self . assertRaises ( ValueError ) : <EOL> self . core . tracklist . filter ( { '<STR_LIT>' : <NUM_LIT:3> } ) <EOL> def test_filter_fails_if_values_is_a_string ( self ) : <EOL> with self . assertRaises ( ValueError ) : <EOL> self . core . tracklist . filter ( { '<STR_LIT>' : '<STR_LIT:a>' } ) <EOL> class TracklistIndexTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> config = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> } <EOL> self . tracks = [ <EOL> Track ( uri = '<STR_LIT>' , name = '<STR_LIT:foo>' ) , <EOL> Track ( uri = '<STR_LIT>' , name = '<STR_LIT:foo>' ) , <EOL> Track ( uri = '<STR_LIT>' , name = '<STR_LIT:bar>' ) , <EOL> ] <EOL> def lookup ( uris ) : <EOL> return { u : [ t for t in self . tracks if t . uri == u ] for u in uris } <EOL> self . core = core . Core ( config , mixer = None , backends = [ ] ) <EOL> self . core . library = mock . Mock ( spec = core . LibraryController ) <EOL> self . core . library . lookup . side_effect = lookup <EOL> self . core . playback = mock . Mock ( spec = core . PlaybackController ) <EOL> self . tl_tracks = self . core . tracklist . add ( uris = [ <EOL> t . uri for t in self . tracks ] ) <EOL> def test_index_returns_index_of_track ( self ) : <EOL> self . assertEqual ( <NUM_LIT:0> , self . core . tracklist . index ( self . tl_tracks [ <NUM_LIT:0> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , self . core . tracklist . index ( self . tl_tracks [ <NUM_LIT:1> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , self . core . tracklist . index ( self . tl_tracks [ <NUM_LIT:2> ] ) ) <EOL> def test_index_returns_none_if_item_not_found ( self ) : <EOL> tl_track = TlTrack ( <NUM_LIT:0> , Track ( ) ) <EOL> self . assertEqual ( self . core . tracklist . index ( tl_track ) , None ) <EOL> def test_index_returns_none_if_called_with_none ( self ) : <EOL> self . assertEqual ( self . core . tracklist . index ( None ) , None ) <EOL> def test_index_errors_out_for_invalid_tltrack ( self ) : <EOL> with self . assertRaises ( ValueError ) : <EOL> self . core . tracklist . index ( '<STR_LIT:abc>' ) <EOL> def test_index_return_index_when_called_with_tlids ( self ) : <EOL> tl_tracks = self . tl_tracks <EOL> self . assertEqual ( <NUM_LIT:0> , self . core . tracklist . index ( tlid = tl_tracks [ <NUM_LIT:0> ] . tlid ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , self . core . tracklist . index ( tlid = tl_tracks [ <NUM_LIT:1> ] . tlid ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , self . core . tracklist . index ( tlid = tl_tracks [ <NUM_LIT:2> ] . tlid ) ) <EOL> def test_index_returns_none_if_tlid_not_found ( self ) : <EOL> self . assertEqual ( self . core . tracklist . index ( tlid = <NUM_LIT> ) , None ) <EOL> def test_index_returns_none_if_called_with_tlid_none ( self ) : <EOL> self . assertEqual ( self . core . tracklist . index ( tlid = None ) , None ) <EOL> def test_index_errors_out_for_invalid_tlid ( self ) : <EOL> with self . assertRaises ( ValueError ) : <EOL> self . core . tracklist . index ( tlid = - <NUM_LIT:1> ) <EOL> def test_index_without_args_returns_current_tl_track_index ( self ) : <EOL> self . core . playback . get_current_tl_track . side_effect = [ <EOL> None , self . tl_tracks [ <NUM_LIT:0> ] , self . tl_tracks [ <NUM_LIT:1> ] , self . tl_tracks [ <NUM_LIT:2> ] ] <EOL> self . assertEqual ( None , self . core . tracklist . index ( ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . core . tracklist . index ( ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , self . core . tracklist . index ( ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , self . core . tracklist . index ( ) ) </s>
