Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing Bigtable Table.delete(). #1277

Merged
merged 1 commit into from
Dec 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions gcloud/bigtable/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Table(object):
We can use a :class:`Table` to:

* :meth:`create` the table
* :meth:`delete` the table

:type table_id: str
:param table_id: The ID of the table.
Expand All @@ -51,6 +52,24 @@ def __init__(self, table_id, cluster):
self.table_id = table_id
self._cluster = cluster

@property
def name(self):
"""Table name used in requests.

.. note::

This property will not change if ``table_id`` does not, but the
return value is not cached.

The table name is of the form

``"projects/../zones/../clusters/../tables/{table_id}"``

:rtype: str
:returns: The table name.
"""
return self._cluster.name + '/tables/' + self.table_id

def column_family(self, column_family_id):
"""Factory to create a column family associated with this table.

Expand Down Expand Up @@ -119,3 +138,10 @@ def create(self, initial_split_keys=None):
client = self._cluster._client
# We expect a `._generated.bigtable_table_data_pb2.Table`
client._table_stub.CreateTable(request_pb, client.timeout_seconds)

def delete(self):
"""Delete this table."""
request_pb = messages_pb2.DeleteTableRequest(name=self.name)
client = self._cluster._client
# We expect a `._generated.empty_pb2.Empty`
client._table_stub.DeleteTable(request_pb, client.timeout_seconds)
49 changes: 49 additions & 0 deletions gcloud/bigtable/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ def test_constructor(self):
self.assertEqual(table.table_id, table_id)
self.assertTrue(table._cluster is cluster)

def test_name_property(self):
table_id = 'table-id'
cluster_name = 'cluster_name'

cluster = _Cluster(cluster_name)
table = self._makeOne(table_id, cluster)
expected_name = cluster_name + '/tables/' + table_id
self.assertEqual(table.name, expected_name)

def test_column_family_factory(self):
from gcloud.bigtable.column_family import ColumnFamily

Expand Down Expand Up @@ -134,6 +143,46 @@ def test_create_with_split_keys(self):
initial_split_keys = ['s1', 's2']
self._create_test_helper(initial_split_keys)

def test_delete(self):
from gcloud.bigtable._generated import (
bigtable_table_service_messages_pb2 as messages_pb2)
from gcloud.bigtable._generated import empty_pb2
from gcloud.bigtable._testing import _FakeStub

project_id = 'project-id'
zone = 'zone'
cluster_id = 'cluster-id'
table_id = 'table-id'
timeout_seconds = 871
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.DeleteTableRequest(name=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, timeout_seconds),
{},
)])


class _Client(object):

Expand Down