def test_not_batched(mocker): client = Client(URL) client.auth_data = {"token": "some-token"} mocker.patch.object(client, '_execute') # success client._execute.return_value = (True, [{ "success": True, "data": { "a": "b" } }]) assert client.get_record(REQUEST['recordName']) == { "success": True, "data": { "a": "b" } } client._execute.assert_called_with([REQUEST]) # false response client._execute.return_value = (True, [{ "success": False, "error": "Some" }]) assert client.get_record(REQUEST['recordName']) == { "success": False, "error": "Some" } client._execute.assert_called_with([REQUEST])
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.emit_event(REQUEST['eventName']) client._execute.assert_called_with([REQUEST]) # success with data client._execute.return_value = (True, [{ "success": True, }]) assert client.emit_event(REQUEST['eventName'], REQUEST_WITH_DATA['data']) client._execute.assert_called_with([REQUEST_WITH_DATA]) # false response with data client._execute.return_value = (False, [{"success": False}]) assert not client.emit_event( REQUEST['eventName'], REQUEST_WITH_DATA['data'] ) client._execute.assert_called_with([REQUEST_WITH_DATA])
def test_not_batched(mocker): client = Client(URL) client.auth_data = {"token": "some-token"} mocker.patch.object(client, '_execute') # success with data client._execute.return_value = (True, [{ "success": True, }]) assert client.set_list(REQUEST['listName'], REQUEST['data']) == { "success": True, } client._execute.assert_called_with([REQUEST]) # false response with data client._execute.return_value = (True, [{ "success": False, "error": "Some" }]) assert client.set_list(REQUEST['listName'], REQUEST['data']) == { "success": False, "error": "Some" } client._execute.assert_called_with([REQUEST])
def test_invalid_request(): client = Client(URL) client.auth_data = {"token": "some-token"} with requests_mock.mock() as m: m.post(URL, status_code=400, text="Some error") with pytest.raises(DeepstreamioHTTPError): client._execute([{"something": "something"}])
def test_batched(mocker): client = Client(URL) client.auth_data = {"token": "some-token"} mocker.patch.object(client, '_execute') assert isinstance(client.start_batch().get_record(REQUEST['recordName']), Client) assert client._batch == [REQUEST] client._execute.assert_not_called()
def test_batched(mocker): client = Client(URL) client.auth_data = {"token": "some-token"} mocker.patch.object(client, '_execute') # without data assert isinstance( client.start_batch().delete_list(REQUEST['listName']), Client ) assert client._batch == [REQUEST] client._execute.assert_not_called()
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, []) assert client.delete_record(REQUEST['recordName']) client._execute.assert_called_with([REQUEST]) # false response with data client._execute.return_value = (False, []) assert client.delete_record(REQUEST['recordName']) is False client._execute.assert_called_with([REQUEST])
def test_not_batched(): client = Client(URL) client.auth_data = {"token": "some-token"} # success with requests_mock.mock() as m: m.post(URL, status_code=200, json={ 'result': 'SUCCESS', 'body': [{ "success": True, "data": "hoj" }] }) body = [{ 'topic': 'record', 'action': 'head', 'recordName': 'record-name', }] assert client._execute(body) == (True, [{ "success": True, "data": "hoj" }]) # success with requests_mock.mock() as m: m.post(URL, status_code=200, json={ 'result': 'FAILURE', 'body': [{ "success": False, "error": "Some" }] }) body = [{ 'topic': 'record', 'action': 'head', 'recordName': 'record-name', }] assert client._execute(body) == (False, [{ "success": False, "error": "Some" }])
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, "version": 6 }]) assert client.get_record_version(REQUEST['recordName']) == 6 client._execute.assert_called_with([REQUEST]) # false response with data client._execute.return_value = (False, [{ "sucess": False }]) assert client.get_record_version(REQUEST['recordName']) is None client._execute.assert_called_with([REQUEST])
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_batched(mocker): client = Client(URL) client.auth_data = {"token": "some-token"} mocker.patch.object(client, '_execute') # with data assert isinstance( client.start_batch().emit_event( REQUEST['eventName'], REQUEST_WITH_DATA['data'] ), Client ) assert client._batch == [REQUEST_WITH_DATA] client._execute.assert_not_called() # without data assert isinstance( client.start_batch().emit_event(REQUEST['eventName']), Client ) assert client._batch == [REQUEST] client._execute.assert_not_called()