From 0e8059e26e8d7a93a193a54bd0d3a1c4b3b076ee Mon Sep 17 00:00:00 2001 From: Mike <45373284+munkhuushmgl@users.noreply.github.com> Date: Wed, 19 May 2021 14:08:06 -0700 Subject: [PATCH] =?UTF-8?q?chore:=20refactored=20all=20glossaries=20sample?= =?UTF-8?q?s=20to=20use=20predefined=20glossary=20i=E2=80=A6=20(#159)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...slate_text_with_glossary_and_model_test.py | 35 ++--------------- ...batch_translate_text_with_glossary_test.py | 35 ++--------------- .../translate_v3_get_glossary_test.py | 38 ++----------------- .../translate_v3_list_glossary_test.py | 38 ++----------------- ...te_v3_translate_text_with_glossary_test.py | 38 ++----------------- 5 files changed, 15 insertions(+), 169 deletions(-) diff --git a/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model_test.py b/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model_test.py index 5abadec4..2c40d6ee 100644 --- a/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model_test.py +++ b/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model_test.py @@ -15,45 +15,16 @@ import os import uuid -import backoff -from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError from google.cloud import storage -from google.cloud.exceptions import NotFound import pytest import translate_v3_batch_translate_text_with_glossary_and_model -import translate_v3_create_glossary -import translate_v3_delete_glossary PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" +GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY" MODEL_ID = "TRL3128559826197068699" -@pytest.fixture(scope="session") -def glossary(): - """Get the ID of a glossary available to session (do not mutate/delete).""" - glossary_id = "must-start-with-letters-" + str(uuid.uuid1()) - translate_v3_create_glossary.create_glossary( - project_id=PROJECT_ID, input_uri=GLOSSARY_INPUT_URI, glossary_id=glossary_id - ) - - yield glossary_id - - # clean up - @backoff.on_exception( - backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 - ) - def delete_glossary(): - try: - translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) - except NotFound as e: - # Ignoring this case. - print("Got NotFound, detail: {}".format(str(e))) - - delete_glossary() - - @pytest.fixture(scope="function") def bucket(): """Create a temporary bucket to store annotation output.""" @@ -66,13 +37,13 @@ def bucket(): bucket.delete(force=True) -def test_batch_translate_text_with_glossary_and_model(capsys, bucket, glossary): +def test_batch_translate_text_with_glossary_and_model(capsys, bucket): translate_v3_batch_translate_text_with_glossary_and_model.batch_translate_text_with_glossary_and_model( "gs://cloud-samples-data/translation/text_with_custom_model_and_glossary.txt", "gs://{}/translation/BATCH_TRANSLATION_GLOS_MODEL_OUTPUT/".format(bucket.name), PROJECT_ID, MODEL_ID, - glossary, + GLOSSARY_ID, ) out, _ = capsys.readouterr() diff --git a/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py b/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py index ffac21a1..d2b1f211 100644 --- a/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py +++ b/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py @@ -15,43 +15,14 @@ import os import uuid -import backoff -from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError from google.cloud import storage -from google.cloud.exceptions import NotFound import pytest import translate_v3_batch_translate_text_with_glossary -import translate_v3_create_glossary -import translate_v3_delete_glossary PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" - - -@pytest.fixture(scope="session") -def glossary(): - """Get the ID of a glossary available to session (do not mutate/delete).""" - glossary_id = "test-{}".format(uuid.uuid4()) - translate_v3_create_glossary.create_glossary( - PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id - ) - - yield glossary_id - - # cleanup - @backoff.on_exception( - backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 - ) - def delete_glossary(): - try: - translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) - except NotFound as e: - # Ignoring this case. - print("Got NotFound, detail: {}".format(str(e))) - - delete_glossary() +GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY" @pytest.fixture(scope="function") @@ -67,12 +38,12 @@ def bucket(): @pytest.mark.flaky(max_runs=3, min_passes=1) -def test_batch_translate_text_with_glossary(capsys, bucket, glossary): +def test_batch_translate_text_with_glossary(capsys, bucket): translate_v3_batch_translate_text_with_glossary.batch_translate_text_with_glossary( "gs://cloud-samples-data/translation/text_with_glossary.txt", "gs://{}/translation/BATCH_TRANSLATION_GLOS_OUTPUT/".format(bucket.name), PROJECT_ID, - glossary, + GLOSSARY_ID, 320, ) diff --git a/samples/snippets/translate_v3_get_glossary_test.py b/samples/snippets/translate_v3_get_glossary_test.py index a4fc3231..9dfa5e81 100644 --- a/samples/snippets/translate_v3_get_glossary_test.py +++ b/samples/snippets/translate_v3_get_glossary_test.py @@ -13,47 +13,15 @@ # limitations under the License. import os -import uuid -import backoff -from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError -from google.cloud.exceptions import NotFound -import pytest - -import translate_v3_create_glossary -import translate_v3_delete_glossary import translate_v3_get_glossary PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" - - -@pytest.fixture(scope="session") -def glossary(): - """Get the ID of a glossary available to session (do not mutate/delete).""" - glossary_id = "must-start-with-letters-" + str(uuid.uuid1()) - translate_v3_create_glossary.create_glossary( - PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id - ) - - yield glossary_id - - # cleanup - @backoff.on_exception( - backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 - ) - def delete_glossary(): - try: - translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) - except NotFound as e: - # Ignoring this case. - print("Got NotFound, detail: {}".format(str(e))) - - delete_glossary() +GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY" -def test_get_glossary(capsys, glossary): - translate_v3_get_glossary.get_glossary(PROJECT_ID, glossary) +def test_get_glossary(capsys): + translate_v3_get_glossary.get_glossary(PROJECT_ID, GLOSSARY_ID) out, _ = capsys.readouterr() assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out diff --git a/samples/snippets/translate_v3_list_glossary_test.py b/samples/snippets/translate_v3_list_glossary_test.py index ed2a4754..9d87f0cd 100644 --- a/samples/snippets/translate_v3_list_glossary_test.py +++ b/samples/snippets/translate_v3_list_glossary_test.py @@ -13,48 +13,16 @@ # limitations under the License. import os -import uuid -import backoff -from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError -from google.cloud.exceptions import NotFound -import pytest - -import translate_v3_create_glossary -import translate_v3_delete_glossary import translate_v3_list_glossary PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" - - -@pytest.fixture(scope="session") -def glossary(): - """Get the ID of a glossary available to session (do not mutate/delete).""" - glossary_id = "must-start-with-letters-" + str(uuid.uuid1()) - translate_v3_create_glossary.create_glossary( - PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id - ) - - yield glossary_id - - # clean up - @backoff.on_exception( - backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 - ) - def delete_glossary(): - try: - translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) - except NotFound as e: - # Ignoring this case. - print("Got NotFound, detail: {}".format(str(e))) - - delete_glossary() +GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY" -def test_list_glossary(capsys, glossary): +def test_list_glossary(capsys): translate_v3_list_glossary.list_glossaries(PROJECT_ID) out, _ = capsys.readouterr() - assert glossary in out + assert GLOSSARY_ID in out assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out diff --git a/samples/snippets/translate_v3_translate_text_with_glossary_test.py b/samples/snippets/translate_v3_translate_text_with_glossary_test.py index 46724dde..ef5a9082 100644 --- a/samples/snippets/translate_v3_translate_text_with_glossary_test.py +++ b/samples/snippets/translate_v3_translate_text_with_glossary_test.py @@ -14,49 +14,17 @@ # limitations under the License. import os -import uuid -import backoff -from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError -from google.cloud.exceptions import NotFound -import pytest - -import translate_v3_create_glossary -import translate_v3_delete_glossary import translate_v3_translate_text_with_glossary PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" - - -@pytest.fixture(scope="session") -def glossary(): - """Get the ID of a glossary available to session (do not mutate/delete).""" - glossary_id = "must-start-with-letters-" + str(uuid.uuid1()) - translate_v3_create_glossary.create_glossary( - PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id - ) - - yield glossary_id - - # cleanup - @backoff.on_exception( - backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 - ) - def delete_glossary(): - try: - translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) - except NotFound as e: - # Ignoring this case. - print("Got NotFound, detail: {}".format(str(e))) - - delete_glossary() +GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY" -def test_translate_text_with_glossary(capsys, glossary): +def test_translate_text_with_glossary(capsys): translate_v3_translate_text_with_glossary.translate_text_with_glossary( - "account", PROJECT_ID, glossary + "account", PROJECT_ID, GLOSSARY_ID ) out, _ = capsys.readouterr() assert "アカウント" or "口座" in out