def setUp(self): self.video = VideoFactory(primary_audio_language_code='en') self.user = UserFactory(is_staff=True) self.language = SubtitleLanguageFactory(video=self.video, language_code='en') self.show_private_versions = mock.Mock(return_value=True) self.serializer_context = { 'video': self.video, 'show_private_versions': self.show_private_versions, 'request': APIRequestFactory().get("/mock-url/"), } self.serializer = SubtitleLanguageSerializer( context=self.serializer_context)
class SubtitleLanguageSerializerTest(TestCase): def setUp(self): self.video = VideoFactory(primary_audio_language_code='en') self.user = UserFactory(is_staff=True) self.language = SubtitleLanguageFactory(video=self.video, language_code='en') self.show_private_versions = mock.Mock(return_value=True) self.serializer_context = { 'video': self.video, 'show_private_versions': self.show_private_versions, 'request': APIRequestFactory().get("/mock-url/"), 'allow_extra': False, } self.serializer = SubtitleLanguageSerializer( context=self.serializer_context) def get_serializer_data(self): return self.serializer.to_representation(self.language) def test_fields(self): serializer_data = self.get_serializer_data() assert_equal(serializer_data['id'], self.language.id) assert_equal(serializer_data['created'], format_datetime_field(self.language.created)) assert_equal(serializer_data['is_original'], True) assert_equal(serializer_data['is_primary_audio_language'], True) assert_equal(serializer_data['is_rtl'], self.language.is_rtl()) assert_equal(serializer_data['published'], self.language.has_public_version()) assert_equal(serializer_data['language_code'], self.language.language_code) assert_equal(serializer_data['name'], self.language.get_language_code_display()) assert_equal(serializer_data['title'], self.language.get_title()) assert_equal(serializer_data['description'], self.language.get_description()) assert_equal(serializer_data['metadata'], self.language.get_metadata()) assert_equal(serializer_data['subtitle_count'], self.language.get_subtitle_count()) assert_equal(serializer_data['subtitles_complete'], self.language.subtitles_complete) assert_equal(serializer_data['is_translation'], compat.subtitlelanguage_is_translation(self.language)) assert_equal( serializer_data['original_language_code'], compat.subtitlelanguage_original_language_code(self.language)) assert_equal(serializer_data['resource_uri'], reverse('api:subtitle-language-detail', kwargs={ 'video_id': self.video.video_id, 'language_code': self.language.language_code, }, request=APIRequestFactory().get("/"))) def make_version(self, language_code, **kwargs): return pipeline.add_subtitles(self.video, language_code, SubtitleSetFactory(), **kwargs) def test_versions_field(self): user1 = UserFactory(username='******') user2 = UserFactory(username='******') self.make_version('en', visibility='public', author=user1) self.make_version('en', visibility='private', author=user2) serializer_data = self.get_serializer_data() assert_equal(serializer_data['num_versions'], 2) assert_equal(serializer_data['versions'], [ { 'author': user_field_data(user2), 'published': False, 'version_no': 2, }, { 'author': user_field_data(user1), 'published': True, 'version_no': 1, }, ]) def test_hiding_private_versions(self): # Test show_private_versions being False self.show_private_versions.return_value = False self.make_version('en', visibility='private', author=self.user) self.make_version('en', visibility='public', author=self.user) serializer_data = self.get_serializer_data() assert_equal(serializer_data['num_versions'], 1) assert_equal(serializer_data['versions'], [ { 'author': user_field_data(self.user), 'published': True, 'version_no': 2, }, ]) # check the arguments passed to show_private_versions assert_equal(self.show_private_versions.call_args, mock.call('en')) def test_reviewed_and_approved_by(self): # For reviewed_by and approved_by, the values are set on subtitle # versions, but we return it for the language as a whole. # # We should return the value from the earliest version in the # language. This seems wrong, but that's how we originally # implemented it. u1 = UserFactory(username='******') u2 = UserFactory(username='******') u3 = UserFactory(username='******') v1 = self.make_version('en') v2 = self.make_version('en') v3 = self.make_version('en') v1.set_reviewed_by(u1) v2.set_reviewed_by(u2) v2.set_approved_by(u2) v3.set_approved_by(u3) serializer_data = self.get_serializer_data() assert_equal(serializer_data['reviewer'], 'user1') assert_equal(serializer_data['approver'], 'user2') def run_create(self, data): serializer = SubtitleLanguageSerializer( data=data, context=self.serializer_context) serializer.is_valid(raise_exception=True) return serializer.save() def run_update(self, language, data): serializer = SubtitleLanguageSerializer( instance=language, data=data, context=self.serializer_context) serializer.is_valid(raise_exception=True) serializer.save() def test_create(self): language = self.run_create({ 'language_code': 'es', 'is_primary_audio_language': True, 'subtitles_complete': True, }) assert_equal(language.video, self.video) assert_equal(language.language_code, 'es') assert_equal(self.video.primary_audio_language_code, 'es') assert_equal(language.subtitles_complete, True) test_utils.assert_saved(language) test_utils.assert_saved(self.video) def test_create_with_only_language_code(self): language = self.run_create({ 'language_code': 'es', }) assert_equal(language.video, self.video) assert_equal(language.language_code, 'es') assert_equal(self.video.primary_audio_language_code, 'en') assert_equal(language.subtitles_complete, False) def test_handle_capital_letters(self): language = self.run_create({ 'language_code': 'pt-BR', }) assert_equal(language.language_code, 'pt-br') def test_try_recreate(self): language = SubtitleLanguageFactory(video=self.video, language_code='es') with assert_raises(ValidationError): self.run_create({ 'language_code': 'es', 'is_primary_audio_language': True, 'subtitles_complete': True, }) def test_update(self): language = SubtitleLanguageFactory(video=self.video, language_code='es') self.run_update(language, { 'is_primary_audio_language': True, 'subtitles_complete': True, }) assert_equal(language.subtitles_complete, True) assert_equal(self.video.primary_audio_language_code, 'es') test_utils.assert_saved(language) test_utils.assert_saved(self.video) def test_cant_change_language_code(self): language = SubtitleLanguageFactory(video=self.video, language_code='es') self.run_update(language, { 'language_code': 'fr', }) assert_equal(language.language_code, 'es') def test_deprecated_aliases(self): language = SubtitleLanguageFactory(video=self.video, language_code='es') self.run_update(language, { 'is_original': True, 'is_complete': True, }) self.video = test_utils.reload_obj(self.video) assert_equal(self.video.primary_audio_language_code, 'es') assert_equal(test_utils.reload_obj(language).subtitles_complete, True) def test_runs_tasks(self): language = self.run_create({'language_code': 'es'}) assert_equal(test_utils.video_changed_tasks.delay.call_count, 1) self.run_update(language, {}) assert_equal(test_utils.video_changed_tasks.delay.call_count, 2) def test_language_code_read_only(self): serializer = SubtitleLanguageSerializer( context=self.serializer_context, instance=self.language) assert_true(serializer.fields['language_code'].read_only)
def test_language_code_read_only(self): serializer = SubtitleLanguageSerializer( context=self.serializer_context, instance=self.language) assert_true(serializer.fields['language_code'].read_only)
def run_update(self, language, data): serializer = SubtitleLanguageSerializer( instance=language, data=data, context=self.serializer_context) serializer.is_valid(raise_exception=True) serializer.save()
def run_create(self, data): serializer = SubtitleLanguageSerializer( data=data, context=self.serializer_context) serializer.is_valid(raise_exception=True) return serializer.save()