def test_collect_filter_good(self):
    source = {'outer': [_LETTER_DICT, _NUMBER_DICT]}
    filter_pred = TestEqualsPredicate(2)
    pred = PathPredicate(PATH_SEP.join(['outer', 'b']), pred=filter_pred)

    # This is a precise test against the exact result returned.
    builder = PathPredicateResultBuilder(source, pred)
    path_0 = PATH_SEP.join(['outer[0]', 'b'])
    path_1 = PATH_SEP.join(['outer[1]', 'b'])

    bad_result = PathValueResult(source=source, target_path=pred.path,
                                 path_value=PathValue(path_0, 'B'),
                                 valid=False, pred=filter_pred)
    good_result = PathValueResult(source=source, target_path=pred.path,
                                  path_value=PathValue(path_1, 2),
                                  valid=True, pred=filter_pred)
    builder.add_result_candidate(PathValue(path_0, 'B'), bad_result)
    builder.add_result_candidate(PathValue(path_1, 2), good_result)
    expect = builder.build(True)

    pred_result = pred(source)
    self.assertEqual([PathValue(PATH_SEP.join(['outer[1]', 'b']), 2)],
                     pred_result.path_values)

    self.assertEqual(expect, pred_result)
    self.assertEqual(
        [jp.PathPredicateResultCandidate(PathValue(path_0, 'B'), bad_result)],
        pred_result.invalid_candidates)
    self.assertEqual(
        [jp.PathPredicateResultCandidate(PathValue(path_1, 2), good_result)],
        pred_result.valid_candidates)
    self.assertEqual([], pred_result.path_failures)
Example #2
0
 def __call__(self, context, value):
     valid = value == context.eval(self.__operand)
     return PathValueResult(pred=self,
                            source=value,
                            target_path='',
                            path_value=PathValue('', value),
                            valid=valid)
Example #3
0
    def test_collect_from_dict_transform(self):
        context = ExecutionContext()
        source = {'a': 7, 'b': 4}
        pred = PathPredicate('',
                             transform=lambda ctxt, val: val['a'] - val['b'])
        expect = 7 - 4
        values = pred(context, source)

        builder = PathPredicateResultBuilder(source, pred)
        builder.add_result_candidate(
            PathValue('', source),
            PathValueResult(source=source,
                            target_path='',
                            path_value=PathValue('', expect),
                            valid=True))
        self.assertEqual(builder.build(True), values)
Example #4
0
    def __call__(self, context, value):
        """Implements ValuePredicate.

    Args:
       context: [ExecutionContext] The execution context to evaluate within.
       values: [array of dict] A list of dictionary resource quota descriptions
          following the format returned described by the "quotas" attribute of
          https://cloud.google.com/compute/docs/reference/latest/projects#resource
    """
        builder = KeyedPredicateResultBuilder(self)
        # NOTE(20180710):
        # b/111302333: get() because 'metric' is not always provided.
        dictified = {elem.get('metric'): elem for elem in value}

        bad_metrics = []
        for metric, expect in self.__minimum_quota.items():
            found = dictified.get(metric)
            if found is None:
                bad_metrics.append(metric)
                builder.add_result(
                    metric,
                    PathValueResult(source=None,
                                    target_path='metric',
                                    path_value=PathValue('', value),
                                    valid=False,
                                    pred=EQUIVALENT(metric),
                                    comment='Missing metric value.'))
                continue
            pred = PathPredicate('',
                                 pred=NUM_GE(expect),
                                 transform=self.__diff)
            result = pred(context, found)
            builder.add_result(metric, result)
            if not result:
                bad_metrics.append(metric)

        if bad_metrics:
            builder.comment = 'Insufficient {0}.'.format(
                ' and '.join(bad_metrics))
            valid = False
        else:
            valid = True
            builder.comment = 'Satisfied all {0} metrics.'.format(
                len(self.__minimum_quota))

        return builder.build(valid)
  def test_collect_from_dict_identity(self):
    source = _LETTER_DICT
    pred = PathPredicate('')
    values = pred(source)

    builder = PathPredicateResultBuilder(source, pred)
    builder.add_result_candidate(
        PathValue('', source),
        PathValueResult(source=source, target_path='',
                        path_value=PathValue('', source), valid=True))
    self.assertEqual(builder.build(True), values)

    self.assertEqual([PathValue('', source)], values.path_values)
    self.assertEqual([], values.path_failures)

    pred = PathPredicate('/')
    values = pred(source)
    self.assertEqual([PathValue('', source)], values.path_values)
    self.assertEqual([], values.path_failures)