Skip to content

Commit

Permalink
Adding HappyBase Connection.table factory.
Browse files Browse the repository at this point in the history
Also adding the Connection._table_name prefix helper.
  • Loading branch information
dhermes committed Sep 5, 2015
1 parent 56c6094 commit e0bc1c8
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 7 deletions.
24 changes: 21 additions & 3 deletions gcloud_bigtable/happybase/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import six

from gcloud_bigtable.client import Client
from gcloud_bigtable.happybase.table import Table


# Constants reproduced here for compatibility, though values are
Expand Down Expand Up @@ -202,6 +203,21 @@ def __del__(self):
else:
self.close()

def _table_name(self, name):
"""Construct a table name by optionally adding a table name prefix.
:type name: str
:param name: The name to have a prefix added to it.
:rtype: str
:returns: The prefixed name, if the current connection has a table
prefix set.
"""
if self.table_prefix is None:
return name

return self.table_prefix + self.table_prefix_separator + name

def table(self, name, use_prefix=True):
"""Table factory.
Expand All @@ -211,10 +227,12 @@ def table(self, name, use_prefix=True):
:type use_prefix: bool
:param use_prefix: Whether to use the table prefix (if any).
:raises: :class:`NotImplementedError <exceptions.NotImplementedError>`
temporarily until the method is implemented.
:rtype: `Table <gcloud_bigtable.happybase.table.Table>`
:returns: Table instance owned by this connection.
"""
raise NotImplementedError('Temporarily not implemented.')
if use_prefix:
name = self._table_name(name)
return Table(name, self)

def tables(self):
"""Return a list of table names available to this connection.
Expand Down
65 changes: 61 additions & 4 deletions gcloud_bigtable/happybase/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,71 @@ def test___del__bad_initialization(self):
connection.__del__()
self.assertEqual(cluster.client.stop_calls, 0)

def test_table(self):
def test__table_name_with_prefix_set(self):
table_prefix = 'table-prefix'
table_prefix_separator = '<>'
cluster = _Cluster()

connection = self._makeOne(
autoconnect=False,
table_prefix=table_prefix,
table_prefix_separator=table_prefix_separator,
cluster=cluster)

name = 'some-name'
prefixed = connection._table_name(name)
self.assertEqual(prefixed,
table_prefix + table_prefix_separator + name)

def test__table_name_with_no_prefix_set(self):
cluster = _Cluster()
connection = self._makeOne(autoconnect=False,
cluster=cluster)

name = 'some-name'
prefixed = connection._table_name(name)
self.assertEqual(prefixed, name)

def test_table_factory(self):
from gcloud_bigtable.happybase.table import Table

cluster = _Cluster() # Avoid implicit environ check.
connection = self._makeOne(autoconnect=False, cluster=cluster)

name = 'table-name'
use_prefix = False
with self.assertRaises(NotImplementedError):
connection.table(name, use_prefix=use_prefix)
table = connection.table(name)

self.assertTrue(isinstance(table, Table))
self.assertEqual(table.name, name)
self.assertEqual(table.connection, connection)

def _table_factory_prefix_helper(self, use_prefix=True):
from gcloud_bigtable.happybase.table import Table

cluster = _Cluster() # Avoid implicit environ check.
table_prefix = 'table-prefix'
table_prefix_separator = '<>'
connection = self._makeOne(
autoconnect=False, table_prefix=table_prefix,
table_prefix_separator=table_prefix_separator,
cluster=cluster)

name = 'table-name'
table = connection.table(name, use_prefix=use_prefix)

self.assertTrue(isinstance(table, Table))
prefixed_name = table_prefix + table_prefix_separator + name
if use_prefix:
self.assertEqual(table.name, prefixed_name)
else:
self.assertEqual(table.name, name)
self.assertEqual(table.connection, connection)

def test_table_factory_with_prefix(self):
self._table_factory_prefix_helper(use_prefix=True)

def test_table_factory_with_ignored_prefix(self):
self._table_factory_prefix_helper(use_prefix=False)

def test_tables(self):
cluster = _Cluster() # Avoid implicit environ check.
Expand Down

0 comments on commit e0bc1c8

Please sign in to comment.