diff --git a/videointelligence/google/cloud/videointelligence_v1/gapic/transports/video_intelligence_service_grpc_transport.py b/videointelligence/google/cloud/videointelligence_v1/gapic/transports/video_intelligence_service_grpc_transport.py index 48fa08b11c48..fbebbbe0cd19 100644 --- a/videointelligence/google/cloud/videointelligence_v1/gapic/transports/video_intelligence_service_grpc_transport.py +++ b/videointelligence/google/cloud/videointelligence_v1/gapic/transports/video_intelligence_service_grpc_transport.py @@ -63,6 +63,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 = { @@ -99,6 +101,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 annotate_video(self): """Return the gRPC stub for {$apiMethod.name}. diff --git a/videointelligence/google/cloud/videointelligence_v1/gapic/video_intelligence_service_client.py b/videointelligence/google/cloud/videointelligence_v1/gapic/video_intelligence_service_client.py index 8b5b2f413965..c8fe253312de 100644 --- a/videointelligence/google/cloud/videointelligence_v1/gapic/video_intelligence_service_client.py +++ b/videointelligence/google/cloud/videointelligence_v1/gapic/video_intelligence_service_client.py @@ -73,7 +73,7 @@ def __init__(self, transport=None, channel=None, credentials=None, - client_config=video_intelligence_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 = video_intelligence_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/videointelligence/google/cloud/videointelligence_v1beta1/gapic/transports/video_intelligence_service_grpc_transport.py b/videointelligence/google/cloud/videointelligence_v1beta1/gapic/transports/video_intelligence_service_grpc_transport.py index dbe0f3b93bcd..c1771121d0cf 100644 --- a/videointelligence/google/cloud/videointelligence_v1beta1/gapic/transports/video_intelligence_service_grpc_transport.py +++ b/videointelligence/google/cloud/videointelligence_v1beta1/gapic/transports/video_intelligence_service_grpc_transport.py @@ -63,6 +63,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 = { @@ -99,6 +101,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 annotate_video(self): """Return the gRPC stub for {$apiMethod.name}. diff --git a/videointelligence/google/cloud/videointelligence_v1beta1/gapic/video_intelligence_service_client.py b/videointelligence/google/cloud/videointelligence_v1beta1/gapic/video_intelligence_service_client.py index be8daf3b6faf..69d180976d34 100644 --- a/videointelligence/google/cloud/videointelligence_v1beta1/gapic/video_intelligence_service_client.py +++ b/videointelligence/google/cloud/videointelligence_v1beta1/gapic/video_intelligence_service_client.py @@ -73,7 +73,7 @@ def __init__(self, transport=None, channel=None, credentials=None, - client_config=video_intelligence_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 = video_intelligence_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/videointelligence/google/cloud/videointelligence_v1beta2/gapic/transports/video_intelligence_service_grpc_transport.py b/videointelligence/google/cloud/videointelligence_v1beta2/gapic/transports/video_intelligence_service_grpc_transport.py index e63606e6c153..51b38fe47bbf 100644 --- a/videointelligence/google/cloud/videointelligence_v1beta2/gapic/transports/video_intelligence_service_grpc_transport.py +++ b/videointelligence/google/cloud/videointelligence_v1beta2/gapic/transports/video_intelligence_service_grpc_transport.py @@ -63,6 +63,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 = { @@ -99,6 +101,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 annotate_video(self): """Return the gRPC stub for {$apiMethod.name}. diff --git a/videointelligence/google/cloud/videointelligence_v1beta2/gapic/video_intelligence_service_client.py b/videointelligence/google/cloud/videointelligence_v1beta2/gapic/video_intelligence_service_client.py index 5efdd5d181b8..de20c63a8043 100644 --- a/videointelligence/google/cloud/videointelligence_v1beta2/gapic/video_intelligence_service_client.py +++ b/videointelligence/google/cloud/videointelligence_v1beta2/gapic/video_intelligence_service_client.py @@ -73,7 +73,7 @@ def __init__(self, transport=None, channel=None, credentials=None, - client_config=video_intelligence_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 = video_intelligence_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/videointelligence/google/cloud/videointelligence_v1p1beta1/gapic/transports/video_intelligence_service_grpc_transport.py b/videointelligence/google/cloud/videointelligence_v1p1beta1/gapic/transports/video_intelligence_service_grpc_transport.py index aeb7118eaf99..f94a57974ebe 100644 --- a/videointelligence/google/cloud/videointelligence_v1p1beta1/gapic/transports/video_intelligence_service_grpc_transport.py +++ b/videointelligence/google/cloud/videointelligence_v1p1beta1/gapic/transports/video_intelligence_service_grpc_transport.py @@ -63,6 +63,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 = { @@ -99,6 +101,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 annotate_video(self): """Return the gRPC stub for {$apiMethod.name}. diff --git a/videointelligence/google/cloud/videointelligence_v1p1beta1/gapic/video_intelligence_service_client.py b/videointelligence/google/cloud/videointelligence_v1p1beta1/gapic/video_intelligence_service_client.py index dda81d1ac567..3baca53f5814 100644 --- a/videointelligence/google/cloud/videointelligence_v1p1beta1/gapic/video_intelligence_service_client.py +++ b/videointelligence/google/cloud/videointelligence_v1p1beta1/gapic/video_intelligence_service_client.py @@ -73,7 +73,7 @@ def __init__(self, transport=None, channel=None, credentials=None, - client_config=video_intelligence_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 = video_intelligence_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/videointelligence/google/cloud/videointelligence_v1p2beta1/gapic/transports/video_intelligence_service_grpc_transport.py b/videointelligence/google/cloud/videointelligence_v1p2beta1/gapic/transports/video_intelligence_service_grpc_transport.py index 3bf8d56c2afa..0575633e287a 100644 --- a/videointelligence/google/cloud/videointelligence_v1p2beta1/gapic/transports/video_intelligence_service_grpc_transport.py +++ b/videointelligence/google/cloud/videointelligence_v1p2beta1/gapic/transports/video_intelligence_service_grpc_transport.py @@ -63,6 +63,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 = { @@ -99,6 +101,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 annotate_video(self): """Return the gRPC stub for {$apiMethod.name}. diff --git a/videointelligence/google/cloud/videointelligence_v1p2beta1/gapic/video_intelligence_service_client.py b/videointelligence/google/cloud/videointelligence_v1p2beta1/gapic/video_intelligence_service_client.py index 958bb58f12f0..f26b4c1dc507 100644 --- a/videointelligence/google/cloud/videointelligence_v1p2beta1/gapic/video_intelligence_service_client.py +++ b/videointelligence/google/cloud/videointelligence_v1p2beta1/gapic/video_intelligence_service_client.py @@ -73,7 +73,7 @@ def __init__(self, transport=None, channel=None, credentials=None, - client_config=video_intelligence_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 = video_intelligence_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/videointelligence/synth.py b/videointelligence/synth.py index c4c907f8c003..b159b288d210 100644 --- a/videointelligence/synth.py +++ b/videointelligence/synth.py @@ -17,6 +17,7 @@ import synthtool as s from synthtool import gcp + gapic = gcp.GAPICGenerator() versions = ['v1beta1', 'v1beta2', 'v1p1beta1', 'v1p2beta1', 'v1'] @@ -32,15 +33,25 @@ s.move( library, excludes=[ - 'setup.py', 'README.rst', 'docs/index.rst', + 'setup.py', + 'nox*.py', + 'README.rst', + 'docs/index.rst', f'tests/system/gapic/{version}/' f'test_system_video_intelligence_service_{version}.py', - f'tests/unit/gapic/{version}/' - f'test_video_intelligence_service_client_{version}.py', - f'nox.py', + #f'tests/unit/gapic/{version}/' + #f'test_video_intelligence_service_client_{version}.py', ]) -s.replace("**/*/video_intelligence_service_client.py", - "'google-cloud-video-intelligence', \).version", - "'google-cloud-videointelligence', ).version") - +s.replace( + "**/*/video_intelligence_service_client.py", + "'google-cloud-video-intelligence', \).version", + "'google-cloud-videointelligence', ).version", +) + +s.replace( + "tests/unit/gapic/**/test_video_intelligence_service_client_*.py", + "^(\s+)expected_request = video_intelligence_pb2.AnnotateVideoRequest\(\)", + "\g<1>expected_request = video_intelligence_pb2.AnnotateVideoRequest(\n" + "\g<1> input_uri=input_uri, features=features)", +) diff --git a/videointelligence/tests/unit/gapic/v1/test_video_intelligence_service_client_v1.py b/videointelligence/tests/unit/gapic/v1/test_video_intelligence_service_client_v1.py index 7db83362710d..b7308cdd7b8e 100644 --- a/videointelligence/tests/unit/gapic/v1/test_video_intelligence_service_client_v1.py +++ b/videointelligence/tests/unit/gapic/v1/test_video_intelligence_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,11 +15,13 @@ # limitations under the License. """Unit tests.""" +import mock import pytest from google.rpc import status_pb2 from google.cloud import videointelligence_v1 +from google.cloud.videointelligence_v1 import enums from google.cloud.videointelligence_v1.proto import video_intelligence_pb2 from google.longrunning import operations_pb2 @@ -73,15 +77,24 @@ def test_annotate_video(self): # Mock the API response channel = ChannelStub(responses=[operation]) - client = videointelligence_v1.VideoIntelligenceServiceClient( - channel=channel) - - response = client.annotate_video() + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = videointelligence_v1.VideoIntelligenceServiceClient() + + # Setup Request + input_uri = 'gs://demomaker/cat.mp4' + features_element = enums.Feature.LABEL_DETECTION + features = [features_element] + + response = client.annotate_video( + input_uri=input_uri, features=features) result = response.result() assert expected_response == result assert len(channel.requests) == 1 - expected_request = video_intelligence_pb2.AnnotateVideoRequest() + expected_request = video_intelligence_pb2.AnnotateVideoRequest( + input_uri=input_uri, features=features) actual_request = channel.requests[0][1] assert expected_request == actual_request @@ -94,9 +107,17 @@ def test_annotate_video_exception(self): # Mock the API response channel = ChannelStub(responses=[operation]) - client = videointelligence_v1.VideoIntelligenceServiceClient( - channel=channel) - - response = client.annotate_video() + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = videointelligence_v1.VideoIntelligenceServiceClient() + + # Setup Request + input_uri = 'gs://demomaker/cat.mp4' + features_element = enums.Feature.LABEL_DETECTION + features = [features_element] + + response = client.annotate_video( + input_uri=input_uri, features=features) exception = response.exception() assert exception.errors[0] == error diff --git a/videointelligence/tests/unit/gapic/v1beta1/test_video_intelligence_service_client_v1beta1.py b/videointelligence/tests/unit/gapic/v1beta1/test_video_intelligence_service_client_v1beta1.py index 576fdf278cff..23a5c61f65aa 100644 --- a/videointelligence/tests/unit/gapic/v1beta1/test_video_intelligence_service_client_v1beta1.py +++ b/videointelligence/tests/unit/gapic/v1beta1/test_video_intelligence_service_client_v1beta1.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.rpc import status_pb2 @@ -74,8 +77,10 @@ def test_annotate_video(self): # Mock the API response channel = ChannelStub(responses=[operation]) - client = videointelligence_v1beta1.VideoIntelligenceServiceClient( - channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = videointelligence_v1beta1.VideoIntelligenceServiceClient() # Setup Request input_uri = 'gs://demomaker/cat.mp4' @@ -101,8 +106,10 @@ def test_annotate_video_exception(self): # Mock the API response channel = ChannelStub(responses=[operation]) - client = videointelligence_v1beta1.VideoIntelligenceServiceClient( - channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = videointelligence_v1beta1.VideoIntelligenceServiceClient() # Setup Request input_uri = 'gs://demomaker/cat.mp4' diff --git a/videointelligence/tests/unit/gapic/v1beta2/test_video_intelligence_service_client_v1beta2.py b/videointelligence/tests/unit/gapic/v1beta2/test_video_intelligence_service_client_v1beta2.py index d1d3ca7e82f5..aeaf9c799c23 100644 --- a/videointelligence/tests/unit/gapic/v1beta2/test_video_intelligence_service_client_v1beta2.py +++ b/videointelligence/tests/unit/gapic/v1beta2/test_video_intelligence_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,11 +15,13 @@ # limitations under the License. """Unit tests.""" +import mock import pytest from google.rpc import status_pb2 from google.cloud import videointelligence_v1beta2 +from google.cloud.videointelligence_v1beta2 import enums from google.cloud.videointelligence_v1beta2.proto import video_intelligence_pb2 from google.longrunning import operations_pb2 @@ -73,15 +77,24 @@ def test_annotate_video(self): # Mock the API response channel = ChannelStub(responses=[operation]) - client = videointelligence_v1beta2.VideoIntelligenceServiceClient( - channel=channel) - - response = client.annotate_video() + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = videointelligence_v1beta2.VideoIntelligenceServiceClient() + + # Setup Request + input_uri = 'gs://demomaker/cat.mp4' + features_element = enums.Feature.LABEL_DETECTION + features = [features_element] + + response = client.annotate_video( + input_uri=input_uri, features=features) result = response.result() assert expected_response == result assert len(channel.requests) == 1 - expected_request = video_intelligence_pb2.AnnotateVideoRequest() + expected_request = video_intelligence_pb2.AnnotateVideoRequest( + input_uri=input_uri, features=features) actual_request = channel.requests[0][1] assert expected_request == actual_request @@ -94,9 +107,17 @@ def test_annotate_video_exception(self): # Mock the API response channel = ChannelStub(responses=[operation]) - client = videointelligence_v1beta2.VideoIntelligenceServiceClient( - channel=channel) - - response = client.annotate_video() + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = videointelligence_v1beta2.VideoIntelligenceServiceClient() + + # Setup Request + input_uri = 'gs://demomaker/cat.mp4' + features_element = enums.Feature.LABEL_DETECTION + features = [features_element] + + response = client.annotate_video( + input_uri=input_uri, features=features) exception = response.exception() assert exception.errors[0] == error diff --git a/videointelligence/tests/unit/gapic/v1p1beta1/test_video_intelligence_service_client_v1p1beta1.py b/videointelligence/tests/unit/gapic/v1p1beta1/test_video_intelligence_service_client_v1p1beta1.py index e5dd239b8576..65f8d3813763 100644 --- a/videointelligence/tests/unit/gapic/v1p1beta1/test_video_intelligence_service_client_v1p1beta1.py +++ b/videointelligence/tests/unit/gapic/v1p1beta1/test_video_intelligence_service_client_v1p1beta1.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- +# # Copyright 2018 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,6 +15,7 @@ # limitations under the License. """Unit tests.""" +import mock import pytest from google.rpc import status_pb2 @@ -74,8 +77,11 @@ def test_annotate_video(self): # Mock the API response channel = ChannelStub(responses=[operation]) - client = videointelligence_v1p1beta1.VideoIntelligenceServiceClient( - channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = videointelligence_v1p1beta1.VideoIntelligenceServiceClient( + ) # Setup Request input_uri = 'gs://demomaker/cat.mp4' @@ -102,8 +108,11 @@ def test_annotate_video_exception(self): # Mock the API response channel = ChannelStub(responses=[operation]) - client = videointelligence_v1p1beta1.VideoIntelligenceServiceClient( - channel=channel) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = videointelligence_v1p1beta1.VideoIntelligenceServiceClient( + ) # Setup Request input_uri = 'gs://demomaker/cat.mp4' diff --git a/videointelligence/tests/unit/gapic/v1p2beta1/test_video_intelligence_service_client_v1p2beta1.py b/videointelligence/tests/unit/gapic/v1p2beta1/test_video_intelligence_service_client_v1p2beta1.py new file mode 100644 index 000000000000..9e6e9d0332d1 --- /dev/null +++ b/videointelligence/tests/unit/gapic/v1p2beta1/test_video_intelligence_service_client_v1p2beta1.py @@ -0,0 +1,125 @@ +# -*- 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. +"""Unit tests.""" + +import mock +import pytest + +from google.rpc import status_pb2 + +from google.cloud import videointelligence_v1p2beta1 +from google.cloud.videointelligence_v1p2beta1 import enums +from google.cloud.videointelligence_v1p2beta1.proto import video_intelligence_pb2 +from google.longrunning import operations_pb2 + + +class MultiCallableStub(object): + """Stub for the grpc.UnaryUnaryMultiCallable interface.""" + + def __init__(self, method, channel_stub): + self.method = method + self.channel_stub = channel_stub + + def __call__(self, request, timeout=None, metadata=None, credentials=None): + self.channel_stub.requests.append((self.method, request)) + + response = None + if self.channel_stub.responses: + response = self.channel_stub.responses.pop() + + if isinstance(response, Exception): + raise response + + if response: + return response + + +class ChannelStub(object): + """Stub for the grpc.Channel interface.""" + + def __init__(self, responses=[]): + self.responses = responses + self.requests = [] + + def unary_unary(self, + method, + request_serializer=None, + response_deserializer=None): + return MultiCallableStub(method, self) + + +class CustomException(Exception): + pass + + +class TestVideoIntelligenceServiceClient(object): + def test_annotate_video(self): + # Setup Expected Response + expected_response = {} + expected_response = video_intelligence_pb2.AnnotateVideoResponse( + **expected_response) + operation = operations_pb2.Operation( + name='operations/test_annotate_video', done=True) + operation.response.Pack(expected_response) + + # Mock the API response + channel = ChannelStub(responses=[operation]) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = videointelligence_v1p2beta1.VideoIntelligenceServiceClient( + ) + + # Setup Request + input_uri = 'gs://demomaker/cat.mp4' + features_element = enums.Feature.LABEL_DETECTION + features = [features_element] + + response = client.annotate_video( + input_uri=input_uri, features=features) + result = response.result() + assert expected_response == result + + assert len(channel.requests) == 1 + expected_request = video_intelligence_pb2.AnnotateVideoRequest( + input_uri=input_uri, features=features) + actual_request = channel.requests[0][1] + assert expected_request == actual_request + + def test_annotate_video_exception(self): + # Setup Response + error = status_pb2.Status() + operation = operations_pb2.Operation( + name='operations/test_annotate_video_exception', done=True) + operation.error.CopyFrom(error) + + # Mock the API response + channel = ChannelStub(responses=[operation]) + patch = mock.patch('google.api_core.grpc_helpers.create_channel') + with patch as create_channel: + create_channel.return_value = channel + client = videointelligence_v1p2beta1.VideoIntelligenceServiceClient( + ) + + # Setup Request + input_uri = 'gs://demomaker/cat.mp4' + features_element = enums.Feature.LABEL_DETECTION + features = [features_element] + + response = client.annotate_video( + input_uri=input_uri, features=features) + exception = response.exception() + assert exception.errors[0] == error