Skip to content

Commit

Permalink
Merge pull request #1322 from dhermes/bigtable-column-family-create
Browse files Browse the repository at this point in the history
Implementing Bigtable ColumnFamily.create().
  • Loading branch information
dhermes committed Dec 23, 2015
2 parents e2d0e1c + 1f6af79 commit 7422824
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
19 changes: 19 additions & 0 deletions gcloud/bigtable/column_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class ColumnFamily(object):
We can use a :class:`ColumnFamily` to:
* :meth:`create` itself
* :meth:`update` itself
* :meth:`delete` itself
Expand Down Expand Up @@ -245,6 +246,24 @@ def __eq__(self, other):
def __ne__(self, other):
return not self.__eq__(other)

def create(self):
"""Create this column family."""
if self.gc_rule is None:
column_family = data_pb2.ColumnFamily()
else:
column_family = data_pb2.ColumnFamily(gc_rule=self.gc_rule.to_pb())
request_pb = messages_pb2.CreateColumnFamilyRequest(
name=self._table.name,
column_family_id=self.column_family_id,
column_family=column_family,
)
client = self._table._cluster._client
# We expect a `.data_pb2.ColumnFamily`. We ignore it since the only
# data it contains are the GC rule and the column family ID already
# stored on this instance.
client._table_stub.CreateColumnFamily(request_pb,
client.timeout_seconds)

def update(self):
"""Update this column family.
Expand Down
59 changes: 59 additions & 0 deletions gcloud/bigtable/test_column_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,65 @@ def test___ne__(self):
column_family2 = self._makeOne('column_family_id2', None)
self.assertNotEqual(column_family1, column_family2)

def _create_test_helper(self, gc_rule=None):
from gcloud.bigtable._generated import (
bigtable_table_data_pb2 as data_pb2)
from gcloud.bigtable._generated import (
bigtable_table_service_messages_pb2 as messages_pb2)
from gcloud.bigtable._testing import _FakeStub

project_id = 'project-id'
zone = 'zone'
cluster_id = 'cluster-id'
table_id = 'table-id'
column_family_id = 'column-family-id'
timeout_seconds = 4
table_name = ('projects/' + project_id + '/zones/' + zone +
'/clusters/' + cluster_id + '/tables/' + table_id)

client = _Client(timeout_seconds=timeout_seconds)
table = _Table(table_name, client=client)
column_family = self._makeOne(column_family_id, table, gc_rule=gc_rule)

# Create request_pb
if gc_rule is None:
column_family_pb = data_pb2.ColumnFamily()
else:
column_family_pb = data_pb2.ColumnFamily(gc_rule=gc_rule.to_pb())
request_pb = messages_pb2.CreateColumnFamilyRequest(
name=table_name,
column_family_id=column_family_id,
column_family=column_family_pb,
)

# Create response_pb
response_pb = data_pb2.ColumnFamily()

# Patch the stub used by the API method.
client._table_stub = stub = _FakeStub(response_pb)

# Create expected_result.
expected_result = None # create() has no return value.

# Perform the method and check the result.
self.assertEqual(stub.results, (response_pb,))
result = column_family.create()
self.assertEqual(stub.results, ())
self.assertEqual(result, expected_result)
self.assertEqual(stub.method_calls, [(
'CreateColumnFamily',
(request_pb, timeout_seconds),
{},
)])

def test_create(self):
self._create_test_helper(gc_rule=None)

def test_create_with_gc_rule(self):
from gcloud.bigtable.column_family import MaxVersionsGCRule
gc_rule = MaxVersionsGCRule(1337)
self._create_test_helper(gc_rule=gc_rule)

def _update_test_helper(self, gc_rule=None):
from gcloud.bigtable._generated import (
bigtable_table_data_pb2 as data_pb2)
Expand Down

0 comments on commit 7422824

Please sign in to comment.