Пример #1
0
    def test_object_observation_verifier_one_constraint_not_found(self):
        context = ExecutionContext()
        pred_list = [jp.PathPredicate('a', jp.STR_EQ('NOT_FOUND'))]

        # This is our object verifier for these tests.
        verifier = (jc.ValueObservationVerifierBuilder(
            'Cannot find one').contains_match(pred_list).build())

        test_cases = [('array', _DICT_ARRAY), ('dict', _LETTER_DICT),
                      ('array', _DICT_ARRAY), ('multi', _MULTI_ARRAY)]

        for test in test_cases:
            observation = jc.Observation()
            builder = jc.ObservationVerifyResultBuilder(observation)

            obj = test[1]
            if isinstance(test, list):
                observation.add_all_objects(obj)
            else:
                observation.add_object(obj)

            for pred in pred_list:
                builder.add_path_predicate_result(
                    pred(context, observation.objects))

            # None of these tests succeed.
            verify_results = builder.build(False)
            self.assertEqual(pred_list, verify_results.failed_constraints)

            try:
                self._try_verify(context, verifier, observation, False,
                                 verify_results)
            except:
                print 'testing {0}'.format(test[0])
                raise
    def test_object_observation_verifier_multiple_constraint_found(self):
        pred_list = [
            jp.PathPredicate('a', jp.STR_EQ('A')),
            jp.PathPredicate('b', jp.STR_EQ('B'))
        ]
        # This is our object verifier for these tests.
        verifier = jc.ValueObservationVerifier(title='Find Both',
                                               constraints=pred_list)

        test_cases = [('dict', _LETTER_DICT), ('array', _DICT_ARRAY),
                      ('multi', _MULTI_ARRAY)]
        for test in test_cases:
            observation = jc.Observation()
            builder = jc.ObservationVerifyResultBuilder(observation)

            obj = test[1]
            if isinstance(test, list):
                observation.add_all_objects(obj)
            else:
                observation.add_object(obj)

            for pred in pred_list:
                builder.add_path_predicate_result(pred(observation.objects))

            # All of these tests succeed.
            verify_results = builder.build(True)

            try:
                self._try_verify(verifier, observation, True, verify_results)
            except:
                print 'testing {0}'.format(test[0])
                raise
Пример #3
0
    def test_object_observation_verifier_with_conditional(self):
        # We need strict True here because we want each object to pass
        # the constraint test. Otherwise, if any object passes, then the whole
        # observation would pass. This causes a problem when we say that
        # we dont ever want to see 'name' unless it has a particular 'value'.
        # Without strict test, we'd allow this to occur as long as another object
        # satisfied that constraint.
        # When we use 'excludes', it applies to the whole observation since this
        # is normally the intent. However, here we are excluding values under
        # certain context -- "If the 'name' field is 'NAME' then it must contain
        # a value field 'VALUE'". Excluding name='NAME' everywhere would
        # not permit the context where value='VALUE' which we want to permit.
        verifier_builder = jc.ValueObservationVerifierBuilder(
            title='Test Conditional', strict=True)

        name_eq_pred = jp.PathEqPredicate('name', 'NAME')
        value_eq_pred = jp.PathEqPredicate('value', 'VALUE')

        conditional = jp.IF(name_eq_pred, value_eq_pred)
        pred_list = [jp.PathPredicate('', conditional)]

        verifier_builder.add_constraint(conditional)

        match_name_value_obj = {'name': 'NAME', 'value': 'VALUE'}
        match_value_not_name_obj = {'name': 'GOOD', 'value': 'VALUE'}
        match_neither_obj = {'name': 'GOOD', 'value': 'GOOD'}
        match_name_not_value_obj = {'name': 'NAME', 'value': 'BAD'}  # bad

        test_cases = [(True, [match_name_value_obj, match_neither_obj]),
                      (True, [match_value_not_name_obj, match_neither_obj]),
                      (False, [match_neither_obj, match_name_not_value_obj])]

        context = ExecutionContext()
        verifier = verifier_builder.build()
        for test in test_cases:
            observation = jc.Observation()
            result_builder = jc.ObservationVerifyResultBuilder(observation)

            expect_valid = test[0]
            obj_list = test[1]
            observation.add_all_objects(obj_list)

            result_builder.add_observation_predicate_result(
                jc.ObservationValuePredicate(conditional)(context,
                                                          observation))

            # All of these tests succeed.
            verify_results = result_builder.build(expect_valid)
            try:
                self._try_verify(context, verifier, observation, expect_valid,
                                 verify_results)
            except:
                print 'testing {0}'.format(obj_list)
                raise
