Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
chore: refactored all glossaries samples to use predefined glossary i… (
Browse files Browse the repository at this point in the history
  • Loading branch information
munkhuushmgl authored May 19, 2021
1 parent e6ca33a commit 0e8059e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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,
)

Expand Down
38 changes: 3 additions & 35 deletions samples/snippets/translate_v3_get_glossary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 3 additions & 35 deletions samples/snippets/translate_v3_list_glossary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 3 additions & 35 deletions samples/snippets/translate_v3_translate_text_with_glossary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0e8059e

Please sign in to comment.