Commit b1e11c9a by Nimisha Asthagiri

PLAT-612: Fix VAL error on re-import

parent d6087908
...@@ -437,7 +437,7 @@ def import_from_xml(xml, edx_video_id, course_id=None): ...@@ -437,7 +437,7 @@ def import_from_xml(xml, edx_video_id, course_id=None):
course_id, course_id,
) )
if course_id: if course_id:
CourseVideo.create_with_validation(video=video, course_id=course_id) CourseVideo.get_or_create_with_validation(video=video, course_id=course_id)
return return
except ValidationError as err: except ValidationError as err:
logger.exception(err.message) logger.exception(err.message)
......
...@@ -32,6 +32,18 @@ class ModelFactoryWithValidation(object): ...@@ -32,6 +32,18 @@ class ModelFactoryWithValidation(object):
ret_val.full_clean() ret_val.full_clean()
ret_val.save() ret_val.save()
@classmethod
def get_or_create_with_validation(cls, *args, **kwargs):
"""
Factory method that gets or creates-and-validates the model object before it is saved.
Similar to the get_or_create method on Models, it returns a tuple of (object, created),
where created is a boolean specifying whether an object was created.
"""
try:
return cls.objects.get(*args, **kwargs), False
except cls.DoesNotExist:
return cls.create_with_validation(*args, **kwargs), True
class Profile(models.Model): class Profile(models.Model):
""" """
......
...@@ -743,6 +743,7 @@ class ExportTest(TestCase): ...@@ -743,6 +743,7 @@ class ExportTest(TestCase):
api.export_to_xml("unknown_video") api.export_to_xml("unknown_video")
@ddt
class ImportTest(TestCase): class ImportTest(TestCase):
"""Tests import_from_xml""" """Tests import_from_xml"""
def setUp(self): def setUp(self):
...@@ -831,9 +832,13 @@ class ImportTest(TestCase): ...@@ -831,9 +832,13 @@ class ImportTest(TestCase):
self.assertFalse(video.encoded_videos.all().exists()) self.assertFalse(video.encoded_videos.all().exists())
self.assertFalse(video.courses.all().exists()) self.assertFalse(video.courses.all().exists())
def test_existing_video(self): @data(
new_course_id = "new_course_id" # import into another course, where the video already exists, but is not associated with the course.
"new_course_id",
# re-import case, where the video and course association already exists.
"existing_course_id"
)
def test_existing_video(self, course_id):
xml = self.make_import_xml( xml = self.make_import_xml(
video_dict={ video_dict={
"client_video_id": "new_client_video_id", "client_video_id": "new_client_video_id",
...@@ -849,7 +854,7 @@ class ImportTest(TestCase): ...@@ -849,7 +854,7 @@ class ImportTest(TestCase):
}, },
] ]
) )
api.import_from_xml(xml, constants.VIDEO_DICT_FISH["edx_video_id"], new_course_id) api.import_from_xml(xml, constants.VIDEO_DICT_FISH["edx_video_id"], course_id)
video = Video.objects.get(edx_video_id=constants.VIDEO_DICT_FISH["edx_video_id"]) video = Video.objects.get(edx_video_id=constants.VIDEO_DICT_FISH["edx_video_id"])
self.assert_video_matches_dict(video, constants.VIDEO_DICT_FISH) self.assert_video_matches_dict(video, constants.VIDEO_DICT_FISH)
...@@ -860,7 +865,8 @@ class ImportTest(TestCase): ...@@ -860,7 +865,8 @@ class ImportTest(TestCase):
self.assertFalse( self.assertFalse(
video.encoded_videos.filter(profile__profile_name=constants.PROFILE_DESKTOP).exists() video.encoded_videos.filter(profile__profile_name=constants.PROFILE_DESKTOP).exists()
) )
self.assertTrue(video.courses.filter(course_id=new_course_id).exists()) self.assertTrue(video.courses.filter(course_id=course_id).exists())
def test_existing_video_with_invalid_course_id(self): def test_existing_video_with_invalid_course_id(self):
xml = self.make_import_xml(video_dict=constants.VIDEO_DICT_FISH) xml = self.make_import_xml(video_dict=constants.VIDEO_DICT_FISH)
......
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