Пример #1
0
 def test_path_predicate_result_builder(self):
   source = {}
   for valid in [True, False]:
     for path_pred in [PathPredicate('x'), None]:
       builder = PathPredicateResultBuilder(source, path_pred)
       self.assertEqual(PathPredicateResult(valid, path_pred, source,
                                            valid_candidates=[],
                                            invalid_candidates=[]),
                        builder.build(valid))
Пример #2
0
  def test_collect_from_dict_transform(self):
    source = {'a': 7, 'b': 4}
    pred = PathPredicate('', transform=lambda x: x['a'] - x['b'])
    expect = 7 - 4
    values = pred(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)
Пример #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)
Пример #4
0
  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)
Пример #5
0
  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)
Пример #6
0
  def test_path_conjunction(self):
    context = ExecutionContext()
    text = 'x=X, a=A, b=B, z=Z'
    aA = jp.STR_SUBSTR('a=A')
    bB = jp.STR_SUBSTR('b=B')
    conjunction = jp.AND([aA, bB])
    pred = PathPredicate('value', conjunction)
    source = {'value': text, 'wrong': 'a=A', 'another': 'b=B'}

    conjunction_result = conjunction(context, text)
    builder = PathPredicateResultBuilder(source, pred)
    builder.add_result_candidate(
        PathValue('value', text),
        conjunction_result.clone_with_source(source=source,
                                             base_target_path='value',
                                             base_value_path='value'))
    expect = builder.build(True)

    pred_result = pred(context, source)
    self.assertEqual(expect, pred_result)
Пример #7
0
    def test_path_conjunction(self):
        context = ExecutionContext()
        text = 'x=X, a=A, b=B, z=Z'
        aA = jp.STR_SUBSTR('a=A')
        bB = jp.STR_SUBSTR('b=B')
        conjunction = jp.AND([aA, bB])
        pred = PathPredicate('value', conjunction)
        source = {'value': text, 'wrong': 'a=A', 'another': 'b=B'}

        conjunction_result = conjunction(context, text)
        builder = PathPredicateResultBuilder(source, pred)
        builder.add_result_candidate(
            PathValue('value', text),
            conjunction_result.clone_with_source(source=source,
                                                 base_target_path='value',
                                                 base_value_path='value'))
        expect = builder.build(True)

        pred_result = pred(context, source)
        self.assertEqual(expect, pred_result)
Пример #8
0
  def test_collect_filter_good(self):
    context = ExecutionContext()
    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(context, 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)