def test_success_with_constructor(self, function, constructor): decorated = error_catcher(success_constructor=constructor)(function) assert decorated(1, foo='bar') == '42' function.assert_called_once_with(1, foo='bar') constructor.assert_called_once_with( args=(1, ), kwargs={'foo': 'bar'}, function_name='a_module.a_function', result=42)
def test_error_with_constructor(self, error_function, constructor, error): decorated = error_catcher( error_constructor=constructor)(error_function) assert decorated(1, foo='bar') == '42' error_function.assert_called_once_with(1, foo='bar') constructor.assert_called_once_with( args=(1, ), kwargs={'foo': 'bar'}, function_name='a_module.a_function', error=error)
def test_error_with_constructor(self, error_function, constructor, error): decorated = error_catcher(error_constructor=constructor)(error_function) assert decorated(1, foo='bar') == '42' error_function.assert_called_once_with(1, foo='bar') constructor.assert_called_once_with( args=(1,), kwargs={'foo': 'bar'}, function_name='a_module.a_function', error=error )
def test_success_with_constructor(self, function, constructor): decorated = error_catcher(success_constructor=constructor)(function) assert decorated(1, foo='bar') == '42' function.assert_called_once_with(1, foo='bar') constructor.assert_called_once_with( args=(1,), kwargs={'foo': 'bar'}, function_name='a_module.a_function', result=42 )
def decorator(f: InteractorFunction): """ The actual decorator of an interactor function. Enwraps decorated with decorators of validation, error handling and dependency injection. """ decorated_f = validated_by(*validators)(f) decorated_f = error_catcher( error_class=error_class, error_constructor=error_handler)(decorated_f) decorated_f = inject(decorated_f) decorated_f = container_supplier(decorated_f) return decorated_f
def test_error_wo_constructor(self, error_function, error): decorated = error_catcher()(error_function) assert decorated(1, foo='bar') == error error_function.assert_called_once_with(1, foo='bar')
def test_success_wo_constructor(self, function): decorated = error_catcher()(function) assert decorated(1, foo='bar') == 42 function.assert_called_once_with(1, foo='bar')
def test_error_multiple_error_class(self, error_function): decorated = error_catcher(error_class=(ValueError, MyError))(error_function) decorated(1, foo='bar') error_function.assert_called_once_with(1, foo='bar')
def test_error_narrowed_error_class_caught(self, error_function): decorated = error_catcher(error_class=ValueError)(error_function) with pytest.raises(MyError): decorated(1, foo='bar') error_function.assert_called_once_with(1, foo='bar')