Example #1
0
 def setUp(self):
     self.user = UserFactory()
     self.video = VideoFactory(title='test-video-title',
                               description='test-video-description')
     self.version = pipeline.add_subtitles(
         self.video, 'en', SubtitleSetFactory(),
         title='test-title', description='test-description',
         metadata={'location': 'test-location'})
     self.context = {
         'user': self.user,
         'video': self.video,
         'language_code': 'en',
         'request': None,
         'version_number': None,
         'sub_format': 'srt',
     }
     self.serializer = SubtitlesSerializer(context=self.context)
Example #2
0
 def run_create(self, data):
     serializer = SubtitlesSerializer(data=data, context=self.context)
     serializer.is_valid(raise_exception=True)
     return serializer.save()
Example #3
0
class SubtitlesSerializerTest(TestCase):
    def setUp(self):
        self.user = UserFactory()
        self.video = VideoFactory(title='test-video-title',
                                  description='test-video-description')
        self.version = pipeline.add_subtitles(
            self.video, 'en', SubtitleSetFactory(),
            title='test-title', description='test-description',
            metadata={'location': 'test-location'})
        self.context = {
            'user': self.user,
            'video': self.video,
            'language_code': 'en',
            'request': None,
            'version_number': None,
            'sub_format': 'srt',
            'allow_language_extra': False,
        }
        self.serializer = SubtitlesSerializer(context=self.context)

    def test_simple_fields(self):
        data = self.serializer.to_representation(self.version)
        assert_equal(data['version_number'], self.version.version_number)
        assert_equal(data['title'], self.version.title)
        assert_equal(data['description'], self.version.description)
        assert_equal(data['metadata'], self.version.get_metadata())
        assert_equal(data['video_title'], self.video.title)
        assert_equal(data['video_description'], self.video.description)
        assert_equal(data['resource_uri'],
                     reverse('api:subtitles', kwargs={
                             'video_id': self.video.video_id,
                             'language_code': self.version.language_code,
                     }))
        assert_equal(data['site_uri'],
                     reverse('videos:subtitleversion_detail', kwargs={
                         'video_id': self.video.video_id,
                         'lang': self.version.language_code,
                         'lang_id': self.version.subtitle_language_id,
                         'version_id': self.version.id,
                     }))
        assert_equal(data['language'], {
            'code': 'en',
            'name': 'English',
            'dir': 'ltr',
        })

    def test_subtitles(self):
        # test the subtitles and sub_format fields
        self.context['sub_format'] = 'vtt'
        data = self.serializer.to_representation(self.version)
        assert_equal(data['subtitles'],
                     babelsubs.to(self.version.get_subtitles(), 'vtt'))
        assert_equal(data['sub_format'], 'vtt')

    def test_json_sub_format(self):
        # for the json sub_format, we should return actual JSON data, not a
        # that data encoded as a string
        self.context['sub_format'] = 'json'
        data = self.serializer.to_representation(self.version)
        json_encoding = babelsubs.to(self.version.get_subtitles(), 'json')
        assert_equal(data['subtitles'], json.loads(json_encoding))

    def run_create(self, data):
        serializer = SubtitlesSerializer(data=data, context=self.context)
        serializer.is_valid(raise_exception=True)
        return serializer.save()

    def test_create(self):
        subtitles = SubtitleSetFactory(num_subs=2)
        data = {
            'sub_format': 'dfxp',
            'subtitles': subtitles.to_xml(),
            'title': 'test-title',
            'description': 'test-description',
            'metadata': {
                'location': 'test-location',
            },
        }
        version = self.run_create(data)
        assert_equal(version.get_subtitles(), subtitles)
        assert_equal(version.video, self.video)
        assert_equal(version.language_code, 'en')
        assert_equal(version.title, data['title'])
        assert_equal(version.description, data['description'])
        assert_equal(version.get_metadata(), data['metadata'])
        assert_equal(version.author, self.user)
        assert_equal(version.origin, ORIGIN_API)

    def test_create_url(self):
        subtitles = SubtitleSetFactory(num_subs=2)
        data = {
            'sub_format': 'srt',
            'title': 'test-title',
            'description': 'test-description',
            'metadata': {
                'location': 'test-location',
            },
        }
        with assert_raises(ValidationError):
            self.run_create(data)
        data['subtitles_url'] = "https://s3.amazonaws.com/pculture-test-data/simple.srt"
        version = self.run_create(data)
        assert_equal(len(version.get_subtitles()), 19)
        assert_equal(version.video, self.video)
        assert_equal(version.language_code, 'en')
        assert_equal(version.title, data['title'])
        assert_equal(version.description, data['description'])
        assert_equal(version.get_metadata(), data['metadata'])
        assert_equal(version.author, self.user)
        assert_equal(version.origin, ORIGIN_API)

    def test_from_editor(self):
        version = self.run_create({
            'subtitles': SubtitleSetFactory().to_xml(),
            'origin': 'editor',
        })
        assert_equal(version.origin, ORIGIN_WEB_EDITOR)

    def test_uploaded(self):
        version = self.run_create({
            'subtitles': SubtitleSetFactory().to_xml(),
            'origin': 'upload',
        })
        assert_equal(version.origin, ORIGIN_UPLOAD)

    def test_sub_format(self):
        subtitles = SubtitleSetFactory(num_subs=2)
        version = self.run_create({
            'sub_format': 'vtt',
            'subtitles': babelsubs.to(subtitles, 'vtt'),
        })
        assert_equal(version.get_subtitles(), subtitles)

    def test_action(self):
        version = self.run_create({
            'subtitles': SubtitleSetFactory().to_xml(),
            'action': 'publish',
        })
        assert_true(version.subtitle_language.subtitles_complete)

    def test_is_complete(self):
        version = self.run_create({
            'subtitles': SubtitleSetFactory().to_xml(),
            'is_complete': True,
        })
        assert_true(version.subtitle_language.subtitles_complete)

    def test_is_complete_null(self):
        # when is_complete is None, we leave subtitles_complete alone
        language = self.video.subtitle_language('en')
        language.subtitles_complete = False
        language.save()
        data = {
            'subtitles': SubtitleSetFactory().to_xml(),
            'is_complete': None,
        }
        self.run_create(data)
        assert_false(test_utils.reload_obj(language).subtitles_complete)

        language.subtitles_complete = True
        language.save()
        self.run_create(data)
        assert_true(test_utils.reload_obj(language).subtitles_complete)

    def test_invalid_subtitles(self):
        with assert_raises(ValidationError):
            self.run_create({
                'sub_format': 'dfxp',
                'subtitles': 'bad-dfxp-data',
            })

    def test_subtitles_wrong_type(self):
        with assert_raises(ValidationError):
            self.run_create({
                'sub_format': 'dfxp',
                'subtitles': 123,
                'title': 'test-title',
                'description': 'test-description',
            })