1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
import unittest
from unittest import mock
from mopidy import backend, core
from mopidy.models import Playlist, Ref, Track
class BasePlaylistsTest(unittest.TestCase):
def setUp(self): # noqa: N802
self.plr1a = Ref.playlist(name="A", uri="dummy1:pl:a")
self.plr1b = Ref.playlist(name="B", uri="dummy1:pl:b")
self.plr2a = Ref.playlist(name="A", uri="dummy2:pl:a")
self.plr2b = Ref.playlist(name="B", uri="dummy2:pl:b")
self.pl1a = Playlist(name="A", tracks=[Track(uri="dummy1:t:a")])
self.pl1b = Playlist(name="B", tracks=[Track(uri="dummy1:t:b")])
self.pl2a = Playlist(name="A", tracks=[Track(uri="dummy2:t:a")])
self.pl2b = Playlist(name="B", tracks=[Track(uri="dummy2:t:b")])
self.sp1 = mock.Mock(spec=backend.PlaylistsProvider)
self.sp1.as_list.return_value.get.return_value = [
self.plr1a,
self.plr1b,
]
self.sp1.lookup.return_value.get.side_effect = [self.pl1a, self.pl1b]
self.sp2 = mock.Mock(spec=backend.PlaylistsProvider)
self.sp2.as_list.return_value.get.return_value = [
self.plr2a,
self.plr2b,
]
self.sp2.lookup.return_value.get.side_effect = [self.pl2a, self.pl2b]
self.backend1 = mock.Mock()
self.backend1.actor_ref.actor_class.__name__ = "Backend1"
self.backend1.uri_schemes.get.return_value = ["dummy1"]
self.backend1.playlists = self.sp1
self.backend2 = mock.Mock()
self.backend2.actor_ref.actor_class.__name__ = "Backend2"
self.backend2.uri_schemes.get.return_value = ["dummy2"]
self.backend2.playlists = self.sp2
# A backend without the optional playlists provider
self.backend3 = mock.Mock()
self.backend3.uri_schemes.get.return_value = ["dummy3"]
self.backend3.has_playlists().get.return_value = False
self.backend3.playlists = None
self.core = core.Core(
mixer=None, backends=[self.backend3, self.backend1, self.backend2]
)
class PlaylistTest(BasePlaylistsTest):
def test_as_list_combines_result_from_backends(self):
result = self.core.playlists.as_list()
assert self.plr1a in result
assert self.plr1b in result
assert self.plr2a in result
assert self.plr2b in result
def test_as_list_ignores_backends_that_dont_support_it(self):
self.sp2.as_list.return_value.get.side_effect = NotImplementedError
result = self.core.playlists.as_list()
assert len(result) == 2
assert self.plr1a in result
assert self.plr1b in result
def test_get_items_selects_the_matching_backend(self):
ref = Ref.track()
self.sp2.get_items.return_value.get.return_value = [ref]
result = self.core.playlists.get_items("dummy2:pl:a")
assert [ref] == result
assert not self.sp1.get_items.called
self.sp2.get_items.assert_called_once_with("dummy2:pl:a")
def test_get_items_with_unknown_uri_scheme_does_nothing(self):
result = self.core.playlists.get_items("unknown:a")
assert result is None
assert not self.sp1.delete.called
assert not self.sp2.delete.called
def test_create_without_uri_scheme_uses_first_backend(self):
playlist = Playlist()
self.sp1.create.return_value.get.return_value = playlist
result = self.core.playlists.create("foo")
assert playlist == result
self.sp1.create.assert_called_once_with("foo")
assert not self.sp2.create.called
def test_create_without_uri_scheme_ignores_none_result(self):
playlist = Playlist()
self.sp1.create.return_value.get.return_value = None
self.sp2.create.return_value.get.return_value = playlist
result = self.core.playlists.create("foo")
assert playlist == result
self.sp1.create.assert_called_once_with("foo")
self.sp2.create.assert_called_once_with("foo")
def test_create_without_uri_scheme_ignores_exception(self):
playlist = Playlist()
self.sp1.create.return_value.get.side_effect = Exception
self.sp2.create.return_value.get.return_value = playlist
result = self.core.playlists.create("foo")
assert playlist == result
self.sp1.create.assert_called_once_with("foo")
self.sp2.create.assert_called_once_with("foo")
def test_create_with_uri_scheme_selects_the_matching_backend(self):
playlist = Playlist()
self.sp2.create.return_value.get.return_value = playlist
result = self.core.playlists.create("foo", uri_scheme="dummy2")
assert playlist == result
assert not self.sp1.create.called
self.sp2.create.assert_called_once_with("foo")
def test_create_with_unsupported_uri_scheme_uses_first_backend(self):
playlist = Playlist()
self.sp1.create.return_value.get.return_value = playlist
result = self.core.playlists.create("foo", uri_scheme="dummy3")
assert playlist == result
self.sp1.create.assert_called_once_with("foo")
assert not self.sp2.create.called
def test_delete_selects_the_dummy1_backend(self):
success = self.core.playlists.delete("dummy1:a")
assert success
self.sp1.delete.assert_called_once_with("dummy1:a")
assert not self.sp2.delete.called
def test_delete_selects_the_dummy2_backend(self):
success = self.core.playlists.delete("dummy2:a")
assert success
assert not self.sp1.delete.called
self.sp2.delete.assert_called_once_with("dummy2:a")
def test_delete_with_unknown_uri_scheme_does_nothing(self):
success = self.core.playlists.delete("unknown:a")
assert not success
assert not self.sp1.delete.called
assert not self.sp2.delete.called
def test_delete_ignores_backend_without_playlist_support(self):
success = self.core.playlists.delete("dummy3:a")
assert not success
assert not self.sp1.delete.called
assert not self.sp2.delete.called
def test_lookup_selects_the_dummy1_backend(self):
self.core.playlists.lookup("dummy1:a")
self.sp1.lookup.assert_called_once_with("dummy1:a")
assert not self.sp2.lookup.called
def test_lookup_selects_the_dummy2_backend(self):
self.core.playlists.lookup("dummy2:a")
assert not self.sp1.lookup.called
self.sp2.lookup.assert_called_once_with("dummy2:a")
def test_lookup_track_in_backend_without_playlists_fails(self):
result = self.core.playlists.lookup("dummy3:a")
assert result is None
assert not self.sp1.lookup.called
assert not self.sp2.lookup.called
def test_refresh_without_uri_scheme_refreshes_all_backends(self):
self.core.playlists.refresh()
self.sp1.refresh.assert_called_once_with()
self.sp2.refresh.assert_called_once_with()
def test_refresh_with_uri_scheme_refreshes_matching_backend(self):
self.core.playlists.refresh(uri_scheme="dummy2")
assert not self.sp1.refresh.called
self.sp2.refresh.assert_called_once_with()
def test_refresh_with_unknown_uri_scheme_refreshes_nothing(self):
self.core.playlists.refresh(uri_scheme="foobar")
assert not self.sp1.refresh.called
assert not self.sp2.refresh.called
def test_refresh_ignores_backend_without_playlist_support(self):
self.core.playlists.refresh(uri_scheme="dummy3")
assert not self.sp1.refresh.called
assert not self.sp2.refresh.called
def test_save_selects_the_dummy1_backend(self):
playlist = Playlist(uri="dummy1:a")
self.sp1.save.return_value.get.return_value = playlist
result = self.core.playlists.save(playlist)
assert playlist == result
self.sp1.save.assert_called_once_with(playlist)
assert not self.sp2.save.called
def test_save_selects_the_dummy2_backend(self):
playlist = Playlist(uri="dummy2:a")
self.sp2.save.return_value.get.return_value = playlist
result = self.core.playlists.save(playlist)
assert playlist == result
assert not self.sp1.save.called
self.sp2.save.assert_called_once_with(playlist)
def test_save_does_nothing_if_playlist_uri_is_unset(self):
result = self.core.playlists.save(Playlist())
assert result is None
assert not self.sp1.save.called
assert not self.sp2.save.called
def test_save_does_nothing_if_playlist_uri_has_unknown_scheme(self):
result = self.core.playlists.save(Playlist(uri="foobar:a"))
assert result is None
assert not self.sp1.save.called
assert not self.sp2.save.called
def test_save_ignores_backend_without_playlist_support(self):
result = self.core.playlists.save(Playlist(uri="dummy3:a"))
assert result is None
assert not self.sp1.save.called
assert not self.sp2.save.called
def test_get_uri_schemes(self):
result = self.core.playlists.get_uri_schemes()
assert result == ["dummy1", "dummy2"]
class MockBackendCorePlaylistsBase(unittest.TestCase):
def setUp(self): # noqa: N802
self.playlists = mock.Mock(spec=backend.PlaylistsProvider)
self.backend = mock.Mock()
self.backend.actor_ref.actor_class.__name__ = "DummyBackend"
self.backend.uri_schemes.get.return_value = ["dummy"]
self.backend.playlists = self.playlists
self.core = core.Core(mixer=None, backends=[self.backend])
@mock.patch("mopidy.core.playlists.logger")
class AsListBadBackendsTest(MockBackendCorePlaylistsBase):
def test_backend_raises_exception(self, logger):
self.playlists.as_list.return_value.get.side_effect = Exception
assert [] == self.core.playlists.as_list()
logger.exception.assert_called_with(mock.ANY, "DummyBackend")
def test_backend_returns_none(self, logger):
self.playlists.as_list.return_value.get.return_value = None
assert [] == self.core.playlists.as_list()
assert not logger.error.called
def test_backend_returns_wrong_type(self, logger):
self.playlists.as_list.return_value.get.return_value = "abc"
assert [] == self.core.playlists.as_list()
logger.error.assert_called_with(mock.ANY, "DummyBackend", mock.ANY)
@mock.patch("mopidy.core.playlists.logger")
class GetItemsBadBackendsTest(MockBackendCorePlaylistsBase):
def test_backend_raises_exception(self, logger):
self.playlists.get_items.return_value.get.side_effect = Exception
assert self.core.playlists.get_items("dummy:/1") is None
logger.exception.assert_called_with(mock.ANY, "DummyBackend")
def test_backend_returns_none(self, logger):
self.playlists.get_items.return_value.get.return_value = None
assert self.core.playlists.get_items("dummy:/1") is None
assert not logger.error.called
def test_backend_returns_wrong_type(self, logger):
self.playlists.get_items.return_value.get.return_value = "abc"
assert self.core.playlists.get_items("dummy:/1") is None
logger.error.assert_called_with(mock.ANY, "DummyBackend", mock.ANY)
@mock.patch("mopidy.core.playlists.logger")
class CreateBadBackendsTest(MockBackendCorePlaylistsBase):
def test_backend_raises_exception(self, logger):
self.playlists.create.return_value.get.side_effect = Exception
assert self.core.playlists.create("foobar") is None
logger.exception.assert_called_with(mock.ANY, "DummyBackend")
def test_backend_returns_none(self, logger):
self.playlists.create.return_value.get.return_value = None
assert self.core.playlists.create("foobar") is None
assert not logger.error.called
def test_backend_returns_wrong_type(self, logger):
self.playlists.create.return_value.get.return_value = "abc"
assert self.core.playlists.create("foobar") is None
logger.error.assert_called_with(mock.ANY, "DummyBackend", mock.ANY)
@mock.patch("mopidy.core.playlists.logger")
class DeleteBadBackendsTest(MockBackendCorePlaylistsBase):
def test_backend_raises_exception(self, logger):
self.playlists.delete.return_value.get.side_effect = Exception
assert not self.core.playlists.delete("dummy:/1")
logger.exception.assert_called_with(mock.ANY, "DummyBackend")
@mock.patch("mopidy.core.playlists.logger")
class LookupBadBackendsTest(MockBackendCorePlaylistsBase):
def test_backend_raises_exception(self, logger):
self.playlists.lookup.return_value.get.side_effect = Exception
assert self.core.playlists.lookup("dummy:/1") is None
logger.exception.assert_called_with(mock.ANY, "DummyBackend")
def test_backend_returns_none(self, logger):
self.playlists.lookup.return_value.get.return_value = None
assert self.core.playlists.lookup("dummy:/1") is None
assert not logger.error.called
def test_backend_returns_wrong_type(self, logger):
self.playlists.lookup.return_value.get.return_value = "abc"
assert self.core.playlists.lookup("dummy:/1") is None
logger.error.assert_called_with(mock.ANY, "DummyBackend", mock.ANY)
@mock.patch("mopidy.core.playlists.logger")
class RefreshBadBackendsTest(MockBackendCorePlaylistsBase):
@mock.patch("mopidy.core.listener.CoreListener.send")
def test_backend_raises_exception(self, send, logger):
self.playlists.refresh.return_value.get.side_effect = Exception
self.core.playlists.refresh()
assert not send.called
logger.exception.assert_called_with(mock.ANY, "DummyBackend")
@mock.patch("mopidy.core.listener.CoreListener.send")
def test_backend_raises_exception_called_with_uri(self, send, logger):
self.playlists.refresh.return_value.get.side_effect = Exception
self.core.playlists.refresh("dummy")
assert not send.called
logger.exception.assert_called_with(mock.ANY, "DummyBackend")
@mock.patch("mopidy.core.playlists.logger")
class SaveBadBackendsTest(MockBackendCorePlaylistsBase):
def test_backend_raises_exception(self, logger):
playlist = Playlist(uri="dummy:/1")
self.playlists.save.return_value.get.side_effect = Exception
assert self.core.playlists.save(playlist) is None
logger.exception.assert_called_with(mock.ANY, "DummyBackend")
def test_backend_returns_none(self, logger):
playlist = Playlist(uri="dummy:/1")
self.playlists.save.return_value.get.return_value = None
assert self.core.playlists.save(playlist) is None
assert not logger.error.called
def test_backend_returns_wrong_type(self, logger):
playlist = Playlist(uri="dummy:/1")
self.playlists.save.return_value.get.return_value = "abc"
assert self.core.playlists.save(playlist) is None
logger.error.assert_called_with(mock.ANY, "DummyBackend", mock.ANY)
|