Skip to content
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
25 changes: 25 additions & 0 deletions gcloud/bigtable/happybase/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <exceptions.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.

Expand Down
42 changes: 42 additions & 0 deletions gcloud/bigtable/happybase/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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