def test_delete_last_video(self): playlist = PlayList.createPlaylist('playlist for test') playlist.save() video1 = Video.createVideo('video 1', 'http://localhost:9080/video/1234') video1.save() video2 = Video.createVideo('video 1', 'http://localhost:9080/video/1234') video2.save() video3 = Video.createVideo('video 1', 'http://localhost:9080/video/1234') video3.save() playlist.addVideo(video1) playlist.addVideo(video2) playlist.addVideo(video3) playlist.deleteVideo(video3.id) playlistVideos = playlist.getVideos() self.assertEqual(len(playlistVideos), 2) self.assertEqual(playlistVideos[0].id, video1.id) self.assertEqual(playlistVideos[1].id, video2.id)
def test_create_and_find_video(self): video = Video.createVideo('video for test', 'http://localhost:9080/video/1234') video.save() id = video.id # find the video newVideo = Video.findVideoById(id) self.assertFalse(id is None) self.assertEqual(newVideo.id, video.id)
def test_modify_video(self): video = Video.createVideo('video for test', 'http://localhost:9080/video/1234') video.save() self.assertFalse(video.id is None) self.assertEqual(video.title, 'video for test') video.title = 'modified video title for test' video.save() self.assertEqual(video.title, 'modified video title for test') newVideo = Video.findVideoById(video.id) self.assertEqual(newVideo.title, 'modified video title for test')
def add_video_to_playlist(playlist_id, video_id): """add a video to the end of playlist arguments: playlist_id -- the id of the playlist video_id -- the id of the video """ try: playlist = PlayList.findPlaylistById(playlist_id) if playlist is None: raise DataLayerException({ 'code': 404, 'message': 'No play list found' }) video = Video.findVideoById(video_id) if video is None: raise DataLayerException({ 'code': 404, 'message': 'No video found' }) playlist.addVideo(video) except ValueError as e: response.status = 400 return json.dumps({'error': 'Bad Request'}) except DataLayerException as e: response.status = e.errorArgs['code'] return json.dumps({'error': e.errorArgs['message']}) response.headers['Content-Type'] = 'application/json' return json.dumps({'data': 'OK'})
def get_all_videos(): """ get all videos """ try: videos = Video.findAll() except DataLayerException: response.status = e.errorArgs['code'] return json.dumps({'error': e.errorArgs['message']}) response.headers['Content-Type'] = 'application/json' return json.dumps({'data': map(lambda v: v.toJSONSerializable(), videos)})
def create_video(): """create a new video, it accepts an json object as the body: { "title": "the title of the video", "thumbnail": "the url of the video" } """ try: try: data = request.json except: raise ValueError if data is None: raise ValueError if data['title'] is None: raise ValueError if data['thumbnail'] is None: raise ValueError video = Video.createVideo(data['title'], data['thumbnail']) video.save() except ValueError as e: response.status = 400 return json.dumps({'error': 'Bad Request'}) except DataLayerException as e: response.status = e.errorArgs['code'] return json.dumps({'error': e.errorArgs['message']}) response.headers['Content-Type'] = 'application/json' return json.dumps({'data': video.toJSONSerializable()})
def getVideos(self): result = get_playlist_videos(self) return map(lambda v: Video(v[0], v[1], v[2]), result)