Skip to content

Commit b05e400

Browse files
munkhuushmgldanoscarmike
authored andcommitted
translate: fix glossary leak issue [(#3572)](GoogleCloudPlatform/python-docs-samples#3572)
* fix glossary leak issue * removed try/catch from teardown methods, removed sample_ prefix from all other methods * added specific exceptions to tests, added backoff tags to tests * fixed the lint issues * reordered imports * moved backoff inside methd and removed Retry * corrected import nit
1 parent 33b4f6e commit b05e400

14 files changed

+114
-38
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
backoff==1.10.0
12
flaky==3.6.1
2-
pytest==5.3.2
3+
pytest==5.3.2

packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_test.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313
# limitations under the License.
1414

1515
import os
16-
import pytest
17-
import translate_v3_batch_translate_text
1816
import uuid
17+
18+
import pytest
19+
1920
from google.cloud import storage
2021

22+
import translate_v3_batch_translate_text
23+
24+
2125
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
2226

2327

packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@
1313
# limitations under the License.
1414

1515
import os
16-
import pytest
1716
import uuid
17+
18+
import backoff
19+
import pytest
20+
21+
from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError
22+
from google.cloud.exceptions import NotFound
23+
from google.cloud import storage
24+
1825
import translate_v3_batch_translate_text_with_glossary
1926
import translate_v3_create_glossary
2027
import translate_v3_delete_glossary
21-
from google.cloud import storage
28+
2229

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

3542
yield glossary_id
3643

37-
try:
38-
translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id)
39-
except Exception:
40-
pass
44+
# cleanup
45+
@backoff.on_exception(
46+
backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
47+
)
48+
def delete_glossary():
49+
try:
50+
translate_v3_delete_glossary.delete_glossary(
51+
PROJECT_ID, glossary_id)
52+
except NotFound as e:
53+
# Ignoring this case.
54+
print("Got NotFound, detail: {}".format(str(e)))
55+
delete_glossary()
4156

4257

4358
@pytest.fixture(scope="function")

packages/google-cloud-translate/samples/snippets/translate_v3_create_glossary_test.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@
1515
import os
1616
import uuid
1717

18+
import backoff
1819
import pytest
1920

21+
from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError
22+
from google.cloud.exceptions import NotFound
23+
2024
import translate_v3_create_glossary
2125
import translate_v3_delete_glossary
2226

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

@@ -36,9 +41,15 @@ def test_create_glossary(capsys):
3641
assert "Created:" in out
3742
assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out
3843
finally:
39-
# clean up after use
40-
try:
41-
translate_v3_delete_glossary.delete_glossary(
42-
PROJECT_ID, glossary_id)
43-
except Exception:
44-
pass
44+
# cleanup
45+
@backoff.on_exception(
46+
backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
47+
)
48+
def delete_glossary():
49+
try:
50+
translate_v3_delete_glossary.delete_glossary(
51+
PROJECT_ID, glossary_id)
52+
except NotFound as e:
53+
# Ignoring this case.
54+
print("Got NotFound, detail: {}".format(str(e)))
55+
delete_glossary()

packages/google-cloud-translate/samples/snippets/translate_v3_detect_language.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from google.cloud import translate
1717

1818

19-
def sample_detect_language(project_id="YOUR_PROJECT_ID"):
19+
def detect_language(project_id="YOUR_PROJECT_ID"):
2020
"""Detecting the language of a text string."""
2121

2222
client = translate.TranslationServiceClient()

packages/google-cloud-translate/samples/snippets/translate_v3_detect_language_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020

2121
def test_detect_language(capsys):
22-
translate_v3_detect_language.sample_detect_language(PROJECT_ID)
22+
translate_v3_detect_language.detect_language(PROJECT_ID)
2323
out, _ = capsys.readouterr()
2424
assert "en" in out

packages/google-cloud-translate/samples/snippets/translate_v3_get_glossary_test.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@
1313
# limitations under the License.
1414

1515
import os
16+
import uuid
17+
18+
import backoff
1619
import pytest
20+
21+
from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError
22+
from google.cloud.exceptions import NotFound
23+
1724
import translate_v3_create_glossary
1825
import translate_v3_delete_glossary
1926
import translate_v3_get_glossary
20-
import uuid
27+
2128

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

3441
yield glossary_id
3542

36-
try:
37-
translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id)
38-
except Exception:
39-
pass
43+
# cleanup
44+
@backoff.on_exception(
45+
backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
46+
)
47+
def delete_glossary():
48+
try:
49+
translate_v3_delete_glossary.delete_glossary(
50+
PROJECT_ID, glossary_id)
51+
except NotFound as e:
52+
# Ignoring this case.
53+
print("Got NotFound, detail: {}".format(str(e)))
54+
delete_glossary()
4055

