From 2793320988a9be54b06bb22d122fbd8718881274 Mon Sep 17 00:00:00 2001 From: Nigel Small Date: Thu, 21 Jan 2016 17:14:10 +0000 Subject: [PATCH] records->stream; fixed tx.success on __exit__ --- examples/test_examples.py | 2 +- neo4j/__main__.py | 2 +- neo4j/v1/session.py | 5 +++-- test/tck/tck_util.py | 4 ++-- test/test_session.py | 20 ++++++++++---------- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/examples/test_examples.py b/examples/test_examples.py index 1f651342a..b3b38de62 100644 --- a/examples/test_examples.py +++ b/examples/test_examples.py @@ -131,7 +131,7 @@ def test_result_retention(self): # tag::retain-result-process[] cursor = session.run("MATCH (person:Person) WHERE person.dept = {dept} " "RETURN id(person) AS minion", {"dept": "IT"}) - minion_records = list(cursor.records()) + minion_records = list(cursor.stream()) for record in minion_records: session.run("MATCH (person) WHERE id(person) = {id} " diff --git a/neo4j/__main__.py b/neo4j/__main__.py index 8f4c2993c..13d5db8c8 100644 --- a/neo4j/__main__.py +++ b/neo4j/__main__.py @@ -68,7 +68,7 @@ def main(): else: if not args.quiet: has_results = False - for i, record in enumerate(cursor.records()): + for i, record in enumerate(cursor.stream()): has_results = True if i == 0: stdout.write("%s\r\n" % "\t".join(record.keys())) diff --git a/neo4j/v1/session.py b/neo4j/v1/session.py index 33d624665..ddda2f5c8 100644 --- a/neo4j/v1/session.py +++ b/neo4j/v1/session.py @@ -187,7 +187,7 @@ def at_end(self): self._connection.fetch_next() return self.at_end() - def records(self): + def stream(self): """ Yield all subsequent records. """ while self.next(): @@ -518,7 +518,8 @@ def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): - self.success = False if exc_type else True + if exc_value: + self.success = False self.close() def run(self, statement, parameters=None): diff --git a/test/tck/tck_util.py b/test/tck/tck_util.py index c3a9599a0..2b7c779a5 100644 --- a/test/tck/tck_util.py +++ b/test/tck/tck_util.py @@ -11,14 +11,14 @@ def send_string(text): session = driver.session() cursor = session.run(text) session.close() - return list(cursor.records()) + return list(cursor.stream()) def send_parameters(statement, parameters): session = driver.session() cursor = session.run(statement, parameters) session.close() - return list(cursor.records()) + return list(cursor.stream()) def get_bolt_value(type, value): diff --git a/test/test_session.py b/test/test_session.py index ac0623f99..f2a0f1d1d 100644 --- a/test/test_session.py +++ b/test/test_session.py @@ -85,7 +85,7 @@ def test_sessions_are_not_reused_if_still_in_use(self): def test_can_run_simple_statement(self): session = GraphDatabase.driver("bolt://localhost").session() count = 0 - for record in session.run("RETURN 1 AS n").records(): + for record in session.run("RETURN 1 AS n").stream(): assert record[0] == 1 assert record["n"] == 1 with self.assertRaises(KeyError): @@ -104,7 +104,7 @@ def test_can_run_simple_statement(self): def test_can_run_simple_statement_with_params(self): session = GraphDatabase.driver("bolt://localhost").session() count = 0 - for record in session.run("RETURN {x} AS n", {"x": {"abc": ["d", "e", "f"]}}).records(): + for record in session.run("RETURN {x} AS n", {"x": {"abc": ["d", "e", "f"]}}).stream(): assert record[0] == {"abc": ["d", "e", "f"]} assert record["n"] == {"abc": ["d", "e", "f"]} assert repr(record) @@ -126,7 +126,7 @@ def test_fails_on_missing_parameter(self): def test_can_run_simple_statement_from_bytes_string(self): session = GraphDatabase.driver("bolt://localhost").session() count = 0 - for record in session.run(b"RETURN 1 AS n").records(): + for record in session.run(b"RETURN 1 AS n").stream(): assert record[0] == 1 assert record["n"] == 1 assert repr(record) @@ -138,7 +138,7 @@ def test_can_run_simple_statement_from_bytes_string(self): def test_can_run_statement_that_returns_multiple_records(self): session = GraphDatabase.driver("bolt://localhost").session() count = 0 - for record in session.run("unwind(range(1, 10)) AS z RETURN z").records(): + for record in session.run("unwind(range(1, 10)) AS z RETURN z").stream(): assert 1 <= record[0] <= 10 count += 1 session.close() @@ -146,14 +146,14 @@ def test_can_run_statement_that_returns_multiple_records(self): def test_can_use_with_to_auto_close_session(self): with GraphDatabase.driver("bolt://localhost").session() as session: - record_list = list(session.run("RETURN 1").records()) + record_list = list(session.run("RETURN 1").stream()) assert len(record_list) == 1 for record in record_list: assert record[0] == 1 def test_can_return_node(self): with GraphDatabase.driver("bolt://localhost").session() as session: - record_list = list(session.run("MERGE (a:Person {name:'Alice'}) RETURN a").records()) + record_list = list(session.run("MERGE (a:Person {name:'Alice'}) RETURN a").stream()) assert len(record_list) == 1 for record in record_list: alice = record[0] @@ -164,7 +164,7 @@ def test_can_return_node(self): def test_can_return_relationship(self): with GraphDatabase.driver("bolt://localhost").session() as session: reocrd_list = list(session.run("MERGE ()-[r:KNOWS {since:1999}]->() " - "RETURN r").records()) + "RETURN r").stream()) assert len(reocrd_list) == 1 for record in reocrd_list: rel = record[0] @@ -175,7 +175,7 @@ def test_can_return_relationship(self): def test_can_return_path(self): with GraphDatabase.driver("bolt://localhost").session() as session: record_list = list(session.run("MERGE p=({name:'Alice'})-[:KNOWS]->({name:'Bob'}) " - "RETURN p").records()) + "RETURN p").stream()) assert len(record_list) == 1 for record in record_list: path = record[0] @@ -403,7 +403,7 @@ def test_can_rollback_transaction(self): # Check the property value cursor = session.run("MATCH (a) WHERE id(a) = {n} " "RETURN a.foo", {"n": node_id}) - assert len(list(cursor.records())) == 0 + assert len(list(cursor.stream())) == 0 def test_can_commit_transaction_using_with_block(self): with GraphDatabase.driver("bolt://localhost").session() as session: @@ -443,4 +443,4 @@ def test_can_rollback_transaction_using_with_block(self): # Check the property value cursor = session.run("MATCH (a) WHERE id(a) = {n} " "RETURN a.foo", {"n": node_id}) - assert len(list(cursor.records())) == 0 + assert len(list(cursor.stream())) == 0