Пример #1
0
def test_argument_error(dispatch, invoke_with):
    method, args, kwargs, result = invoke_with

    protocol = JSONRPCProtocol()

    @dispatch.public
    def fn_a(a, b):
        return a-b

    @dispatch.public
    def fn_b(*a):
        return a[0]-a[1]

    @dispatch.public
    def fn_c(**a):
        return a['a']-a['b']

    mock_request = Mock(RPCRequest)
    mock_request.args = args
    mock_request.kwargs = kwargs
    mock_request.method = method
    dispatch._dispatch(mock_request)
    if inspect.isclass(result) and issubclass(result, Exception):
        assert type(mock_request.error_respond.call_args[0][0]) == result
    else:
        mock_request.respond.assert_called_with(result)
Пример #2
0
def mock_request(method='subtract', args=None, kwargs=None):
    mock_request = Mock(RPCRequest)
    mock_request.method = method
    mock_request.args = args or [4, 6]
    mock_request.kwargs = kwargs or {}

    return mock_request
Пример #3
0
def mock_request(method='subtract', args=None, kwargs=None):
    mock_request = Mock(RPCRequest)
    mock_request.method = method
    mock_request.args = args or [4, 6]
    mock_request.kwargs = kwargs or {}

    return mock_request
Пример #4
0
def test_argument_error(dispatch, invoke_with):
    method, args, kwargs, result = invoke_with

    protocol = JSONRPCProtocol()

    @dispatch.public
    def fn_a(a, b):
        return a - b

    @dispatch.public
    def fn_b(*a):
        return a[0] - a[1]

    @dispatch.public
    def fn_c(**a):
        return a['a'] - a['b']

    mock_request = Mock(RPCRequest)
    mock_request.args = args
    mock_request.kwargs = kwargs
    mock_request.method = method
    dispatch._dispatch(mock_request, getattr(protocol, '_caller', None))
    if inspect.isclass(result) and issubclass(result, Exception):
        assert type(mock_request.error_respond.call_args[0][0]) == result
    else:
        mock_request.respond.assert_called_with(result)
Пример #5
0
def test_class_method_argument_error(dispatch, invoke_with):
    method, args, kwargs, result = invoke_with

    protocol = JSONRPCProtocol()

    class Test:
        c = 0

        @classmethod
        @public
        def fn_a(cls, a, b):
            return a - b - cls.c

        @classmethod
        @public
        def fn_b(cls, *a):
            return a[0] - a[1] - cls.c

        @classmethod
        @public
        def fn_c(cls, **a):
            return a['a'] - a['b'] - cls.c

    test = Test()
    dispatch.register_instance(test)
    mock_request = Mock(RPCRequest)
    mock_request.args = args
    mock_request.kwargs = kwargs
    mock_request.method = method
    dispatch._dispatch(mock_request, getattr(protocol, '_caller', None))
    if inspect.isclass(result) and issubclass(result, Exception):
        assert type(mock_request.error_respond.call_args[0][0]) == result
    else:
        mock_request.respond.assert_called_with(result)
Пример #6
0
def test_client_uses_correct_transport(client, mock_protocol, method_name,
                                       method_args, method_kwargs,
                                       async_kwargs, mock_transport):
    req = Mock(RPCRequest)
    req.unique_id = '0'
    req.method = 'test_method'
    req.callback = async_kwargs['callback']
    req.sync = async_kwargs[
        'asynchronize'] if 'asynchronize' in async_kwargs else True
    req.args = method_args
    req.kwargs = method_kwargs
    mock_protocol.create_request = Mock(return_value=req)
    method_kwargs.update(async_kwargs)
    client.call(method_name, method_args, method_kwargs)
    assert mock_transport.send_message.called
Пример #7
0
def test_client_passes_correct_reply(client, mock_protocol, method_name,
                                     method_args, method_kwargs, async_kwargs,
                                     mock_transport):
    req = Mock(RPCRequest)
    req.unique_id = '0'
    req.method = 'test_method'
    req.callback = async_kwargs['callback']
    req.sync = async_kwargs[
        'asynchronize'] if 'asynchronize' in async_kwargs else True
    req.args = method_args
    req.kwargs = method_kwargs
    method_kwargs.update(async_kwargs)
    mock_protocol.create_request = Mock(return_value=req)
    transport_return = '023hoisdfh'
    client.receiver.transport.receive_reply = Mock(
        return_value=transport_return)
    time.sleep(0.1)
    client.call(method_name, method_args, method_kwargs)
    mock_protocol.parse_reply.assert_called_with(transport_return)