def test_with_create_cluster_metadata(self): from google.protobuf import any_pb2 from google.protobuf.timestamp_pb2 import Timestamp from gcloud.bigtable._generated import (bigtable_cluster_data_pb2 as data_pb2) from gcloud.bigtable._generated import ( bigtable_cluster_service_messages_pb2 as messages_pb2) type_url = ('type.googleapis.com/' + messages_pb2._CREATECLUSTERMETADATA.full_name) metadata = messages_pb2.CreateClusterMetadata( request_time=Timestamp(seconds=1, nanos=1234), finish_time=Timestamp(seconds=10, nanos=891011), original_request=messages_pb2.CreateClusterRequest( name='foo', cluster_id='bar', cluster=data_pb2.Cluster( display_name='quux', serve_nodes=1337, ), ), ) any_val = any_pb2.Any( type_url=type_url, value=metadata.SerializeToString(), ) result = self._callFUT(any_val) self.assertEqual(result, metadata)
def test_it(self): from google.longrunning import operations_pb2 from gcloud._testing import _Monkey from gcloud.bigtable._generated import ( bigtable_cluster_service_messages_pb2 as messages_pb2) from gcloud.bigtable import cluster as MUT project = 'PROJECT' zone = 'zone' cluster_id = 'cluster-id' expected_operation_id = 234 operation_name = ('operations/projects/%s/zones/%s/clusters/%s/' 'operations/%d' % (project, zone, cluster_id, expected_operation_id)) current_op = operations_pb2.Operation(name=operation_name) # Create mocks. request_metadata = messages_pb2.CreateClusterMetadata() parse_pb_any_called = [] def mock_parse_pb_any_to_native(any_val, expected_type=None): parse_pb_any_called.append((any_val, expected_type)) return request_metadata expected_operation_begin = object() ts_to_dt_called = [] def mock_pb_timestamp_to_datetime(timestamp): ts_to_dt_called.append(timestamp) return expected_operation_begin # Exectute method with mocks in place. with _Monkey(MUT, _parse_pb_any_to_native=mock_parse_pb_any_to_native, _pb_timestamp_to_datetime=mock_pb_timestamp_to_datetime): operation_id, operation_begin = self._callFUT(current_op) # Check outputs. self.assertEqual(operation_id, expected_operation_id) self.assertTrue(operation_begin is expected_operation_begin) # Check mocks were used correctly. self.assertEqual(parse_pb_any_called, [(current_op.metadata, None)]) self.assertEqual(ts_to_dt_called, [request_metadata.request_time])