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 8 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 @@ -12,18 +12,26 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import backoff
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
import os
import pytest
import uuid
import translate_v3_batch_translate_text_with_glossary
import translate_v3_create_glossary
import translate_v3_delete_glossary

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

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


@backoff.on_exception(
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
backoff.expo, (DeadlineExceeded, GoogleAPICallError,
RetryError), max_time=60
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
)
@pytest.fixture(scope="session")
def glossary():
"""Get the ID of a glossary available to session (do not mutate/delete)."""
Expand All @@ -34,10 +42,12 @@ def glossary():

yield glossary_id

# cleanup
try:
translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id)
except Exception:
pass
except NotFound as e:
# Ignoring this case.
print("Got NotFound, detail: {}".format(str(e)))


@pytest.fixture(scope="function")
Expand Down
14 changes: 11 additions & 3 deletions translate/cloud-client/translate_v3_create_glossary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import backoff
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
import os
import uuid

import pytest

import translate_v3_create_glossary
import translate_v3_delete_glossary
from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError, RetryError
from google.cloud.exceptions import NotFound

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


@backoff.on_exception(
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
backoff.expo, (DeadlineExceeded, GoogleAPICallError,
RetryError), max_time=60
)
@pytest.mark.flaky(max_runs=3, min_passes=1)
def test_create_glossary(capsys):
try:
Expand All @@ -36,9 +43,10 @@ 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
# cleanup
try:
translate_v3_delete_glossary.delete_glossary(
PROJECT_ID, glossary_id)
except Exception:
pass
except NotFound as e:
# Ignoring this case.
print("Got NotFound, detail: {}".format(str(e)))
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
14 changes: 12 additions & 2 deletions translate/cloud-client/translate_v3_get_glossary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import backoff
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
import os
import pytest
import translate_v3_create_glossary
import translate_v3_delete_glossary
import translate_v3_get_glossary
import uuid

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

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


@backoff.on_exception(
backoff.expo, (DeadlineExceeded, GoogleAPICallError,
RetryError), max_time=60
)
@pytest.fixture(scope="session")
def glossary():
"""Get the ID of a glossary available to session (do not mutate/delete)."""
Expand All @@ -33,10 +41,12 @@ def glossary():

yield glossary_id

# cleanup
try:
translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id)
except Exception:
pass
except NotFound as e:
# Ignoring this case.
print("Got NotFound, detail: {}".format(str(e)))


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
18 changes: 14 additions & 4 deletions translate/cloud-client/translate_v3_list_glossary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import backoff
import os
import pytest
import translate_v3_create_glossary
import translate_v3_delete_glossary
import translate_v3_list_glossary
import uuid

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

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


@backoff.on_exception(
backoff.expo, (DeadlineExceeded, GoogleAPICallError,
RetryError), max_time=60
)
@pytest.fixture(scope="session")
def glossary():
"""Get the ID of a glossary available to session (do not mutate/delete)."""
Expand All @@ -33,14 +41,16 @@ def glossary():

yield glossary_id

# clean up
try:
translate_v3_delete_glossary.sample_delete_glossary(PROJECT_ID, glossary_id)
except Exception:
pass
translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id)
except NotFound as e:
# Ignoring this case.
print("Got NotFound, detail: {}".format(str(e)))


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 @@ -13,17 +13,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import backoff
import os
import pytest
import translate_v3_create_glossary
import translate_v3_delete_glossary
import translate_v3_translate_text_with_glossary
import uuid

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

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


@backoff.on_exception(
backoff.expo, (DeadlineExceeded, GoogleAPICallError,
RetryError), max_time=60
)
@pytest.fixture(scope="session")
def glossary():
"""Get the ID of a glossary available to session (do not mutate/delete)."""
Expand All @@ -34,10 +42,12 @@ def glossary():

yield glossary_id

# cleanup
try:
translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id)
except Exception:
pass
except NotFound as e:
# Ignoring this case.
print("Got NotFound, detail: {}".format(str(e)))


def test_translate_text_with_glossary(capsys, glossary):
Expand Down