Пример #4
0
    def test_result_builder_add_bad_result(self):
        observation = jc.Observation()
        observation.add_object('A')

        pred = jp.PathPredicate(None, jp.STR_EQ('B'))
        builder = jc.ObservationVerifyResultBuilder(observation)

        map_pred = jp.MapPredicate(pred)
        map_result = map_pred(observation.objects)
        builder.add_map_result(map_result)
        verify_results = builder.build(False)

        self.assertFalse(verify_results)
        self.assertEqual(observation, verify_results.observation)
        self.assertEqual([], verify_results.good_results)
        self.assertEqual([pred], verify_results.failed_constraints)
        self.assertEqual(map_result.bad_object_result_mappings,
                         verify_results.bad_results)
Пример #5
0
    def test_object_observation_verifier_some_but_not_all_constraints_found(
            self):
        context = ExecutionContext()
        pred_list = [
            jp.PathPredicate('a', jp.STR_EQ('NOT_FOUND')),
            jp.PathPredicate('b', jp.STR_EQ('B'))
        ]

        # This is our object verifier for these tests.
        verifier = jc.ValueObservationVerifier(title='Find one of two',
                                               constraints=pred_list)

        test_cases = [('array', _DICT_ARRAY), ('dict', _LETTER_DICT),
                      ('array', _DICT_ARRAY), ('multi', _MULTI_ARRAY)]

        for test in test_cases:
            observation = jc.Observation()
            builder = jc.ObservationVerifyResultBuilder(observation)

            obj = test[1]
            if isinstance(test, list):
                observation.add_all_objects(obj)
            else:
                observation.add_object(obj)

            for pred in pred_list:
                pred_result = jp.PathPredicate('', pred)(context,
                                                         observation.objects)
                builder.add_path_predicate_result(
                    pred(context, observation.objects))

            # None of these tests succeed.
            verify_results = builder.build(False)

            try:
                self._try_verify(context, verifier, observation, False,
                                 verify_results)
            except:
                print 'testing {0}'.format(test[0])
                raise
Пример #6
0
    def test_object_observation_verifier_multiple_constraint_found(self):
        context = ExecutionContext()
        pred_list = [
            jp.PathPredicate('a', jp.STR_EQ('A')),
            jp.PathPredicate('b', jp.STR_EQ('B'))
        ]
        # This is our object verifier for these tests.
        verifier = (jc.ValueObservationVerifierBuilder('Find Both').EXPECT(
            pred_list[0]).AND(pred_list[1]).build())

        test_cases = [('dict', _LETTER_DICT), ('array', _DICT_ARRAY),
                      ('multi', _MULTI_ARRAY)]
        for test in test_cases:
            observation = jc.Observation()
            builder = jc.ObservationVerifyResultBuilder(observation)

            obj = test[1]
            if isinstance(test, list):
                observation.add_all_objects(obj)
            else:
                observation.add_object(obj)

            for pred in pred_list:
                builder.add_observation_predicate_result(
                    jc.ObservationValuePredicate(pred)(context, observation))

            # All of these tests succeed.
            verify_results = builder.build(True)
            self.assertEqual([], verify_results.failed_constraints)

            try:
                self._try_verify(context, verifier, observation, True,
                                 verify_results)
            except:
                print 'testing {0}'.format(test[0])
                raise