Skip to content

Commit

Permalink
records->stream; fixed tx.success on __exit__
Browse files Browse the repository at this point in the history
  • Loading branch information
technige committed Jan 21, 2016
1 parent 8243a42 commit 2793320
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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} "
Expand Down
2 changes: 1 addition & 1 deletion neo4j/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
5 changes: 3 additions & 2 deletions neo4j/v1/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions test/tck/tck_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
20 changes: 10 additions & 10 deletions test/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -138,22 +138,22 @@ 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()
assert count == 10

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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit 2793320

Please sign in to comment.