Exemple #1
0
 def execute(self, context: 'Context') -> None:
     hook = CloudTextToSpeechHook(
         gcp_conn_id=self.gcp_conn_id,
         impersonation_chain=self.impersonation_chain,
     )
     result = hook.synthesize_speech(
         input_data=self.input_data,
         voice=self.voice,
         audio_config=self.audio_config,
         retry=self.retry,
         timeout=self.timeout,
     )
     with NamedTemporaryFile() as temp_file:
         temp_file.write(result.audio_content)
         cloud_storage_hook = GCSHook(
             gcp_conn_id=self.gcp_conn_id,
             impersonation_chain=self.impersonation_chain,
         )
         cloud_storage_hook.upload(bucket_name=self.target_bucket_name,
                                   object_name=self.target_filename,
                                   filename=temp_file.name)
         FileDetailsLink.persist(
             context=context,
             task_instance=self,
             uri=f"{self.target_bucket_name}/{self.target_filename}",
             project_id=cloud_storage_hook.project_id,
         )
class TestTextToSpeechHook(unittest.TestCase):
    def setUp(self):
        with patch(
                "airflow.providers.google.common.hooks.base_google.GoogleBaseHook.__init__",
                new=mock_base_gcp_hook_default_project_id,
        ):
            self.gcp_text_to_speech_hook = CloudTextToSpeechHook(
                gcp_conn_id="test")

    @patch(
        "airflow.providers.google.cloud.hooks.text_to_speech.CloudTextToSpeechHook.client_info",
        new_callable=PropertyMock,
    )
    @patch(
        "airflow.providers.google.cloud.hooks.text_to_speech.CloudTextToSpeechHook._get_credentials"
    )
    @patch(
        "airflow.providers.google.cloud.hooks.text_to_speech.TextToSpeechClient"
    )
    def test_text_to_speech_client_creation(self, mock_client, mock_get_creds,
                                            mock_client_info):
        result = self.gcp_text_to_speech_hook.get_conn()
        mock_client.assert_called_once_with(
            credentials=mock_get_creds.return_value,
            client_info=mock_client_info.return_value)
        assert mock_client.return_value == result
        assert self.gcp_text_to_speech_hook._client == result

    @patch(
        "airflow.providers.google.cloud.hooks.text_to_speech.CloudTextToSpeechHook.get_conn"
    )
    def test_synthesize_speech(self, get_conn):
        synthesize_method = get_conn.return_value.synthesize_speech
        synthesize_method.return_value = None
        self.gcp_text_to_speech_hook.synthesize_speech(
            input_data=INPUT, voice=VOICE, audio_config=AUDIO_CONFIG)
        synthesize_method.assert_called_once_with(input_=INPUT,
                                                  voice=VOICE,
                                                  audio_config=AUDIO_CONFIG,
                                                  retry=None,
                                                  timeout=None)
 def execute(self, context):
     hook = CloudTextToSpeechHook(gcp_conn_id=self.gcp_conn_id)
     result = hook.synthesize_speech(
         input_data=self.input_data,
         voice=self.voice,
         audio_config=self.audio_config,
         retry=self.retry,
         timeout=self.timeout,
     )
     with NamedTemporaryFile() as temp_file:
         temp_file.write(result.audio_content)
         cloud_storage_hook = GCSHook(google_cloud_storage_conn_id=self.gcp_conn_id)
         cloud_storage_hook.upload(
             bucket_name=self.target_bucket_name, object_name=self.target_filename, filename=temp_file.name
         )