2222from unittest import TestCase
2323
2424from mock import patch
25+ from neo4j .v1 .exceptions import ResultError
2526from neo4j .v1 .session import GraphDatabase , CypherError , Record , record
2627from neo4j .v1 .typesystem import Node , Relationship , Path
2728
@@ -85,7 +86,9 @@ def test_sessions_are_not_reused_if_still_in_use(self):
8586 def test_can_run_simple_statement (self ):
8687 session = GraphDatabase .driver ("bolt://localhost" ).session ()
8788 count = 0
88- for record in session .run ("RETURN 1 AS n" ).stream ():
89+ cursor = session .run ("RETURN 1 AS n" )
90+ assert cursor .position == - 1
91+ for record in cursor .stream ():
8992 assert record [0 ] == 1
9093 assert record ["n" ] == 1
9194 with self .assertRaises (KeyError ):
@@ -97,6 +100,7 @@ def test_can_run_simple_statement(self):
97100 _ = record [object ()]
98101 assert repr (record )
99102 assert len (record ) == 1
103+ assert cursor .position == count
100104 count += 1
101105 session .close ()
102106 assert count == 1
@@ -191,25 +195,59 @@ def test_can_handle_cypher_error(self):
191195 with self .assertRaises (CypherError ):
192196 session .run ("X" ).close ()
193197
194- def test_can_obtain_summary_info (self ):
198+ def test_keys_are_available_before_and_after_stream (self ):
199+ with GraphDatabase .driver ("bolt://localhost" ).session () as session :
200+ cursor = session .run ("UNWIND range(1, 10) AS n RETURN n" )
201+ assert list (cursor .keys ()) == ["n" ]
202+ _ = list (cursor .stream ())
203+ assert list (cursor .keys ()) == ["n" ]
204+
205+ def test_keys_with_an_error (self ):
206+ with GraphDatabase .driver ("bolt://localhost" ).session () as session :
207+ cursor = session .run ("X" )
208+ with self .assertRaises (CypherError ):
209+ _ = list (cursor .keys ())
210+
211+
212+ class SummaryTestCase (TestCase ):
213+
214+ def test_can_obtain_summary_after_consuming_result (self ):
195215 with GraphDatabase .driver ("bolt://localhost" ).session () as session :
196216 cursor = session .run ("CREATE (n) RETURN n" )
197- summary = cursor .summarize ()
217+ list (cursor .stream ())
218+ summary = cursor .summary
198219 assert summary .statement == "CREATE (n) RETURN n"
199220 assert summary .parameters == {}
200221 assert summary .statement_type == "rw"
201- assert summary .statistics .nodes_created == 1
222+ assert summary .counters .nodes_created == 1
223+
224+ def test_cannot_obtain_summary_without_consuming_result (self ):
225+ with GraphDatabase .driver ("bolt://localhost" ).session () as session :
226+ cursor = session .run ("CREATE (n) RETURN n" )
227+ with self .assertRaises (ResultError ):
228+ _ = cursor .summary
229+
230+ # def test_can_obtain_summary_immediately_if_empty_result(self):
231+ # with GraphDatabase.driver("bolt://localhost").session() as session:
232+ # cursor = session.run("CREATE (n)")
233+ # summary = cursor.summary
234+ # assert summary.statement == "CREATE (n)"
235+ # assert summary.parameters == {}
236+ # assert summary.statement_type == "rw"
237+ # assert summary.counters.nodes_created == 1
202238
203239 def test_no_plan_info (self ):
204240 with GraphDatabase .driver ("bolt://localhost" ).session () as session :
205241 cursor = session .run ("CREATE (n) RETURN n" )
206- assert cursor .summarize ().plan is None
207- assert cursor .summarize ().profile is None
242+ list (cursor .stream ())
243+ assert cursor .summary .plan is None
244+ assert cursor .summary .profile is None
208245
209246 def test_can_obtain_plan_info (self ):
210247 with GraphDatabase .driver ("bolt://localhost" ).session () as session :
211248 cursor = session .run ("EXPLAIN CREATE (n) RETURN n" )
212- plan = cursor .summarize ().plan
249+ list (cursor .stream ())
250+ plan = cursor .summary .plan
213251 assert plan .operator_type == "ProduceResults"
214252 assert plan .identifiers == ["n" ]
215253 assert plan .arguments == {"planner" : "COST" , "EstimatedRows" : 1.0 , "version" : "CYPHER 3.0" ,
@@ -220,7 +258,8 @@ def test_can_obtain_plan_info(self):
220258 def test_can_obtain_profile_info (self ):
221259 with GraphDatabase .driver ("bolt://localhost" ).session () as session :
222260 cursor = session .run ("PROFILE CREATE (n) RETURN n" )
223- profile = cursor .summarize ().profile
261+ list (cursor .stream ())
262+ profile = cursor .summary .profile
224263 assert profile .db_hits == 0
225264 assert profile .rows == 1
226265 assert profile .operator_type == "ProduceResults"
@@ -232,14 +271,16 @@ def test_can_obtain_profile_info(self):
232271
233272 def test_no_notification_info (self ):
234273 with GraphDatabase .driver ("bolt://localhost" ).session () as session :
235- result = session .run ("CREATE (n) RETURN n" )
236- notifications = result .summarize ().notifications
274+ cursor = session .run ("CREATE (n) RETURN n" )
275+ list (cursor .stream ())
276+ notifications = cursor .summary .notifications
237277 assert notifications == []
238278
239279 def test_can_obtain_notification_info (self ):
240280 with GraphDatabase .driver ("bolt://localhost" ).session () as session :
241- result = session .run ("EXPLAIN MATCH (n), (m) RETURN n, m" )
242- notifications = result .summarize ().notifications
281+ cursor = session .run ("EXPLAIN MATCH (n), (m) RETURN n, m" )
282+ list (cursor .stream ())
283+ notifications = cursor .summary .notifications
243284
244285 assert len (notifications ) == 1
245286 notification = notifications [0 ]
@@ -261,19 +302,6 @@ def test_can_obtain_notification_info(self):
261302 assert position .line == 1
262303 assert position .column == 1
263304
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-
277305
278306class ResetTestCase (TestCase ):
279307
0 commit comments