Skip to content

Commit

Permalink
Implementing HappyBase Connection.tables().
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Sep 5, 2015
1 parent e0bc1c8 commit 0e97263
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
27 changes: 23 additions & 4 deletions gcloud_bigtable/happybase/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,31 @@ def table(self, name, use_prefix=True):
def tables(self):
"""Return a list of table names available to this connection.
The connection is limited to a cluster in Cloud Bigtable.
.. note::
:raises: :class:`NotImplementedError <exceptions.NotImplementedError>`
temporarily until the method is implemented.
This lists every table in the cluster owned by this connection,
**not** every table that a given user may have access to.
.. note::
If ``table_prefix`` is set on this connection, only returns the
table names which match that prefix.
:rtype: list
:returns: List of string table names.
"""
raise NotImplementedError('Temporarily not implemented.')
low_level_table_instances = self._cluster.list_tables()
table_names = [table_instance.table_id
for table_instance in low_level_table_instances]

# Filter using prefix, and strip prefix from names
if self.table_prefix is not None:
prefix = self._table_name('')
offset = len(prefix)
table_names = [name[offset:] for name in table_names
if name.startswith(prefix)]

return table_names

def create_table(self, name, families):
"""Create a table.
Expand Down
33 changes: 31 additions & 2 deletions gcloud_bigtable/happybase/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,35 @@ def test_table_factory_with_ignored_prefix(self):
self._table_factory_prefix_helper(use_prefix=False)

def test_tables(self):
from gcloud_bigtable.table import Table

cluster = _Cluster() # Avoid implicit environ check.
table_name1 = 'table-name1'
table_name2 = 'table-name2'
cluster.list_tables_result = [Table(table_name1, None),
Table(table_name2, None)]
connection = self._makeOne(autoconnect=False, cluster=cluster)
with self.assertRaises(NotImplementedError):
connection.tables()
result = connection.tables()
self.assertEqual(result, [table_name1, table_name2])

def test_tables_with_prefix(self):
from gcloud_bigtable.table import Table

cluster = _Cluster() # Avoid implicit environ check.
table_prefix = 'prefix'
table_prefix_separator = '<>'
unprefixed_table_name1 = 'table-name1'

table_name1 = (table_prefix + table_prefix_separator +
unprefixed_table_name1)
table_name2 = 'table-name2'
cluster.list_tables_result = [Table(table_name1, None),
Table(table_name2, None)]
connection = self._makeOne(
autoconnect=False, cluster=cluster, table_prefix=table_prefix,
table_prefix_separator=table_prefix_separator)
result = connection.tables()
self.assertEqual(result, [unprefixed_table_name1])

def test_create_table(self):
cluster = _Cluster() # Avoid implicit environ check.
Expand Down Expand Up @@ -349,6 +374,7 @@ def __init__(self, *copies):
self.copies = list(copies)
# Included to support Connection.__del__
self.client = _Client()
self.list_tables_result = []

def copy(self):
if self.copies:
Expand All @@ -357,3 +383,6 @@ def copy(self):
return result
else:
return self

def list_tables(self):
return self.list_tables_result

0 comments on commit 0e97263

Please sign in to comment.