class TestTextToSpeechHook(unittest.TestCase):
    def setUp(self):
        with patch(
            "airflow.gcp.hooks.base.GoogleCloudBaseHook.__init__",
            new=mock_base_gcp_hook_default_project_id,
        ):
            self.gcp_text_to_speech_hook = CloudTextToSpeechHook(gcp_conn_id="test")

    @patch("airflow.gcp.hooks.text_to_speech.CloudTextToSpeechHook.client_info", new_callable=PropertyMock)
    @patch("airflow.gcp.hooks.text_to_speech.CloudTextToSpeechHook._get_credentials")
    @patch("airflow.gcp.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
        )
        self.assertEqual(mock_client.return_value, result)
        self.assertEqual(self.gcp_text_to_speech_hook._client, result)

    @patch("airflow.gcp.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
        )
예제 #2
0
 def setUp(self):
     with patch(
             "airflow.gcp.hooks.base.CloudBaseHook.__init__",
             new=mock_base_gcp_hook_default_project_id,
     ):
         self.gcp_text_to_speech_hook = CloudTextToSpeechHook(
             gcp_conn_id="test")
예제 #3
0
 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 = GoogleCloudStorageHook(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
         )