Example #1
0
    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()
Example #2
0
    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,
        )