def test_post_validate_decorator(mocker): def none_filter(result): return list(filter(None, result)) test_func = mocker.MagicMock(return_value=[1, None, 2]) func = validate(none_filter, post=True)(test_func) result = func() test_func.assert_called_once() assert result == [1, 2]
def test_pre_validate_update_args(): def test_validator(args): args.w += 50 del args.x args.y = 100 def test_func(w=1, x=5, y=10, z=15): pass mock = create_autospec(test_func) func = validate(test_validator)(mock) func(100, 100) mock.assert_called_once_with(w=150, y=100, z=15)
def test_pre_validate_decorator_required_args(mocker): mock = mocker.MagicMock() func_mock = create_autospec(_test_func) func = validate(mock)(func_mock) func("x", "y") func_mock.assert_called_once_with("x", "y", j=3, k=5) mock.assert_called_once() (args,) = mock.call_args[0] assert args.x == "x" assert args.y == "y" assert args.args == () assert args.j == 3 assert args.k == 5 assert args.kwargs == {}
def test_pre_validate_decorator_kwargs_args(mocker): mock = mocker.MagicMock() func_mock = create_autospec(_test_func) func = validate(mock)(func_mock) func("x", "y", "part", "of", "args", j=1, k=10, m=100, n=1000) func_mock.assert_called_once_with( "x", "y", "part", "of", "args", j=1, k=10, m=100, n=1000 ) mock.assert_called_once() (args,) = mock.call_args[0] assert args.x == "x" assert args.y == "y" assert args.args == ("part", "of", "args") assert args.j == 1 assert args.k == 10 assert args.kwargs == {"m": 100, "n": 1000}