def test_multiple_ranges(self):
   """Test multiple ranges."""
   ranges = [repr(e) for e in UpdaterConfig.instances_to_ranges([1, 2, 3, 5, 7, 8])]
   assert 3 == len(ranges), "Wrong number of ranges:%s" % len(ranges)
   assert repr(Range(first=1, last=3)) in ranges, "Missing range [1,3]"
   assert repr(Range(first=5, last=5)) in ranges, "Missing range [5,5]"
   assert repr(Range(first=7, last=8)) in ranges, "Missing range [7,8]"
Exemple #2
0
 def get_job_update_diff_result(cls, task=None):
   diff = cls.create_simple_success_response()
   if task is None:
     task = cls.create_task_config('foo')
   diff.result = Result(getJobUpdateDiffResult=GetJobUpdateDiffResult(
       add=set([ConfigGroup(
           config=task,
           instances=frozenset([Range(first=10, last=10), Range(first=12, last=14)]))]),
       remove=frozenset(),
       update=frozenset([ConfigGroup(
           config=task,
           instances=frozenset([Range(first=11, last=11)]))]),
       unchanged=frozenset([ConfigGroup(
           config=task,
           instances=frozenset([Range(first=0, last=9)]))])
   ))
   return diff
Exemple #3
0
 def get_job_update_no_change_diff_result(cls):
   diff = cls.create_simple_success_response()
   task = cls.create_task_config('foo')
   diff.result = Result(getJobUpdateDiffResult=GetJobUpdateDiffResult(
       add=frozenset(),
       remove=frozenset(),
       update=frozenset(),
       unchanged=frozenset([ConfigGroup(
           config=task,
           instances=frozenset([Range(first=0, last=3)]))])
   ))
   return diff
    def instances_to_ranges(cls, instances):
        """Groups instance IDs into a set of contiguous integer ranges.

    Every Range(first, last) represents a closed span of instance IDs. For example,
    :param instances:
    instances=[0,1,2,5,8,9] would result in the following set of ranges:
    (Range(first=0, last=2], Range(first=5, last=5), Range(first=8, last=9))

    Algorithm: http://stackoverflow.com/questions/3149440

    Arguments:
    instances - sorted list of instance IDs.
    """
        if not instances:
            return None

        ranges = set()
        for _, group in groupby(enumerate(instances), lambda
                                (element, position): element - position):
            range_seq = map(itemgetter(1), group)
            ranges.add(Range(first=range_seq[0], last=range_seq[-1]))
 def test_one_element(self):
     """Test one ID in the list."""
     ranges = [repr(e) for e in UpdaterConfig.instances_to_ranges([1])]
     assert 1 == len(ranges), "Wrong number of ranges:%s" % len(ranges)
     assert repr(Range(first=1, last=1)) in ranges, "Missing range [1,1]"