2222import pytest
2323
2424
25- from neo4j .exceptions import Neo4jError
26-
27-
28- def test_can_consume_result_immediately (session ):
29-
30- def f (tx ):
31- result = tx .run ("UNWIND range(1, 3) AS n RETURN n" )
32- assert [record [0 ] for record in result ] == [1 , 2 , 3 ]
33-
34- session .read_transaction (f )
35-
36-
37- def test_can_consume_result_from_buffer (session ):
38-
39- def f (tx ):
40- result = tx .run ("UNWIND range(1, 3) AS n RETURN n" )
41- result ._buffer_all ()
42- assert [record [0 ] for record in result ] == [1 , 2 , 3 ]
43-
44- session .read_transaction (f )
45-
46-
47- @pytest .mark .skip (reason = "This behaviour have changed in 4.0. transaction.commit/rollback -> DISCARD n=-1, COMMIT/ROLL_BACK" )
48- def test_can_consume_result_after_commit (session ):
49- tx = session .begin_transaction ()
50- result = tx .run ("UNWIND range(1, 3) AS n RETURN n" )
51- tx .commit ()
52- assert [record [0 ] for record in result ] == [1 , 2 , 3 ]
53-
54-
55- @pytest .mark .skip (reason = "This behaviour have changed in 4.0. transaction.commit/rollback -> DISCARD n=-1, COMMIT/ROLL_BACK" )
56- def test_can_consume_result_after_rollback (session ):
57- tx = session .begin_transaction ()
58- result = tx .run ("UNWIND range(1, 3) AS n RETURN n" )
59- tx .rollback ()
60- assert [record [0 ] for record in result ] == [1 , 2 , 3 ]
61-
62-
63- @pytest .mark .skip (reason = "This behaviour have changed. transaction.commit/rollback -> DISCARD n=-1, COMMIT/ROLL_BACK" )
64- def test_can_consume_result_after_session_close (bolt_driver ):
65- with bolt_driver .session () as session :
66- tx = session .begin_transaction ()
67- result = tx .run ("UNWIND range(1, 3) AS n RETURN n" )
68- tx .commit ()
69- assert [record [0 ] for record in result ] == [1 , 2 , 3 ]
70-
71-
72- @pytest .mark .skip (reason = "This behaviour have changed. transaction.commit/rollback -> DISCARD n=-1, COMMIT/ROLL_BACK" )
73- def test_can_consume_result_after_session_reuse (bolt_driver ):
74- session = bolt_driver .session ()
75- tx = session .begin_transaction ()
76- result_a = tx .run ("UNWIND range(1, 3) AS n RETURN n" )
77- tx .commit ()
78- session .close ()
79- session = bolt_driver .session ()
80- tx = session .begin_transaction ()
81- result_b = tx .run ("UNWIND range(4, 6) AS n RETURN n" )
82- tx .commit ()
83- session .close ()
84- assert [record [0 ] for record in result_a ] == [1 , 2 , 3 ]
85- assert [record [0 ] for record in result_b ] == [4 , 5 , 6 ]
86-
87-
88- def test_can_consume_results_after_harsh_session_death (bolt_driver ):
89- session = bolt_driver .session ()
90- result_a = session .run ("UNWIND range(1, 3) AS n RETURN n" )
91- del session
92- session = bolt_driver .session ()
93- result_b = session .run ("UNWIND range(4, 6) AS n RETURN n" )
94- del session
95- assert [record [0 ] for record in result_a ] == [1 , 2 , 3 ]
96- assert [record [0 ] for record in result_b ] == [4 , 5 , 6 ]
97-
98-
99- @pytest .mark .skip (reason = "This behaviour have changed. transaction.commit/rollback -> DISCARD n=-1, COMMIT/ROLL_BACK" )
100- def test_can_consume_result_after_session_with_error (bolt_driver ):
101- session = bolt_driver .session ()
102- with pytest .raises (Neo4jError ):
103- session .run ("X" ).consume ()
104- session .close ()
105- session = bolt_driver .session ()
106- tx = session .begin_transaction ()
107- result = tx .run ("UNWIND range(1, 3) AS n RETURN n" )
108- tx .commit ()
109- session .close ()
110- assert [record [0 ] for record in result ] == [1 , 2 , 3 ]
111-
112-
113- def test_single_with_exactly_one_record (session ):
114- result = session .run ("UNWIND range(1, 1) AS n RETURN n" )
115- record = result .single ()
116- assert list (record .values ()) == [1 ]
117-
118-
119- # def test_value_with_no_records(session):
120- # result = session.run("CREATE ()")
121- # assert result.value() == []
122- #
123- #
124- # def test_values_with_no_records(session):
125- # result = session.run("CREATE ()")
126- # assert result.values() == []
127-
128-
129- def test_peek_can_look_one_ahead (session ):
130- result = session .run ("UNWIND range(1, 3) AS n RETURN n" )
131- record = result .peek ()
132- assert list (record .values ()) == [1 ]
133-
134-
135- def test_peek_fails_if_nothing_remains (neo4j_driver ):
136- with neo4j_driver .session () as session :
137- result = session .run ("CREATE ()" )
138- upcoming = result .peek ()
139- assert upcoming is None
140-
141-
142- def test_peek_does_not_advance_cursor (session ):
143- result = session .run ("UNWIND range(1, 3) AS n RETURN n" )
144- result .peek ()
145- assert [record [0 ] for record in result ] == [1 , 2 , 3 ]
146-
147-
148- def test_peek_at_different_stages (session ):
149- result = session .run ("UNWIND range(0, 9) AS n RETURN n" )
150- # Peek ahead to the first record
151- expected_next = 0
152- upcoming = result .peek ()
153- assert upcoming [0 ] == expected_next
154- # Then look through all the other records
155- for expected , record in enumerate (result ):
156- # Check this record is as expected
157- assert record [0 ] == expected
158- # Check the upcoming record is as expected...
159- if expected < 9 :
160- # ...when one should follow
161- expected_next = expected + 1
162- upcoming = result .peek ()
163- assert upcoming [0 ] == expected_next
164- else :
165- # ...when none should follow
166- upcoming = result .peek ()
167- assert upcoming is None
168-
169-
170- def test_can_safely_exit_session_without_consuming_result (session ):
171- session .run ("RETURN 1" )
172- assert True
173-
174-
175- def test_multiple_record_value_case_a (session ):
176- result = session .run ("UNWIND range(1, 3) AS n "
177- "RETURN 1 * n AS x, 2 * n AS y, 3 * n AS z" )
178- values = []
179- for record in result :
180- values .append (record .value (key = 0 , default = None ))
181- assert values == [1 , 2 , 3 ]
182-
183-
184- def test_multiple_record_value_case_b (session ):
185- result = session .run ("UNWIND range(1, 3) AS n "
186- "RETURN 1 * n AS x, 2 * n AS y, 3 * n AS z" )
187- values = []
188- for record in result :
189- values .append (record .value (key = 2 , default = None ))
190- assert values == [3 , 6 , 9 ]
191-
192-
193- def test_multiple_record_value_case_c (session ):
194- result = session .run ("UNWIND range(1, 3) AS n "
195- "RETURN 1 * n AS x, 2 * n AS y, 3 * n AS z" )
196- values = []
197- for record in result :
198- values .append (record .value (key = "z" , default = None ))
199- assert values == [3 , 6 , 9 ]
200-
201-
202- def test_record_values_case_a (session ):
203- result = session .run ("UNWIND range(1, 3) AS n "
204- "RETURN 1 * n AS x, 2 * n AS y, 3 * n AS z" )
205- values = []
206- for record in result :
207- values .append (record .values ())
208- assert values == [[1 , 2 , 3 ], [2 , 4 , 6 ], [3 , 6 , 9 ]]
209-
210-
211- def test_record_values_case_b (session ):
212- result = session .run ("UNWIND range(1, 3) AS n "
213- "RETURN 1 * n AS x, 2 * n AS y, 3 * n AS z" )
214- values = []
215- for record in result :
216- values .append (record .values (2 , 0 ))
217- assert values == [[3 , 1 ], [6 , 2 ], [9 , 3 ]]
218-
219-
220- def test_record_values_case_c (session ):
221- result = session .run ("UNWIND range(1, 3) AS n "
222- "RETURN 1 * n AS x, 2 * n AS y, 3 * n AS z" )
223- values = []
224- for record in result :
225- values .append (record .values ("z" , "x" ))
226- assert values == [[3 , 1 ], [6 , 2 ], [9 , 3 ]]
227-
228-
229- def test_no_records (neo4j_driver ):
230- with neo4j_driver .session () as session :
231- result = session .run ("CREATE ()" )
232- values = []
233- for record in result :
234- values .append (record .value ())
235- assert values == []
236-
237-
25+ # TODO: this test will stay until a uniform behavior for `.single()` across the
26+ # drivers has been specified and tests are created in testkit
23827def test_result_single_with_no_records (session ):
23928 result = session .run ("CREATE ()" )
24029 record = result .single ()
24130 assert record is None
24231
24332
33+ # TODO: this test will stay until a uniform behavior for `.single()` across the
34+ # drivers has been specified and tests are created in testkit
24435def test_result_single_with_one_record (session ):
24536 result = session .run ("UNWIND [1] AS n RETURN n" )
24637 record = result .single ()
24738 assert record ["n" ] == 1
24839
24940
41+ # TODO: this test will stay until a uniform behavior for `.single()` across the
42+ # drivers has been specified and tests are created in testkit
25043def test_result_single_with_multiple_records (session ):
25144 import warnings
25245 result = session .run ("UNWIND [1, 2, 3] AS n RETURN n" )
@@ -255,57 +48,11 @@ def test_result_single_with_multiple_records(session):
25548 assert record [0 ] == 1
25649
25750
51+ # TODO: this test will stay until a uniform behavior for `.single()` across the
52+ # drivers has been specified and tests are created in testkit
25853def test_result_single_consumes_the_result (session ):
25954 result = session .run ("UNWIND [1, 2, 3] AS n RETURN n" )
26055 with pytest .warns (UserWarning , match = "Expected a result with a single record" ):
26156 _ = result .single ()
26257 records = list (result )
26358 assert records == []
264-
265-
266- def test_single_value (session ):
267- result = session .run ("RETURN 1 AS x, 2 AS y, 3 AS z" )
268- assert result .single ().value () == 1
269-
270-
271- def test_single_indexed_value (session ):
272- result = session .run ("RETURN 1 AS x, 2 AS y, 3 AS z" )
273- assert result .single ().value (2 ) == 3
274-
275-
276- def test_single_keyed_value (session ):
277- result = session .run ("RETURN 1 AS x, 2 AS y, 3 AS z" )
278- assert result .single ().value ("z" ) == 3
279-
280-
281- def test_single_values (session ):
282- result = session .run ("RETURN 1 AS x, 2 AS y, 3 AS z" )
283- assert result .single ().values () == [1 , 2 , 3 ]
284-
285-
286- def test_single_indexed_values (session ):
287- result = session .run ("RETURN 1 AS x, 2 AS y, 3 AS z" )
288- assert result .single ().values (2 , 0 ) == [3 , 1 ]
289-
290-
291- def test_single_keyed_values (session ):
292- result = session .run ("RETURN 1 AS x, 2 AS y, 3 AS z" )
293- assert result .single ().values ("z" , "x" ) == [3 , 1 ]
294-
295-
296- def test_result_with_helper_function_value (session ):
297-
298- def f (tx ):
299- result = tx .run ("UNWIND range(1, 3) AS n RETURN n" )
300- assert result .value (0 ) == [1 , 2 , 3 ]
301-
302- session .read_transaction (f )
303-
304-
305- def test_result_with_helper_function_values (session ):
306-
307- def f (tx ):
308- result = tx .run ("UNWIND range(1, 3) AS n RETURN n, 0" )
309- assert result .values (0 , 1 ) == [[1 , 0 ], [2 , 0 ], [3 , 0 ]]
310-
311- session .read_transaction (f )
0 commit comments