Skip to content

Commit

Permalink
Moving over Operation class from upstream.
Browse files Browse the repository at this point in the history
Currently breaks the way operation_finished() is
handled. Will be fixed upstream and ported here
in short order.
  • Loading branch information
dhermes committed Dec 8, 2015
1 parent 698b257 commit fa466c5
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 9 deletions.
41 changes: 38 additions & 3 deletions gcloud_bigtable/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,38 @@ def _process_operation(operation_pb):
return operation_id, operation_begin


class Operation(object):
"""Representation of a Google API Long-Running Operation.
In particular, the
:type op_type: str
:param op_type: The type of operation being performed. Expect
``create``, ``update`` or ``undelete``.
:type op_id: int
:param op_id: The ID of the operation.
:type begin: :class:`datetime.datetime`
:param begin: The time when the operation was started.
"""

def __init__(self, op_type, op_id, begin):
self.op_type = op_type
self.op_id = op_id
self.begin = begin

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return (other.op_type == self.op_type and
other.op_id == self.op_id and
other.begin == self.begin)

def __ne__(self, other):
return not self.__eq__(other)


class Cluster(object):
"""Representation of a Google Cloud Bigtable Cluster.
Expand Down Expand Up @@ -348,16 +380,19 @@ def create(self, timeout_seconds=None):
:param timeout_seconds: Number of seconds for request time-out.
If not passed, defaults to value set on
cluster.
:rtype: :class:`Operation`
:returns: The long-running operation corresponding to the
create operation.
"""
request_pb = _prepare_create_request(self)
timeout_seconds = timeout_seconds or self.timeout_seconds
# We expect an `operations_pb2.Operation`.
cluster_pb = self.client._cluster_stub.CreateCluster(
request_pb, timeout_seconds)

self._operation_type = 'create'
self._operation_id, self._operation_begin = _process_operation(
cluster_pb.current_operation)
op_id, op_begin = _process_operation(cluster_pb.current_operation)
return Operation('create', op_id, op_begin)

def update(self, timeout_seconds=None):
"""Update this cluster.
Expand Down
59 changes: 53 additions & 6 deletions gcloud_bigtable/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,56 @@ def test_failure(self):
self._callFUT(cluster)


class TestOperation(unittest2.TestCase):

def _getTargetClass(self):
from gcloud_bigtable.cluster import Operation
return Operation

def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)

def test_constructor(self):
import datetime
op_type = 'fake-op'
op_id = 8915
begin = datetime.datetime(2015, 10, 22, 1, 1)
operation = self._makeOne(op_type, op_id, begin)

self.assertEqual(operation.op_type, op_type)
self.assertEqual(operation.op_id, op_id)
self.assertEqual(operation.begin, begin)

def test___eq__(self):
import datetime
op_type = 'fake-op'
op_id = 8915
begin = datetime.datetime(2015, 10, 22, 1, 1)
operation1 = self._makeOne(op_type, op_id, begin)
operation2 = self._makeOne(op_type, op_id, begin)
self.assertEqual(operation1, operation2)

def test___eq__type_differ(self):
operation1 = self._makeOne('foo', 123, None)
operation2 = object()
self.assertNotEqual(operation1, operation2)

def test___ne__same_value(self):
import datetime
op_type = 'fake-op'
op_id = 8915
begin = datetime.datetime(2015, 10, 22, 1, 1)
operation1 = self._makeOne(op_type, op_id, begin)
operation2 = self._makeOne(op_type, op_id, begin)
comparison_val = (operation1 != operation2)
self.assertFalse(comparison_val)

def test___ne__(self):
operation1 = self._makeOne('foo', 123, None)
operation2 = self._makeOne('bar', 456, None)
self.assertNotEqual(operation1, operation2)


class TestCluster(unittest2.TestCase):

def _getTargetClass(self):
Expand Down Expand Up @@ -420,13 +470,13 @@ def test_create(self):
client._cluster_stub = stub = StubMock(response_pb)

# Create expected_result.
expected_result = None
op_id = 5678
op_begin = object()
expected_result = MUT.Operation('create', op_id, op_begin)

# Perform the method and check the result.
timeout_seconds = 578
mock_prepare_create_request = _MockCalled(request_pb)
op_id = 5678
op_begin = object()
mock_process_operation = _MockCalled((op_id, op_begin))
with _Monkey(MUT, _prepare_create_request=mock_prepare_create_request,
_process_operation=mock_process_operation):
Expand All @@ -438,9 +488,6 @@ def test_create(self):
(request_pb, timeout_seconds),
{},
)])
self.assertEqual(cluster._operation_type, 'create')
self.assertEqual(cluster._operation_id, op_id)
self.assertTrue(cluster._operation_begin is op_begin)
mock_prepare_create_request.check_called(self, [(cluster,)])
mock_process_operation.check_called(self, [(current_op,)])

Expand Down

0 comments on commit fa466c5

Please sign in to comment.