1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import _compat
from six.moves.mock import Mock, MagicMock
import pytest
import inspect
from tinyrpc.dispatch import RPCDispatcher, public
from tinyrpc import RPCRequest, RPCBatchRequest, RPCBatchResponse
from tinyrpc.protocols.jsonrpc import JSONRPCProtocol, JSONRPCInvalidParamsError
@pytest.fixture
def dispatch():
return RPCDispatcher()
@pytest.fixture()
def subdispatch():
return RPCDispatcher()
@pytest.fixture()
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
def test_function_decorating_without_paramters(dispatch):
@dispatch.public
def foo(bar):
pass
assert dispatch.get_method('foo') == foo
def test_function_decorating_with_empty_paramters(dispatch):
@dispatch.public()
def foo(bar):
pass
assert dispatch.get_method('foo') == foo
def test_function_decorating_with_paramters(dispatch):
@dispatch.public(name='baz')
def foo(bar):
pass
with pytest.raises(KeyError):
dispatch.get_method('foo')
assert dispatch.get_method('baz') == foo
def test_subdispatchers(dispatch, subdispatch):
@dispatch.public()
def foo(bar):
pass
@subdispatch.public(name='foo')
def subfoo(bar):
pass
dispatch.add_subdispatch(subdispatch, 'sub.')
assert dispatch.get_method('foo') == foo
assert dispatch.get_method('sub.foo') == subfoo
def test_object_method_marking():
class Foo(object):
def foo1(self):
pass
@public
def foo2(self):
pass
@public(name='baz')
def foo3(self):
pass
f = Foo()
assert not hasattr(f.foo1, '_rpc_public_name')
assert f.foo2._rpc_public_name == 'foo2'
assert f.foo3._rpc_public_name == 'baz'
def test_object_method_register(dispatch):
class Foo(object):
def foo1(self):
pass
@public
def foo2(self):
pass
@public(name='baz')
def foo3(self):
pass
f = Foo()
dispatch.register_instance(f)
with pytest.raises(KeyError):
assert dispatch.get_method('foo1')
assert dispatch.get_method('foo2') == f.foo2
assert dispatch.get_method('baz') == f.foo3
def test_object_method_register_with_prefix(dispatch):
class Foo(object):
def foo1(self):
pass
@public
def foo2(self):
pass
@public(name='baz')
def foo3(self):
pass
f = Foo()
dispatch.register_instance(f, 'myprefix')
with pytest.raises(KeyError):
assert dispatch.get_method('foo1')
with pytest.raises(KeyError):
assert dispatch.get_method('myprefixfoo1')
with pytest.raises(KeyError):
assert dispatch.get_method('foo2')
with pytest.raises(KeyError):
assert dispatch.get_method('foo3')
assert dispatch.get_method('myprefixfoo2') == f.foo2
assert dispatch.get_method('myprefixbaz') == f.foo3
def test_dispatch_calls_method_and_responds(dispatch, mock_request):
m = Mock()
m.subtract = Mock(return_value=-2)
dispatch.add_method(m.subtract, 'subtract')
response = dispatch.dispatch(mock_request)
assert m.subtract.called
mock_request.respond.assert_called_with(-2)
def test_dispatch_handles_in_function_exceptions(dispatch, mock_request):
m = Mock()
m.subtract = Mock(return_value=-2)
class MockError(Exception):
pass
m.subtract.side_effect = MockError('mock error')
dispatch.add_method(m.subtract, 'subtract')
response = dispatch.dispatch(mock_request)
assert m.subtract.called
mock_request.error_respond.assert_called_with(m.subtract.side_effect)
def test_batch_dispatch(dispatch):
method1 = Mock(return_value='rv1')
method2 = Mock(return_value=None)
dispatch.add_method(method1, 'method1')
dispatch.add_method(method2, 'method2')
batch_request = RPCBatchRequest()
batch_request.error_respond = Mock(return_value='ERROR')
batch_request.append(mock_request('method1', args=[1,2]))
batch_request.append(mock_request('non_existant_method', args=[5,6]))
batch_request.append(mock_request('method2', args=[3,4]))
batch_request.create_batch_response = lambda: RPCBatchResponse()
assert batch_request.error_respond.call_count == 0
response = dispatch.dispatch(batch_request)
# assert all methods are called
method1.assert_called_with(1, 2)
method2.assert_called_with(3, 4)
# FIXME: could use better checking?
def test_dispatch_raises_key_error(dispatch):
with pytest.raises(KeyError):
dispatch.get_method('foo')
@pytest.fixture(params=[
('fn_a', [4, 6], {}, -2),
('fn_a', [4], {}, JSONRPCInvalidParamsError),
('fn_a', [], {'a':4, 'b':6}, -2),
('fn_a', [4], {'b':6}, -2),
('fn_b', [4, 6], {}, -2),
('fn_b', [], {'a':4, 'b':6}, JSONRPCInvalidParamsError),
('fn_b', [4], {}, IndexError),
# a[1] doesn't exist, can't be detected beforehand
('fn_c', [4, 6], {}, JSONRPCInvalidParamsError),
('fn_c', [], {'a':4, 'b':6}, -2),
('fn_c', [], {'a':4}, KeyError)
# a['b'] doesn't exist, can't be detected beforehand
])
def invoke_with(request):
return request.param
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)
|