From 8ae6e3787a4dd08c4ba8c330288f58bdbd51ff68 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Date: Wed, 3 Jun 2020 15:15:59 -0700 Subject: [PATCH] [translate] fix hybrid glossaries tests [(#3936)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/3936) --- .../hybrid_glossaries/hybrid_tutorial.py | 19 +++++++++++++------ ...orial_tests.py => hybrid_tutorial_test.py} | 10 ++++------ .../hybrid_glossaries/requirements.txt | 2 +- .../resources/non_standard_format.txt | 2 +- translation/samples/snippets/pytest.ini | 3 +++ 5 files changed, 22 insertions(+), 14 deletions(-) rename translation/samples/snippets/hybrid_glossaries/{hybrid_tutorial_tests.py => hybrid_tutorial_test.py} (93%) create mode 100644 translation/samples/snippets/pytest.ini diff --git a/translation/samples/snippets/hybrid_glossaries/hybrid_tutorial.py b/translation/samples/snippets/hybrid_glossaries/hybrid_tutorial.py index 0da84b722078..1b320e5b2a49 100644 --- a/translation/samples/snippets/hybrid_glossaries/hybrid_tutorial.py +++ b/translation/samples/snippets/hybrid_glossaries/hybrid_tutorial.py @@ -193,21 +193,28 @@ def text_to_speech(text, outfile): client = texttospeech.TextToSpeechClient() # Sets the text input to be synthesized - synthesis_input = texttospeech.types.SynthesisInput(ssml=ssml) + synthesis_input = texttospeech.SynthesisInput(ssml=ssml) # Builds the voice request, selects the language code ("en-US") and # the SSML voice gender ("MALE") - voice = texttospeech.types.VoiceSelectionParams( + voice = texttospeech.VoiceSelectionParams( language_code='en-US', - ssml_gender=texttospeech.enums.SsmlVoiceGender.MALE) + ssml_gender=texttospeech.SsmlVoiceGender.MALE) # Selects the type of audio file to return - audio_config = texttospeech.types.AudioConfig( - audio_encoding=texttospeech.enums.AudioEncoding.MP3) + audio_config = texttospeech.AudioConfig( + audio_encoding=texttospeech.AudioEncoding.MP3) # Performs the text-to-speech request on the text input with the selected # voice parameters and audio file type - response = client.synthesize_speech(synthesis_input, voice, audio_config) + + request = texttospeech.SynthesizeSpeechRequest( + input=synthesis_input, + voice=voice, + audio_config=audio_config + ) + + response = client.synthesize_speech(request=request) # Writes the synthetic audio to the output file. with open(outfile, 'wb') as out: diff --git a/translation/samples/snippets/hybrid_glossaries/hybrid_tutorial_tests.py b/translation/samples/snippets/hybrid_glossaries/hybrid_tutorial_test.py similarity index 93% rename from translation/samples/snippets/hybrid_glossaries/hybrid_tutorial_tests.py rename to translation/samples/snippets/hybrid_glossaries/hybrid_tutorial_test.py index 5224ca524a91..f99be330b92d 100644 --- a/translation/samples/snippets/hybrid_glossaries/hybrid_tutorial_tests.py +++ b/translation/samples/snippets/hybrid_glossaries/hybrid_tutorial_test.py @@ -13,7 +13,9 @@ # limitations under the License. import os +import re import sys +import uuid from hybrid_tutorial import create_glossary from hybrid_tutorial import pic_to_text @@ -28,14 +30,10 @@ def test_vision_standard_format(): - - expected_text = 'This is\na test!\n' - alt_expected_text = 'This\nis\na test!\n' - # Generate text using Vision API text = pic_to_text('resources/standard_format.jpeg') - assert (text == expected_text) or (text == alt_expected_text) + assert re.match(r"This\s?is\s?a\s?test!\s?", text) def test_vision_non_standard_format(): @@ -58,7 +56,7 @@ def test_create_and_delete_glossary(): from beta_snippets import delete_glossary languages = ['fr', 'en'] - glossary_name = 'test-glossary' + glossary_name = f'test-glossary-{uuid.uuid4()}' glossary_uri = 'gs://cloud-samples-data/translation/bistro_glossary.csv' # create_glossary will raise an exception if creation fails diff --git a/translation/samples/snippets/hybrid_glossaries/requirements.txt b/translation/samples/snippets/hybrid_glossaries/requirements.txt index d13c5f7d8080..ed9bbd1eae82 100644 --- a/translation/samples/snippets/hybrid_glossaries/requirements.txt +++ b/translation/samples/snippets/hybrid_glossaries/requirements.txt @@ -1,3 +1,3 @@ google-cloud-translate==2.0.1 google-cloud-vision==1.0.0 -google-cloud-texttospeech==1.0.1 +google-cloud-texttospeech==2.0.0 diff --git a/translation/samples/snippets/hybrid_glossaries/resources/non_standard_format.txt b/translation/samples/snippets/hybrid_glossaries/resources/non_standard_format.txt index 8a6e3c113d18..529799ee94b1 100644 --- a/translation/samples/snippets/hybrid_glossaries/resources/non_standard_format.txt +++ b/translation/samples/snippets/hybrid_glossaries/resources/non_standard_format.txt @@ -19,11 +19,11 @@ $10 Firebase Fruit Salad watermelon, honeydew melon, and pineapple +$6 Compute Engine Burger quarter-pound burger with cheddar cheese $10 -$6 BigQuery BLT bacon, lettuce, and tomato sandwich diff --git a/translation/samples/snippets/pytest.ini b/translation/samples/snippets/pytest.ini new file mode 100644 index 000000000000..f139880676b3 --- /dev/null +++ b/translation/samples/snippets/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +# don't recurse into hybrid_glossaries directory +norecursedirs = * \ No newline at end of file