4156

4257
def test_get_glossary(capsys, glossary):

packages/google-cloud-translate/samples/snippets/translate_v3_get_supported_languages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from google.cloud import translate
1717

1818

19-
def sample_get_supported_languages(project_id="YOUR_PROJECT_ID"):
19+
def get_supported_languages(project_id="YOUR_PROJECT_ID"):
2020
"""Getting a list of supported language codes."""
2121

2222
client = translate.TranslationServiceClient()

packages/google-cloud-translate/samples/snippets/translate_v3_get_supported_languages_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020

2121
def test_list_languages(capsys):
22-
translate_v3_get_supported_languages.sample_get_supported_languages(PROJECT_ID)
22+
translate_v3_get_supported_languages.get_supported_languages(PROJECT_ID)
2323
out, _ = capsys.readouterr()
2424
assert "zh-CN" in out

packages/google-cloud-translate/samples/snippets/translate_v3_list_glossary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from google.cloud import translate
1717

1818

19-
def sample_list_glossaries(project_id="YOUR_PROJECT_ID"):
19+
def list_glossaries(project_id="YOUR_PROJECT_ID"):
2020
"""List Glossaries."""
2121

2222
client = translate.TranslationServiceClient()

packages/google-cloud-translate/samples/snippets/translate_v3_list_glossary_test.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@
1313
# limitations under the License.
1414

1515
import os
16+
import uuid
17+
18+
import backoff
1619
import pytest
20+
21+
from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError
22+
from google.cloud.exceptions import NotFound
23+
1724
import translate_v3_create_glossary
1825
import translate_v3_delete_glossary
1926
import translate_v3_list_glossary
20-
import uuid
27+
2128

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

3441
yield glossary_id
3542

36-
try:
37-
translate_v3_delete_glossary.sample_delete_glossary(PROJECT_ID, glossary_id)
38-
except Exception:
39-
pass
43+
# clean up
44+
@backoff.on_exception(
45+
backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
46+
)
47+
def delete_glossary():
48+
try:
49+
translate_v3_delete_glossary.delete_glossary(
50+
PROJECT_ID, glossary_id)
51+
except NotFound as e:
52+
# Ignoring this case.
53+
print("Got NotFound, detail: {}".format(str(e)))
54+
delete_glossary()
4055

4156

4257
def test_list_glossary(capsys, glossary):
43-
translate_v3_list_glossary.sample_list_glossaries(PROJECT_ID)
58+
translate_v3_list_glossary.list_glossaries(PROJECT_ID)
4459
out, _ = capsys.readouterr()
4560
assert glossary in out
4661
assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out

packages/google-cloud-translate/samples/snippets/translate_v3_translate_text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from google.cloud import translate
1717

1818

19-
def sample_translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"):
19+
def translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"):
2020
"""Translating Text."""
2121

2222
client = translate.TranslationServiceClient()

packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
def test_translate_text(capsys):
22-
translate_v3_translate_text.sample_translate_text(
22+
translate_v3_translate_text.translate_text(
2323
"Hello World!", PROJECT_ID)
2424
out, _ = capsys.readouterr()
2525
assert "Bonjour le monde" in out

packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_with_glossary_test.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@
1414
# limitations under the License.
1515

1616
import os
17+
import uuid
18+
19+
import backoff
1720
import pytest
21+
22+
from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError
23+
from google.cloud.exceptions import NotFound
24+
1825
import translate_v3_create_glossary
1926
import translate_v3_delete_glossary
2027
import translate_v3_translate_text_with_glossary
21-
import uuid
28+
2229

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

3542
yield glossary_id
3643

37-
try:
38-
translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id)
39-
except Exception:
40-
pass
44+
# cleanup
45+
@backoff.on_exception(
46+
backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60
47+
)
48+
def delete_glossary():
49+
try:
50+
translate_v3_delete_glossary.delete_glossary(
51+
PROJECT_ID, glossary_id)
52+
except NotFound as e:
53+
# Ignoring this case.
54+
print("Got NotFound, detail: {}".format(str(e)))
55+
delete_glossary()
4156

4257

4358
def test_translate_text_with_glossary(capsys, glossary):

0 commit comments

Comments
 (0)