Skip to content

Commit

Permalink
Implementing HappyBase Table.counter_get()/counter_dec().
Browse files Browse the repository at this point in the history
These don't really do anything because Table.counter_inc()
is not implemented yet.
  • Loading branch information
dhermes committed Sep 7, 2015
1 parent 031243b commit 64adc6f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
14 changes: 8 additions & 6 deletions gcloud_bigtable/happybase/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,12 @@ def counter_get(self, row, column):
:type column: str
:param column: Column we are ``get``-ing from; of the form ``fam:col``.
:raises: :class:`NotImplementedError <exceptions.NotImplementedError>`
temporarily until the method is implemented.
:rtype: int
:returns: Counter value (after initializing / incrementing by 0).
"""
raise NotImplementedError('Temporarily not implemented.')
# Don't query directly, but increment with value=0 so that the counter
# is correctly initialised if didn't exist yet.
return self.counter_inc(row, column, value=0)

def counter_set(self, row, column, value=0):
"""Set a counter column to a specific value.
Expand Down Expand Up @@ -524,7 +526,7 @@ def counter_dec(self, row, column, value=1):
:param value: Amount to decrement the counter by. (If negative,
this is equivalent to increment.)
:raises: :class:`NotImplementedError <exceptions.NotImplementedError>`
temporarily until the method is implemented.
:rtype: int
:returns: Counter value after decrementing.
"""
raise NotImplementedError('Temporarily not implemented.')
return self.counter_inc(row, column, -value)
44 changes: 37 additions & 7 deletions gcloud_bigtable/happybase/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,29 @@ def test_batch(self):
self.assertEqual(result.kwargs, expected_kwargs)

def test_counter_get(self):
klass = self._getTargetClass()
counter_value = 1337

class TableWithInc(klass):

incremented = []
value = counter_value

def counter_inc(self, row, column, value=1):
self.incremented.append((row, column, value))
self.value += value
return self.value

name = 'table-name'
connection = object()
table = self._makeOne(name, connection)
table = TableWithInc(name, connection)

row = 'row-key'
column = 'fam:col1'
with self.assertRaises(NotImplementedError):
table.counter_get(row, column)
self.assertEqual(TableWithInc.incremented, [])
result = table.counter_get(row, column)
self.assertEqual(result, counter_value)
self.assertEqual(TableWithInc.incremented, [(row, column, 0)])

def test_counter_set(self):
name = 'table-name'
Expand All @@ -404,15 +419,30 @@ def test_counter_inc(self):
table.counter_inc(row, column, value=value)

def test_counter_dec(self):
klass = self._getTargetClass()
counter_value = 42

class TableWithInc(klass):

incremented = []
value = counter_value

def counter_inc(self, row, column, value=1):
self.incremented.append((row, column, value))
self.value += value
return self.value

name = 'table-name'
connection = object()
table = self._makeOne(name, connection)
table = TableWithInc(name, connection)

row = 'row-key'
column = 'fam:col1'
value = 42
with self.assertRaises(NotImplementedError):
table.counter_dec(row, column, value=value)
dec_value = 987
self.assertEqual(TableWithInc.incremented, [])
result = table.counter_dec(row, column, value=dec_value)
self.assertEqual(result, counter_value - dec_value)
self.assertEqual(TableWithInc.incremented, [(row, column, -dec_value)])


class _MockLowLevelTable(object):
Expand Down

0 comments on commit 64adc6f

Please sign in to comment.