diff --git a/gcloud/bigtable/column_family.py b/gcloud/bigtable/column_family.py index 411ba4f875ce..8b5610a205dc 100644 --- a/gcloud/bigtable/column_family.py +++ b/gcloud/bigtable/column_family.py @@ -116,6 +116,58 @@ def to_pb(self): return data_pb2.GcRule(max_age=max_age) +class GCRuleUnion(GarbageCollectionRule): + """Union of garbage collection rules. + + :type rules: list + :param rules: List of :class:`GarbageCollectionRule`, + """ + + def __init__(self, rules): + self.rules = rules + + def __eq__(self, other): + if not isinstance(other, self.__class__): + return False + return other.rules == self.rules + + def to_pb(self): + """Converts the union into a single gc rule as a protobuf. + + :rtype: :class:`.data_pb2.GcRule` + :returns: The converted current object. + """ + union = data_pb2.GcRule.Union( + rules=[rule.to_pb() for rule in self.rules]) + return data_pb2.GcRule(union=union) + + +class GCRuleIntersection(GarbageCollectionRule): + """Intersection of garbage collection rules. + + :type rules: list + :param rules: List of :class:`GarbageCollectionRule`. + """ + + def __init__(self, rules): + self.rules = rules + + def __eq__(self, other): + if not isinstance(other, self.__class__): + return False + return other.rules == self.rules + + def to_pb(self): + """Converts the intersection into a single gc rule as a protobuf. + + :rtype: :class:`.data_pb2.GcRule` + :returns: The converted current object. + """ + intersection = data_pb2.GcRule.Intersection( + rules=[rule.to_pb() for rule in self.rules]) + return data_pb2.GcRule(intersection=intersection) + + class ColumnFamily(object): """Representation of a Google Cloud Bigtable Column Family. diff --git a/gcloud/bigtable/test_column_family.py b/gcloud/bigtable/test_column_family.py index 67444fb6bb9a..d52f00dbf98d 100644 --- a/gcloud/bigtable/test_column_family.py +++ b/gcloud/bigtable/test_column_family.py @@ -137,6 +137,185 @@ def test_to_pb(self): self.assertEqual(pb_val, data_pb2.GcRule(max_age=duration)) +class TestGCRuleUnion(unittest2.TestCase): + + def _getTargetClass(self): + from gcloud.bigtable.column_family import GCRuleUnion + return GCRuleUnion + + def _makeOne(self, *args, **kwargs): + return self._getTargetClass()(*args, **kwargs) + + def test_constructor(self): + rules = object() + rule_union = self._makeOne(rules) + self.assertTrue(rule_union.rules is rules) + + def test___eq__(self): + rules = object() + gc_rule1 = self._makeOne(rules) + gc_rule2 = self._makeOne(rules) + self.assertEqual(gc_rule1, gc_rule2) + + def test___eq__type_differ(self): + rules = object() + gc_rule1 = self._makeOne(rules) + gc_rule2 = object() + self.assertNotEqual(gc_rule1, gc_rule2) + + def test___ne__same_value(self): + rules = object() + gc_rule1 = self._makeOne(rules) + gc_rule2 = self._makeOne(rules) + comparison_val = (gc_rule1 != gc_rule2) + self.assertFalse(comparison_val) + + def test_to_pb(self): + import datetime + from gcloud.bigtable._generated import ( + bigtable_table_data_pb2 as data_pb2) + from gcloud.bigtable._generated import duration_pb2 + from gcloud.bigtable.column_family import MaxAgeGCRule + from gcloud.bigtable.column_family import MaxVersionsGCRule + + max_num_versions = 42 + rule1 = MaxVersionsGCRule(max_num_versions) + pb_rule1 = data_pb2.GcRule(max_num_versions=max_num_versions) + + max_age = datetime.timedelta(seconds=1) + rule2 = MaxAgeGCRule(max_age) + pb_rule2 = data_pb2.GcRule(max_age=duration_pb2.Duration(seconds=1)) + + rule3 = self._makeOne(rules=[rule1, rule2]) + pb_rule3 = data_pb2.GcRule( + union=data_pb2.GcRule.Union(rules=[pb_rule1, pb_rule2])) + + gc_rule_pb = rule3.to_pb() + self.assertEqual(gc_rule_pb, pb_rule3) + + def test_to_pb_nested(self): + import datetime + from gcloud.bigtable._generated import ( + bigtable_table_data_pb2 as data_pb2) + from gcloud.bigtable._generated import duration_pb2 + from gcloud.bigtable.column_family import MaxAgeGCRule + from gcloud.bigtable.column_family import MaxVersionsGCRule + + max_num_versions1 = 42 + rule1 = MaxVersionsGCRule(max_num_versions1) + pb_rule1 = data_pb2.GcRule(max_num_versions=max_num_versions1) + + max_age = datetime.timedelta(seconds=1) + rule2 = MaxAgeGCRule(max_age) + pb_rule2 = data_pb2.GcRule(max_age=duration_pb2.Duration(seconds=1)) + + rule3 = self._makeOne(rules=[rule1, rule2]) + pb_rule3 = data_pb2.GcRule( + union=data_pb2.GcRule.Union(rules=[pb_rule1, pb_rule2])) + + max_num_versions2 = 1337 + rule4 = MaxVersionsGCRule(max_num_versions2) + pb_rule4 = data_pb2.GcRule(max_num_versions=max_num_versions2) + + rule5 = self._makeOne(rules=[rule3, rule4]) + pb_rule5 = data_pb2.GcRule( + union=data_pb2.GcRule.Union(rules=[pb_rule3, pb_rule4])) + + gc_rule_pb = rule5.to_pb() + self.assertEqual(gc_rule_pb, pb_rule5) + + +class TestGCRuleIntersection(unittest2.TestCase): + + def _getTargetClass(self): + from gcloud.bigtable.column_family import GCRuleIntersection + return GCRuleIntersection + + def _makeOne(self, *args, **kwargs): + return self._getTargetClass()(*args, **kwargs) + + def test_constructor(self): + rules = object() + rule_intersection = self._makeOne(rules) + self.assertTrue(rule_intersection.rules is rules) + + def test___eq__(self): + rules = object() + gc_rule1 = self._makeOne(rules) + gc_rule2 = self._makeOne(rules) + self.assertEqual(gc_rule1, gc_rule2) + + def test___eq__type_differ(self): + rules = object() + gc_rule1 = self._makeOne(rules) + gc_rule2 = object() + self.assertNotEqual(gc_rule1, gc_rule2) + + def test___ne__same_value(self): + rules = object() + gc_rule1 = self._makeOne(rules) + gc_rule2 = self._makeOne(rules) + comparison_val = (gc_rule1 != gc_rule2) + self.assertFalse(comparison_val) + + def test_to_pb(self): + import datetime + from gcloud.bigtable._generated import ( + bigtable_table_data_pb2 as data_pb2) + from gcloud.bigtable._generated import duration_pb2 + from gcloud.bigtable.column_family import MaxAgeGCRule + from gcloud.bigtable.column_family import MaxVersionsGCRule + + max_num_versions = 42 + rule1 = MaxVersionsGCRule(max_num_versions) + pb_rule1 = data_pb2.GcRule(max_num_versions=max_num_versions) + + max_age = datetime.timedelta(seconds=1) + rule2 = MaxAgeGCRule(max_age) + pb_rule2 = data_pb2.GcRule(max_age=duration_pb2.Duration(seconds=1)) + + rule3 = self._makeOne(rules=[rule1, rule2]) + pb_rule3 = data_pb2.GcRule( + intersection=data_pb2.GcRule.Intersection( + rules=[pb_rule1, pb_rule2])) + + gc_rule_pb = rule3.to_pb() + self.assertEqual(gc_rule_pb, pb_rule3) + + def test_to_pb_nested(self): + import datetime + from gcloud.bigtable._generated import ( + bigtable_table_data_pb2 as data_pb2) + from gcloud.bigtable._generated import duration_pb2 + from gcloud.bigtable.column_family import MaxAgeGCRule + from gcloud.bigtable.column_family import MaxVersionsGCRule + + max_num_versions1 = 42 + rule1 = MaxVersionsGCRule(max_num_versions1) + pb_rule1 = data_pb2.GcRule(max_num_versions=max_num_versions1) + + max_age = datetime.timedelta(seconds=1) + rule2 = MaxAgeGCRule(max_age) + pb_rule2 = data_pb2.GcRule(max_age=duration_pb2.Duration(seconds=1)) + + rule3 = self._makeOne(rules=[rule1, rule2]) + pb_rule3 = data_pb2.GcRule( + intersection=data_pb2.GcRule.Intersection( + rules=[pb_rule1, pb_rule2])) + + max_num_versions2 = 1337 + rule4 = MaxVersionsGCRule(max_num_versions2) + pb_rule4 = data_pb2.GcRule(max_num_versions=max_num_versions2) + + rule5 = self._makeOne(rules=[rule3, rule4]) + pb_rule5 = data_pb2.GcRule( + intersection=data_pb2.GcRule.Intersection( + rules=[pb_rule3, pb_rule4])) + + gc_rule_pb = rule5.to_pb() + self.assertEqual(gc_rule_pb, pb_rule5) + + class TestColumnFamily(unittest2.TestCase): def _getTargetClass(self):