Skip to content

Commit

Permalink
Updating Client.copy() per review in main repo.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Nov 18, 2015
1 parent 621f6b7 commit ba61b11
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 60 deletions.
24 changes: 9 additions & 15 deletions gcloud_bigtable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,21 +344,15 @@ def copy(self):
:rtype: :class:`.Client`
:returns: A copy of the current client.
"""
data_stub = self._data_stub_internal
cluster_stub = self._cluster_stub_internal
operations_stub = self._operations_stub_internal
table_stub = self._table_stub_internal
try:
self._data_stub_internal = None
self._cluster_stub_internal = None
self._operations_stub_internal = None
self._table_stub_internal = None
return copy.deepcopy(self)
finally:
self._data_stub_internal = data_stub
self._cluster_stub_internal = cluster_stub
self._operations_stub_internal = operations_stub
self._table_stub_internal = table_stub
copied_creds = copy.deepcopy(self._credentials)
return self.__class__(
self.project,
copied_creds,
READ_ONLY_SCOPE in copied_creds.scopes,
self._admin,
self.user_agent,
self.timeout_seconds,
)

@property
def credentials(self):
Expand Down
66 changes: 21 additions & 45 deletions gcloud_bigtable/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,20 +373,28 @@ def test_from_service_account_p12(self):
# Load private key (via _get_contents) from the key path.
mock_get_contents.check_called(self, [(private_key_path,)])

def test_copy(self):
from gcloud_bigtable._testing import _MockWithAttachedMethods

def _copy_test_helper(self, read_only=False, admin=False):
class Credentials(object):

def __init__(self, value):
scopes = None

def __init__(self, value=None):
self.value = value

def create_scoped(self, scope):
self.scopes = scope
return self

def __eq__(self, other):
return self.value == other.value

scoped_creds = Credentials('value')
credentials = _MockWithAttachedMethods(scoped_creds)
client = self._makeOne(project=PROJECT, credentials=credentials)
credentials = Credentials('value')
timeout_seconds = 123
user_agent = 'you-sir-age-int'
client = self._makeOne(project=PROJECT, credentials=credentials,
read_only=read_only, admin=admin,
timeout_seconds=timeout_seconds,
user_agent=user_agent)
# Put some fake stubs in place so that we can verify they
# don't get copied.
client._data_stub_internal = object()
Expand All @@ -409,46 +417,14 @@ def __eq__(self, other):
self.assertEqual(new_client._operations_stub_internal, None)
self.assertEqual(new_client._table_stub_internal, None)

def test_copy_partial_failure(self):
from gcloud_bigtable._testing import _MockWithAttachedMethods
from gcloud_bigtable._testing import _Monkey
from gcloud_bigtable import client as MUT

captured_stubs = {}

def always_fail(client_to_copy):
captured_stubs['data_stub'] = client_to_copy._data_stub_internal
captured_stubs['cluster_stub'] = (
client_to_copy._cluster_stub_internal)
captured_stubs['operations_stub'] = (
client_to_copy._operations_stub_internal)
captured_stubs['table_stub'] = client_to_copy._table_stub_internal
raise ValueError('cannot copy', client_to_copy)
def test_copy(self):
self._copy_test_helper()

scoped_creds = object()
credentials = _MockWithAttachedMethods(scoped_creds)
client = self._makeOne(project=PROJECT, credentials=credentials)
def test_copy_admin(self):
self._copy_test_helper(admin=True)

client._data_stub_internal = data_stub = object()
client._cluster_stub_internal = cluster_stub = object()
client._operations_stub_internal = operations_stub = object()
client._table_stub_internal = table_stub = object()
with _Monkey(MUT.copy, deepcopy=always_fail):
with self.assertRaises(ValueError):
client.copy()

# Make sure none of the stubs were present in the deepcopy().
self.assertEqual(captured_stubs, {
'data_stub': None,
'cluster_stub': None,
'operations_stub': None,
'table_stub': None,
})
# Make sure **all** the stubs were restored after the failure.
self.assertEqual(client._data_stub_internal, data_stub)
self.assertEqual(client._cluster_stub_internal, cluster_stub)
self.assertEqual(client._operations_stub_internal, operations_stub)
self.assertEqual(client._table_stub_internal, table_stub)
def test_copy_read_only(self):
self._copy_test_helper(read_only=True)

def test_credentials_getter(self):
from gcloud_bigtable._testing import _MockWithAttachedMethods
Expand Down

0 comments on commit ba61b11

Please sign in to comment.