def test_verifier_builder_add_constraint(self): aA = jp.PathPredicate('a', jp.STR_EQ('A')) bB = jp.PathPredicate('b', jp.STR_EQ('B')) builder = jc.ValueObservationVerifierBuilder('TestAddConstraint') builder.EXPECT(aA).AND(bB) verifier = builder.build() self.assertEqual('TestAddConstraint', verifier.title) self.assertEqual([[ jc.ObservationValuePredicate(aA), jc.ObservationValuePredicate(bB) ]], verifier.dnf_verifiers)
def test_verifier_builder_contains_pred_list(self): aA = jp.PathPredicate('a', jp.STR_EQ('A')) bB = jp.PathPredicate('b', jp.STR_EQ('B')) builder = jc.ValueObservationVerifierBuilder('TestContainsGroup') builder.contains_path_pred('a', jp.STR_EQ('A')) builder.contains_path_pred('b', jp.STR_EQ('B')) verifier = builder.build() count_aA = jp.CardinalityPredicate(aA, 1, None) count_bB = jp.CardinalityPredicate(bB, 1, None) self.assertEqual([[ jc.ObservationValuePredicate(count_aA), jc.ObservationValuePredicate(count_bB) ]], verifier.dnf_verifiers)
def test_observation_failure_or_found(self): context = ExecutionContext() observation = jc.Observation() observation.add_object(_LETTER_DICT) failure_predicate = jc.ObservationErrorPredicate( jp.ExceptionMatchesPredicate(ValueError, regex='an error')) failure_result = failure_predicate(context, observation) self.assertFalse(failure_result) good_predicate = jc.ObservationValuePredicate( jp.PathPredicate('a', jp.STR_EQ('A'))) builder = jc.ObservationVerifierBuilder('TestAddConstraint') verifier = ( builder.EXPECT(failure_predicate).OR(good_predicate).build()) expect = jc.ObservationVerifyResult( valid=True, observation=observation, bad_results=[failure_result], good_results=[good_predicate(context, observation)], failed_constraints=[]) got = verifier(context, observation) self.assertEqual(expect, got)
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
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