def test_raise_validation_errors_no_func(self): """ raise_validation_errors works without a function to wrap """ with raise_validation_errors() as ve: pass with self.assertRaises(ValidationError) as raise_context: with raise_validation_errors() as ve: ve.add_error('hello', 'world') self.assertEqual(raise_context.exception.message_dict, {'hello': ['world']})
def test_raise_validation_errors(self): """ Basic operation of raise_validation_errors() """ test_cases = ( # wrapped_function, add_error args|None, expected ValidationError contents|None ( self.raise_nothing, None, None, ), ( self.raise_nothing, ('field', 'abc'), {'field': ['abc']}, ), ( self.raise_nothing, (None, ['abc']), ['abc'], ), ( self.raise_ve(''), (None, 'abc'), ['', 'abc'], ), ( self.raise_ve('abc'), None, 'abc', ), ( self.raise_ve({'field': 'abc'}), (None, ['def']), {'field': ['abc'], NON_FIELD_ERRORS: ['def']}, ), ( self.raise_ve({'field': 'abc'}), ('field', ['def']), {'field': ['abc', 'def']}, ), ) for func, add_error_args, expected in test_cases: raise_context = self.assertRaises(ValidationError) if expected is not None else nullcontext() with raise_context: with raise_validation_errors(func) as ve: if add_error_args is not None: ve.add_error(*add_error_args) if expected is not None: self.assertValidationErrorMatch(raise_context.exception, expected)
def test_capture_validation_error(self): """ capture_validation_error behaviour """ # normal ValidationError with self.assertRaises(ValidationError) as raise_context: with raise_validation_errors() as ve: with ve.capture_validation_error(): raise ValidationError({'foo': 'hello world'}) ve.add_error('foo', 'bar') self.assertEqual(raise_context.exception.message_dict, {'foo': ['hello world', 'bar']}) # non-ValidationError raised with self.assertRaises(ValueError): with raise_validation_errors() as ve: with ve.capture_validation_error(): raise ValueError('dont catch me') # nothing raised with raise_validation_errors() as ve: with ve.capture_validation_error(): pass
def test_raise_validation_errors_non_validation_error(self): """ If you raise something that's not a ValidationError in the wrapped function then let that exception flow up and don't enter the context block """ def raise_value_error(): raise ValueError('ValueError1!') x = False with self.assertRaises(ValueError) as raise_context: with raise_validation_errors(raise_value_error) as ve: x = True raise ValueError('ValueError2!') self.assertFalse(x) self.assertEqual(str(raise_context.exception), 'ValueError1!')
def test_raise_validation_errors_manual(self): """ If you raise an exception manually then that overrides anything in raise_context_exeption """ test_wrapped_func = ( self.raise_ve('abc'), self.raise_nothing, ) test_exceptions = ( ValueError('ValueError!'), ValidationError('ValidationError!'), ) for wrapped_func in test_wrapped_func: for exception in test_exceptions: with self.assertRaises(type(exception)) as raise_context: with raise_validation_errors(wrapped_func): raise exception self.assertEqual(str(raise_context.exception), str(exception))