From cb7e900ee2feb8bb01539536b563f929e22be031 Mon Sep 17 00:00:00 2001 From: Music Lee <635716260@qq.com> Date: Wed, 9 Oct 2019 12:22:35 -0700 Subject: [PATCH 1/3] test(translation): add VPC-SC system tests (#9272) --- tests/system/test_vpcsc.py | 166 +++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 tests/system/test_vpcsc.py diff --git a/tests/system/test_vpcsc.py b/tests/system/test_vpcsc.py new file mode 100644 index 00000000..427d0be6 --- /dev/null +++ b/tests/system/test_vpcsc.py @@ -0,0 +1,166 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2019 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Unit tests for VPC-SC.""" + +import os +import pytest + +from google.api_core import exceptions +from google.cloud import translate_v3beta1 + + +IS_INSIDE_VPCSC = "GOOGLE_CLOUD_TESTS_IN_VPCSC" in os.environ +# If IS_INSIDE_VPCSC is set, these environment variables should also be set +if IS_INSIDE_VPCSC: + PROJECT_INSIDE = os.environ["PROJECT_ID"] + PROJECT_OUTSIDE = os.environ["GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT"] + + +class TestVPCServiceControl(object): + @classmethod + def setup(self): + self._client = translate_v3beta1.TranslationServiceClient() + self._parent_inside = self._client.location_path(PROJECT_INSIDE, "us-central1") + self._parent_outside = self._client.location_path( + PROJECT_OUTSIDE, "us-central1" + ) + + def make_glossary_name(project_id): + return "projects/{0}/locations/us-central1/glossaries/fake_glossary".format( + project_id + ) + + self._glossary_name_inside = make_glossary_name(PROJECT_INSIDE) + self._glossary_name_outside = make_glossary_name(PROJECT_OUTSIDE) + + @staticmethod + def _is_rejected(call): + try: + responses = call() + print("responses: ", responses) + except exceptions.PermissionDenied as e: + print("PermissionDenied Exception: ", e) + return e.message == "Request is prohibited by organization's policy" + except Exception as e: + print("Other Exception: ", e) + pass + return False + + @staticmethod + def _do_test(delayed_inside, delayed_outside): + assert TestVPCServiceControl._is_rejected(delayed_outside) + assert not (TestVPCServiceControl._is_rejected(delayed_inside)) + + @pytest.mark.skipif( + not IS_INSIDE_VPCSC, + reason="This test must be run in VPCSC. To enable this test, set the environment variable GOOGLE_CLOUD_TESTS_IN_VPCSC to True", + ) + def test_create_glossary(self): + def make_glossary(project_id): + return { + "name": "projects/{0}/locations/us-central1/glossaries/fake_glossary".format( + project_id + ), + "language_codes_set": {"language_codes": ["en", "ja"]}, + "input_config": { + "gcs_source": {"input_uri": "gs://fake-bucket/fake_glossary.csv"} + }, + } + + glossary_inside = make_glossary(PROJECT_INSIDE) + + def delayed_inside(): + return self._client.create_glossary(self._parent_inside, glossary_inside) + + glossary_outside = make_glossary(PROJECT_OUTSIDE) + + def delayed_outside(): + return self._client.create_glossary(self._parent_outside, glossary_outside) + + TestVPCServiceControl._do_test(delayed_inside, delayed_outside) + + @pytest.mark.skipif( + not IS_INSIDE_VPCSC, + reason="This test must be run in VPCSC. To enable this test, set the environment variable GOOGLE_CLOUD_TESTS_IN_VPCSC to True", + ) + def test_list_glossaries(self): + # list_glossaries() returns an GRPCIterator instance, and we need to actually iterate through it + # by calling _next_page() to get real response. + def delayed_inside(): + return self._client.list_glossaries(self._parent_inside)._next_page() + + def delayed_outside(): + return self._client.list_glossaries(self._parent_outside)._next_page() + + TestVPCServiceControl._do_test(delayed_inside, delayed_outside) + + @pytest.mark.skipif( + not IS_INSIDE_VPCSC, + reason="This test must be run in VPCSC. To enable this test, set the environment variable GOOGLE_CLOUD_TESTS_IN_VPCSC to True", + ) + def test_get_glossary(self): + def delayed_inside(): + return self._client.get_glossary(self._glossary_name_inside) + + def delayed_outside(): + return self._client.get_glossary(self._glossary_name_outside) + + TestVPCServiceControl._do_test(delayed_inside, delayed_outside) + + @pytest.mark.skipif( + not IS_INSIDE_VPCSC, + reason="This test must be run in VPCSC. To enable this test, set the environment variable GOOGLE_CLOUD_TESTS_IN_VPCSC to True", + ) + def test_delete_glossary(self): + def delayed_inside(): + return self._client.delete_glossary(self._glossary_name_inside) + + def delayed_outside(): + return self._client.delete_glossary(self._glossary_name_outside) + + TestVPCServiceControl._do_test(delayed_inside, delayed_outside) + + @pytest.mark.skipif( + not IS_INSIDE_VPCSC, + reason="This test must be run in VPCSC. To enable this test, set the environment variable GOOGLE_CLOUD_TESTS_IN_VPCSC to True", + ) + def test_batch_translate_text(self): + source_language_code = "en" + target_language_codes = ["es"] + input_configs = [{"gcs_source": {"input_uri": "gs://fake-bucket/*"}}] + output_config = { + "gcs_destination": {"output_uri_prefix": "gs://fake-bucket/output/"} + } + + def delayed_inside(): + return self._client.batch_translate_text( + self._parent_inside, + source_language_code, + target_language_codes, + input_configs, + output_config, + ) + + def delayed_outside(): + return self._client.batch_translate_text( + self._parent_outside, + source_language_code, + target_language_codes, + input_configs, + output_config, + ) + + TestVPCServiceControl._do_test(delayed_inside, delayed_outside) From 89cfa0f23631f38d7f3560651c06bf90f174f8f2 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Date: Wed, 23 Oct 2019 10:56:33 -0700 Subject: [PATCH 2/3] feat(translate)!: make v3 the default client (#9498) --- google/cloud/translate.py | 19 ++++++++----------- synth.metadata | 10 +++++----- synth.py | 3 +++ tests/system.py | 4 ++-- tests/unit/__init__.py | 13 ------------- tests/unit/{ => v2}/test__http.py | 0 tests/unit/{ => v2}/test_client.py | 2 +- 7 files changed, 19 insertions(+), 32 deletions(-) delete mode 100644 tests/unit/__init__.py rename tests/unit/{ => v2}/test__http.py (100%) rename tests/unit/{ => v2}/test_client.py (99%) diff --git a/google/cloud/translate.py b/google/cloud/translate.py index 947f8c8a..27d23f13 100644 --- a/google/cloud/translate.py +++ b/google/cloud/translate.py @@ -1,10 +1,12 @@ -# Copyright 2017 Google LLC +# -*- coding: utf-8 -*- +# +# Copyright 2019 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, @@ -12,16 +14,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Google Cloud Translation API wrapper.""" - -from google.cloud.translate_v2 import __version__ -from google.cloud.translate_v2.client import Client +from __future__ import absolute_import -# These constants are essentially deprecated; strings should be used instead. -# They are imported here for backwards compatibility. -from google.cloud.translate_v2.client import BASE -from google.cloud.translate_v2.client import NMT +from google.cloud.translate_v3 import TranslationServiceClient +from google.cloud.translate_v3 import types -__all__ = ("__version__", "BASE", "Client", "NMT") +__all__ = ("types", "TranslationServiceClient") diff --git a/synth.metadata b/synth.metadata index fe995d41..6c4de912 100644 --- a/synth.metadata +++ b/synth.metadata @@ -1,19 +1,19 @@ { - "updateTime": "2019-10-05T12:42:06.391804Z", + "updateTime": "2019-10-18T22:49:41.466785Z", "sources": [ { "generator": { "name": "artman", - "version": "0.38.0", - "dockerImage": "googleapis/artman@sha256:0d2f8d429110aeb8d82df6550ef4ede59d40df9062d260a1580fce688b0512bf" + "version": "0.40.0", + "dockerImage": "googleapis/artman@sha256:fd2b49cce3d652929cc80157ec2d91bebe993f7cd4e89afaad80f9c785f8bf36" } }, { "git": { "name": "googleapis", "remote": "https://github.com/googleapis/googleapis.git", - "sha": "ceb8e2fb12f048cc94caae532ef0b4cf026a78f3", - "internalRef": "272971705" + "sha": "0e9a6d15fcb944ed40921ba0aad2082ee1bc7edd", + "internalRef": "275543900" } }, { diff --git a/synth.py b/synth.py index f9f3dbc9..14a2ad46 100644 --- a/synth.py +++ b/synth.py @@ -48,6 +48,9 @@ f"google.cloud.translate_{version}.proto", ) +# Use the highest version library to generate documentation import alias. +s.move(library / "google/cloud/translate.py") + s.replace( "google/cloud/**/translation_service_pb2.py", r"""record delimiters are ':raw-latex:`\\n`' instead of diff --git a/tests/system.py b/tests/system.py index c586411b..b92ead12 100644 --- a/tests/system.py +++ b/tests/system.py @@ -16,7 +16,7 @@ import unittest -from google.cloud import translate +from google.cloud import translate_v2 class Config(object): @@ -30,7 +30,7 @@ class Config(object): def setUpModule(): - Config.CLIENT = translate.Client() + Config.CLIENT = translate_v2.Client() class TestTranslate(unittest.TestCase): diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py deleted file mode 100644 index df379f1e..00000000 --- a/tests/unit/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -# Copyright 2016 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. diff --git a/tests/unit/test__http.py b/tests/unit/v2/test__http.py similarity index 100% rename from tests/unit/test__http.py rename to tests/unit/v2/test__http.py diff --git a/tests/unit/test_client.py b/tests/unit/v2/test_client.py similarity index 99% rename from tests/unit/test_client.py rename to tests/unit/v2/test_client.py index dfbc5aff..2c0f72b1 100644 --- a/tests/unit/test_client.py +++ b/tests/unit/v2/test_client.py @@ -18,7 +18,7 @@ class TestClient(unittest.TestCase): @staticmethod def _get_target_class(): - from google.cloud.translate import Client + from google.cloud.translate_v2 import Client return Client From 957565bf21d19094c39387d8fc8c60dd58de9da2 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Date: Thu, 24 Oct 2019 10:23:44 -0700 Subject: [PATCH 3/3] chore(translate): release 2.0.0 (#9527) --- CHANGELOG.md | 10 ++++++++++ setup.py | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 595e3a57..594d539a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ [1]: https://pypi.org/project/google-cloud-translate/#history +## 2.0.0 + +10-23-2019 11:13 PDT + +### New Features +- Make v3 the default client. ([#9498](https://github.com/googleapis/google-cloud-python/pull/9498)) + +### Internal / Testing Changes +- Add VPC-SC system tests. ([#9272](https://github.com/googleapis/google-cloud-python/pull/9272)) + ## 1.7.0 10-07-2019 14:57 PDT diff --git a/setup.py b/setup.py index 82c5b394..24d632a9 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ name = "google-cloud-translate" description = "Google Cloud Translation API client library" -version = "1.7.0" +version = "2.0.0" # Should be one of: # 'Development Status :: 3 - Alpha' # 'Development Status :: 4 - Beta'