def test_value_predicates(): """Test the use of value predicates.""" field_names = ('foo', 'bar') validator = CSVValidator(field_names) foo_predicate = lambda v: math.pow(float(v), 2) < 64 validator.add_value_predicate('foo', foo_predicate) bar_predicate = lambda v: math.sqrt(float(v)) > 8 validator.add_value_predicate('bar', bar_predicate, 'X3', 'custom message') data = ( ('foo', 'bar'), ('4', '81'), # valid ('9', '81'), # foo invalid ('4', '49') # bar invalid ) problems = validator.validate(data) assert len(problems) == 2, len(problems) p0 = problems[0] assert p0['code'] == VALUE_PREDICATE_FALSE assert p0['message'] == MESSAGES[VALUE_PREDICATE_FALSE] assert p0['row'] == 3 assert p0['column'] == 1 assert p0['field'] == 'foo' assert p0['value'] == '9' assert p0['record'] == ('9', '81') p1 = problems[1] assert p1['code'] == 'X3' assert p1['message'] == 'custom message' assert p1['row'] == 4 assert p1['column'] == 2 assert p1['field'] == 'bar' assert p1['value'] == '49' assert p1['record'] == ('4', '49')
def test_exception_handling(): """Establish expectations for exception handling.""" field_names = ('foo', 'bar') validator = CSVValidator(field_names) validator.add_value_check('foo', int) def buggy_value_check(v): """I am a buggy value check.""" raise Exception('something went wrong') validator.add_value_check('bar', buggy_value_check) def buggy_value_predicate(v): """I am a buggy value predicate.""" raise Exception('something went wrong') validator.add_value_predicate('bar', buggy_value_predicate) def buggy_record_check(r): """I am a buggy record check.""" raise Exception('something went wrong') validator.add_record_check(buggy_record_check) def buggy_record_predicate(r): """I am a buggy record predicate.""" raise Exception('something went wrong') validator.add_record_predicate(buggy_record_predicate) def buggy_assert(r): """I am a buggy assert.""" raise Exception('something went wrong') validator.assert_something_buggy = buggy_assert def buggy_check(r): """I am a buggy check.""" raise Exception('something went wrong') validator.check_something_buggy = buggy_check def buggy_each(r): """I am a buggy each.""" raise Exception('something went wrong') validator.each_something_buggy = buggy_each def buggy_finally_assert(): """I am a buggy finally assert.""" raise Exception('something went wrong') validator.finally_assert_something_buggy = buggy_finally_assert def buggy_skip(record): """I am a buggy skip.""" raise Exception('something went wrong') validator.add_skip(buggy_skip) data = ( ('foo', 'bar'), ('ab', '56') ) problems = validator.validate(data, report_unexpected_exceptions=False) n = len(problems) assert n == 1, n p = problems[0] assert p['row'] == 2 problems = validator.validate(data) # by default, exceptions are reported as problems n = len(problems) assert n == 10, n unexpected_problems = [p for p in problems if p['code'] == UNEXPECTED_EXCEPTION] assert len(unexpected_problems) == 9 for p in unexpected_problems: e = p['exception'] assert e.args[0] == 'something went wrong', e.args
def test_exception_handling(): """Establish expectations for exception handling.""" field_names = ('foo', 'bar') validator = CSVValidator(field_names) validator.add_value_check('foo', int) def buggy_value_check(v): """I am a buggy value check.""" raise Exception('something went wrong') validator.add_value_check('bar', buggy_value_check) def buggy_value_predicate(v): """I am a buggy value predicate.""" raise Exception('something went wrong') validator.add_value_predicate('bar', buggy_value_predicate) def buggy_record_check(r): """I am a buggy record check.""" raise Exception('something went wrong') validator.add_record_check(buggy_record_check) def buggy_record_predicate(r): """I am a buggy record predicate.""" raise Exception('something went wrong') validator.add_record_predicate(buggy_record_predicate) def buggy_assert(r): """I am a buggy assert.""" raise Exception('something went wrong') validator.assert_something_buggy = buggy_assert def buggy_check(r): """I am a buggy check.""" raise Exception('something went wrong') validator.check_something_buggy = buggy_check def buggy_each(r): """I am a buggy each.""" raise Exception('something went wrong') validator.each_something_buggy = buggy_each def buggy_finally_assert(): """I am a buggy finally assert.""" raise Exception('something went wrong') validator.finally_assert_something_buggy = buggy_finally_assert def buggy_skip(record): """I am a buggy skip.""" raise Exception('something went wrong') validator.add_skip(buggy_skip) data = (('foo', 'bar'), ('ab', '56')) problems = validator.validate(data, report_unexpected_exceptions=False) n = len(problems) assert n == 1, n p = problems[0] assert p['row'] == 2 problems = validator.validate( data) # by default, exceptions are reported as problems n = len(problems) assert n == 10, n unexpected_problems = [ p for p in problems if p['code'] == UNEXPECTED_EXCEPTION ] assert len(unexpected_problems) == 9 for p in unexpected_problems: e = p['exception'] assert e.args[0] == 'something went wrong', e.args