Skip to content

Commit 9dc874d

Browse files
committed
Renamed summarize to summary and altered semantics
1 parent f56a0ee commit 9dc874d

File tree

5 files changed

+69
-32
lines changed

5 files changed

+69
-32
lines changed

examples/test_examples.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def test_result_summary_query_profile(self):
174174
# tag::result-summary-query-profile[]
175175
cursor = session.run("PROFILE MATCH (p:Person {name: {name}}) "
176176
"RETURN id(p)", {"name": "The One"})
177-
summary = cursor.summarize()
177+
summary = cursor.summary()
178178
print(summary.statement_type)
179179
print(summary.profile)
180180
# end::result-summary-query-profile[]
@@ -184,7 +184,7 @@ def test_result_summary_notifications(self):
184184
driver = GraphDatabase.driver("bolt://localhost")
185185
session = driver.session()
186186
# tag::result-summary-notifications[]
187-
summary = session.run("EXPLAIN MATCH (a), (b) RETURN a,b").summarize()
187+
summary = session.run("EXPLAIN MATCH (a), (b) RETURN a,b").summary()
188188
for notification in summary.notifications:
189189
print(notification)
190190
# end::result-summary-notifications[]

neo4j/__main__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def main():
7575
stdout.write("%s\r\n" % "\t".join(map(repr, record)))
7676
if has_results:
7777
stdout.write("\r\n")
78-
if args.summarize:
79-
summary = cursor.summarize()
78+
if args.summary:
79+
summary = cursor.summary()
8080
stdout.write("Statement : %r\r\n" % summary.statement)
8181
stdout.write("Parameters : %r\r\n" % summary.parameters)
8282
stdout.write("Statement Type : %r\r\n" % summary.statement_type)

neo4j/v1/exceptions.py

+7
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,10 @@ def __init__(self, data):
3838
for key, value in data.items():
3939
if not key.startswith("_"):
4040
setattr(self, key, value)
41+
42+
43+
class DriverError(Exception):
44+
""" Raised when the driver is used incorrectly.
45+
"""
46+
47+
pass

neo4j/v1/session.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class which can be used to obtain `Driver` instances that are used for
3232

3333
from .compat import integer, string, urlparse
3434
from .connection import connect, Response, RUN, PULL_ALL
35-
from .exceptions import CypherError
35+
from .exceptions import CypherError, DriverError
3636
from .typesystem import hydrated
3737

3838

@@ -216,13 +216,18 @@ def get(self, item, default=None):
216216
except (IndexError, KeyError):
217217
return default
218218

