def _stop_method_helper(self, admin): from gcloud.bigtable._testing import _FakeStub credentials = _Credentials() project = 'PROJECT' client = self._makeOne(project=project, credentials=credentials, admin=admin) stub1 = _FakeStub() stub2 = _FakeStub() client._data_stub_internal = stub1 client._cluster_stub_internal = stub2 client._operations_stub_internal = stub2 client._table_stub_internal = stub2 client.stop() self.assertTrue(client._data_stub_internal is None) self.assertTrue(client._cluster_stub_internal is None) self.assertTrue(client._operations_stub_internal is None) self.assertTrue(client._table_stub_internal is None) self.assertEqual(stub1._entered, 0) self.assertEqual(stub2._entered, 0) exc_none_triple = (None, None, None) self.assertEqual(stub1._exited, [exc_none_triple]) if admin: self.assertEqual(stub2._exited, [exc_none_triple] * 3) else: self.assertEqual(stub2._exited, [])
def _stop_method_helper(self, admin): from gcloud.bigtable._testing import _FakeStub credentials = _Credentials() project = 'PROJECT' client = self._makeOne(project=project, credentials=credentials, admin=admin) stub1 = _FakeStub() stub2 = _FakeStub() client._data_stub_internal = stub1 client._instance_stub_internal = stub2 client._operations_stub_internal = stub2 client._table_stub_internal = stub2 client.stop() self.assertTrue(client._data_stub_internal is None) self.assertTrue(client._instance_stub_internal is None) self.assertTrue(client._operations_stub_internal is None) self.assertTrue(client._table_stub_internal is None) self.assertEqual(stub1._entered, 0) self.assertEqual(stub2._entered, 0) exc_none_triple = (None, None, None) self.assertEqual(stub1._exited, [exc_none_triple]) if admin: self.assertEqual(stub2._exited, [exc_none_triple] * 3) else: self.assertEqual(stub2._exited, [])
def test_update(self): from gcloud.bigtable._generated import ( instance_pb2 as data_v2_pb2) from gcloud.bigtable._testing import _FakeStub client = _Client(self.PROJECT, timeout_seconds=self.TIMEOUT_SECONDS) instance = self._makeOne(self.INSTANCE_ID, client, self.LOCATION_ID, display_name=self.DISPLAY_NAME) # Create request_pb request_pb = data_v2_pb2.Instance( name=self.INSTANCE_NAME, display_name=self.DISPLAY_NAME, ) # Create response_pb response_pb = data_v2_pb2.Instance() # Patch the stub used by the API method. client._instance_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # Perform the method and check the result. result = instance.update() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'UpdateInstance', (request_pb, self.TIMEOUT_SECONDS), {}, )])
def _list_tables_helper(self, table_id, table_name=None): from gcloud.bigtable._generated import bigtable_table_data_pb2 as table_data_pb2 from gcloud.bigtable._generated import bigtable_table_service_messages_pb2 as table_messages_pb2 from gcloud.bigtable._testing import _FakeStub project = "PROJECT" zone = "zone" cluster_id = "cluster-id" timeout_seconds = 45 client = _Client(project, timeout_seconds=timeout_seconds) cluster = self._makeOne(zone, cluster_id, client) # Create request_ cluster_name = "projects/" + project + "/zones/" + zone + "/clusters/" + cluster_id request_pb = table_messages_pb2.ListTablesRequest(name=cluster_name) # Create response_pb table_name = table_name or (cluster_name + "/tables/" + table_id) response_pb = table_messages_pb2.ListTablesResponse(tables=[table_data_pb2.Table(name=table_name)]) # Patch the stub used by the API method. client._table_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_table = cluster.table(table_id) expected_result = [expected_table] # Perform the method and check the result. result = cluster.list_tables() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [("ListTables", (request_pb, timeout_seconds), {})])
def test_delete(self): from google.protobuf import empty_pb2 from gcloud.bigtable._generated import ( bigtable_instance_admin_pb2 as messages_v2_pb) from gcloud.bigtable._testing import _FakeStub client = _Client(self.PROJECT, timeout_seconds=self.TIMEOUT_SECONDS) instance = self._makeOne(self.INSTANCE_ID, client, self.LOCATION_ID) # Create request_pb request_pb = messages_v2_pb.DeleteInstanceRequest( name=self.INSTANCE_NAME) # Create response_pb response_pb = empty_pb2.Empty() # Patch the stub used by the API method. client._instance_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # delete() has no return value. # Perform the method and check the result. result = instance.delete() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'DeleteInstance', (request_pb, self.TIMEOUT_SECONDS), {}, )])
def test_delete(self): from google.protobuf import empty_pb2 from gcloud.bigtable._generated import (bigtable_instance_admin_pb2 as messages_v2_pb) from gcloud.bigtable._testing import _FakeStub client = _Client(self.PROJECT, timeout_seconds=self.TIMEOUT_SECONDS) instance = self._makeOne(self.INSTANCE_ID, client, self.LOCATION_ID) # Create request_pb request_pb = messages_v2_pb.DeleteInstanceRequest( name=self.INSTANCE_NAME) # Create response_pb response_pb = empty_pb2.Empty() # Patch the stub used by the API method. client._instance_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # delete() has no return value. # Perform the method and check the result. result = instance.delete() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'DeleteInstance', (request_pb, self.TIMEOUT_SECONDS), {}, )])
def test_sample_row_keys(self): from gcloud.bigtable._testing import _FakeStub client = _Client(timeout_seconds=self.TIMEOUT_SECONDS) instance = _Instance(self.INSTANCE_NAME, client=client) table = self._makeOne(self.TABLE_ID, instance) # Create request_pb request_pb = _SampleRowKeysRequestPB(table_name=self.TABLE_NAME) # Create response_iterator response_iterator = object() # Just passed to a mock. # Patch the stub used by the API method. client._data_stub = stub = _FakeStub(response_iterator) # Create expected_result. expected_result = response_iterator # Perform the method and check the result. result = table.sample_row_keys() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'SampleRowKeys', (request_pb, self.TIMEOUT_SECONDS), {}, )])
def test_update(self): from google.longrunning import operations_pb2 from gcloud._testing import _Monkey from gcloud.bigtable._generated import ( bigtable_cluster_data_pb2 as data_pb2) from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable import cluster as MUT project = 'PROJECT' zone = 'zone' cluster_id = 'cluster-id' serve_nodes = 81 display_name = 'display_name' timeout_seconds = 9 client = _Client(project, timeout_seconds=timeout_seconds) cluster = self._makeOne(zone, cluster_id, client, display_name=display_name, serve_nodes=serve_nodes) # Create request_pb cluster_name = ('projects/' + project + '/zones/' + zone + '/clusters/' + cluster_id) request_pb = data_pb2.Cluster( name=cluster_name, display_name=display_name, serve_nodes=serve_nodes, ) # Create response_pb current_op = operations_pb2.Operation() response_pb = data_pb2.Cluster(current_operation=current_op) # Patch the stub used by the API method. client._cluster_stub = stub = _FakeStub(response_pb) # Create expected_result. op_id = 5678 op_begin = object() expected_result = MUT.Operation('update', op_id, op_begin, cluster=cluster) # Create mocks process_operation_called = [] def mock_process_operation(operation_pb): process_operation_called.append(operation_pb) return op_id, op_begin # Perform the method and check the result. with _Monkey(MUT, _process_operation=mock_process_operation): result = cluster.update() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'UpdateCluster', (request_pb, timeout_seconds), {}, )]) self.assertEqual(process_operation_called, [current_op])
def test_delete(self): from google.protobuf import empty_pb2 from gcloud.bigtable._generated import bigtable_cluster_service_messages_pb2 as messages_pb2 from gcloud.bigtable._testing import _FakeStub project = "PROJECT" zone = "zone" cluster_id = "cluster-id" timeout_seconds = 57 client = _Client(project, timeout_seconds=timeout_seconds) cluster = self._makeOne(zone, cluster_id, client) # Create request_pb cluster_name = "projects/" + project + "/zones/" + zone + "/clusters/" + cluster_id request_pb = messages_pb2.DeleteClusterRequest(name=cluster_name) # Create response_pb response_pb = empty_pb2.Empty() # Patch the stub used by the API method. client._cluster_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # delete() has no return value. # Perform the method and check the result. result = cluster.delete() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [("DeleteCluster", (request_pb, timeout_seconds), {})])
def _start_method_helper(self, admin): from gcloud._testing import _Monkey from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable import client as MUT credentials = _Credentials() project = 'PROJECT' client = self._makeOne(project=project, credentials=credentials, admin=admin) stub = _FakeStub() make_stub_args = [] def mock_make_stub(*args): make_stub_args.append(args) return stub with _Monkey(MUT, _make_stub=mock_make_stub): client.start() self.assertTrue(client._data_stub_internal is stub) if admin: self.assertTrue(client._instance_stub_internal is stub) self.assertTrue(client._operations_stub_internal is stub) self.assertTrue(client._table_stub_internal is stub) self.assertEqual(stub._entered, 4) self.assertEqual(len(make_stub_args), 4) else: self.assertTrue(client._instance_stub_internal is None) self.assertTrue(client._operations_stub_internal is None) self.assertTrue(client._table_stub_internal is None) self.assertEqual(stub._entered, 1) self.assertEqual(len(make_stub_args), 1) self.assertEqual(stub._exited, [])
def test_create(self): from gcloud._testing import _Monkey from gcloud.bigtable._generated import ( bigtable_cluster_data_pb2 as data_pb2) from gcloud.bigtable._generated import operations_pb2 from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable import cluster as MUT project = 'PROJECT' zone = 'zone' cluster_id = 'cluster-id' timeout_seconds = 578 client = _Client(project, timeout_seconds=timeout_seconds) cluster = self._makeOne(zone, cluster_id, client) # Create request_pb. Just a mock since we monkey patch # _prepare_create_request request_pb = object() # Create response_pb op_id = 5678 op_begin = object() op_name = ('operations/projects/%s/zones/%s/clusters/%s/' 'operations/%d' % (project, zone, cluster_id, op_id)) current_op = operations_pb2.Operation(name=op_name) response_pb = data_pb2.Cluster(current_operation=current_op) # Patch the stub used by the API method. client._cluster_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = MUT.Operation('create', op_id, op_begin) # Create the mocks. prep_create_called = [] def mock_prep_create_req(cluster): prep_create_called.append(cluster) return request_pb process_operation_called = [] def mock_process_operation(operation_pb): process_operation_called.append(operation_pb) return op_id, op_begin # Perform the method and check the result. with _Monkey(MUT, _prepare_create_request=mock_prep_create_req, _process_operation=mock_process_operation): result = cluster.create() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'CreateCluster', (request_pb, timeout_seconds), {}, )]) self.assertEqual(prep_create_called, [cluster]) self.assertEqual(process_operation_called, [current_op])
def test_update(self): from gcloud.bigtable._generated import (instance_pb2 as data_v2_pb2) from gcloud.bigtable._testing import _FakeStub client = _Client(self.PROJECT, timeout_seconds=self.TIMEOUT_SECONDS) instance = self._makeOne(self.INSTANCE_ID, client, self.LOCATION_ID, display_name=self.DISPLAY_NAME) # Create request_pb request_pb = data_v2_pb2.Instance( name=self.INSTANCE_NAME, display_name=self.DISPLAY_NAME, ) # Create response_pb response_pb = data_v2_pb2.Instance() # Patch the stub used by the API method. client._instance_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # Perform the method and check the result. result = instance.update() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'UpdateInstance', (request_pb, self.TIMEOUT_SECONDS), {}, )])
def _list_column_families_helper(self): from gcloud.bigtable._testing import _FakeStub client = _Client(timeout_seconds=self.TIMEOUT_SECONDS) instance = _Instance(self.INSTANCE_NAME, client=client) table = self._makeOne(self.TABLE_ID, instance) # Create request_pb request_pb = _GetTableRequestPB(name=self.TABLE_NAME) # Create response_pb COLUMN_FAMILY_ID = 'foo' column_family = _ColumnFamilyPB() response_pb = _TablePB( column_families={COLUMN_FAMILY_ID: column_family}, ) # Patch the stub used by the API method. client._table_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = { COLUMN_FAMILY_ID: table.column_family(COLUMN_FAMILY_ID), } # Perform the method and check the result. result = table.list_column_families() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'GetTable', (request_pb, self.TIMEOUT_SECONDS), {}, )])
def test_delete(self): from google.protobuf import empty_pb2 from gcloud.bigtable._testing import _FakeStub client = _Client(timeout_seconds=self.TIMEOUT_SECONDS) instance = _Instance(self.INSTANCE_NAME, client=client) table = self._makeOne(self.TABLE_ID, instance) # Create request_pb request_pb = _DeleteTableRequestPB(name=self.TABLE_NAME) # Create response_pb response_pb = empty_pb2.Empty() # Patch the stub used by the API method. client._table_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # delete() has no return value. # Perform the method and check the result. result = table.delete() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'DeleteTable', (request_pb, self.TIMEOUT_SECONDS), {}, )])
def test_delete(self): from google.protobuf import empty_pb2 from gcloud.bigtable._testing import _FakeStub client = _Client(self.PROJECT, timeout_seconds=self.TIMEOUT_SECONDS) instance = _Instance(self.INSTANCE_ID, client) cluster = self._makeOne(self.CLUSTER_ID, instance) # Create request_pb request_pb = _DeleteClusterRequestPB(name=self.CLUSTER_NAME) # Create response_pb response_pb = empty_pb2.Empty() # Patch the stub used by the API method. client._instance_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # delete() has no return value. # Perform the method and check the result. result = cluster.delete() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'DeleteCluster', (request_pb, self.TIMEOUT_SECONDS), {}, )])
def test_delete(self): from google.protobuf import empty_pb2 from gcloud.bigtable._testing import _FakeStub client = _Client(self.PROJECT) instance = _Instance(self.INSTANCE_ID, client) cluster = self._makeOne(self.CLUSTER_ID, instance) # Create request_pb request_pb = _DeleteClusterRequestPB(name=self.CLUSTER_NAME) # Create response_pb response_pb = empty_pb2.Empty() # Patch the stub used by the API method. client._instance_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # delete() has no return value. # Perform the method and check the result. result = cluster.delete() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'DeleteCluster', (request_pb, ), {}, )])
def _create_test_helper(self, initial_split_keys): from gcloud._helpers import _to_bytes from gcloud.bigtable._testing import _FakeStub client = _Client(timeout_seconds=self.TIMEOUT_SECONDS) instance = _Instance(self.INSTANCE_NAME, client=client) table = self._makeOne(self.TABLE_ID, instance) # Create request_pb splits_pb = [ _CreateTableRequestSplitPB(key=_to_bytes(key)) for key in initial_split_keys or ()] request_pb = _CreateTableRequestPB( initial_splits=splits_pb, parent=self.INSTANCE_NAME, table_id=self.TABLE_ID, ) # Create response_pb response_pb = _TablePB() # Patch the stub used by the API method. client._table_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # create() has no return value. # Perform the method and check the result. result = table.create(initial_split_keys=initial_split_keys) self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'CreateTable', (request_pb, self.TIMEOUT_SECONDS), {}, )])
def _start_method_helper(self, admin): from gcloud._testing import _Monkey from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable import client as MUT credentials = _Credentials() project = 'PROJECT' client = self._makeOne(project=project, credentials=credentials, admin=admin) stub = _FakeStub() make_stub_args = [] def mock_make_stub(*args): make_stub_args.append(args) return stub with _Monkey(MUT, _make_stub=mock_make_stub): client.start() self.assertTrue(client._data_stub_internal is stub) if admin: self.assertTrue(client._cluster_stub_internal is stub) self.assertTrue(client._operations_stub_internal is stub) self.assertTrue(client._table_stub_internal is stub) self.assertEqual(stub._entered, 4) self.assertEqual(len(make_stub_args), 4) else: self.assertTrue(client._cluster_stub_internal is None) self.assertTrue(client._operations_stub_internal is None) self.assertTrue(client._table_stub_internal is None) self.assertEqual(stub._entered, 1) self.assertEqual(len(make_stub_args), 1) self.assertEqual(stub._exited, [])
def test_update(self): from gcloud._testing import _Monkey from gcloud.bigtable._generated import ( bigtable_cluster_data_pb2 as data_pb2) from gcloud.bigtable._generated import operations_pb2 from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable import cluster as MUT project = 'PROJECT' zone = 'zone' cluster_id = 'cluster-id' serve_nodes = 81 display_name = 'display_name' timeout_seconds = 9 client = _Client(project, timeout_seconds=timeout_seconds) cluster = self._makeOne(zone, cluster_id, client, display_name=display_name, serve_nodes=serve_nodes) # Create request_pb cluster_name = ('projects/' + project + '/zones/' + zone + '/clusters/' + cluster_id) request_pb = data_pb2.Cluster( name=cluster_name, display_name=display_name, serve_nodes=serve_nodes, ) # Create response_pb current_op = operations_pb2.Operation() response_pb = data_pb2.Cluster(current_operation=current_op) # Patch the stub used by the API method. client._cluster_stub = stub = _FakeStub(response_pb) # Create expected_result. op_id = 5678 op_begin = object() expected_result = MUT.Operation('update', op_id, op_begin, cluster=cluster) # Create mocks process_operation_called = [] def mock_process_operation(operation_pb): process_operation_called.append(operation_pb) return op_id, op_begin # Perform the method and check the result. with _Monkey(MUT, _process_operation=mock_process_operation): result = cluster.update() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'UpdateCluster', (request_pb, timeout_seconds), {}, )]) self.assertEqual(process_operation_called, [current_op])
def test_create_w_explicit_serve_nodes(self): from google.longrunning import operations_pb2 from gcloud._testing import _Monkey from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable import instance as MUT SERVE_NODES = 5 client = _Client(self.PROJECT, timeout_seconds=self.TIMEOUT_SECONDS) instance = self._makeOne(self.INSTANCE_ID, client, self.LOCATION_ID, serve_nodes=SERVE_NODES) # Create request_pb. Just a mock since we monkey patch # _prepare_create_request request_pb = object() # Create response_pb OP_BEGIN = object() response_pb = operations_pb2.Operation(name=self.OP_NAME) # Patch the stub used by the API method. client._instance_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = MUT.Operation('create', self.OP_ID, OP_BEGIN, self.LOCATION_ID, instance=instance) # Create the mocks. prep_create_called = [] def mock_prep_create_req(instance): prep_create_called.append(instance) return request_pb process_operation_called = [] def mock_process_operation(operation_pb): process_operation_called.append(operation_pb) return self.OP_ID, self.LOCATION_ID, OP_BEGIN # Perform the method and check the result. with _Monkey(MUT, _prepare_create_request=mock_prep_create_req, _process_operation=mock_process_operation): result = instance.create() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'CreateInstance', (request_pb, self.TIMEOUT_SECONDS), {}, )]) self.assertEqual(prep_create_called, [instance]) self.assertEqual(process_operation_called, [response_pb])
def test_commit(self): from gcloud._testing import _Monkey from gcloud.bigtable._generated import bigtable_data_pb2 as data_pb2 from gcloud.bigtable._generated import ( bigtable_service_messages_pb2 as messages_pb2) from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable import row as MUT row_key = b'row_key' table_name = 'projects/more-stuff' column_family_id = u'column_family_id' column = b'column' timeout_seconds = 87 client = _Client(timeout_seconds=timeout_seconds) table = _Table(table_name, client=client) row = self._makeOne(row_key, table) # Create request_pb value = b'bytes-value' # We will call row.append_cell_value(COLUMN_FAMILY_ID, COLUMN, value). request_pb = messages_pb2.ReadModifyWriteRowRequest( table_name=table_name, row_key=row_key, rules=[ data_pb2.ReadModifyWriteRule( family_name=column_family_id, column_qualifier=column, append_value=value, ), ], ) # Create response_pb response_pb = object() # Patch the stub used by the API method. client._data_stub = stub = _FakeStub(response_pb) # Create expected_result. row_responses = [] expected_result = object() def mock_parse_rmw_row_response(row_response): row_responses.append(row_response) return expected_result # Perform the method and check the result. with _Monkey(MUT, _parse_rmw_row_response=mock_parse_rmw_row_response): row.append_cell_value(column_family_id, column, value) result = row.commit() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'ReadModifyWriteRow', (request_pb, timeout_seconds), {}, )]) self.assertEqual(row_responses, [response_pb]) self.assertEqual(row._rule_pb_list, [])
def test_read_rows(self): from gcloud._testing import _Monkey from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable.row_data import PartialRowsData from gcloud.bigtable import table as MUT project_id = 'project-id' zone = 'zone' cluster_id = 'cluster-id' table_id = 'table-id' timeout_seconds = 1111 client = _Client(timeout_seconds=timeout_seconds) cluster_name = ('projects/' + project_id + '/zones/' + zone + '/clusters/' + cluster_id) cluster = _Cluster(cluster_name, client=client) table = self._makeOne(table_id, cluster) # Create request_pb request_pb = object() # Returned by our mock. mock_created = [] def mock_create_row_request(table_name, **kwargs): mock_created.append((table_name, kwargs)) return request_pb # Create response_iterator response_iterator = object() # Patch the stub used by the API method. client._data_stub = stub = _FakeStub(response_iterator) # Create expected_result. expected_result = PartialRowsData(response_iterator) # Perform the method and check the result. start_key = b'start-key' end_key = b'end-key' filter_obj = object() allow_row_interleaving = True limit = 22 with _Monkey(MUT, _create_row_request=mock_create_row_request): result = table.read_rows( start_key=start_key, end_key=end_key, filter_=filter_obj, allow_row_interleaving=allow_row_interleaving, limit=limit) self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'ReadRows', (request_pb, timeout_seconds), {}, )]) created_kwargs = { 'start_key': start_key, 'end_key': end_key, 'filter_': filter_obj, 'allow_row_interleaving': allow_row_interleaving, 'limit': limit, } self.assertEqual(mock_created, [(table.name, created_kwargs)])
def _read_row_helper(self, chunks): from gcloud._testing import _Monkey from gcloud.bigtable._generated import ( bigtable_service_messages_pb2 as messages_pb2) from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable.row_data import PartialRowData from gcloud.bigtable import table as MUT project_id = 'project-id' zone = 'zone' cluster_id = 'cluster-id' table_id = 'table-id' timeout_seconds = 596 client = _Client(timeout_seconds=timeout_seconds) cluster_name = ('projects/' + project_id + '/zones/' + zone + '/clusters/' + cluster_id) cluster = _Cluster(cluster_name, client=client) table = self._makeOne(table_id, cluster) # Create request_pb request_pb = object() # Returned by our mock. mock_created = [] def mock_create_row_request(table_name, row_key, filter_): mock_created.append((table_name, row_key, filter_)) return request_pb # Create response_iterator row_key = b'row-key' response_pb = messages_pb2.ReadRowsResponse(row_key=row_key, chunks=chunks) response_iterator = [response_pb] # Patch the stub used by the API method. client._data_stub = stub = _FakeStub(response_iterator) # Create expected_result. if chunks: expected_result = PartialRowData(row_key) expected_result._committed = True expected_result._chunks_encountered = True else: expected_result = None # Perform the method and check the result. filter_obj = object() with _Monkey(MUT, _create_row_request=mock_create_row_request): result = table.read_row(row_key, filter_=filter_obj) self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'ReadRows', (request_pb, timeout_seconds), {}, )]) self.assertEqual(mock_created, [(table.name, row_key, filter_obj)])
def test_list_clusters(self): from gcloud.bigtable._generated import ( instance_pb2 as instance_v2_pb2) from gcloud.bigtable._generated import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from gcloud.bigtable._testing import _FakeStub FAILED_LOCATION = 'FAILED' FAILED_LOCATIONS = [FAILED_LOCATION] CLUSTER_ID1 = 'cluster-id1' CLUSTER_ID2 = 'cluster-id2' SERVE_NODES = 4 client = _Client(self.PROJECT) instance = self._makeOne(self.INSTANCE_ID, client, self.LOCATION_ID) CLUSTER_NAME1 = (instance.name + '/clusters/' + CLUSTER_ID1) CLUSTER_NAME2 = (instance.name + '/clusters/' + CLUSTER_ID2) # Create request_pb request_pb = messages_v2_pb2.ListClustersRequest( parent=instance.name, ) # Create response_pb response_pb = messages_v2_pb2.ListClustersResponse( failed_locations=[FAILED_LOCATION], clusters=[ instance_v2_pb2.Cluster( name=CLUSTER_NAME1, serve_nodes=SERVE_NODES, ), instance_v2_pb2.Cluster( name=CLUSTER_NAME2, serve_nodes=SERVE_NODES, ), ], ) # Patch the stub used by the API method. client._instance_stub = stub = _FakeStub(response_pb) # Create expected_result. clusters = [ instance.cluster(CLUSTER_ID1), instance.cluster(CLUSTER_ID2), ] expected_result = (clusters, FAILED_LOCATIONS) # Perform the method and check the result. result = instance.list_clusters() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'ListClusters', (request_pb,), {}, )])
def test_list_clusters(self): from gcloud.bigtable._generated import ( instance_pb2 as instance_v2_pb2) from gcloud.bigtable._generated import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from gcloud.bigtable._testing import _FakeStub FAILED_LOCATION = 'FAILED' FAILED_LOCATIONS = [FAILED_LOCATION] CLUSTER_ID1 = 'cluster-id1' CLUSTER_ID2 = 'cluster-id2' SERVE_NODES = 4 client = _Client(self.PROJECT, timeout_seconds=self.TIMEOUT_SECONDS) instance = self._makeOne(self.INSTANCE_ID, client, self.LOCATION_ID) CLUSTER_NAME1 = (instance.name + '/clusters/' + CLUSTER_ID1) CLUSTER_NAME2 = (instance.name + '/clusters/' + CLUSTER_ID2) # Create request_pb request_pb = messages_v2_pb2.ListClustersRequest( parent=instance.name, ) # Create response_pb response_pb = messages_v2_pb2.ListClustersResponse( failed_locations=[FAILED_LOCATION], clusters=[ instance_v2_pb2.Cluster( name=CLUSTER_NAME1, serve_nodes=SERVE_NODES, ), instance_v2_pb2.Cluster( name=CLUSTER_NAME2, serve_nodes=SERVE_NODES, ), ], ) # Patch the stub used by the API method. client._instance_stub = stub = _FakeStub(response_pb) # Create expected_result. clusters = [ instance.cluster(CLUSTER_ID1), instance.cluster(CLUSTER_ID2), ] expected_result = (clusters, FAILED_LOCATIONS) # Perform the method and check the result. result = instance.list_clusters() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'ListClusters', (request_pb, self.TIMEOUT_SECONDS), {}, )])
def test_create(self): import datetime from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any from gcloud.bigtable._generated import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from gcloud._helpers import _datetime_to_pb_timestamp from gcloud.bigtable._testing import _FakeStub from gcloud.operation import Operation from gcloud.bigtable.cluster import DEFAULT_SERVE_NODES from gcloud.bigtable.instance import _CREATE_INSTANCE_METADATA_URL NOW = datetime.datetime.utcnow() NOW_PB = _datetime_to_pb_timestamp(NOW) client = _Client(self.PROJECT) instance = self._makeOne(self.INSTANCE_ID, client, self.LOCATION_ID, display_name=self.DISPLAY_NAME) # Create response_pb metadata = messages_v2_pb2.CreateInstanceMetadata(request_time=NOW_PB) response_pb = operations_pb2.Operation( name=self.OP_NAME, metadata=Any( type_url=_CREATE_INSTANCE_METADATA_URL, value=metadata.SerializeToString(), ) ) # Patch the stub used by the API method. client._instance_stub = stub = _FakeStub(response_pb) # Perform the method and check the result. result = instance.create() self.assertTrue(isinstance(result, Operation)) self.assertEqual(result.name, self.OP_NAME) self.assertTrue(result.target is instance) self.assertTrue(result.client is client) self.assertIsInstance(result.pb_metadata, messages_v2_pb2.CreateInstanceMetadata) self.assertEqual(result.pb_metadata.request_time, NOW_PB) self.assertEqual(result.metadata, {'request_type': 'CreateInstance'}) self.assertEqual(len(stub.method_calls), 1) api_name, args, kwargs = stub.method_calls[0] self.assertEqual(api_name, 'CreateInstance') request_pb, = args self.assertTrue( isinstance(request_pb, messages_v2_pb2.CreateInstanceRequest)) self.assertEqual(request_pb.parent, 'projects/%s' % (self.PROJECT,)) self.assertEqual(request_pb.instance_id, self.INSTANCE_ID) self.assertEqual(request_pb.instance.display_name, self.DISPLAY_NAME) cluster = request_pb.clusters[self.INSTANCE_ID] self.assertEqual(cluster.serve_nodes, DEFAULT_SERVE_NODES) self.assertEqual(kwargs, {})
def test_create(self): from google.longrunning import operations_pb2 from gcloud._testing import _Monkey from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable import cluster as MUT client = _Client(self.PROJECT, timeout_seconds=self.TIMEOUT_SECONDS) instance = _Instance(self.INSTANCE_ID, client) cluster = self._makeOne(self.CLUSTER_ID, instance) # Create request_pb. Just a mock since we monkey patch # _prepare_create_request request_pb = object() # Create response_pb OP_ID = 5678 OP_NAME = ( 'operations/projects/%s/instances/%s/clusters/%s/operations/%d' % (self.PROJECT, self.INSTANCE_ID, self.CLUSTER_ID, OP_ID)) response_pb = operations_pb2.Operation(name=OP_NAME) # Patch the stub used by the API method. client._instance_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = MUT.Operation('create', OP_ID, cluster=cluster) # Create the mocks. prep_create_called = [] def mock_prep_create_req(cluster): prep_create_called.append(cluster) return request_pb process_operation_called = [] def mock_process_operation(operation_pb): process_operation_called.append(operation_pb) return OP_ID # Perform the method and check the result. with _Monkey(MUT, _prepare_create_request=mock_prep_create_req, _process_operation=mock_process_operation): result = cluster.create() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'CreateCluster', (request_pb, self.TIMEOUT_SECONDS), {}, )]) self.assertEqual(prep_create_called, [cluster]) self.assertEqual(process_operation_called, [response_pb])
def test_undelete(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._testing import _FakeStub from gcloud.bigtable import cluster as MUT project = 'PROJECT' zone = 'zone' cluster_id = 'cluster-id' timeout_seconds = 78 client = _Client(project, timeout_seconds=timeout_seconds) cluster = self._makeOne(zone, cluster_id, client) # Create request_pb cluster_name = ('projects/' + project + '/zones/' + zone + '/clusters/' + cluster_id) request_pb = messages_pb2.UndeleteClusterRequest(name=cluster_name) # Create response_pb response_pb = operations_pb2.Operation() # Patch the stub used by the API method. client._cluster_stub = stub = _FakeStub(response_pb) # Create expected_result. op_id = 5678 op_begin = object() expected_result = MUT.Operation('undelete', op_id, op_begin, cluster=cluster) # Create the mocks. process_operation_called = [] def mock_process_operation(operation_pb): process_operation_called.append(operation_pb) return op_id, op_begin # Perform the method and check the result. with _Monkey(MUT, _process_operation=mock_process_operation): result = cluster.undelete() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'UndeleteCluster', (request_pb, timeout_seconds), {}, )]) self.assertEqual(process_operation_called, [response_pb])
def test_commit(self): from google.protobuf import empty_pb2 from gcloud.bigtable._generated import bigtable_data_pb2 as data_pb2 from gcloud.bigtable._generated import ( bigtable_service_messages_pb2 as messages_pb2) from gcloud.bigtable._testing import _FakeStub row_key = b'row_key' table_name = 'projects/more-stuff' column_family_id = u'column_family_id' column = b'column' timeout_seconds = 711 client = _Client(timeout_seconds=timeout_seconds) table = _Table(table_name, client=client) row = self._makeOne(row_key, table) # Create request_pb value = b'bytes-value' mutation = data_pb2.Mutation( set_cell=data_pb2.Mutation.SetCell( family_name=column_family_id, column_qualifier=column, timestamp_micros=-1, # Default value. value=value, ), ) request_pb = messages_pb2.MutateRowRequest( table_name=table_name, row_key=row_key, mutations=[mutation], ) # Create response_pb response_pb = empty_pb2.Empty() # Patch the stub used by the API method. client._data_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # commit() has no return value when no filter. # Perform the method and check the result. row.set_cell(column_family_id, column, value) result = row.commit() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'MutateRow', (request_pb, timeout_seconds), {}, )]) self.assertEqual(row._pb_mutations, [])
def test_reload(self): 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) from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable.cluster import _DEFAULT_SERVE_NODES project = 'PROJECT' zone = 'zone' cluster_id = 'cluster-id' timeout_seconds = 123 client = _Client(project, timeout_seconds=timeout_seconds) cluster = self._makeOne(zone, cluster_id, client) # Create request_pb cluster_name = ('projects/' + project + '/zones/' + zone + '/clusters/' + cluster_id) request_pb = messages_pb2.GetClusterRequest(name=cluster_name) # Create response_pb serve_nodes = 31 display_name = u'hey-hi-hello' response_pb = data_pb2.Cluster( display_name=display_name, serve_nodes=serve_nodes, ) # Patch the stub used by the API method. client._cluster_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # reload() has no return value. # Check Cluster optional config values before. self.assertEqual(cluster.serve_nodes, _DEFAULT_SERVE_NODES) self.assertEqual(cluster.display_name, cluster_id) # Perform the method and check the result. result = cluster.reload() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'GetCluster', (request_pb, timeout_seconds), {}, )]) # Check Cluster optional config values before. self.assertEqual(cluster.serve_nodes, serve_nodes) self.assertEqual(cluster.display_name, display_name)
def test_read_rows(self): from gcloud._testing import _Monkey from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable.row_data import PartialRowsData from gcloud.bigtable import table as MUT client = _Client(timeout_seconds=self.TIMEOUT_SECONDS) instance = _Instance(self.INSTANCE_NAME, client=client) table = self._makeOne(self.TABLE_ID, instance) # Create request_pb request_pb = object() # Returned by our mock. mock_created = [] def mock_create_row_request(table_name, **kwargs): mock_created.append((table_name, kwargs)) return request_pb # Create response_iterator response_iterator = object() # Patch the stub used by the API method. client._data_stub = stub = _FakeStub(response_iterator) # Create expected_result. expected_result = PartialRowsData(response_iterator) # Perform the method and check the result. start_key = b'start-key' end_key = b'end-key' filter_obj = object() limit = 22 with _Monkey(MUT, _create_row_request=mock_create_row_request): result = table.read_rows(start_key=start_key, end_key=end_key, filter_=filter_obj, limit=limit) self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'ReadRows', (request_pb, self.TIMEOUT_SECONDS), {}, )]) created_kwargs = { 'start_key': start_key, 'end_key': end_key, 'filter_': filter_obj, 'limit': limit, } self.assertEqual(mock_created, [(table.name, created_kwargs)])
def test_reload(self): 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) from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable.cluster import DEFAULT_SERVE_NODES project = 'PROJECT' zone = 'zone' cluster_id = 'cluster-id' timeout_seconds = 123 client = _Client(project, timeout_seconds=timeout_seconds) cluster = self._makeOne(zone, cluster_id, client) # Create request_pb cluster_name = ('projects/' + project + '/zones/' + zone + '/clusters/' + cluster_id) request_pb = messages_pb2.GetClusterRequest(name=cluster_name) # Create response_pb serve_nodes = 31 display_name = u'hey-hi-hello' response_pb = data_pb2.Cluster( display_name=display_name, serve_nodes=serve_nodes, ) # Patch the stub used by the API method. client._cluster_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # reload() has no return value. # Check Cluster optional config values before. self.assertEqual(cluster.serve_nodes, DEFAULT_SERVE_NODES) self.assertEqual(cluster.display_name, cluster_id) # Perform the method and check the result. result = cluster.reload() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'GetCluster', (request_pb, timeout_seconds), {}, )]) # Check Cluster optional config values before. self.assertEqual(cluster.serve_nodes, serve_nodes) self.assertEqual(cluster.display_name, display_name)
def _create_test_helper(self, gc_rule=None): from gcloud.bigtable._generated import ( bigtable_table_data_pb2 as data_pb2) from gcloud.bigtable._generated import ( bigtable_table_service_messages_pb2 as messages_pb2) from gcloud.bigtable._testing import _FakeStub project_id = 'project-id' zone = 'zone' cluster_id = 'cluster-id' table_id = 'table-id' column_family_id = 'column-family-id' timeout_seconds = 4 table_name = ('projects/' + project_id + '/zones/' + zone + '/clusters/' + cluster_id + '/tables/' + table_id) client = _Client(timeout_seconds=timeout_seconds) table = _Table(table_name, client=client) column_family = self._makeOne(column_family_id, table, gc_rule=gc_rule) # Create request_pb if gc_rule is None: column_family_pb = data_pb2.ColumnFamily() else: column_family_pb = data_pb2.ColumnFamily(gc_rule=gc_rule.to_pb()) request_pb = messages_pb2.CreateColumnFamilyRequest( name=table_name, column_family_id=column_family_id, column_family=column_family_pb, ) # Create response_pb response_pb = data_pb2.ColumnFamily() # Patch the stub used by the API method. client._table_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # create() has no return value. # Perform the method and check the result. self.assertEqual(stub.results, (response_pb,)) result = column_family.create() self.assertEqual(stub.results, ()) self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'CreateColumnFamily', (request_pb, timeout_seconds), {}, )])
def _update_test_helper(self, gc_rule=None): from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable._generated import ( bigtable_table_admin_pb2 as table_admin_v2_pb2) project_id = 'project-id' zone = 'zone' cluster_id = 'cluster-id' table_id = 'table-id' column_family_id = 'column-family-id' timeout_seconds = 28 table_name = ('projects/' + project_id + '/zones/' + zone + '/clusters/' + cluster_id + '/tables/' + table_id) client = _Client(timeout_seconds=timeout_seconds) table = _Table(table_name, client=client) column_family = self._makeOne( column_family_id, table, gc_rule=gc_rule) # Create request_pb if gc_rule is None: column_family_pb = _ColumnFamilyPB() else: column_family_pb = _ColumnFamilyPB(gc_rule=gc_rule.to_pb()) request_pb = table_admin_v2_pb2.ModifyColumnFamiliesRequest( name=table_name) request_pb.modifications.add( id=column_family_id, update=column_family_pb, ) # Create response_pb response_pb = _ColumnFamilyPB() # Patch the stub used by the API method. client._table_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # update() has no return value. # Perform the method and check the result. self.assertEqual(stub.results, (response_pb,)) result = column_family.update() self.assertEqual(stub.results, ()) self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'ModifyColumnFamilies', (request_pb, timeout_seconds), {}, )])
def test_read_rows(self): from gcloud._testing import _Monkey from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable.row_data import PartialRowsData from gcloud.bigtable import table as MUT client = _Client(timeout_seconds=self.TIMEOUT_SECONDS) instance = _Instance(self.INSTANCE_NAME, client=client) table = self._makeOne(self.TABLE_ID, instance) # Create request_pb request_pb = object() # Returned by our mock. mock_created = [] def mock_create_row_request(table_name, **kwargs): mock_created.append((table_name, kwargs)) return request_pb # Create response_iterator response_iterator = object() # Patch the stub used by the API method. client._data_stub = stub = _FakeStub(response_iterator) # Create expected_result. expected_result = PartialRowsData(response_iterator) # Perform the method and check the result. start_key = b'start-key' end_key = b'end-key' filter_obj = object() limit = 22 with _Monkey(MUT, _create_row_request=mock_create_row_request): result = table.read_rows( start_key=start_key, end_key=end_key, filter_=filter_obj, limit=limit) self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'ReadRows', (request_pb, self.TIMEOUT_SECONDS), {}, )]) created_kwargs = { 'start_key': start_key, 'end_key': end_key, 'filter_': filter_obj, 'limit': limit, } self.assertEqual(mock_created, [(table.name, created_kwargs)])
def _create_test_helper(self, gc_rule=None): from gcloud.bigtable._generated import (bigtable_table_data_pb2 as data_pb2) from gcloud.bigtable._generated import ( bigtable_table_service_messages_pb2 as messages_pb2) from gcloud.bigtable._testing import _FakeStub project_id = 'project-id' zone = 'zone' cluster_id = 'cluster-id' table_id = 'table-id' column_family_id = 'column-family-id' timeout_seconds = 4 table_name = ('projects/' + project_id + '/zones/' + zone + '/clusters/' + cluster_id + '/tables/' + table_id) client = _Client(timeout_seconds=timeout_seconds) table = _Table(table_name, client=client) column_family = self._makeOne(column_family_id, table, gc_rule=gc_rule) # Create request_pb if gc_rule is None: column_family_pb = data_pb2.ColumnFamily() else: column_family_pb = data_pb2.ColumnFamily(gc_rule=gc_rule.to_pb()) request_pb = messages_pb2.CreateColumnFamilyRequest( name=table_name, column_family_id=column_family_id, column_family=column_family_pb, ) # Create response_pb response_pb = data_pb2.ColumnFamily() # Patch the stub used by the API method. client._table_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # create() has no return value. # Perform the method and check the result. self.assertEqual(stub.results, (response_pb, )) result = column_family.create() self.assertEqual(stub.results, ()) self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'CreateColumnFamily', (request_pb, timeout_seconds), {}, )])
def _update_test_helper(self, gc_rule=None): from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable._generated_v2 import (bigtable_table_admin_pb2 as table_admin_v2_pb2) project_id = 'project-id' zone = 'zone' cluster_id = 'cluster-id' table_id = 'table-id' column_family_id = 'column-family-id' timeout_seconds = 28 table_name = ('projects/' + project_id + '/zones/' + zone + '/clusters/' + cluster_id + '/tables/' + table_id) client = _Client(timeout_seconds=timeout_seconds) table = _Table(table_name, client=client) column_family = self._makeOne(column_family_id, table, gc_rule=gc_rule) # Create request_pb if gc_rule is None: column_family_pb = _ColumnFamilyPB() else: column_family_pb = _ColumnFamilyPB(gc_rule=gc_rule.to_pb()) request_pb = table_admin_v2_pb2.ModifyColumnFamiliesRequest( name=table_name) request_pb.modifications.add( id=column_family_id, update=column_family_pb, ) # Create response_pb response_pb = _ColumnFamilyPB() # Patch the stub used by the API method. client._table_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # update() has no return value. # Perform the method and check the result. self.assertEqual(stub.results, (response_pb, )) result = column_family.update() self.assertEqual(stub.results, ()) self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'ModifyColumnFamilies', (request_pb, timeout_seconds), {}, )])
def _list_column_families_helper(self, column_family_name=None): from gcloud.bigtable._generated import ( bigtable_table_data_pb2 as data_pb2) from gcloud.bigtable._generated import ( bigtable_table_service_messages_pb2 as messages_pb2) from gcloud.bigtable._testing import _FakeStub project_id = 'project-id' zone = 'zone' cluster_id = 'cluster-id' table_id = 'table-id' timeout_seconds = 502 cluster_name = ('projects/' + project_id + '/zones/' + zone + '/clusters/' + cluster_id) client = _Client(timeout_seconds=timeout_seconds) cluster = _Cluster(cluster_name, client=client) table = self._makeOne(table_id, cluster) # Create request_pb table_name = cluster_name + '/tables/' + table_id request_pb = messages_pb2.GetTableRequest(name=table_name) # Create response_pb column_family_id = 'foo' if column_family_name is None: column_family_name = (table_name + '/columnFamilies/' + column_family_id) column_family = data_pb2.ColumnFamily(name=column_family_name) response_pb = data_pb2.Table( column_families={column_family_id: column_family}, ) # Patch the stub used by the API method. client._table_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = { column_family_id: table.column_family(column_family_id), } # Perform the method and check the result. result = table.list_column_families() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'GetTable', (request_pb, timeout_seconds), {}, )])
def _finished_helper(self, done): import datetime from google.longrunning import operations_pb2 from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable.cluster import Cluster project = "PROJECT" zone = "zone" cluster_id = "cluster-id" op_type = "fake-op" op_id = 789 begin = datetime.datetime(2015, 10, 22, 1, 1) timeout_seconds = 1 client = _Client(project, timeout_seconds=timeout_seconds) cluster = Cluster(zone, cluster_id, client) operation = self._makeOne(op_type, op_id, begin, cluster=cluster) # Create request_pb op_name = ( "operations/projects/" + project + "/zones/" + zone + "/clusters/" + cluster_id + "/operations/%d" % (op_id,) ) request_pb = operations_pb2.GetOperationRequest(name=op_name) # Create response_pb response_pb = operations_pb2.Operation(done=done) # Patch the stub used by the API method. client._operations_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = done # Perform the method and check the result. result = operation.finished() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [("GetOperation", (request_pb, timeout_seconds), {})]) if done: self.assertTrue(operation._complete) else: self.assertFalse(operation._complete)
def test_update(self): from google.longrunning import operations_pb2 from gcloud._testing import _Monkey from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable import cluster as MUT SERVE_NODES = 81 client = _Client(self.PROJECT, timeout_seconds=self.TIMEOUT_SECONDS) instance = _Instance(self.INSTANCE_ID, client) cluster = self._makeOne(self.CLUSTER_ID, instance, serve_nodes=SERVE_NODES) # Create request_pb request_pb = _ClusterPB( name=self.CLUSTER_NAME, serve_nodes=SERVE_NODES, ) # Create response_pb response_pb = operations_pb2.Operation() # Patch the stub used by the API method. client._instance_stub = stub = _FakeStub(response_pb) # Create expected_result. OP_ID = 5678 expected_result = MUT.Operation('update', OP_ID, cluster=cluster) # Create mocks process_operation_called = [] def mock_process_operation(operation_pb): process_operation_called.append(operation_pb) return OP_ID # Perform the method and check the result. with _Monkey(MUT, _process_operation=mock_process_operation): result = cluster.update() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'UpdateCluster', (request_pb, self.TIMEOUT_SECONDS), {}, )]) self.assertEqual(process_operation_called, [response_pb])
def _finished_helper(self, done): import datetime from gcloud.bigtable._generated import operations_pb2 from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable.cluster import Cluster project = 'PROJECT' zone = 'zone' cluster_id = 'cluster-id' op_type = 'fake-op' op_id = 789 begin = datetime.datetime(2015, 10, 22, 1, 1) timeout_seconds = 1 client = _Client(project, timeout_seconds=timeout_seconds) cluster = Cluster(zone, cluster_id, client) operation = self._makeOne(op_type, op_id, begin, cluster=cluster) # Create request_pb op_name = ('operations/projects/' + project + '/zones/' + zone + '/clusters/' + cluster_id + '/operations/%d' % (op_id,)) request_pb = operations_pb2.GetOperationRequest(name=op_name) # Create response_pb response_pb = operations_pb2.Operation(done=done) # Patch the stub used by the API method. client._operations_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = done # Perform the method and check the result. result = operation.finished() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'GetOperation', (request_pb, timeout_seconds), {}, )]) if done: self.assertTrue(operation._complete) else: self.assertFalse(operation._complete)
def _finished_helper(self, done): from google.longrunning import operations_pb2 from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable.cluster import Cluster PROJECT = 'PROJECT' INSTANCE_ID = 'instance-id' CLUSTER_ID = 'cluster-id' OP_TYPE = 'fake-op' OP_ID = 789 timeout_seconds = 1 client = _Client(PROJECT, timeout_seconds=timeout_seconds) instance = _Instance(INSTANCE_ID, client) cluster = Cluster(CLUSTER_ID, instance) operation = self._makeOne(OP_TYPE, OP_ID, cluster=cluster) # Create request_pb op_name = ('operations/projects/' + PROJECT + '/instances/' + INSTANCE_ID + '/clusters/' + CLUSTER_ID + '/operations/%d' % (OP_ID,)) request_pb = operations_pb2.GetOperationRequest(name=op_name) # Create response_pb response_pb = operations_pb2.Operation(done=done) # Patch the stub used by the API method. client._operations_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = done # Perform the method and check the result. result = operation.finished() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'GetOperation', (request_pb, timeout_seconds), {}, )]) if done: self.assertTrue(operation._complete) else: self.assertFalse(operation._complete)
def test_commit_no_rules(self): from gcloud.bigtable._testing import _FakeStub row_key = b'row_key' client = _Client() table = _Table(None, client=client) row = self._makeOne(row_key, table) self.assertEqual(row._rule_pb_list, []) # Patch the stub used by the API method. client._data_stub = stub = _FakeStub() # Perform the method and check the result. result = row.commit() self.assertEqual(result, {}) # Make sure no request was sent. self.assertEqual(stub.method_calls, [])
def _finished_helper(self, done): from google.longrunning import operations_pb2 from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable.instance import Instance PROJECT = 'PROJECT' INSTANCE_ID = 'instance-id' TIMEOUT_SECONDS = 1 client = _Client(PROJECT, timeout_seconds=TIMEOUT_SECONDS) instance = Instance(INSTANCE_ID, client, self.LOCATION_ID) operation = self._makeOne(self.OP_TYPE, self.OP_ID, self.BEGIN, self.LOCATION_ID, instance=instance) # Create request_pb op_name = ('operations/projects/' + PROJECT + '/instances/' + INSTANCE_ID + '/locations/' + self.LOCATION_ID + '/operations/%d' % (self.OP_ID, )) request_pb = operations_pb2.GetOperationRequest(name=op_name) # Create response_pb response_pb = operations_pb2.Operation(done=done) # Patch the stub used by the API method. client._operations_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = done # Perform the method and check the result. result = operation.finished() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'GetOperation', (request_pb, TIMEOUT_SECONDS), {}, )]) if done: self.assertTrue(operation._complete) else: self.assertFalse(operation._complete)
def test_delete(self): from google.protobuf import empty_pb2 from gcloud.bigtable._generated import ( bigtable_table_admin_pb2 as table_admin_v2_pb2) from gcloud.bigtable._testing import _FakeStub project_id = 'project-id' zone = 'zone' cluster_id = 'cluster-id' table_id = 'table-id' column_family_id = 'column-family-id' timeout_seconds = 7 table_name = ('projects/' + project_id + '/zones/' + zone + '/clusters/' + cluster_id + '/tables/' + table_id) client = _Client(timeout_seconds=timeout_seconds) table = _Table(table_name, client=client) column_family = self._makeOne(column_family_id, table) # Create request_pb request_pb = table_admin_v2_pb2.ModifyColumnFamiliesRequest( name=table_name) request_pb.modifications.add( id=column_family_id, drop=True) # Create response_pb response_pb = empty_pb2.Empty() # Patch the stub used by the API method. client._table_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # delete() has no return value. # Perform the method and check the result. self.assertEqual(stub.results, (response_pb,)) result = column_family.delete() self.assertEqual(stub.results, ()) self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'ModifyColumnFamilies', (request_pb, timeout_seconds), {}, )])
def _create_test_helper(self, initial_split_keys, column_families=()): from gcloud._helpers import _to_bytes from gcloud.bigtable._testing import _FakeStub client = _Client() instance = _Instance(self.INSTANCE_NAME, client=client) table = self._makeOne(self.TABLE_ID, instance) # Create request_pb splits_pb = [ _CreateTableRequestSplitPB(key=_to_bytes(key)) for key in initial_split_keys or () ] table_pb = None if column_families: table_pb = _TablePB() for cf in column_families: cf_pb = table_pb.column_families[cf.column_family_id] if cf.gc_rule is not None: cf_pb.gc_rule.MergeFrom(cf.gc_rule.to_pb()) request_pb = _CreateTableRequestPB( initial_splits=splits_pb, parent=self.INSTANCE_NAME, table_id=self.TABLE_ID, table=table_pb, ) # Create response_pb response_pb = _TablePB() # Patch the stub used by the API method. client._table_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = None # create() has no return value. # Perform the method and check the result. result = table.create(initial_split_keys=initial_split_keys, column_families=column_families) self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'CreateTable', (request_pb, ), {}, )])
def _finished_helper(self, done): from google.longrunning import operations_pb2 from gcloud.bigtable._testing import _FakeStub from gcloud.bigtable.instance import Instance PROJECT = 'PROJECT' INSTANCE_ID = 'instance-id' TIMEOUT_SECONDS = 1 client = _Client(PROJECT, timeout_seconds=TIMEOUT_SECONDS) instance = Instance(INSTANCE_ID, client, self.LOCATION_ID) operation = self._makeOne( self.OP_TYPE, self.OP_ID, self.BEGIN, self.LOCATION_ID, instance=instance) # Create request_pb op_name = ('operations/projects/' + PROJECT + '/instances/' + INSTANCE_ID + '/locations/' + self.LOCATION_ID + '/operations/%d' % (self.OP_ID,)) request_pb = operations_pb2.GetOperationRequest(name=op_name) # Create response_pb response_pb = operations_pb2.Operation(done=done) # Patch the stub used by the API method. client._operations_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = done # Perform the method and check the result. result = operation.finished() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'GetOperation', (request_pb, TIMEOUT_SECONDS), {}, )]) if done: self.assertTrue(operation._complete) else: self.assertFalse(operation._complete)