示例#1
0
    def __init__(self, session, run_response, pull_all_response):
        super(BoltStatementResult, self).__init__(session, PackStreamHydrator(session._connection.protocol_version))

        all_metadata = {}

        def on_header(metadata):
            # Called on receipt of the result header.
            all_metadata.update(metadata)
            self._keys = tuple(metadata.get("fields", ()))

        def on_records(records):
            # Called on receipt of one or more result records.
            self._records.extend(map(lambda record: self.zipper(self.keys(), self._hydrant.hydrate(record)), records))

        def on_footer(metadata):
            # Called on receipt of the result footer.
            connection = self.session._connection
            all_metadata.update(metadata, statement=self.statement, parameters=self.parameters,
                                server=connection.server, protocol_version=connection.protocol_version)
            self._summary = BoltStatementResultSummary(**all_metadata)
            self._session, session_ = None, self._session
            session_.detach(self)

        def on_failure(metadata):
            # Called on execution failure.
            self.session._connection.acknowledge_failure()
            on_footer(metadata)
            raise CypherError.hydrate(**metadata)

        run_response.on_success = on_header
        run_response.on_failure = on_failure

        pull_all_response.on_records = on_records
        pull_all_response.on_success = on_footer
        pull_all_response.on_failure = on_failure
示例#2
0
class HydrationTestCase(TestCase):
    def setUp(self):
        self.hydrant = PackStreamHydrator(1)

    def test_can_hydrate_node_structure(self):
        struct = Structure(b'N', 123, ["Person"], {"name": "Alice"})
        alice, = self.hydrant.hydrate([struct])
        self.assertEqual(alice.id, 123)
        self.assertEqual(alice.labels, {"Person"})
        self.assertEqual(set(alice.keys()), {"name"})
        self.assertEqual(alice.get("name"), "Alice")

    def test_hydrating_unknown_structure_returns_same(self):
        struct = Structure(b'?', "foo")
        mystery, = self.hydrant.hydrate([struct])
        self.assertEqual(mystery, struct)

    def test_can_hydrate_in_list(self):
        struct = Structure(b'N', 123, ["Person"], {"name": "Alice"})
        alice_in_list, = self.hydrant.hydrate([[struct]])
        self.assertIsInstance(alice_in_list, list)
        alice, = alice_in_list
        self.assertEqual(alice.id, 123)
        self.assertEqual(alice.labels, {"Person"})
        self.assertEqual(set(alice.keys()), {"name"})
        self.assertEqual(alice.get("name"), "Alice")

    def test_can_hydrate_in_dict(self):
        struct = Structure(b'N', 123, ["Person"], {"name": "Alice"})
        alice_in_dict, = self.hydrant.hydrate([{"foo": struct}])
        self.assertIsInstance(alice_in_dict, dict)
        alice = alice_in_dict["foo"]
        self.assertEqual(alice.id, 123)
        self.assertEqual(alice.labels, {"Person"})
        self.assertEqual(set(alice.keys()), {"name"})
        self.assertEqual(alice.get("name"), "Alice")
示例#3
0
 def setUp(self):
     self.hydrant = PackStreamHydrator(1)