def test_batched(): client = Client(URL) client.auth_data = {"token": "some-token"} client.start_batch() client.get_record("recordName") client.make_rpc("rpcName") client.emit_event("eventName") assert client._batch == [{ 'action': 'read', 'recordName': 'recordName', 'topic': 'record' }, { 'action': 'make', 'data': None, 'rpcName': 'rpcName', 'topic': 'rpc' }, { 'action': 'emit', 'data': None, 'eventName': 'eventName', 'topic': 'event' }] with requests_mock.mock() as m: res = { 'result': "SUCCESS", 'body': [{ "success": True, "data": "data" }, { "success": True }, { "success": True }] } m.post(URL, status_code=200, json=res) assert client.execute_batch() == res['body']
def test_not_batched(mocker): client = Client(URL) client.auth_data = {"token": "some-token"} mocker.patch.object(client, '_execute') # success without data client._execute.return_value = (True, [{ "success": True, "data": 7 }]) assert client.make_rpc(REQUEST['rpcName']) == { "success": True, "data": 7 } client._execute.assert_called_with([REQUEST]) # success with data client._execute.return_value = (True, [{ "success": True, "data": 7 }]) assert client.make_rpc(REQUEST['rpcName'], REQUEST_WITH_DATA['data']) == { "success": True, "data": 7 } client._execute.assert_called_with([REQUEST_WITH_DATA]) # false response with data client._execute.return_value = (True, [{ "success": False, "error": "Some" }]) assert client.make_rpc(REQUEST['rpcName'], REQUEST_WITH_DATA['data']) == { "success": False, "error": "Some" } client._execute.assert_called_with([REQUEST_WITH_DATA])