From 31c0fff7d421efbc335f7ae0433fe4d9cfdd2ebf Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Wed, 14 Nov 2018 22:01:48 -0500 Subject: [PATCH] Language: pick up fixes to GAPIC generator. (#6521) Closes #6500. Includes fixes from these PRs: - https://github.com/googleapis/gapic-generator/pull/2407 - https://github.com/googleapis/gapic-generator/pull/2396 --- .../gapic/language_service_client.py | 17 +++-- .../language_service_grpc_transport.py | 11 +++ .../gapic/language_service_client.py | 17 +++-- .../language_service_grpc_transport.py | 11 +++ language/synth.py | 2 + .../v1/test_system_language_service_v1.py | 9 +-- .../test_system_language_service_v1beta2.py | 31 +++++++++ .../v1/test_language_service_client_v1.py | 67 +++++++++++++++---- .../test_language_service_client_v1beta2.py | 67 +++++++++++++++---- 9 files changed, 190 insertions(+), 42 deletions(-) create mode 100644 language/tests/system/gapic/v1beta2/test_system_language_service_v1beta2.py diff --git a/language/google/cloud/language_v1/gapic/language_service_client.py b/language/google/cloud/language_v1/gapic/language_service_client.py index b4cdae03c39c7..e5daf10e6d877 100644 --- a/language/google/cloud/language_v1/gapic/language_service_client.py +++ b/language/google/cloud/language_v1/gapic/language_service_client.py @@ -73,7 +73,7 @@ def __init__(self, transport=None, channel=None, credentials=None, - client_config=language_service_client_config.config, + client_config=None, client_info=None): """Constructor. @@ -106,13 +106,20 @@ def __init__(self, your own client library. """ # Raise deprecation warnings for things we want to go away. - if client_config: - warnings.warn('The `client_config` argument is deprecated.', - PendingDeprecationWarning) + if client_config is not None: + warnings.warn( + 'The `client_config` argument is deprecated.', + PendingDeprecationWarning, + stacklevel=2) + else: + client_config = language_service_client_config.config + if channel: warnings.warn( 'The `channel` argument is deprecated; use ' - '`transport` instead.', PendingDeprecationWarning) + '`transport` instead.', + PendingDeprecationWarning, + stacklevel=2) # Instantiate the transport. # The transport is responsible for handling serialization and diff --git a/language/google/cloud/language_v1/gapic/transports/language_service_grpc_transport.py b/language/google/cloud/language_v1/gapic/transports/language_service_grpc_transport.py index 7f30da413473f..a6f62471a1cd8 100644 --- a/language/google/cloud/language_v1/gapic/transports/language_service_grpc_transport.py +++ b/language/google/cloud/language_v1/gapic/transports/language_service_grpc_transport.py @@ -62,6 +62,8 @@ def __init__(self, credentials=credentials, ) + self._channel = channel + # gRPC uses objects called "stubs" that are bound to the # channel and provide a basic method for each RPC. self._stubs = { @@ -92,6 +94,15 @@ def create_channel(cls, scopes=cls._OAUTH_SCOPES, ) + @property + def channel(self): + """The gRPC channel used by the transport. + + Returns: + grpc.Channel: A gRPC channel object. + """ + return self._channel + @property def analyze_sentiment(self): """Return the gRPC stub for {$apiMethod.name}. diff --git a/language/google/cloud/language_v1beta2/gapic/language_service_client.py b/language/google/cloud/language_v1beta2/gapic/language_service_client.py index 4bc82b53f3a9d..a5215cf38b0a1 100644 --- a/language/google/cloud/language_v1beta2/gapic/language_service_client.py +++ b/language/google/cloud/language_v1beta2/gapic/language_service_client.py @@ -73,7 +73,7 @@ def __init__(self, transport=None, channel=None, credentials=None, - client_config=language_service_client_config.config, + client_config=None, client_info=None): """Constructor. @@ -106,13 +106,20 @@ def __init__(self, your own client library. """ # Raise deprecation warnings for things we want to go away. - if client_config: - warnings.warn('The `client_config` argument is deprecated.', - PendingDeprecationWarning) + if client_config is not None: + warnings.warn( + 'The `client_config` argument is deprecated.', + PendingDeprecationWarning, + stacklevel=2) + else: + client_config = language_service_client_config.config + if channel: warnings.warn( 'The `channel` argument is deprecated; use ' - '`transport` instead.', PendingDeprecationWarning) + '`transport` instead.', + PendingDeprecationWarning, + stacklevel=2) # Instantiate the transport. # The transport is responsible for handling serialization and diff --git a/language/google/cloud/language_v1beta2/gapic/transports/language_service_grpc_transport.py b/language/google/cloud/language_v1beta2/gapic/transports/language_service_grpc_transport.py index 0cf6b652bd257..a8527b3a0a763 100644 --- a/language/google/cloud/language_v1beta2/gapic/transports/language_service_grpc_transport.py +++ b/language/google/cloud/language_v1beta2/gapic/transports/language_service_grpc_transport.py @@ -62,6 +62,8 @@ def __init__(self, credentials=credentials, ) + self._channel = channel + # gRPC uses objects called "stubs" that are bound to the # channel and provide a basic method for each RPC. self._stubs = { @@ -92,6 +94,15 @@ def create_channel(cls, scopes=cls._OAUTH_SCOPES, ) + @property + def channel(self): + """The gRPC channel used by the transport. + + Returns: + grpc.Channel: A gRPC channel object. + """ + return self._channel + @property def analyze_sentiment(self): """Return the gRPC stub for {$apiMethod.name}. diff --git a/language/synth.py b/language/synth.py index eab04478c63c0..1d1d307d24852 100644 --- a/language/synth.py +++ b/language/synth.py @@ -31,3 +31,5 @@ s.move(library / f'google/cloud/language_{version}/proto') s.move(library / f'google/cloud/language_{version}/gapic') + s.move(library / f'tests/unit/gapic/{version}') + s.move(library / f'tests/system/gapic/{version}') diff --git a/language/tests/system/gapic/v1/test_system_language_service_v1.py b/language/tests/system/gapic/v1/test_system_language_service_v1.py index 60a0214ccfcd4..cd0be245b24a5 100644 --- a/language/tests/system/gapic/v1/test_system_language_service_v1.py +++ b/language/tests/system/gapic/v1/test_system_language_service_v1.py @@ -1,10 +1,12 @@ -# Copyright 2017, Google LLC All rights reserved. +# -*- coding: utf-8 -*- +# +# Copyright 2018 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, @@ -13,14 +15,13 @@ # limitations under the License. import time -import unittest from google.cloud import language_v1 from google.cloud.language_v1 import enums from google.cloud.language_v1.proto import language_service_pb2 -class TestSystemLanguageService(unittest.TestCase): +class TestSystemLanguageService(object): def test_analyze_sentiment(self): client = language_v1.LanguageServiceClient() diff --git a/language/tests/system/gapic/v1beta2/test_system_language_service_v1beta2.py b/language/tests/system/gapic/v1beta2/test_system_language_service_v1beta2.py new file mode 100644 index 0000000000000..751b3a5680811 --- /dev/null +++ b/language/tests/system/gapic/v1beta2/test_system_language_service_v1beta2.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2018 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. + +import time + +from google.cloud import language_v1beta2 +from google.cloud.language_v1beta2 import enums +from google.cloud.language_v1beta2.proto import language_service_pb2 + + +class TestSystemLanguageService(object): + def test_analyze_sentiment(self): + + client = language_v1beta2.LanguageServiceClient() + content = 'Hello, world!' + type_ = enums.Document.Type.PLAIN_TEXT + document = {'content': content, 'type': type_} + response = client.analyze_sentiment(document) diff --git a/language/tests/unit/gapic/v1/test_language_service_client_v1.py b/language/tests/unit/gapic/v1/test_language_service_client_v1.py index 1ba0632b26b65..6d2da21a14ba6 100644 --- a/language/tests/unit/gapic/v1/test_language_service_client_v1.py +++ b/language/tests/unit/gapic/v1/test_language_service_client_v1.py @@ -1,10 +1,12 @@ -# Copyright 2017, Google LLC All rights reserved. +# -*- coding: utf-8 -*- +# +# Copyright 2018 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, @@ -13,6 +15,7 @@ # limitations under the License. """Unit tests.""" +import mock import pytest from google.cloud import language_v1 @@ -68,7 +71,10 @@ def test_analyze_sentiment(self): # Mock the API response channel = ChannelStub(responses=[expected_response]) - client = language_v1.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1.LanguageServiceClient() # Setup Request document = {} @@ -85,7 +91,10 @@ def test_analyze_sentiment(self): def test_analyze_sentiment_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) - client = language_v1.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1.LanguageServiceClient() # Setup request document = {} @@ -102,7 +111,10 @@ def test_analyze_entities(self): # Mock the API response channel = ChannelStub(responses=[expected_response]) - client = language_v1.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1.LanguageServiceClient() # Setup Request document = {} @@ -119,7 +131,10 @@ def test_analyze_entities(self): def test_analyze_entities_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) - client = language_v1.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1.LanguageServiceClient() # Setup request document = {} @@ -136,7 +151,10 @@ def test_analyze_entity_sentiment(self): # Mock the API response channel = ChannelStub(responses=[expected_response]) - client = language_v1.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1.LanguageServiceClient() # Setup Request document = {} @@ -153,7 +171,10 @@ def test_analyze_entity_sentiment(self): def test_analyze_entity_sentiment_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) - client = language_v1.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1.LanguageServiceClient() # Setup request document = {} @@ -170,7 +191,10 @@ def test_analyze_syntax(self): # Mock the API response channel = ChannelStub(responses=[expected_response]) - client = language_v1.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1.LanguageServiceClient() # Setup Request document = {} @@ -187,7 +211,10 @@ def test_analyze_syntax(self): def test_analyze_syntax_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) - client = language_v1.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1.LanguageServiceClient() # Setup request document = {} @@ -203,7 +230,10 @@ def test_classify_text(self): # Mock the API response channel = ChannelStub(responses=[expected_response]) - client = language_v1.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1.LanguageServiceClient() # Setup Request document = {} @@ -220,7 +250,10 @@ def test_classify_text(self): def test_classify_text_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) - client = language_v1.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1.LanguageServiceClient() # Setup request document = {} @@ -237,7 +270,10 @@ def test_annotate_text(self): # Mock the API response channel = ChannelStub(responses=[expected_response]) - client = language_v1.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1.LanguageServiceClient() # Setup Request document = {} @@ -255,7 +291,10 @@ def test_annotate_text(self): def test_annotate_text_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) - client = language_v1.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1.LanguageServiceClient() # Setup request document = {} diff --git a/language/tests/unit/gapic/v1beta2/test_language_service_client_v1beta2.py b/language/tests/unit/gapic/v1beta2/test_language_service_client_v1beta2.py index f8350319b62e0..920ee01b15b9c 100644 --- a/language/tests/unit/gapic/v1beta2/test_language_service_client_v1beta2.py +++ b/language/tests/unit/gapic/v1beta2/test_language_service_client_v1beta2.py @@ -1,10 +1,12 @@ -# Copyright 2017, Google LLC All rights reserved. +# -*- coding: utf-8 -*- +# +# Copyright 2018 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, @@ -13,6 +15,7 @@ # limitations under the License. """Unit tests.""" +import mock import pytest from google.cloud import language_v1beta2 @@ -68,7 +71,10 @@ def test_analyze_sentiment(self): # Mock the API response channel = ChannelStub(responses=[expected_response]) - client = language_v1beta2.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1beta2.LanguageServiceClient() # Setup Request document = {} @@ -85,7 +91,10 @@ def test_analyze_sentiment(self): def test_analyze_sentiment_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) - client = language_v1beta2.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1beta2.LanguageServiceClient() # Setup request document = {} @@ -102,7 +111,10 @@ def test_analyze_entities(self): # Mock the API response channel = ChannelStub(responses=[expected_response]) - client = language_v1beta2.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1beta2.LanguageServiceClient() # Setup Request document = {} @@ -119,7 +131,10 @@ def test_analyze_entities(self): def test_analyze_entities_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) - client = language_v1beta2.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1beta2.LanguageServiceClient() # Setup request document = {} @@ -136,7 +151,10 @@ def test_analyze_entity_sentiment(self): # Mock the API response channel = ChannelStub(responses=[expected_response]) - client = language_v1beta2.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1beta2.LanguageServiceClient() # Setup Request document = {} @@ -153,7 +171,10 @@ def test_analyze_entity_sentiment(self): def test_analyze_entity_sentiment_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) - client = language_v1beta2.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1beta2.LanguageServiceClient() # Setup request document = {} @@ -170,7 +191,10 @@ def test_analyze_syntax(self): # Mock the API response channel = ChannelStub(responses=[expected_response]) - client = language_v1beta2.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1beta2.LanguageServiceClient() # Setup Request document = {} @@ -187,7 +211,10 @@ def test_analyze_syntax(self): def test_analyze_syntax_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) - client = language_v1beta2.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1beta2.LanguageServiceClient() # Setup request document = {} @@ -203,7 +230,10 @@ def test_classify_text(self): # Mock the API response channel = ChannelStub(responses=[expected_response]) - client = language_v1beta2.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1beta2.LanguageServiceClient() # Setup Request document = {} @@ -220,7 +250,10 @@ def test_classify_text(self): def test_classify_text_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) - client = language_v1beta2.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1beta2.LanguageServiceClient() # Setup request document = {} @@ -237,7 +270,10 @@ def test_annotate_text(self): # Mock the API response channel = ChannelStub(responses=[expected_response]) - client = language_v1beta2.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1beta2.LanguageServiceClient() # Setup Request document = {} @@ -255,7 +291,10 @@ def test_annotate_text(self): def test_annotate_text_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) - client = language_v1beta2.LanguageServiceClient(channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = language_v1beta2.LanguageServiceClient() # Setup request document = {}