Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translate: fix glossary leak issue #3572

Merged
merged 14 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion translate/cloud-client/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
backoff==1.10.0
flaky==3.6.1
pytest==5.3.2
pytest==5.3.2
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
# limitations under the License.

import os
import pytest
import translate_v3_batch_translate_text
import uuid

import pytest

from google.cloud import storage

import translate_v3_batch_translate_text


PROJECT_ID = os.environ["GCLOUD_PROJECT"]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@
# limitations under the License.

import os
import pytest
import uuid

import backoff
import pytest

from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError
from google.cloud.exceptions import NotFound
from google.cloud import storage

import translate_v3_batch_translate_text_with_glossary
import translate_v3_create_glossary
import translate_v3_delete_glossary
from google.cloud import storage


PROJECT_ID = os.environ["GCLOUD_PROJECT"]
GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv"
Expand All @@ -34,10 +41,18 @@ def glossary():

yield glossary_id

try:
translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id)
except Exception:
pass
# 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()


@pytest.fixture(scope="function")
Expand Down
23 changes: 17 additions & 6 deletions translate/cloud-client/translate_v3_create_glossary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
import os
import uuid

import backoff
import pytest

from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError
from google.cloud.exceptions import NotFound

import translate_v3_create_glossary
import translate_v3_delete_glossary


PROJECT_ID = os.environ["GCLOUD_PROJECT"]
GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv"

Expand All @@ -36,9 +41,15 @@ def test_create_glossary(capsys):
assert "Created:" in out
assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out
finally:
# clean up after use
try:
translate_v3_delete_glossary.delete_glossary(
PROJECT_ID, glossary_id)
except Exception:
pass
# 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()
2 changes: 1 addition & 1 deletion translate/cloud-client/translate_v3_detect_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from google.cloud import translate


def sample_detect_language(project_id="YOUR_PROJECT_ID"):
def detect_language(project_id="YOUR_PROJECT_ID"):
tmatsuo marked this conversation as resolved.
Show resolved Hide resolved
"""Detecting the language of a text string."""

client = translate.TranslationServiceClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@


def test_detect_language(capsys):
translate_v3_detect_language.sample_detect_language(PROJECT_ID)
translate_v3_detect_language.detect_language(PROJECT_ID)
out, _ = capsys.readouterr()
assert "en" in out
25 changes: 20 additions & 5 deletions translate/cloud-client/translate_v3_get_glossary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@
# limitations under the License.

import os
import uuid

import backoff
import pytest

from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError
from google.cloud.exceptions import NotFound

import translate_v3_create_glossary
import translate_v3_delete_glossary
import translate_v3_get_glossary
import uuid


PROJECT_ID = os.environ["GCLOUD_PROJECT"]
GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv"
Expand All @@ -33,10 +40,18 @@ def glossary():

yield glossary_id

try:
translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id)
except Exception:
pass
# 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()


def test_get_glossary(capsys, glossary):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from google.cloud import translate


def sample_get_supported_languages(project_id="YOUR_PROJECT_ID"):
def get_supported_languages(project_id="YOUR_PROJECT_ID"):
"""Getting a list of supported language codes."""

client = translate.TranslationServiceClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@


def test_list_languages(capsys):
translate_v3_get_supported_languages.sample_get_supported_languages(PROJECT_ID)
translate_v3_get_supported_languages.get_supported_languages(PROJECT_ID)
out, _ = capsys.readouterr()
assert "zh-CN" in out
2 changes: 1 addition & 1 deletion translate/cloud-client/translate_v3_list_glossary.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from google.cloud import translate


def sample_list_glossaries(project_id="YOUR_PROJECT_ID"):
def list_glossaries(project_id="YOUR_PROJECT_ID"):
"""List Glossaries."""

client = translate.TranslationServiceClient()
Expand Down
27 changes: 21 additions & 6 deletions translate/cloud-client/translate_v3_list_glossary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@
# limitations under the License.

import os
import uuid

import backoff
import pytest

from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError
from google.cloud.exceptions import NotFound

import translate_v3_create_glossary
import translate_v3_delete_glossary
import translate_v3_list_glossary
import uuid


PROJECT_ID = os.environ["GCLOUD_PROJECT"]
GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv"
Expand All @@ -33,14 +40,22 @@ def glossary():

yield glossary_id

try:
translate_v3_delete_glossary.sample_delete_glossary(PROJECT_ID, glossary_id)
except Exception:
pass
# 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()


def test_list_glossary(capsys, glossary):
translate_v3_list_glossary.sample_list_glossaries(PROJECT_ID)
translate_v3_list_glossary.list_glossaries(PROJECT_ID)
out, _ = capsys.readouterr()
assert glossary in out
assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out
2 changes: 1 addition & 1 deletion translate/cloud-client/translate_v3_translate_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from google.cloud import translate


def sample_translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"):
def translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"):
"""Translating Text."""

client = translate.TranslationServiceClient()
Expand Down
2 changes: 1 addition & 1 deletion translate/cloud-client/translate_v3_translate_text_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


def test_translate_text(capsys):
translate_v3_translate_text.sample_translate_text(
translate_v3_translate_text.translate_text(
"Hello World!", PROJECT_ID)
out, _ = capsys.readouterr()
assert "Bonjour le monde" in out
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@
# limitations under the License.

import os
import uuid

import backoff
import pytest

from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError
from google.cloud.exceptions import NotFound

import translate_v3_create_glossary
import translate_v3_delete_glossary
import translate_v3_translate_text_with_glossary
import uuid


PROJECT_ID = os.environ["GCLOUD_PROJECT"]
GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv"
Expand All @@ -34,10 +41,18 @@ def glossary():

yield glossary_id

try:
translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id)
except Exception:
pass
# 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()


def test_translate_text_with_glossary(capsys, glossary):
Expand Down