def test_bad_recognition_response(self, mock_translate_hook, mock_speech_hook): mock_speech_hook.return_value.recognize_speech.return_value = RecognizeResponse( results=[SpeechRecognitionResult()]) op = GcpTranslateSpeechOperator( audio={"uri": "gs://bucket/object"}, config={"encoding": "LINEAR16"}, target_language='pl', format_='text', source_language=None, model='base', gcp_conn_id=GCP_CONN_ID, task_id='id', ) with self.assertRaises(AirflowException) as cm: op.execute(context=None) err = cm.exception self.assertIn("it should contain 'alternatives' field", str(err)) mock_speech_hook.assert_called_once_with(gcp_conn_id=GCP_CONN_ID) mock_translate_hook.assert_called_once_with(gcp_conn_id=GCP_CONN_ID) mock_speech_hook.return_value.recognize_speech.assert_called_once_with( audio={"uri": "gs://bucket/object"}, config={"encoding": "LINEAR16"}, ) mock_translate_hook.return_value.translate.assert_not_called()
def test_minimal_green_path(self, mock_translate_hook, mock_speech_hook): mock_speech_hook.return_value.recognize_speech.return_value = RecognizeResponse( results=[ SpeechRecognitionResult(alternatives=[ SpeechRecognitionAlternative( transcript='test speech recognition result') ]) ]) mock_translate_hook.return_value.translate.return_value = [{ 'translatedText': 'sprawdzić wynik rozpoznawania mowy', 'detectedSourceLanguage': 'en', 'model': 'base', 'input': 'test speech recognition result', }] op = GcpTranslateSpeechOperator( audio={"uri": "gs://bucket/object"}, config={"encoding": "LINEAR16"}, target_language='pl', format_='text', source_language=None, model='base', gcp_conn_id=GCP_CONN_ID, task_id='id', ) return_value = op.execute(context=None) mock_speech_hook.assert_called_once_with(gcp_conn_id=GCP_CONN_ID) mock_translate_hook.assert_called_once_with(gcp_conn_id=GCP_CONN_ID) mock_speech_hook.return_value.recognize_speech.assert_called_once_with( audio={"uri": "gs://bucket/object"}, config={"encoding": "LINEAR16"}, ) mock_translate_hook.return_value.translate.assert_called_once_with( values='test speech recognition result', target_language='pl', format_='text', source_language=None, model='base', ) self.assertEqual( [{ 'translatedText': 'sprawdzić wynik rozpoznawania mowy', 'detectedSourceLanguage': 'en', 'model': 'base', 'input': 'test speech recognition result', }], return_value, )
voice=VOICE, audio_config=AUDIO_CONFIG, target_bucket_name=BUCKET_NAME, target_filename=FILENAME, task_id="text_to_speech_synthesize_task", ) # [END howto_operator_text_to_speech_synthesize] # [START howto_operator_speech_to_text_recognize] speech_to_text_recognize_task = CloudSpeechToTextRecognizeSpeechOperator( project_id=GCP_PROJECT_ID, config=CONFIG, audio=AUDIO, task_id="speech_to_text_recognize_task" ) # [END howto_operator_speech_to_text_recognize] text_to_speech_synthesize_task >> speech_to_text_recognize_task # [START howto_operator_translate_speech] translate_speech_task = GcpTranslateSpeechOperator( project_id=GCP_PROJECT_ID, audio=AUDIO, config=CONFIG, target_language=TARGET_LANGUAGE, format_=FORMAT, source_language=SOURCE_LANGUAGE, model=MODEL, task_id='translate_speech_task' ) # [END howto_operator_translate_speech] text_to_speech_synthesize_task >> translate_speech_task