def test_create_table_from_snapshot(self): # Setup Expected Response name = 'name3373707' expected_response = {'name': name} expected_response = table_pb2.Table(**expected_response) operation = operations_pb2.Operation( name='operations/test_create_table_from_snapshot', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) client = bigtable_admin_v2.BigtableTableAdminClient(channel=channel) # Setup Request parent = client.instance_path('[PROJECT]', '[INSTANCE]') table_id = 'tableId-895419604' source_snapshot = 'sourceSnapshot-947679896' response = client.create_table_from_snapshot(parent, table_id, source_snapshot) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = bigtable_table_admin_pb2.CreateTableFromSnapshotRequest( parent=parent, table_id=table_id, source_snapshot=source_snapshot) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_table(self): # Setup Expected Response name = "name3373707" expected_response = {"name": name} expected_response = table_pb2.Table(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = bigtable_admin_v2.BigtableTableAdminClient() # Setup Request parent = client.instance_path("[PROJECT]", "[INSTANCE]") table_id = "tableId-895419604" table = {} response = client.create_table(parent, table_id, table) assert expected_response == response assert len(channel.requests) == 1 expected_request = bigtable_table_admin_pb2.CreateTableRequest( parent=parent, table_id=table_id, table=table) actual_request = channel.requests[0][1] assert expected_request == actual_request
def create(self, initial_split_keys=[], column_families={}): """Creates this table. .. note:: A create request returns a :class:`._generated.table_pb2.Table` but we don't use this response. :type initial_split_keys: list :param initial_split_keys: (Optional) list of row keys in bytes that will be used to initially split the table into several tablets. :type column_families: dict :param column_failies: (Optional) A map columns to create. The key is the column_id str and the value is a :class:`GarbageCollectionRule` """ table_client = self._instance._client.table_admin_client instance_name = self._instance.name families = { id: ColumnFamily(id, self, rule).to_pb() for (id, rule) in column_families.items() } table = admin_messages_v2_pb2.Table(column_families=families) split = table_admin_messages_v2_pb2.CreateTableRequest.Split splits = [split(key=_to_bytes(key)) for key in initial_split_keys] table_client.create_table(parent=instance_name, table_id=self.table_id, table=table, initial_splits=splits)
def test_modify_column_families(self): # Setup Expected Response name_2 = "name2-1052831874" expected_response = {"name": name_2} expected_response = table_pb2.Table(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = bigtable_admin_v2.BigtableTableAdminClient() # Setup Request name = client.table_path("[PROJECT]", "[INSTANCE]", "[TABLE]") modifications = [] response = client.modify_column_families(name, modifications) assert expected_response == response assert len(channel.requests) == 1 expected_request = bigtable_table_admin_pb2.ModifyColumnFamiliesRequest( name=name, modifications=modifications) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_table_from_snapshot(self): # Setup Expected Response name = "name3373707" expected_response = {"name": name} expected_response = table_pb2.Table(**expected_response) operation = operations_pb2.Operation( name="operations/test_create_table_from_snapshot", 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 = bigtable_admin_v2.BigtableTableAdminClient() # Setup Request parent = client.instance_path("[PROJECT]", "[INSTANCE]") table_id = "tableId-895419604" source_snapshot = client.snapshot_path("[PROJECT]", "[INSTANCE]", "[CLUSTER]", "[SNAPSHOT]") response = client.create_table_from_snapshot(parent, table_id, source_snapshot) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = bigtable_table_admin_pb2.CreateTableFromSnapshotRequest( parent=parent, table_id=table_id, source_snapshot=source_snapshot) actual_request = channel.requests[0][1] assert expected_request == actual_request
def _list_tables_helper(self, table_name=None): from google.cloud.bigtable_admin_v2.proto import (table_pb2 as table_data_v2_pb2) from google.cloud.bigtable_admin_v2.proto import ( bigtable_table_admin_pb2 as table_messages_v1_pb2) channel = _make_channel() client = self._make_client(project=self.PROJECT, channel=channel, admin=True) instance = self._make_one(self.INSTANCE_ID, client, self.LOCATION_ID) # Create response_pb if table_name is None: table_name = self.TABLE_NAME response_pb = table_messages_v1_pb2.ListTablesResponse(tables=[ table_data_v2_pb2.Table(name=table_name), ], ) # Patch the stub used by the API method. bigtable_table_stub = ( client._table_admin_client.bigtable_table_admin_stub) bigtable_table_stub.ListTables.side_effect = [response_pb] # Create expected_result. expected_table = instance.table(self.TABLE_ID) expected_result = [expected_table] # Perform the method and check the result. result = instance.list_tables() self.assertEqual(result, expected_result)
def _list_tables_helper(self, table_name=None): from google.cloud.bigtable_admin_v2.proto import ( table_pb2 as table_data_v2_pb2) from google.cloud.bigtable_admin_v2.proto import ( bigtable_table_admin_pb2 as table_messages_v1_pb2) from google.cloud.bigtable_admin_v2.gapic import ( bigtable_table_admin_client, bigtable_instance_admin_client) table_api = bigtable_table_admin_client.BigtableTableAdminClient( mock.Mock()) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock())) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) # Create response_pb if table_name is None: table_name = self.TABLE_NAME response_pb = table_messages_v1_pb2.ListTablesResponse( tables=[ table_data_v2_pb2.Table(name=table_name), ], ) # Patch the stub used by the API method. client._table_admin_client = table_api client._instance_admin_client = instance_api bigtable_table_stub = ( client._table_admin_client.transport) bigtable_table_stub.list_tables.side_effect = [response_pb] # Create expected_result. expected_table = instance.table(self.TABLE_ID) expected_result = [expected_table] # Perform the method and check the result. result = instance.list_tables() self.assertEqual(result, expected_result)
def test_get_table(self): # Setup Expected Response name_2 = 'name2-1052831874' expected_response = {'name': name_2} expected_response = table_pb2.Table(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = bigtable_admin_v2.BigtableTableAdminClient(channel=channel) # Setup Request name = client.table_path('[PROJECT]', '[INSTANCE]', '[TABLE]') response = client.get_table(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = bigtable_table_admin_pb2.GetTableRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_modify_column_families(self): # Setup Expected Response name_2 = 'name2-1052831874' expected_response = {'name': name_2} expected_response = table_pb2.Table(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = bigtable_admin_v2.BigtableTableAdminClient(channel=channel) # Setup Request name = client.table_path('[PROJECT]', '[INSTANCE]', '[TABLE]') modifications = [] response = client.modify_column_families(name, modifications) assert expected_response == response assert len(channel.requests) == 1 expected_request = bigtable_table_admin_pb2.ModifyColumnFamiliesRequest( name=name, modifications=modifications) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_table(self): # Setup Expected Response name = 'name3373707' expected_response = {'name': name} expected_response = table_pb2.Table(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = bigtable_admin_v2.BigtableTableAdminClient(channel=channel) # Setup Request parent = client.instance_path('[PROJECT]', '[INSTANCE]') table_id = 'tableId-895419604' table = {} response = client.create_table(parent, table_id, table) assert expected_response == response assert len(channel.requests) == 1 expected_request = bigtable_table_admin_pb2.CreateTableRequest( parent=parent, table_id=table_id, table=table) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_table(self): # Setup Expected Response name_2 = 'name2-1052831874' expected_response = {'name': name_2} expected_response = table_pb2.Table(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = bigtable_admin_v2.BigtableTableAdminClient() # Setup Request name = client.table_path('[PROJECT]', '[INSTANCE]', '[TABLE]') response = client.get_table(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = bigtable_table_admin_pb2.GetTableRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_restore_table(self): # Setup Expected Response name = "name3373707" expected_response = {"name": name} expected_response = table_pb2.Table(**expected_response) operation = operations_pb2.Operation( name="operations/test_restore_table", 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 = bigtable_admin_v2.BigtableTableAdminClient() response = client.restore_table() result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = bigtable_table_admin_pb2.RestoreTableRequest() actual_request = channel.requests[0][1] assert expected_request == actual_request
def _TablePB(*args, **kw): from google.cloud.bigtable_admin_v2.proto import ( table_pb2 as table_v2_pb2) return table_v2_pb2.Table(*args, **kw)