<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 Playlist , Ref , Track <EOL> class BasePlaylistsTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . plr1a = Ref . playlist ( name = '<STR_LIT:A>' , uri = '<STR_LIT>' ) <EOL> self . plr1b = Ref . playlist ( name = '<STR_LIT:B>' , uri = '<STR_LIT>' ) <EOL> self . plr2a = Ref . playlist ( name = '<STR_LIT:A>' , uri = '<STR_LIT>' ) <EOL> self . plr2b = Ref . playlist ( name = '<STR_LIT:B>' , uri = '<STR_LIT>' ) <EOL> self . pl1a = Playlist ( name = '<STR_LIT:A>' , tracks = [ Track ( uri = '<STR_LIT>' ) ] ) <EOL> self . pl1b = Playlist ( name = '<STR_LIT:B>' , tracks = [ Track ( uri = '<STR_LIT>' ) ] ) <EOL> self . pl2a = Playlist ( name = '<STR_LIT:A>' , tracks = [ Track ( uri = '<STR_LIT>' ) ] ) <EOL> self . pl2b = Playlist ( name = '<STR_LIT:B>' , tracks = [ Track ( uri = '<STR_LIT>' ) ] ) <EOL> self . sp1 = mock . Mock ( spec = backend . PlaylistsProvider ) <EOL> self . sp1 . as_list . return_value . get . return_value = [ <EOL> self . plr1a , self . plr1b ] <EOL> self . sp1 . lookup . return_value . get . side_effect = [ self . pl1a , self . pl1b ] <EOL> self . sp2 = mock . Mock ( spec = backend . PlaylistsProvider ) <EOL> self . sp2 . as_list . return_value . get . return_value = [ <EOL> self . plr2a , self . plr2b ] <EOL> self . sp2 . lookup . return_value . get . side_effect = [ self . pl2a , self . pl2b ] <EOL> self . backend1 = mock . Mock ( ) <EOL> self . backend1 . actor_ref . actor_class . __name__ = '<STR_LIT>' <EOL> self . backend1 . uri_schemes . get . return_value = [ '<STR_LIT>' ] <EOL> self . backend1 . playlists = self . sp1 <EOL> self . backend2 = mock . Mock ( ) <EOL> self . backend2 . actor_ref . actor_class . __name__ = '<STR_LIT>' <EOL> self . backend2 . uri_schemes . get . return_value = [ '<STR_LIT>' ] <EOL> self . backend2 . playlists = self . sp2 <EOL> self . backend3 = mock . Mock ( ) <EOL> self . backend3 . uri_schemes . get . return_value = [ '<STR_LIT>' ] <EOL> self . backend3 . has_playlists ( ) . get . return_value = False <EOL> self . backend3 . playlists = None <EOL> self . core = core . Core ( mixer = None , backends = [ <EOL> self . backend3 , self . backend1 , self . backend2 ] ) <EOL> class PlaylistTest ( BasePlaylistsTest ) : <EOL> def test_as_list_combines_result_from_backends ( self ) : <EOL> result = self . core . playlists . as_list ( ) <EOL> self . assertIn ( self . plr1a , result ) <EOL> self . assertIn ( self . plr1b , result ) <EOL> self . assertIn ( self . plr2a , result ) <EOL> self . assertIn ( self . plr2b , result ) <EOL> def test_as_list_ignores_backends_that_dont_support_it ( self ) : <EOL> self . sp2 . as_list . return_value . get . side_effect = NotImplementedError <EOL> result = self . core . playlists . as_list ( ) <EOL> self . assertEqual ( len ( result ) , <NUM_LIT:2> ) <EOL> self . assertIn ( self . plr1a , result ) <EOL> self . assertIn ( self . plr1b , result ) <EOL> def test_get_items_selects_the_matching_backend ( self ) : <EOL> ref = Ref . track ( ) <EOL> self . sp2 . get_items . return_value . get . return_value = [ ref ] <EOL> result = self . core . playlists . get_items ( '<STR_LIT>' ) <EOL> self . assertEqual ( [ ref ] , result ) <EOL> self . assertFalse ( self . sp1 . get_items . called ) <EOL> self . sp2 . get_items . assert_called_once_with ( '<STR_LIT>' ) <EOL> def test_get_items_with_unknown_uri_scheme_does_nothing ( self ) : <EOL> result = self . core . playlists . get_items ( '<STR_LIT>' ) <EOL> self . assertIsNone ( result ) <EOL> self . assertFalse ( self . sp1 . delete . called ) <EOL> self . assertFalse ( self . sp2 . delete . called ) <EOL> def test_create_without_uri_scheme_uses_first_backend ( self ) : <EOL> playlist = Playlist ( ) <EOL> self . sp1 . create . return_value . get . return_value = playlist <EOL> result = self . core . playlists . create ( '<STR_LIT:foo>' ) <EOL> self . assertEqual ( playlist , result ) <EOL> self . sp1 . create . assert_called_once_with ( '<STR_LIT:foo>' ) <EOL> self . assertFalse ( self . sp2 . create . called ) <EOL> def test_create_without_uri_scheme_ignores_none_result ( self ) : <EOL> playlist = Playlist ( ) <EOL> self . sp1 . create . return_value . get . return_value = None <EOL> self . sp2 . create . return_value . get . return_value = playlist <EOL> result = self . core . playlists . create ( '<STR_LIT:foo>' ) <EOL> self . assertEqual ( playlist , result ) <EOL> self . sp1 . create . assert_called_once_with ( '<STR_LIT:foo>' ) <EOL> self . sp2 . create . assert_called_once_with ( '<STR_LIT:foo>' ) <EOL> def test_create_without_uri_scheme_ignores_exception ( self ) : <EOL> playlist = Playlist ( ) <EOL> self . sp1 . create . return_value . get . side_effect = Exception <EOL> self . sp2 . create . return_value . get . return_value = playlist <EOL> result = self . core . playlists . create ( '<STR_LIT:foo>' ) <EOL> self . assertEqual ( playlist , result ) <EOL> self . sp1 . create . assert_called_once_with ( '<STR_LIT:foo>' ) <EOL> self . sp2 . create . assert_called_once_with ( '<STR_LIT:foo>' ) <EOL> def test_create_with_uri_scheme_selects_the_matching_backend ( self ) : <EOL> playlist = Playlist ( ) <EOL> self . sp2 . create . return_value . get . return_value = playlist <EOL> result = self . core . playlists . create ( '<STR_LIT:foo>' , uri_scheme = '<STR_LIT>' ) <EOL> self . assertEqual ( playlist , result ) <EOL> self . assertFalse ( self . sp1 . create . called ) <EOL> self . sp2 . create . assert_called_once_with ( '<STR_LIT:foo>' ) <EOL> def test_create_with_unsupported_uri_scheme_uses_first_backend ( self ) : <EOL> playlist = Playlist ( ) <EOL> self . sp1 . create . return_value . get . return_value = playlist <EOL> result = self . core . playlists . create ( '<STR_LIT:foo>' , uri_scheme = '<STR_LIT>' ) <EOL> self . assertEqual ( playlist , result ) <EOL> self . sp1 . create . assert_called_once_with ( '<STR_LIT:foo>' ) <EOL> self . assertFalse ( self . sp2 . create . called ) <EOL> def test_delete_selects_the_dummy1_backend ( self ) : <EOL> self . core . playlists . delete ( '<STR_LIT>' ) <EOL> self . sp1 . delete . assert_called_once_with ( '<STR_LIT>' ) <EOL> self . assertFalse ( self . sp2 . delete . called ) <EOL> def test_delete_selects_the_dummy2_backend ( self ) : <EOL> self . core . playlists . delete ( '<STR_LIT>' ) <EOL> self . assertFalse ( self . sp1 . delete . called ) <EOL> self . sp2 . delete . assert_called_once_with ( '<STR_LIT>' ) <EOL> def test_delete_with_unknown_uri_scheme_does_nothing ( self ) : <EOL> self . core . playlists . delete ( '<STR_LIT>' ) <EOL> self . assertFalse ( self . sp1 . delete . called ) <EOL> self . assertFalse ( self . sp2 . delete . called ) <EOL> def test_delete_ignores_backend_without_playlist_support ( self ) : <EOL> self . core . playlists . delete ( '<STR_LIT>' ) <EOL> self . assertFalse ( self . sp1 . delete . called ) <EOL> self . assertFalse ( self . sp2 . delete . called ) <EOL> def test_lookup_selects_the_dummy1_backend ( self ) : <EOL> self . core . playlists . lookup ( '<STR_LIT>' ) <EOL> self . sp1 . lookup . assert_called_once_with ( '<STR_LIT>' ) <EOL> self . assertFalse ( self . sp2 . lookup . called ) <EOL> def test_lookup_selects_the_dummy2_backend ( self ) : <EOL> self . core . playlists . lookup ( '<STR_LIT>' ) <EOL> self . assertFalse ( self . sp1 . lookup . called ) <EOL> self . sp2 . lookup . assert_called_once_with ( '<STR_LIT>' ) <EOL> def test_lookup_track_in_backend_without_playlists_fails ( self ) : <EOL> result = self . core . playlists . lookup ( '<STR_LIT>' ) <EOL> self . assertIsNone ( result ) <EOL> self . assertFalse ( self . sp1 . lookup . called ) <EOL> self . assertFalse ( self . sp2 . lookup . called ) <EOL> def test_refresh_without_uri_scheme_refreshes_all_backends ( self ) : <EOL> self . core . playlists . refresh ( ) <EOL> self . sp1 . refresh . assert_called_once_with ( ) <EOL> self . sp2 . refresh . assert_called_once_with ( ) <EOL> def test_refresh_with_uri_scheme_refreshes_matching_backend ( self ) : <EOL> self . core . playlists . refresh ( uri_scheme = '<STR_LIT>' ) <EOL> self . assertFalse ( self . sp1 . refresh . called ) <EOL> self . sp2 . refresh . assert_called_once_with ( ) <EOL> def test_refresh_with_unknown_uri_scheme_refreshes_nothing ( self ) : <EOL> self . core . playlists . refresh ( uri_scheme = '<STR_LIT>' ) <EOL> self . assertFalse ( self . sp1 . refresh . called ) <EOL> self . assertFalse ( self . sp2 . refresh . called ) <EOL> def test_refresh_ignores_backend_without_playlist_support ( self ) : <EOL> self . core . playlists . refresh ( uri_scheme = '<STR_LIT>' ) <EOL> self . assertFalse ( self . sp1 . refresh . called ) <EOL> self . assertFalse ( self . sp2 . refresh . called ) <EOL> def test_save_selects_the_dummy1_backend ( self ) : <EOL> playlist = Playlist ( uri = '<STR_LIT>' ) <EOL> self . sp1 . save . return_value . get . return_value = playlist <EOL> result = self . core . playlists . save ( playlist ) <EOL> self . assertEqual ( playlist , result ) <EOL> self . sp1 . save . assert_called_once_with ( playlist ) <EOL> self . assertFalse ( self . sp2 . save . called ) <EOL> def test_save_selects_the_dummy2_backend ( self ) : <EOL> playlist = Playlist ( uri = '<STR_LIT>' ) <EOL> self . sp2 . save . return_value . get . return_value = playlist <EOL> result = self . core . playlists . save ( playlist ) <EOL> self . assertEqual ( playlist , result ) <EOL> self . assertFalse ( self . sp1 . save . called ) <EOL> self . sp2 . save . assert_called_once_with ( playlist ) <EOL> def test_save_does_nothing_if_playlist_uri_is_unset ( self ) : <EOL> result = self . core . playlists . save ( Playlist ( ) ) <EOL> self . assertIsNone ( result ) <EOL> self . assertFalse ( self . sp1 . save . called ) <EOL> self . assertFalse ( self . sp2 . save . called ) <EOL> def test_save_does_nothing_if_playlist_uri_has_unknown_scheme ( self ) : <EOL> result = self . core . playlists . save ( Playlist ( uri = '<STR_LIT>' ) ) <EOL> self . assertIsNone ( result ) <EOL> self . assertFalse ( self . sp1 . save . called ) <EOL> self . assertFalse ( self . sp2 . save . called ) <EOL> def test_save_ignores_backend_without_playlist_support ( self ) : <EOL> result = self . core . playlists . save ( Playlist ( uri = '<STR_LIT>' ) ) <EOL> self . assertIsNone ( result ) <EOL> self . assertFalse ( self . sp1 . save . called ) <EOL> self . assertFalse ( self . sp2 . save . called ) <EOL> def test_get_uri_schemes ( self ) : <EOL> result = self . core . playlists . get_uri_schemes ( ) <EOL> self . assertEquals ( result , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> class DeprecatedFilterPlaylistsTest ( BasePlaylistsTest ) : <EOL> def run ( self , result = None ) : <EOL> with deprecation . ignore ( ids = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) : <EOL> return super ( DeprecatedFilterPlaylistsTest , self ) . run ( result ) <EOL> def test_filter_returns_matching_playlists ( self ) : <EOL> result = self . core . playlists . filter ( { '<STR_LIT:name>' : '<STR_LIT:A>' } ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( result ) ) <EOL> def test_filter_accepts_dict_instead_of_kwargs ( self ) : <EOL> result = self . core . playlists . filter ( { '<STR_LIT:name>' : '<STR_LIT:A>' } ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( result ) ) <EOL> class DeprecatedGetPlaylistsTest ( BasePlaylistsTest ) : <EOL> def run ( self , result = None ) : <EOL> with deprecation . ignore ( '<STR_LIT>' ) : <EOL> return super ( DeprecatedGetPlaylistsTest , self ) . run ( result ) <EOL> def test_get_playlists_combines_result_from_backends ( self ) : <EOL> result = self . core . playlists . get_playlists ( ) <EOL> self . assertIn ( self . pl1a , result ) <EOL> self . assertIn ( self . pl1b , result ) <EOL> self . assertIn ( self . pl2a , result ) <EOL> self . assertIn ( self . pl2b , result ) <EOL> def test_get_playlists_includes_tracks_by_default ( self ) : <EOL> result = self . core . playlists . get_playlists ( ) <EOL> self . assertEqual ( result [ <NUM_LIT:0> ] . name , '<STR_LIT:A>' ) <EOL> self . assertEqual ( len ( result [ <NUM_LIT:0> ] . tracks ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( result [ <NUM_LIT:1> ] . name , '<STR_LIT:B>' ) <EOL> self . assertEqual ( len ( result [ <NUM_LIT:1> ] . tracks ) , <NUM_LIT:1> ) <EOL> def test_get_playlist_can_strip_tracks_from_returned_playlists ( self ) : <EOL> result = self . core . playlists . get_playlists ( include_tracks = False ) <EOL> self . assertEqual ( result [ <NUM_LIT:0> ] . name , '<STR_LIT:A>' ) <EOL> self . assertEqual ( len ( result [ <NUM_LIT:0> ] . tracks ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( result [ <NUM_LIT:1> ] . name , '<STR_LIT:B>' ) <EOL> self . assertEqual ( len ( result [ <NUM_LIT:1> ] . tracks ) , <NUM_LIT:0> ) <EOL> class MockBackendCorePlaylistsBase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . playlists = mock . Mock ( spec = backend . PlaylistsProvider ) <EOL> self . backend = mock . Mock ( ) <EOL> self . backend . actor_ref . actor_class . __name__ = '<STR_LIT>' <EOL> self . backend . uri_schemes . get . return_value = [ '<STR_LIT>' ] <EOL> self . backend . playlists = self . playlists <EOL> self . core = core . Core ( mixer = None , backends = [ self . backend ] ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> class AsListBadBackendsTest ( MockBackendCorePlaylistsBase ) : <EOL> def test_backend_raises_exception ( self , logger ) : <EOL> self . playlists . as_list . return_value . get . side_effect = Exception <EOL> self . assertEqual ( [ ] , self . core . playlists . as_list ( ) ) <EOL> logger . exception . assert_called_with ( mock . ANY , '<STR_LIT>' ) <EOL> def test_backend_returns_none ( self , logger ) : <EOL> self . playlists . as_list . return_value . get . return_value = None <EOL> self . assertEqual ( [ ] , self . core . playlists . as_list ( ) ) <EOL> self . assertFalse ( logger . error . called ) <EOL> def test_backend_returns_wrong_type ( self , logger ) : <EOL> self . playlists . as_list . return_value . get . return_value = '<STR_LIT:abc>' <EOL> self . assertEqual ( [ ] , self . core . playlists . as_list ( ) ) <EOL> logger . error . assert_called_with ( mock . ANY , '<STR_LIT>' , mock . ANY ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> class GetItemsBadBackendsTest ( MockBackendCorePlaylistsBase ) : <EOL> def test_backend_raises_exception ( self , logger ) : <EOL> self . playlists . get_items . return_value . get . side_effect = Exception <EOL> self . assertIsNone ( self . core . playlists . get_items ( '<STR_LIT>' ) ) <EOL> logger . exception . assert_called_with ( mock . ANY , '<STR_LIT>' ) <EOL> def test_backend_returns_none ( self , logger ) : <EOL> self . playlists . get_items . return_value . get . return_value = None <EOL> self . assertIsNone ( self . core . playlists . get_items ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( logger . error . called ) <EOL> def test_backend_returns_wrong_type ( self , logger ) : <EOL> self . playlists . get_items . return_value . get . return_value = '<STR_LIT:abc>' <EOL> self . assertIsNone ( self . core . playlists . get_items ( '<STR_LIT>' ) ) <EOL> logger . error . assert_called_with ( mock . ANY , '<STR_LIT>' , mock . ANY ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> class CreateBadBackendsTest ( MockBackendCorePlaylistsBase ) : <EOL> def test_backend_raises_exception ( self , logger ) : <EOL> self . playlists . create . return_value . get . side_effect = Exception <EOL> self . assertIsNone ( self . core . playlists . create ( '<STR_LIT>' ) ) <EOL> logger . exception . assert_called_with ( mock . ANY , '<STR_LIT>' ) <EOL> def test_backend_returns_none ( self , logger ) : <EOL> self . playlists . create . return_value . get . return_value = None <EOL> self . assertIsNone ( self . core . playlists . create ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( logger . error . called ) <EOL> def test_backend_returns_wrong_type ( self , logger ) : <EOL> self . playlists . create . return_value . get . return_value = '<STR_LIT:abc>' <EOL> self . assertIsNone ( self . core . playlists . create ( '<STR_LIT>' ) ) <EOL> logger . error . assert_called_with ( mock . ANY , '<STR_LIT>' , mock . ANY ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> class DeleteBadBackendsTest ( MockBackendCorePlaylistsBase ) : <EOL> def test_backend_raises_exception ( self , logger ) : <EOL> self . playlists . delete . return_value . get . side_effect = Exception <EOL> self . assertIsNone ( self . core . playlists . delete ( '<STR_LIT>' ) ) <EOL> logger . exception . assert_called_with ( mock . ANY , '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> class LookupBadBackendsTest ( MockBackendCorePlaylistsBase ) : <EOL> def test_backend_raises_exception ( self , logger ) : <EOL> self . playlists . lookup . return_value . get . side_effect = Exception <EOL> self . assertIsNone ( self . core . playlists . lookup ( '<STR_LIT>' ) ) <EOL> logger . exception . assert_called_with ( mock . ANY , '<STR_LIT>' ) <EOL> def test_backend_returns_none ( self , logger ) : <EOL> self . playlists . lookup . return_value . get . return_value = None <EOL> self . assertIsNone ( self . core . playlists . lookup ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( logger . error . called ) <EOL> def test_backend_returns_wrong_type ( self , logger ) : <EOL> self . playlists . lookup . return_value . get . return_value = '<STR_LIT:abc>' <EOL> self . assertIsNone ( self . core . playlists . lookup ( '<STR_LIT>' ) ) <EOL> logger . error . assert_called_with ( mock . ANY , '<STR_LIT>' , mock . ANY ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> class RefreshBadBackendsTest ( MockBackendCorePlaylistsBase ) : <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_backend_raises_exception ( self , send , logger ) : <EOL> self . playlists . refresh . return_value . get . side_effect = Exception <EOL> self . core . playlists . refresh ( ) <EOL> self . assertFalse ( send . called ) <EOL> logger . exception . assert_called_with ( mock . ANY , '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_backend_raises_exception_called_with_uri ( self , send , logger ) : <EOL> self . playlists . refresh . return_value . get . side_effect = Exception <EOL> self . core . playlists . refresh ( '<STR_LIT>' ) <EOL> self . assertFalse ( send . called ) <EOL> logger . exception . assert_called_with ( mock . ANY , '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> class SaveBadBackendsTest ( MockBackendCorePlaylistsBase ) : <EOL> def test_backend_raises_exception ( self , logger ) : <EOL> playlist = Playlist ( uri = '<STR_LIT>' ) <EOL> self . playlists . save . return_value . get . side_effect = Exception <EOL> self . assertIsNone ( self . core . playlists . save ( playlist ) ) <EOL> logger . exception . assert_called_with ( mock . ANY , '<STR_LIT>' ) <EOL> def test_backend_returns_none ( self , logger ) : <EOL> playlist = Playlist ( uri = '<STR_LIT>' ) <EOL> self . playlists . save . return_value . get . return_value = None <EOL> self . assertIsNone ( self . core . playlists . save ( playlist ) ) <EOL> self . assertFalse ( logger . error . called ) <EOL> def test_backend_returns_wrong_type ( self , logger ) : <EOL> playlist = Playlist ( uri = '<STR_LIT>' ) <EOL> self . playlists . save . return_value . get . return_value = '<STR_LIT:abc>' <EOL> self . assertIsNone ( self . core . playlists . save ( playlist ) ) <EOL> logger . error . assert_called_with ( mock . ANY , '<STR_LIT>' , mock . ANY ) </s>