219-
def summarize(self):
220-
""" Consume the remainder of this result and produce a summary.
219+
def summary(self):
220+
""" Return the summary from the trailing metadata. Note that this is
221+
only available once the entire result stream has been consumed.
222+
Attempting to access the summary before then will raise an error.
221223
222224
:rtype: ResultSummary
225+
:raises DriverError: if the entire result has not yet been consumed
223226
"""
224-
self._consume()
225-
return self._summary
227+
if self._consumed:
228+
return self._summary
229+
else:
230+
raise DriverError("Summary not available until the entire result has been consumed")
226231

227232
def _consume(self):
228233
# Consume the remainder of this result, triggering all appropriate callback functions.

test/test_session.py

+48-23
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from unittest import TestCase
2323

2424
from mock import patch
25+
from neo4j.v1.exceptions import DriverError
2526
from neo4j.v1.session import GraphDatabase, CypherError, Record, record
2627
from neo4j.v1.typesystem import Node, Relationship, Path
2728

@@ -191,25 +192,59 @@ def test_can_handle_cypher_error(self):
191192
with self.assertRaises(CypherError):
192193
session.run("X").close()
193194

194-
def test_can_obtain_summary_info(self):
195+
def test_keys_are_available_before_and_after_stream(self):
196+
with GraphDatabase.driver("bolt://localhost").session() as session:
197+
cursor = session.run("UNWIND range(1, 10) AS n RETURN n")
198+
assert list(cursor.keys()) == ["n"]
199+
_ = list(cursor.stream())
200+
assert list(cursor.keys()) == ["n"]
201+
202+
def test_keys_with_an_error(self):
203+
with GraphDatabase.driver("bolt://localhost").session() as session:
204+
cursor = session.run("X")
205+
with self.assertRaises(CypherError):
206+
_ = list(cursor.keys())
207+
208+
209+
class SummaryTestCase(TestCase):
210+
211+
def test_can_obtain_summary_after_consuming_result(self):
195212
with GraphDatabase.driver("bolt://localhost").session() as session:
196213
cursor = session.run("CREATE (n) RETURN n")
197-
summary = cursor.summarize()
214+
list(cursor.stream())
215+
summary = cursor.summary()
198216
assert summary.statement == "CREATE (n) RETURN n"
199217
assert summary.parameters == {}
200218
assert summary.statement_type == "rw"
201219
assert summary.statistics.nodes_created == 1
202220

221+
def test_cannot_obtain_summary_without_consuming_result(self):
222+
with GraphDatabase.driver("bolt://localhost").session() as session:
223+
cursor = session.run("CREATE (n) RETURN n")
224+
with self.assertRaises(DriverError):
225+
_ = cursor.summary()
226+
227+
# def test_can_obtain_summary_immediately_if_empty_result(self):
228+
# with GraphDatabase.driver("bolt://localhost").session() as session:
229+
# cursor = session.run("CREATE (n)")
230+
# summary = cursor.summary()
231+
# assert summary.statement == "CREATE (n)"
232+
# assert summary.parameters == {}
233+
# assert summary.statement_type == "rw"
234+
# assert summary.statistics.nodes_created == 1
235+
203236
def test_no_plan_info(self):
204237
with GraphDatabase.driver("bolt://localhost").session() as session:
205238
cursor = session.run("CREATE (n) RETURN n")
206-
assert cursor.summarize().plan is None
207-
assert cursor.summarize().profile is None
239+
list(cursor.stream())
240+
assert cursor.summary().plan is None
241+
assert cursor.summary().profile is None
208242

209243
def test_can_obtain_plan_info(self):
210244
with GraphDatabase.driver("bolt://localhost").session() as session:
211245
cursor = session.run("EXPLAIN CREATE (n) RETURN n")
212-
plan = cursor.summarize().plan
246+
list(cursor.stream())
247+
plan = cursor.summary().plan
213248
assert plan.operator_type == "ProduceResults"
214249
assert plan.identifiers == ["n"]
215250
assert plan.arguments == {"planner": "COST", "EstimatedRows": 1.0, "version": "CYPHER 3.0",
@@ -220,7 +255,8 @@ def test_can_obtain_plan_info(self):
220255
def test_can_obtain_profile_info(self):
221256
with GraphDatabase.driver("bolt://localhost").session() as session:
222257
cursor = session.run("PROFILE CREATE (n) RETURN n")
223-
profile = cursor.summarize().profile
258+
list(cursor.stream())
259+
profile = cursor.summary().profile
224260
assert profile.db_hits == 0
225261
assert profile.rows == 1
226262
assert profile.operator_type == "ProduceResults"
@@ -232,14 +268,16 @@ def test_can_obtain_profile_info(self):
232268

233269
def test_no_notification_info(self):
234270
with GraphDatabase.driver("bolt://localhost").session() as session:
235-
result = session.run("CREATE (n) RETURN n")
236-
notifications = result.summarize().notifications
271+
cursor = session.run("CREATE (n) RETURN n")
272+
list(cursor.stream())
273+
notifications = cursor.summary().notifications
237274
assert notifications == []
238275

239276
def test_can_obtain_notification_info(self):
240277
with GraphDatabase.driver("bolt://localhost").session() as session:
241-
result = session.run("EXPLAIN MATCH (n), (m) RETURN n, m")
242-
notifications = result.summarize().notifications
278+
cursor = session.run("EXPLAIN MATCH (n), (m) RETURN n, m")
279+
list(cursor.stream())
280+
notifications = cursor.summary().notifications
243281

244282
assert len(notifications) == 1
245283
notification = notifications[0]
@@ -261,19 +299,6 @@ def test_can_obtain_notification_info(self):
261299
assert position.line == 1
262300
assert position.column == 1
263301

264-
def test_keys_are_available_before_and_after_stream(self):
265-
with GraphDatabase.driver("bolt://localhost").session() as session:
266-
cursor = session.run("UNWIND range(1, 10) AS n RETURN n")
267-
assert list(cursor.keys()) == ["n"]
268-
_ = list(cursor.stream())
269-
assert list(cursor.keys()) == ["n"]
270-
271-
def test_keys_with_an_error(self):
272-
with GraphDatabase.driver("bolt://localhost").session() as session:
273-
cursor = session.run("X")
274-
with self.assertRaises(CypherError):
275-
_ = list(cursor.keys())
276-
277302

278303
class ResetTestCase(TestCase):
279304

0 commit comments

Comments
 (0)