Skip to content

Commit

Permalink
test: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crwilcox committed Mar 16, 2021
1 parent 9eea814 commit ed19cc2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
33 changes: 18 additions & 15 deletions google/cloud/bigtable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@
READ_ONLY_SCOPE = "https://www.googleapis.com/auth/bigtable.data.readonly"
"""Scope for reading table data."""


def _create_gapic_client(client_class, client_options=None, transport=None):
def inner(self):
return client_class(
credentials=None,
client_info=self._client_info,
client_options=client_options,
transport=transport,
)
credentials=None,
client_info=self._client_info,
client_options=client_options,
transport=transport,
)

return inner

Expand Down Expand Up @@ -206,11 +207,15 @@ def _emulator_channel(self, transport, options):
# Note: this code also exists in the firestore client.
if "GrpcAsyncIOTransport" in str(transport.__name__):
return grpc.aio.secure_channel(
self._emulator_host, self._local_composite_credentials(), options=options
self._emulator_host,
self._local_composite_credentials(),
options=options,
)
else:
return grpc.secure_channel(
self._emulator_host, self._local_composite_credentials(), options=options
self._emulator_host,
self._local_composite_credentials(),
options=options,
)

def _local_composite_credentials(self):
Expand Down Expand Up @@ -241,11 +246,11 @@ def _local_composite_credentials(self):

def _create_gapic_client_channel(self, client_class, grpc_transport):
options = {
"grpc.max_send_message_length": -1,
"grpc.max_receive_message_length": -1,
"grpc.keepalive_time_ms": 30000,
"grpc.keepalive_timeout_ms": 10000,
}.items()
"grpc.max_send_message_length": -1,
"grpc.max_receive_message_length": -1,
"grpc.keepalive_time_ms": 30000,
"grpc.keepalive_timeout_ms": 10000,
}.items()
if self._client_options and self._client_options.api_endpoint:
api_endpoint = self._client_options.api_endpoint
else:
Expand All @@ -257,9 +262,7 @@ def _create_gapic_client_channel(self, client_class, grpc_transport):
channel = self._emulator_channel(grpc_transport, options)
else:
channel = grpc_transport.create_channel(
host=api_endpoint,
credentials=self._credentials,
options=options,
host=api_endpoint, credentials=self._credentials, options=options,
)
transport = grpc_transport(channel=channel, host=api_endpoint)
return transport
Expand Down
42 changes: 25 additions & 17 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,24 @@ def test_w_emulator(self):
client_class = mock.Mock()
emulator_host = emulator_channel = object()
credentials = _make_credentials()

client_options = mock.Mock()
transport = mock.Mock()

client = _Client(
credentials, emulator_host=emulator_host, emulator_channel=emulator_channel
)
client_info = client._client_info = mock.Mock()

result = self._invoke_client_factory(client_class)(client)
result = self._invoke_client_factory(
client_class, client_options=client_options, transport=transport
)(client)

self.assertIs(result, client_class.return_value)
client_class.assert_called_once_with(
channel=client._emulator_channel, client_info=client_info
credentials=None,
client_info=client_info,
client_options=client_options,
transport=transport,
)


Expand Down Expand Up @@ -121,7 +129,6 @@ def test_constructor_defaults(self):
self.assertIs(client._client_info, _CLIENT_INFO)
self.assertIsNone(client._channel)
self.assertIsNone(client._emulator_host)
self.assertIsNone(client._emulator_channel)
self.assertEqual(client.SCOPE, (DATA_SCOPE,))

def test_constructor_explicit(self):
Expand Down Expand Up @@ -167,22 +174,23 @@ def test_constructor_with_emulator_host(self):

credentials = _make_credentials()
emulator_host = "localhost:8081"
with mock.patch("os.getenv") as getenv:
getenv.return_value = emulator_host
with mock.patch("grpc.insecure_channel") as factory:
getenv.return_value = emulator_host
with mock.patch("os.environ", {BIGTABLE_EMULATOR: emulator_host}):
with mock.patch("grpc.secure_channel") as factory:
client = self._make_one(project=self.PROJECT, credentials=credentials)
# don't test local_composite_credentials
client._local_composite_credentials = lambda: credentials
# channels are formed when needed, so access a client
# create a gapic channel
client.table_data_client

self.assertEqual(client._emulator_host, emulator_host)
self.assertIs(client._emulator_channel, factory.return_value)
factory.assert_called_once_with(
target=emulator_host,
options={
"grpc.keepalive_time_ms": 30000,
"grpc.keepalive_timeout_ms": 10000,
}.items(),
)
getenv.assert_called_once_with(BIGTABLE_EMULATOR)
options = {
"grpc.max_send_message_length": -1,
"grpc.max_receive_message_length": -1,
"grpc.keepalive_time_ms": 30000,
"grpc.keepalive_timeout_ms": 10000,
}.items()
factory.assert_called_once_with(emulator_host, credentials, options=options)

def test__get_scopes_default(self):
from google.cloud.bigtable.client import DATA_SCOPE
Expand Down

0 comments on commit ed19cc2

Please sign in to comment.