def test_no_profile_validation(self): """ Tests when there are no profiles to validation when deserializing """ data = dict( encoded_videos=[ constants.ENCODED_VIDEO_DICT_MOBILE ], **constants.VIDEO_DICT_FISH ) serializer = VideoSerializer(data=data) with self.assertRaises(ValidationError): serializer.is_valid()
def create_video(video_data): """ Called on to create Video objects in the database create_video is used to create Video objects whose children are EncodedVideo objects which are linked to Profile objects. This is an alternative to the HTTP requests so it can be used internally. The VideoSerializer is used to deserialize this object. If there are duplicate profile_names, the entire creation will be rejected. If the profile is not found in the database, the video will not be created. Args: data (dict): { url: api url to the video edx_video_id: ID of the video duration: Length of video in seconds client_video_id: client ID of video encoded_video: a list of EncodedVideo dicts url: url of the video file_size: size of the video in bytes profile: ID of the profile courses: Courses associated with this video } """ serializer = VideoSerializer(data=video_data) if serializer.is_valid(): serializer.save() return video_data.get("edx_video_id") else: raise ValCannotCreateError(serializer.errors)
def test_invalid_edx_video_id(self): """ Test the Video model regex validation for edx_video_id field """ serializer = VideoSerializer(data=constants.VIDEO_DICT_INVALID_ID) self.assertFalse(serializer.is_valid()) message = serializer.errors.get("edx_video_id")[0] self.assertEqual(message, u"edx_video_id has invalid characters")
def test_wrong_input_type(self): """ Tests an non dict input in the VideoSerializer """ data = "hello" serializer = VideoSerializer(data=data) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors.get("non_field_errors")[0], "Invalid data. Expected a dictionary, but got str.")
def test_invalid_edx_video_id(self): """ Test the Video model regex validation for edx_video_id field """ serializer = VideoSerializer(data=constants.VIDEO_DICT_INVALID_ID) self.assertFalse(serializer.is_valid()) message = serializer.errors.get("edx_video_id")[0] self.assertEqual( message, u"edx_video_id has invalid characters" )
def test_wrong_input_type(self): """ Tests an non dict input in the VideoSerializer """ data = "hello" serializer = VideoSerializer(data=data) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors.get("non_field_errors")[0], "Invalid data. Expected a dictionary, but got str." )
def test_negative_fields_for_video_serializer(self): """ Tests negative inputs for VideoSerializer Tests negative inputs for duration in model Video """ serializer = VideoSerializer( data=constants.VIDEO_DICT_NEGATIVE_DURATION) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors.get('duration')[0], u"Ensure this value is greater than or equal to 0.")
def test_no_profile_validation(self): """ Tests when there are no profiles to validation when deserializing """ data = dict(encoded_videos=[constants.ENCODED_VIDEO_DICT_MOBILE], **constants.VIDEO_DICT_FISH) serializer = VideoSerializer(data=data) self.assertFalse(serializer.is_valid()) self.assertEqual(serializer.errors.get("encoded_videos"), [{ "profile": ["This field is required."] }])
def test_negative_fields_for_video_serializer(self): """ Tests negative inputs for VideoSerializer Tests negative inputs for duration in model Video """ serializer = VideoSerializer(data=constants.VIDEO_DICT_NEGATIVE_DURATION) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors.get('duration')[0], u"Ensure this value is greater than or equal to 0." )
def test_invalid_course_id(self): serializer = VideoSerializer( data={ "edx_video_id": "dummy", "client_video_id": "dummy", "duration": 0, "status": "dummy", "encoded_videos": [], "courses": ["x" * 300], }) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors, { "courses": ["Ensure this value has at most 255 characters (it has 300)."] })
def test_invalid_course_id(self): serializer = VideoSerializer( data={ "edx_video_id": "dummy", "client_video_id": "dummy", "duration": 0, "status": "dummy", "encoded_videos": [], "courses": ["x" * 300], } ) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors, {"courses": ["Ensure this value has at most 255 characters (it has 300)."]} )
def test_no_profile_validation(self): """ Tests when there are no profiles to validation when deserializing """ data = dict( encoded_videos=[ constants.ENCODED_VIDEO_DICT_MOBILE ], **constants.VIDEO_DICT_FISH ) serializer = VideoSerializer(data=data) self.assertFalse(serializer.is_valid()) self.assertEqual( serializer.errors.get("encoded_videos"), [{"profile": ["This field is required."]}] )
def update_video(video_data): """ Called on to update Video objects in the database update_video is used to update Video objects by the given edx_video_id in the video_data. Args: video_data (dict): { url: api url to the video edx_video_id: ID of the video duration: Length of video in seconds client_video_id: client ID of video encoded_video: a list of EncodedVideo dicts url: url of the video file_size: size of the video in bytes profile: ID of the profile courses: Courses associated with this video } Raises: Raises ValVideoNotFoundError if the video cannot be retrieved. Raises ValCannotUpdateError if the video cannot be updated. Returns the successfully updated Video object """ try: video = _get_video(video_data.get("edx_video_id")) except Video.DoesNotExist: error_message = u"Video not found when trying to update video with edx_video_id: {0}".format( video_data.get("edx_video_id")) raise ValVideoNotFoundError(error_message) serializer = VideoSerializer(video, data=video_data) if serializer.is_valid(): serializer.save() return video_data.get("edx_video_id") else: raise ValCannotUpdateError(serializer.errors)
def update_video(video_data): """ Called on to update Video objects in the database update_video is used to update Video objects by the given edx_video_id in the video_data. Args: video_data (dict): { url: api url to the video edx_video_id: ID of the video duration: Length of video in seconds client_video_id: client ID of video encoded_video: a list of EncodedVideo dicts url: url of the video file_size: size of the video in bytes profile: ID of the profile courses: Courses associated with this video } Raises: Raises ValVideoNotFoundError if the video cannot be retrieved. Raises ValCannotUpdateError if the video cannot be updated. Returns the successfully updated Video object """ try: video = _get_video(video_data.get("edx_video_id")) except Video.DoesNotExist: error_message = u"Video not found when trying to update video with edx_video_id: {0}".format(video_data.get("edx_video_id")) raise ValVideoNotFoundError(error_message) serializer = VideoSerializer(video, data=video_data) if serializer.is_valid(): serializer.save() return video_data.get("edx_video_id") else: raise ValCannotUpdateError(serializer.errors)