Commit 0640e912 by José Antonio González Committed by Christopher Lee

Update video through api (#62)

* add feature updating video in api

* make update video endpoint

* fix tests

* add tests for update_video

* update update_video test

* check updated video is Video object, update args doc

* update test cases for update_video

* update docs
parent c70104e9
...@@ -64,3 +64,5 @@ logs/*/*.log* ...@@ -64,3 +64,5 @@ logs/*/*.log*
# Vagrant # Vagrant
.vagrant .vagrant
venv/
...@@ -57,6 +57,13 @@ class ValCannotCreateError(ValError): ...@@ -57,6 +57,13 @@ class ValCannotCreateError(ValError):
pass pass
class ValCannotUpdateError(ValError):
"""
This error is raised when an object cannot be updated
"""
pass
class VideoSortField(Enum): class VideoSortField(Enum):
"""An enum representing sortable fields in the Video model""" """An enum representing sortable fields in the Video model"""
created = "created" created = "created"
...@@ -83,7 +90,7 @@ def create_video(video_data): ...@@ -83,7 +90,7 @@ def create_video(video_data):
creation will be rejected. If the profile is not found in the database, the creation will be rejected. If the profile is not found in the database, the
video will not be created. video will not be created.
Args: Args:
data (dict): video_data (dict):
{ {
url: api url to the video url: api url to the video
edx_video_id: ID of the video edx_video_id: ID of the video
...@@ -95,7 +102,13 @@ def create_video(video_data): ...@@ -95,7 +102,13 @@ def create_video(video_data):
profile: ID of the profile profile: ID of the profile
courses: Courses associated with this video courses: Courses associated with this video
} }
Raises:
Raises ValCannotCreateError if the video cannot be created.
Returns the successfully created Video object
""" """
serializer = VideoSerializer(data=video_data) serializer = VideoSerializer(data=video_data)
if serializer.is_valid(): if serializer.is_valid():
serializer.save() serializer.save()
...@@ -104,6 +117,47 @@ def create_video(video_data): ...@@ -104,6 +117,47 @@ def create_video(video_data):
raise ValCannotCreateError(serializer.errors) raise ValCannotCreateError(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)
def create_profile(profile_name): def create_profile(profile_name):
""" """
Used to create Profile objects in the database Used to create Profile objects in the database
......
...@@ -120,6 +120,12 @@ VIDEO_DICT_FISH = dict( ...@@ -120,6 +120,12 @@ VIDEO_DICT_FISH = dict(
edx_video_id="super-soaker", edx_video_id="super-soaker",
status="test", status="test",
) )
VIDEO_DICT_FISH_UPDATE = dict(
client_video_id="Full Swordfish",
duration=1222.00,
edx_video_id="super-soaker",
status="live",
)
VIDEO_DICT_DIFFERENT_ID_FISH = dict( VIDEO_DICT_DIFFERENT_ID_FISH = dict(
client_video_id="Shallow Swordfish", client_video_id="Shallow Swordfish",
duration=122.00, duration=122.00,
......
...@@ -17,6 +17,7 @@ from edxval import api as api ...@@ -17,6 +17,7 @@ from edxval import api as api
from edxval.api import ( from edxval.api import (
SortDirection, SortDirection,
ValCannotCreateError, ValCannotCreateError,
ValCannotUpdateError,
ValVideoNotFoundError, ValVideoNotFoundError,
VideoSortField, VideoSortField,
) )
...@@ -93,6 +94,7 @@ class CreateVideoTest(TestCase): ...@@ -93,6 +94,7 @@ class CreateVideoTest(TestCase):
""" """
Tests the creation of a video Tests the creation of a video
""" """
video_data = dict( video_data = dict(
encoded_videos=[ encoded_videos=[
constants.ENCODED_VIDEO_DICT_FISH_MOBILE constants.ENCODED_VIDEO_DICT_FISH_MOBILE
...@@ -117,6 +119,73 @@ class CreateVideoTest(TestCase): ...@@ -117,6 +119,73 @@ class CreateVideoTest(TestCase):
api.create_video(data) api.create_video(data)
@ddt
class UpdateVideoTest(TestCase):
"""
Tests the update_video function in api.py.
This function requires that a Video object exist.
This function requires that a Profile object exist.
"""
def setUp(self):
"""
Creation of Video object that will be used to test video update
Creation of Profile objects that will be used to test video update
"""
api.create_profile(constants.PROFILE_DESKTOP)
api.create_profile(constants.PROFILE_MOBILE)
video_data = dict(
encoded_videos=[
constants.ENCODED_VIDEO_DICT_FISH_MOBILE
],
**constants.VIDEO_DICT_FISH
)
api.create_video(video_data)
def test_update_video(self):
"""
Tests the update of a video
"""
video_data = dict(
encoded_videos=[
constants.ENCODED_VIDEO_DICT_FISH_MOBILE
],
**constants.VIDEO_DICT_FISH_UPDATE
)
result = api.update_video(video_data)
videos = Video.objects.all()
updated_video = videos[0]
self.assertEqual(len(videos), 1)
self.assertEqual(type(updated_video), Video)
self.assertEqual(updated_video.client_video_id, "Full Swordfish")
@data(
constants.COMPLETE_SET_INVALID_ENCODED_VIDEO_FISH,
)
def test_update_video_incorrect_data(self, data):
"""
Tests the update of a video with invalid data
"""
with self.assertRaises(ValCannotUpdateError):
api.update_video(data)
@data(
constants.VIDEO_DICT_DIFFERENT_ID_FISH
)
def test_update_video_not_found(self, data):
"""
Tests the update of a video with invalid id
"""
with self.assertRaises(ValVideoNotFoundError):
api.update_video(data)
class CreateProfileTest(TestCase): class CreateProfileTest(TestCase):
""" """
Tests the create_profile function in the api.py Tests the create_profile function in the api.py
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment