def test_with_intersection_two_nested_rules(self): from google.cloud.bigtable.column_family import GCRuleIntersection rule1 = GCRuleIntersection(rules=[]) rule2 = GCRuleIntersection(rules=[]) gc_rule = GCRuleIntersection(rules=[rule1, rule2]) result = self._call_fut(gc_rule) self.assertTrue(result is gc_rule)
def test_intersection(self): import datetime from google.cloud.bigtable.column_family import GCRuleIntersection from google.cloud.bigtable.column_family import MaxAgeGCRule from google.cloud.bigtable.column_family import MaxVersionsGCRule rule1 = MaxVersionsGCRule(1) rule2 = MaxAgeGCRule(datetime.timedelta(seconds=1)) orig_rule = GCRuleIntersection([rule1, rule2]) gc_rule_pb = orig_rule.to_pb() result = self._call_fut(gc_rule_pb) self.assertIsInstance(result, GCRuleIntersection) self.assertEqual(result, orig_rule)
def test_intersection(self): import datetime from google.cloud.bigtable.column_family import GCRuleIntersection from google.cloud.bigtable.column_family import MaxAgeGCRule from google.cloud.bigtable.column_family import MaxVersionsGCRule rule1 = MaxVersionsGCRule(1) rule2 = MaxAgeGCRule(datetime.timedelta(seconds=1)) orig_rule = GCRuleIntersection([rule1, rule2]) gc_rule_pb = orig_rule.to_pb() result = self._callFUT(gc_rule_pb) self.assertTrue(isinstance(result, GCRuleIntersection)) self.assertEqual(result, orig_rule)
def test__gc_rule_from_pb_intersection(): import datetime from google.cloud.bigtable.column_family import _gc_rule_from_pb from google.cloud.bigtable.column_family import GCRuleIntersection from google.cloud.bigtable.column_family import MaxAgeGCRule from google.cloud.bigtable.column_family import MaxVersionsGCRule rule1 = MaxVersionsGCRule(1) rule2 = MaxAgeGCRule(datetime.timedelta(seconds=1)) orig_rule = GCRuleIntersection([rule1, rule2]) gc_rule_pb = orig_rule.to_pb() result = _gc_rule_from_pb(gc_rule_pb) assert isinstance(result, GCRuleIntersection) assert result == orig_rule
def test_with_intersection_two_max_num_versions(self): from google.cloud.bigtable.column_family import GCRuleIntersection from google.cloud.bigtable.column_family import MaxVersionsGCRule rule1 = MaxVersionsGCRule(1) rule2 = MaxVersionsGCRule(2) gc_rule = GCRuleIntersection(rules=[rule1, rule2]) result = self._call_fut(gc_rule) self.assertTrue(result is gc_rule)
def _parse_family_option(option): """Parses a column family option into a garbage collection rule. .. note:: If ``option`` is not a dictionary, the type is not checked. If ``option`` is :data:`None`, there is nothing to do, since this is the correct output. :type option: :class:`dict`, :data:`NoneType <types.NoneType>`, :class:`.GarbageCollectionRule` :param option: A column family option passes as a dictionary value in :meth:`Connection.create_table`. :rtype: :class:`.GarbageCollectionRule` :returns: A garbage collection rule parsed from the input. """ result = option if isinstance(result, dict): if not set(result.keys()) <= set(['max_versions', 'time_to_live']): all_keys = ', '.join(repr(key) for key in result.keys()) warning_msg = ('Cloud Bigtable only supports max_versions and ' 'time_to_live column family settings. ' 'Received: %s' % (all_keys, )) _WARN(warning_msg) max_num_versions = result.get('max_versions') max_age = None if 'time_to_live' in result: max_age = datetime.timedelta(seconds=result['time_to_live']) versions_rule = age_rule = None if max_num_versions is not None: versions_rule = MaxVersionsGCRule(max_num_versions) if max_age is not None: age_rule = MaxAgeGCRule(max_age) if versions_rule is None: result = age_rule else: if age_rule is None: result = versions_rule else: result = GCRuleIntersection(rules=[age_rule, versions_rule]) return result
def test_with_intersection_two_rules(self): import datetime from google.cloud.bigtable.column_family import GCRuleIntersection from google.cloud.bigtable.column_family import MaxAgeGCRule from google.cloud.bigtable.column_family import MaxVersionsGCRule time_to_live = 101 max_age = datetime.timedelta(seconds=time_to_live) rule1 = MaxAgeGCRule(max_age) max_versions = 2 rule2 = MaxVersionsGCRule(max_versions) gc_rule = GCRuleIntersection(rules=[rule1, rule2]) result = self._call_fut(gc_rule) expected_result = { "max_versions": max_versions, "time_to_live": time_to_live } self.assertEqual(result, expected_result)
def test_dictionary_both_keys(self): import datetime from google.cloud.bigtable.column_family import GCRuleIntersection from google.cloud.bigtable.column_family import MaxAgeGCRule from google.cloud.bigtable.column_family import MaxVersionsGCRule versions = 42 time_to_live = 24 * 60 * 60 option = {"max_versions": versions, "time_to_live": time_to_live} result = self._call_fut(option) max_age = datetime.timedelta(days=1) # NOTE: This relies on the order of the rules in the method we are # calling matching this order here. gc_rule1 = MaxAgeGCRule(max_age) gc_rule2 = MaxVersionsGCRule(versions) gc_rule = GCRuleIntersection(rules=[gc_rule1, gc_rule2]) self.assertEqual(result, gc_rule)
def test_with_intersection_other_than_two(self): from google.cloud.bigtable.column_family import GCRuleIntersection gc_rule = GCRuleIntersection(rules=[]) result = self._call_fut(gc_rule) self.assertTrue(result is gc_rule)
def _make_gc_rule_intersection(*args, **kwargs): from google.cloud.bigtable.column_family import GCRuleIntersection return GCRuleIntersection(*args, **kwargs)