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_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_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_data_correct_type(): client = Client(URL) invalid = [ [], True, None, 1, "hoj" ] for item in invalid: with pytest.raises(AssertionError): client.set_record('record', item)
def test_resetting_batch(): request = { 'topic': 'record', 'action': 'read', 'recordName': 'some-record', } client = Client("http://url.com") client.start_batch().add_to_batch(request) assert client.start_batch().add_to_batch(request)._batch == [request] assert client.reset_batch()._batch is None
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_adding_to_created_batch(): additional_request = { 'topic': 'record', 'action': 'delete', 'recordName': 'some-record', } client = Client("http://url.com") client.start_batch() assert client.is_batched assert client._batch == [] assert client.add_to_batch(request)._batch == [request] assert client.add_to_batch(additional_request)._batch == [ request, additional_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()
def test_starting_batch(): client = Client("http://url.com") assert client._batch is None assert client.start_batch()._batch == []
def test_not_json_serializable_record_data(): client = Client(URL) with pytest.raises(AssertionError): client.set_list('list', datetime.utcnow())
def test_is_batched(): client = Client("http://url.com") assert client.start_batch().is_batched is True
def test_is_not_batched(): client = Client("http://url.com") assert client.is_batched is False
def test_not_array_list_data(): client = Client(URL) with pytest.raises(AssertionError): client.set_list('list', {'1': '1'})
def test_adding_to_not_created_batch(): client = Client("http://url.com") with pytest.raises(AssertionError): client.add_to_batch(request)
def test_not_json_serializable_record_data(): client = Client(URL) with pytest.raises(TypeError): client.set_record('record', datetime.utcnow())