def test_notification_noparams(): reality = pro.RpcNotification('topkek', None).to_dict() expectation = { 'method': 'topkek', 'jsonrpc': '2.0', } assert expectation == reality
def test_notification_params(): reality = pro.RpcNotification('topkek', {'ya': 'yeet'}).to_dict() expectation = { 'method': 'topkek', 'jsonrpc': '2.0', 'params': { 'ya': 'yeet' } } assert expectation == reality
def test_batch_request(): expectation = [ { 'method': 'topkek1', 'jsonrpc': '2.0' }, { 'method': 'topkek2', 'jsonrpc': '2.0' }, { 'method': 'topkek3', 'jsonrpc': '2.0' }, ] reality = pro.RpcBatch([ pro.RpcNotification('topkek1', None), pro.RpcNotification('topkek2', None), pro.RpcNotification('topkek2', None), ]) # order invariant equality check matches = len([x for x in reality.to_dict() if x in expectation]) assert matches == len(expectation)
def test_batch_with_malformed_to_entity(utf8: JsonSerializer): r = [{ "jsonrpc": "2.0", "method": "sum", "params": [1, 2, 4], "id": "1" }, { "jsonrpc": "2.0", "method": "notify_hello", "params": [7] }, { "jsonrpc": "2.0", "method": "a", "params": [42, 23], "id": "2" }, { "foo": "boo" }, { "jsonrpc": "2.0", "method": "b", "params": { "x": "y" }, "id": "5" }, { "jsonrpc": "2.0", "method": "get_data", "id": "9" }] reality = utf8.bytes_to_entity(json.dumps(r).encode()) expectation = pro.RpcBatch(entities=[ pro.RpcRequest(id='1', method='sum', params=[1, 2, 4], jsonrpc='2.0'), pro.RpcNotification(method='notify_hello', params=[7], jsonrpc='2.0'), pro.RpcRequest(id='2', method='a', params=[42, 23], jsonrpc='2.0'), pro.RpcRequest(id='5', method='b', params={'x': 'y'}, jsonrpc='2.0'), pro.RpcRequest(id='9', method='get_data', params=None, jsonrpc='2.0') ]) assert isinstance(reality, pro.RpcBatch) # test if all entities are present except the malformed one # because exceptions are sadly not equatable matches = len([x for x in expectation.entities if x in reality.entities]) assert matches == len(expectation.entities) x = [x for x in reality.entities if isinstance(x, pro.RpcMalformed)] assert len(x) == 1 assert not x[0].id
async def notify( self, namespace: typing.Optional[str], name: str, *args: typing.Any, **kwargs: typing.Any ): if not self.running: raise RuntimeError('endpoint not running, please call [start]') if args and kwargs: raise ValueError( 'request must either have positional or named arguments ' + 'but not both' ) if namespace: name = namespace + self.namespace_seperator + name await self.stream.dispatch_entity( protocol.RpcNotification(name, args or kwargs) )
def handle_notification(self, data: dict) -> protocol.RpcNotification: return protocol.RpcNotification(data['method'], data.get('params'), jsonrpc=self.version)
def test_bytes_to_notification(utf8: JsonSerializer): r = pro.RpcNotification('kek', None) expectation = r.to_dict() reality = utf8.bytes_to_entity(utf8.entity_to_bytes(r)).to_dict() assert expectation == reality