def validate(self) -> None: """Validates properties of the MachineTranslation. Raises: ValidationError. One or more attributes of the MachineTranslation are invalid. """ # TODO(#12341): Tidy up this logic once we have a canonical list of # language codes. if not utils.is_supported_audio_language_code( self.source_language_code ) and not utils.is_valid_language_code(self.source_language_code): raise utils.ValidationError('Invalid source language code: %s' % self.source_language_code) # TODO(#12341): Tidy up this logic once we have a canonical list of # language codes. if not utils.is_supported_audio_language_code( self.target_language_code ) and not utils.is_valid_language_code(self.target_language_code): raise utils.ValidationError('Invalid target language code: %s' % self.target_language_code) if self.source_language_code == self.target_language_code: raise utils.ValidationError( ('Expected source_language_code to be different from ' 'target_language_code: "%s" = "%s"') % (self.source_language_code, self.target_language_code))
def _validate_translation_counts(self, translation_counts): """Validates per-language counts of translations. Args: translation_counts: dict. A dict with language code as a key and number of translations in that language as the value. Raises: ValidationError. One or more attributes of the object are invalid. """ for language_code, count in ( translation_counts.items()): if not utils.is_supported_audio_language_code(language_code): raise utils.ValidationError( 'Invalid language_code: %s' % language_code) if not isinstance(count, int): raise utils.ValidationError( 'Expected count for language_code %s to be an integer, ' 'received %s' % (language_code, count)) if count < 0: raise utils.ValidationError( 'Expected count for language_code %s to be a non-negative ' 'integer, received %s' % (language_code, count)) if count > self.content_count: raise utils.ValidationError( 'Expected translation count for language_code %s to be ' 'less than or equal to content_count(%s), received %s' % ( language_code, self.content_count, count))
def validate(self): """Validates different attributes of the class.""" if not isinstance(self.can_review_translation_for_language_codes, list): raise utils.ValidationError( 'Expected can_review_translation_for_language_codes to be a ' 'list, found: %s' % type(self.can_review_translation_for_language_codes)) for language_code in self.can_review_translation_for_language_codes: if not utils.is_supported_audio_language_code(language_code): raise utils.ValidationError('Invalid language_code: %s' % (language_code)) if len(self.can_review_translation_for_language_codes) != len( set(self.can_review_translation_for_language_codes)): raise utils.ValidationError( 'Expected can_review_translation_for_language_codes list not ' 'to have duplicate values, found: %s' % (self.can_review_translation_for_language_codes)) if not isinstance(self.can_review_voiceover_for_language_codes, list): raise utils.ValidationError( 'Expected can_review_voiceover_for_language_codes to be a ' 'list, found: %s' % type(self.can_review_voiceover_for_language_codes)) for language_code in self.can_review_voiceover_for_language_codes: if not utils.is_supported_audio_language_code(language_code): raise utils.ValidationError('Invalid language_code: %s' % (language_code)) if len(self.can_review_voiceover_for_language_codes) != len( set(self.can_review_voiceover_for_language_codes)): raise utils.ValidationError( 'Expected can_review_voiceover_for_language_codes list not to ' 'have duplicate values, found: %s' % (self.can_review_voiceover_for_language_codes)) if not isinstance(self.can_review_questions, bool): raise utils.ValidationError( 'Expected can_review_questions to be a boolean value, ' 'found: %s' % type(self.can_review_questions)) if not isinstance(self.can_submit_questions, bool): raise utils.ValidationError( 'Expected can_submit_questions to be a boolean value, ' 'found: %s' % type(self.can_submit_questions))
def is_supported_audio_language_code(obj: str) -> bool: """Checks if the given obj (a string) represents a valid language code. Args: obj: str. A string. Returns: bool. Whether the given object is a valid audio language code. """ return utils.is_supported_audio_language_code(obj)
def test_is_supported_audio_language_code(self) -> None: self.assertTrue(utils.is_supported_audio_language_code('hi-en')) self.assertFalse(utils.is_supported_audio_language_code('unknown'))