async def test_update(self): req_body = { "name": "test_playlist", "ids": [str(IdentityService.random())] } playlist = self.playlist_repo.get(self.playlist_id) self.expect_and_raise_l([ { "cmd": make_cmd(PlaylistCmd.RenamePlaylist, playlist.id, req_body["name"]), "evt": PlaylistEvt.PlaylistRenamed, "args": { "name": req_body["name"] }, }, { "cmd": make_cmd(PlaylistCmd.UpdatePlaylistContent, playlist.id, req_body["ids"]), "evt": PlaylistEvt.PlaylistContentUpdated, "args": { "ids": req_body["ids"] }, }, ]) resp = await self.client.patch(f"/api/playlists/{playlist.id}", json=req_body) body = await resp.json() self.assertEqual(200, resp.status) self.assertEqual(playlist.to_dict(), body)
async def test_stop(self): self.data_producer.player().video("source").play("source").populate( self.data_facade) video_id = IdentityService.id_video("source") self.expect_and_raise( make_cmd(PlayerCmd.StopPlayer, self.player_id), [ { "type": PlayerEvt.PlayerStateUpdated, "args": { "old_state": PlayerState.PLAYING, "new_state": PlayerState.STOPPED, }, }, { "type": PlayerEvt.PlayerVideoUpdated, "args": { "old_video_id": video_id, "new_video_id": None, }, }, ], ) resp = await self.client.post("/api/player/stop") body = await resp.json() player = self.data_facade.player_repo.get_player() self.assertEqual(200, resp.status) self.assertEqual(player.to_dict(), body)
def expect_dispatch_l(self, commands): calls = [] for command in commands: cmd = make_cmd(command["type"], **command["args"]) calls.append(call(cmd)) self.app_facade.cmd_dispatcher.dispatch.assert_has_calls(calls)
async def test_delete(self): self.expect_and_raise( make_cmd(VideoCmd.DeleteVideo, self.video_id), [{ "type": VideoEvt.VideoDeleted, "args": {} }], ) resp = await self.client.delete(f"/api/videos/{self.video_id}") self.assertEqual(204, resp.status)
async def test_update_error(self): req_body = { "name": "test_playlist", "ids": [str(IdentityService.random())] } playlist = self.playlist_repo.get(self.playlist_id) self.expect_and_raise_l([ { "cmd": make_cmd(PlaylistCmd.RenamePlaylist, playlist.id, req_body["name"]), "evt": PlaylistEvt.PlaylistRenamed, "args": { "name": req_body["name"] }, }, { "cmd": make_cmd(PlaylistCmd.UpdatePlaylistContent, playlist.id, req_body["ids"]), "evt": OperationError, "args": { "error": "Error message" }, }, ]) resp = await self.client.patch(f"/api/playlists/{playlist.id}", json=req_body) body = await resp.json() self.assertEqual(500, resp.status) self.assertEqual( { "message": "Error message", "details": {}, }, body, )
async def test_subtitle_toggle_error(self): self.expect_and_error(make_cmd(PlayerCmd.ToggleSubtitle, self.player_id), error="Error message") resp = await self.client.post("/api/player/subtitle/toggle") body = await resp.json() self.assertEqual(500, resp.status) self.assertEqual( { "message": "Error message", "details": {}, }, body, )
async def test_delete(self): album = self.album_repo.get(self.album_id) self.queueing_service.queue.return_value = [] # Ignore the queueing action self.expect_and_raise( make_cmd(AlbumCmd.DeleteAlbum, album.id), [ { "type": AlbumEvt.AlbumDeleted, "args": {"ids": album.ids}, } ], ) resp = await self.client.delete(f"/api/albums/{album.id}") self.assertEqual(204, resp.status)
async def test_volume_error(self): self.expect_and_error(make_cmd(PlayerCmd.UpdateVolume, self.player_id, 80), error="Error message") resp = await self.client.post("/api/player/volume", params={"value": 80}) body = await resp.json() self.assertEqual(500, resp.status) self.assertEqual( { "message": "Error message", "details": {}, }, body, )
async def test_subtitle_toggle(self): self.expect_and_raise( make_cmd(PlayerCmd.ToggleSubtitle, self.player_id), [{ "type": PlayerEvt.SubtitleStateUpdated, "args": { "state": False } }], ) resp = await self.client.post("/api/player/subtitle/toggle") body = await resp.json() player = self.data_facade.player_repo.get_player() self.assertEqual(200, resp.status) self.assertEqual(player.to_dict(), body)
async def test_delete(self): artist = self.artist_repo.get(self.artist_id) self.queueing_service.queue.return_value = [ ] # Ignore the queueing action self.expect_and_raise( make_cmd(ArtistCmd.DeleteArtist, artist.id), [{ "type": ArtistEvt.ArtistDeleted, "args": { "ids": artist.ids }, }], ) resp = await self.client.delete(f"/api/artists/{artist.id}") self.assertEqual(204, resp.status)
async def test_delete_forbidden(self): self.expect_and_error( make_cmd(PlaylistCmd.DeletePlaylist, HOME_PLAYLIST.id), error="cannot delete generated playlists", ) resp = await self.client.delete(f"/api/playlists/{HOME_PLAYLIST.id}") body = await resp.json() self.assertEqual(403, resp.status) self.assertEqual( { "message": "cannot delete generated playlists", "details": {}, }, body, )
async def test_delete(self): playlist = self.playlist_repo.get(self.playlist_id) self.queueing_service.queue.return_value = [ ] # Ignore the queueing action self.expect_and_raise( make_cmd(PlaylistCmd.DeletePlaylist, playlist.id), [{ "type": PlaylistEvt.PlaylistDeleted, "args": { "name": playlist.name, "ids": playlist.ids }, }], ) resp = await self.client.delete(f"/api/playlists/{playlist.id}") self.assertEqual(204, resp.status)
async def test_volume(self): self.expect_and_raise( make_cmd(PlayerCmd.UpdateVolume, self.player_id, 80), [{ "type": PlayerEvt.VolumeUpdated, "args": { "volume": 80 } }], ) resp = await self.client.post("/api/player/volume", params={"value": 80}) body = await resp.json() player = self.data_facade.player_repo.get_player() self.assertEqual(200, resp.status) self.assertEqual(player.to_dict(), body)
async def test_seek_error(self): duration = 100 self.expect_and_error( make_cmd(PlayerCmd.SeekVideo, self.player_id, duration), error="Error message", ) resp = await self.client.post("/api/player/seek", params={"duration": duration}) body = await resp.json() self.assertEqual(500, resp.status) self.assertEqual( { "message": "Error message", "details": {}, }, body, )
async def test_seek(self): duration = 100 self.expect_and_raise( make_cmd(PlayerCmd.SeekVideo, self.player_id, duration), [{ "type": PlayerEvt.VideoSeeked, "args": { "duration": duration }, }], ) resp = await self.client.post("/api/player/seek", params={"duration": duration}) body = await resp.json() player = self.data_facade.player_repo.get_player() self.assertEqual(200, resp.status) self.assertEqual(player.to_dict(), body)
async def test_play(self): video_id = IdentityService.id_video("source") self.data_producer.select(Player, IdentityService.id_player()).video( "source", state=VideoState.READY).populate(self.data_facade) self.expect_and_raise( make_cmd(PlayerCmd.PlayVideo, self.player_id, video_id), [ { "type": PlayerEvt.PlayerStateUpdated, "args": { "old_state": PlayerState.STOPPED, "new_state": PlayerState.PLAYING, }, }, { "type": PlayerEvt.PlayerVideoUpdated, "args": { "old_video_id": None, "new_video_id": video_id, }, }, { "type": VideoEvt.VideoStateUpdated, "args": { "old_state": VideoState.READY, "new_state": VideoState.PLAYING, "total_playing_duration": timedelta(), "last_play": datetime.now(), }, }, ], ) resp = await self.client.post( "/api/player/play", params={"id": str(video_id)}, ) body = await resp.json() player = self.data_facade.player_repo.get_player() self.assertEqual(200, resp.status) self.assertEqual(player.to_dict(), body)
async def test_subtitle_seek_error(self): self.expect_and_error( make_cmd( PlayerCmd.UpdateSubtitleDelay, self.player_id, 100, ), error="Error message", ) resp = await self.client.post("/api/player/subtitle/seek", params={"duration": 100}) body = await resp.json() self.assertEqual(500, resp.status) self.assertEqual( { "message": "Error message", "details": {}, }, body, )
async def test_subtitle_seek(self): self.expect_and_raise( make_cmd( PlayerCmd.UpdateSubtitleDelay, self.player_id, 100, ), [{ "type": PlayerEvt.SubtitleDelayUpdated, "args": { "delay": 100, }, }], ) resp = await self.client.post("/api/player/subtitle/seek", params={"duration": 100}) body = await resp.json() player = self.data_facade.player_repo.get_player() self.assertEqual(200, resp.status) self.assertEqual(player.to_dict(), body)
async def test_pause(self): self.data_producer.player().video("source").play("source").populate( self.data_facade) self.expect_and_raise( make_cmd(PlayerCmd.TogglePlayerState, self.player_id), [ { "type": PlayerEvt.PlayerStateUpdated, "args": { "old_state": PlayerState.PLAYING, "new_state": PlayerState.PAUSED, }, }, ], ) resp = await self.client.post("/api/player/pause") body = await resp.json() player = self.data_facade.player_repo.get_player() self.assertEqual(200, resp.status) self.assertEqual(player.to_dict(), body)
async def test_play_error(self): self.data_producer.select( Player, IdentityService.id_player()).video("source").populate( self.data_facade) video_id = IdentityService.id_video("source") self.expect_and_error( make_cmd(PlayerCmd.PlayVideo, self.player_id, video_id), error="Error message", ) resp = await self.client.post( "/api/player/play", params={"id": str(video_id)}, ) body = await resp.json() self.assertEqual(500, resp.status) self.assertEqual( { "message": "Error message", "details": {}, }, body, )
def _observe_dispatch(self, evt_cls, cmd_cls, model_id: Id, *args, **kwargs): cmd = make_cmd(cmd_cls, model_id, *args, **kwargs) self._observe(cmd.id, [evt_cls, OperationError]) self._cmd_dispatcher.dispatch(cmd)
def _observe_dispatch(self, evtcls_handler: dict, cmd_cls, component_id, *args, **kwargs): cmd = make_cmd(cmd_cls, component_id, *args, **kwargs) self._evt_dispatcher.observe_result(cmd.id, evtcls_handler, 1) self._cmd_dispatcher.dispatch(cmd)
def expect_dispatch(self, cmd_cls, model_id, *args, **kwargs): cmd = make_cmd(cmd_cls, model_id, *args, **kwargs) self.app_facade.cmd_dispatcher.dispatch.assert_called_once_with(cmd) return cmd
def _dispatch(self, cmd_cls, component_id, *args, **kwargs): cmd = make_cmd(cmd_cls, component_id, *args, **kwargs) self._cmd_dispatcher.dispatch(cmd)