Skip to content

Commit

Permalink
Merge pull request #1595 from dhermes/happybase-sys-test-counter
Browse files Browse the repository at this point in the history
Adding HappyBase system tests for families and counters.
  • Loading branch information
dhermes committed Mar 12, 2016
2 parents 97eb7f5 + 25f22ba commit 71fe17b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 10 deletions.
10 changes: 5 additions & 5 deletions gcloud/bigtable/happybase/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,8 +1265,8 @@ def _column_helper(self, num_filters, versions=None, timestamp=None,

# Relies on the fact that RowFilter instances can
# only have one value set.
self.assertEqual(fam_filter.regex, col_fam)
self.assertEqual(qual_filter.regex, qual)
self.assertEqual(fam_filter.regex, col_fam.encode('utf-8'))
self.assertEqual(qual_filter.regex, qual.encode('utf-8'))

return result

Expand Down Expand Up @@ -1356,14 +1356,14 @@ def test_column_and_column_families(self):
filter2 = result.filters[1]

self.assertTrue(isinstance(filter1, FamilyNameRegexFilter))
self.assertEqual(filter1.regex, col_fam1)
self.assertEqual(filter1.regex, col_fam1.encode('utf-8'))

self.assertTrue(isinstance(filter2, RowFilterChain))
filter2a, filter2b = filter2.filters
self.assertTrue(isinstance(filter2a, FamilyNameRegexFilter))
self.assertEqual(filter2a.regex, col_fam2)
self.assertEqual(filter2a.regex, col_fam2.encode('utf-8'))
self.assertTrue(isinstance(filter2b, ColumnQualifierRegexFilter))
self.assertEqual(filter2b.regex, col_qual2)
self.assertEqual(filter2b.regex, col_qual2.encode('utf-8'))


class Test__row_keys_filter_helper(unittest2.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion gcloud/bigtable/row_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class _RegexFilter(RowFilter):
"""

def __init__(self, regex):
self.regex = regex
self.regex = _to_bytes(regex)

def __eq__(self, other):
if not isinstance(other, self.__class__):
Expand Down
13 changes: 9 additions & 4 deletions gcloud/bigtable/test_row_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,29 @@ def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)

def test_constructor(self):
regex = object()
regex = b'abc'
row_filter = self._makeOne(regex)
self.assertTrue(row_filter.regex is regex)

def test_constructor_non_bytes(self):
regex = u'abc'
row_filter = self._makeOne(regex)
self.assertEqual(row_filter.regex, b'abc')

def test___eq__type_differ(self):
regex = object()
regex = b'def-rgx'
row_filter1 = self._makeOne(regex)
row_filter2 = object()
self.assertNotEqual(row_filter1, row_filter2)

def test___eq__same_value(self):
regex = object()
regex = b'trex-regex'
row_filter1 = self._makeOne(regex)
row_filter2 = self._makeOne(regex)
self.assertEqual(row_filter1, row_filter2)

def test___ne__same_value(self):
regex = object()
regex = b'abc'
row_filter1 = self._makeOne(regex)
row_filter2 = self._makeOne(regex)
comparison_val = (row_filter1 != row_filter2)
Expand Down
78 changes: 78 additions & 0 deletions system_tests/bigtable_happybase.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.


import struct
import time

import unittest2
Expand All @@ -23,6 +24,7 @@
from gcloud.environment_vars import TESTS_PROJECT


_PACK_I64 = struct.Struct('>q').pack
_helpers.PROJECT = TESTS_PROJECT
ZONE = 'us-central1-c'
NOW_MILLIS = int(1000 * time.time())
Expand All @@ -38,6 +40,8 @@
COL_FAM2: {'max_versions': 1, 'time_to_live': TTL_FOR_TEST},
COL_FAM3: {}, # use defaults
}
ROW_KEY1 = 'row-key1'
COL1 = COL_FAM1 + ':qual1'


class Config(object):
Expand Down Expand Up @@ -111,3 +115,77 @@ def test_create_table_failure(self):
with self.assertRaises(ValueError):
connection.create_table(ALT_TABLE_NAME, empty_families)
self.assertFalse(ALT_TABLE_NAME in connection.tables())


class BaseTableTest(unittest2.TestCase):

def setUp(self):
self.rows_to_delete = []

def tearDown(self):
for row_key in self.rows_to_delete:
Config.TABLE.delete(row_key)


class TestTable_families(BaseTableTest):

def test_families(self):
families = Config.TABLE.families()

self.assertEqual(set(families.keys()), set(FAMILIES.keys()))
for col_fam, settings in FAMILIES.items():
retrieved = families[col_fam]
for key, value in settings.items():
self.assertEqual(retrieved[key], value)


class TestTableCounterMethods(BaseTableTest):

def test_counter_get(self):
table = Config.TABLE

# Need to clean-up row1 after.
self.rows_to_delete.append(ROW_KEY1)

self.assertEqual(table.row(ROW_KEY1, columns=[COL1]), {})
initial_counter = table.counter_get(ROW_KEY1, COL1)
self.assertEqual(initial_counter, 0)

self.assertEqual(table.row(ROW_KEY1, columns=[COL1]),
{COL1: _PACK_I64(0)})

def test_counter_inc(self):
table = Config.TABLE

# Need to clean-up row1 after.
self.rows_to_delete.append(ROW_KEY1)

self.assertEqual(table.row(ROW_KEY1, columns=[COL1]), {})
initial_counter = table.counter_get(ROW_KEY1, COL1)
self.assertEqual(initial_counter, 0)

inc_value = 10
updated_counter = table.counter_inc(ROW_KEY1, COL1, value=inc_value)
self.assertEqual(updated_counter, inc_value)

# Check that the value is set (does not seem to occur on HBase).
self.assertEqual(table.row(ROW_KEY1, columns=[COL1]),
{COL1: _PACK_I64(inc_value)})

def test_counter_dec(self):
table = Config.TABLE

# Need to clean-up row1 after.
self.rows_to_delete.append(ROW_KEY1)

self.assertEqual(table.row(ROW_KEY1, columns=[COL1]), {})
initial_counter = table.counter_get(ROW_KEY1, COL1)
self.assertEqual(initial_counter, 0)

dec_value = 10
updated_counter = table.counter_dec(ROW_KEY1, COL1, value=dec_value)
self.assertEqual(updated_counter, -dec_value)

# Check that the value is set (does not seem to occur on HBase).
self.assertEqual(table.row(ROW_KEY1, columns=[COL1]),
{COL1: _PACK_I64(-dec_value)})

0 comments on commit 71fe17b

Please sign in to comment.