def test_unpack_record_string(dummy_read_buffer_pair): dummy, buf = dummy_read_buffer_pair output = serialize_message(Message.RECORD, params=(['a', 'b', 'c'], )).getvalue() dummy.feed_data(output) record = deserialize_message(buf) assert record.fields == ['a', 'b', 'c']
def test_unpack_record_float(dummy_read_buffer_pair): dummy, buf = dummy_read_buffer_pair output = serialize_message(Message.RECORD, params=([1.1, 2.2, 3.3], )).getvalue() dummy.feed_data(output) record = deserialize_message(buf) assert record.fields == [1.1, 2.2, 3.3]
def test_unpack_good_run_message(dummy_read_buffer_pair): dummy, buf = dummy_read_buffer_pair output = serialize_message(Message.RUN, params=("RETURN 1 AS num", {})).getvalue() dummy.feed_data(output) run = deserialize_message(buf) assert run.statement == "RETURN 1 AS num" assert run.parameters == {}
def test_unpack_record_int_sizes(dummy_read_buffer_pair): dummy, buf = dummy_read_buffer_pair output = serialize_message(Message.RECORD, params=([1, 1234, -1234, 40000, -40000], )).getvalue() dummy.feed_data(output) record = deserialize_message(buf) assert record.fields == [1, 1234, -1234, 40000, -40000]
def test_unpack_success_message(dummy_read_buffer_pair): dummy, buf = dummy_read_buffer_pair output = serialize_message(Message.SUCCESS, params=({ "server": "Neo4j/3.1.0" }, )).getvalue() dummy.feed_data(output) success = deserialize_message(buf) assert success.metadata == {"server": "Neo4j/3.1.0"}
def test_unpack_record_mixed_types(dummy_read_buffer_pair): dummy, buf = dummy_read_buffer_pair output = serialize_message(Message.RECORD, params=([1, 'hello', { 'hello': 'world' }], )).getvalue() dummy.feed_data(output) record = deserialize_message(buf) assert record.fields == [1, 'hello', {'hello': 'world'}]
def handle_incoming(self): while self.read_buffer.ready: data = messaging.deserialize_message(self.read_buffer) if self.state == ServerProtocolState.PROTOCOL_READY: if data.signature == messaging.Message.RUN: self.state = ServerProtocolState.PROTOCOL_RUNNING self.on_run(data.statement, data.parameters) elif data.signature == messaging.Message.RESET: self.reset() else: self.state = ServerProtocolState.PROTOCOL_FAILED self.failure({}) self.flush() elif self.state == ServerProtocolState.PROTOCOL_RUNNING: if data.signature == messaging.Message.PULL_ALL: # Client ready to consume stream self.on_pull_all() self.state = ServerProtocolState.PROTOCOL_READY elif data.signature == messaging.Message.DISCARD_ALL: self.on_discard_all() self.write_buffer = buffer.ChunkedWriteBuffer(8192) self.state = ServerProtocolState.PROTOCOL_READY else: self.state = ServerProtocolState.PROTOCOL_FAILED self.failure({}) self.flush() elif self.state == ServerProtocolState.PROTOCOL_FAILED: if data.signature == messaging.Message.ACK_FAILURE: self.on_ack_failure() self.state = ServerProtocolState.PROTOCOL_READY self.success({}) self.flush() elif data.signature == messaging.Message.RESET: self.reset() else: self.ignored({}) self.flush() elif self.state == ServerProtocolState.PROTOCOL_UNINITIALIZED: if data.signature == messaging.Message.INIT: self.on_init(data.auth_token) self.state = ServerProtocolState.PROTOCOL_READY log_debug( "Server session initialized with auth token '{}'". format(data.auth_token)) metadata = self.get_server_metadata() self.success(metadata) self.flush() else: self.state = ServerProtocolState.PROTOCOL_FAILED self.failure({}) self.flush() else: self.state = ServerProtocolState.PROTOCOL_FAILED self.failure({}) self.flush()
def test_unpack_init_message(dummy_read_buffer_pair): dummy, buf = dummy_read_buffer_pair output = serialize_message(Message.INIT, params=("MyClient/1.0", collections.OrderedDict([ ("scheme", "basic"), ("principal", "neo4j"), ("credentials", "secret") ]))).getvalue() dummy.feed_data(output) init = deserialize_message(buf) assert init.client_name == "MyClient/1.0" assert init.auth_token["scheme"] == "basic" assert init.auth_token["principal"] == "neo4j" assert init.auth_token["credentials"] == "secret"
def test_unpack_record_mixed_types_containers(dummy_read_buffer_pair): dummy, buf = dummy_read_buffer_pair output = serialize_message(Message.RECORD, params=([ 1, 'hello', { 'hello': ['world', 'hello', None, True, False] }, [1, 2, 3] ], )).getvalue() dummy.feed_data(output) record = deserialize_message(buf) assert record.fields == [ 1, 'hello', { 'hello': ['world', 'hello', None, True, False] }, [1, 2, 3] ]
async def read(self): """Main API. Read a result from the incoming results stream""" # Await waiter if not self.read_buffer.ready: await self.waiter self.reset_waiter() data = messaging.deserialize_message(self.read_buffer) if data[0] == messaging.Message.RECORD: return data if data[0] == messaging.Message.SUCCESS: return data if data[0] == messaging.Message.RECORD: return data if data[0] == messaging.Message.FAILURE: raise ServerFailedError("{}".format(data.metadata)) if data[0] == messaging.Message.IGNORED: raise ServerIgnoredError()
def test_unpack_pull_all(dummy_read_buffer_pair): dummy, buf = dummy_read_buffer_pair output = serialize_message(Message.PULL_ALL).getvalue() dummy.feed_data(output) pull_all = deserialize_message(buf) assert pull_all.signature == Message.PULL_ALL