diff --git a/gcloud/bigtable/happybase/connection.py b/gcloud/bigtable/happybase/connection.py index 384d80efa0ad..48a876a2266d 100644 --- a/gcloud/bigtable/happybase/connection.py +++ b/gcloud/bigtable/happybase/connection.py @@ -21,6 +21,7 @@ from gcloud.bigtable.client import Client from gcloud.bigtable.happybase.table import Table +from gcloud.bigtable.table import Table as _LowLevelTable # Constants reproduced here for HappyBase compatibility, though values @@ -264,6 +265,30 @@ def tables(self): return table_names + def delete_table(self, name, disable=False): + """Delete the specified table. + + :type name: str + :param name: The name of the table to be deleted. If ``table_prefix`` + is set, a prefix will be added to the ``name``. + + :type disable: bool + :param disable: Whether to first disable the table if needed. This + is provided for compatibility with HappyBase, but is + not relevant for Cloud Bigtable since it has no concept + of enabled / disabled tables. + + :raises: :class:`ValueError ` + if ``disable=True``. + """ + if disable: + raise ValueError('The disable argument should not be used in ' + 'delete_table(). Cloud Bigtable has no concept ' + 'of enabled / disabled tables.') + + name = self._table_name(name) + _LowLevelTable(name, self._cluster).delete() + def enable_table(self, name): """Enable the specified table. diff --git a/gcloud/bigtable/happybase/test_connection.py b/gcloud/bigtable/happybase/test_connection.py index 17094f437d93..9cefa0069245 100644 --- a/gcloud/bigtable/happybase/test_connection.py +++ b/gcloud/bigtable/happybase/test_connection.py @@ -311,6 +311,37 @@ def test_tables_with_prefix(self): result = connection.tables() self.assertEqual(result, [unprefixed_table_name1]) + def test_delete_table(self): + from gcloud._testing import _Monkey + from gcloud.bigtable.happybase import connection as MUT + + cluster = _Cluster() # Avoid implicit environ check. + connection = self._makeOne(autoconnect=False, cluster=cluster) + + tables_created = [] + + def make_table(*args, **kwargs): + result = _MockLowLevelTable(*args, **kwargs) + tables_created.append(result) + return result + + name = 'table-name' + with _Monkey(MUT, _LowLevelTable=make_table): + connection.delete_table(name) + + # Just one table would have been created. + table_instance, = tables_created + self.assertEqual(table_instance.args, (name, cluster)) + self.assertEqual(table_instance.kwargs, {}) + self.assertEqual(table_instance.delete_calls, 1) + + def test_delete_table_disable(self): + cluster = _Cluster() # Avoid implicit environ check. + connection = self._makeOne(autoconnect=False, cluster=cluster) + name = 'table-name' + with self.assertRaises(ValueError): + connection.delete_table(name, disable=True) + def test_enable_table(self): cluster = _Cluster() # Avoid implicit environ check. connection = self._makeOne(autoconnect=False, cluster=cluster) @@ -385,3 +416,14 @@ def copy(self): def list_tables(self): return self.list_tables_result + + +class _MockLowLevelTable(object): + + def __init__(self, *args, **kwargs): + self.args = args + self.kwargs = kwargs + self.delete_calls = 0 + + def delete(self): + self.delete_calls += 1