Skip to content

Commit 913bcd0

Browse files
authored
Add Result.__next__ + clean-up docs (#647)
1 parent 8e4b9cf commit 913bcd0

File tree

7 files changed

+43
-117
lines changed

7 files changed

+43
-117
lines changed

docs/source/api.rst

+2
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,8 @@ A :class:`neo4j.Result` is attached to an active connection, through a :class:`n
745745

746746
.. describe:: iter(result)
747747

748+
.. describe:: next(result)
749+
748750
.. automethod:: keys
749751

750752
.. automethod:: consume

docs/source/async_api.rst

+5-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,11 @@ A :class:`neo4j.AsyncResult` is attached to an active connection, through a :cla
475475

476476
.. autoclass:: neo4j.AsyncResult()
477477

478-
.. describe:: iter(result)
478+
.. method:: result.__aiter__()
479+
:async:
480+
481+
.. method:: result.__anext__()
482+
:async:
479483

480484
.. automethod:: keys
481485

docs/source/results.rst

-110
This file was deleted.

neo4j/_async/work/result.py

+3
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ async def __aiter__(self):
197197

198198
self._closed = True
199199

200+
async def __anext__(self):
201+
return await self.__aiter__().__anext__()
202+
200203
async def _attach(self):
201204
"""Sets the Result object in an attached state by fetching messages from
202205
the connection to the buffer.

neo4j/_sync/work/result.py

+3
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ def __iter__(self):
197197

198198
self._closed = True
199199

200+
def __next__(self):
201+
return self.__iter__().__next__()
202+
200203
def _attach(self):
201204
"""Sets the Result object in an attached state by fetching messages from
202205
the connection to the buffer.

tests/unit/async_/work/test_result.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ async def fetch_and_compare_all_records(
198198
if limit is None:
199199
assert result._closed
200200
elif method == "next":
201+
n = len(expected_records) if limit is None else limit
202+
for _ in range(n):
203+
record = await AsyncUtil.next(result)
204+
received_records.append([record.get(key, None)])
205+
if limit is None:
206+
with pytest.raises(StopAsyncIteration):
207+
await AsyncUtil.next(result)
208+
assert result._closed
209+
elif method == "one iter":
201210
iter_ = AsyncUtil.iter(result)
202211
n = len(expected_records) if limit is None else limit
203212
for _ in range(n):
@@ -223,7 +232,8 @@ async def fetch_and_compare_all_records(
223232
assert received_records == expected_records
224233

225234

226-
@pytest.mark.parametrize("method", ("for loop", "next", "new iter"))
235+
@pytest.mark.parametrize("method",
236+
("for loop", "next", "one iter", "new iter"))
227237
@pytest.mark.parametrize("records", (
228238
[],
229239
[[42]],
@@ -237,7 +247,8 @@ async def test_result_iteration(method, records):
237247
await fetch_and_compare_all_records(result, "x", records, method)
238248

239249

240-
@pytest.mark.parametrize("method", ("for loop", "next", "new iter"))
250+
@pytest.mark.parametrize("method",
251+
("for loop", "next", "one iter", "new iter"))
241252
@pytest.mark.parametrize("invert_fetch", (True, False))
242253
@mark_async_test
243254
async def test_parallel_result_iteration(method, invert_fetch):
@@ -266,7 +277,8 @@ async def test_parallel_result_iteration(method, invert_fetch):
266277
)
267278

268279

269-
@pytest.mark.parametrize("method", ("for loop", "next", "new iter"))
280+
@pytest.mark.parametrize("method",
281+
("for loop", "next", "one iter", "new iter"))
270282
@pytest.mark.parametrize("invert_fetch", (True, False))
271283
@mark_async_test
272284
async def test_interwoven_result_iteration(method, invert_fetch):

tests/unit/sync/work/test_result.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ def fetch_and_compare_all_records(
198198
if limit is None:
199199
assert result._closed
200200
elif method == "next":
201+
n = len(expected_records) if limit is None else limit
202+
for _ in range(n):
203+
record = Util.next(result)
204+
received_records.append([record.get(key, None)])
205+
if limit is None:
206+
with pytest.raises(StopIteration):
207+
Util.next(result)
208+
assert result._closed
209+
elif method == "one iter":
201210
iter_ = Util.iter(result)
202211
n = len(expected_records) if limit is None else limit
203212
for _ in range(n):
@@ -223,7 +232,8 @@ def fetch_and_compare_all_records(
223232
assert received_records == expected_records
224233

225234

226-
@pytest.mark.parametrize("method", ("for loop", "next", "new iter"))
235+
@pytest.mark.parametrize("method",
236+
("for loop", "next", "one iter", "new iter"))
227237
@pytest.mark.parametrize("records", (
228238
[],
229239
[[42]],
@@ -237,7 +247,8 @@ def test_result_iteration(method, records):
237247
fetch_and_compare_all_records(result, "x", records, method)
238248

239249

240-
@pytest.mark.parametrize("method", ("for loop", "next", "new iter"))
250+
@pytest.mark.parametrize("method",
251+
("for loop", "next", "one iter", "new iter"))
241252
@pytest.mark.parametrize("invert_fetch", (True, False))
242253
@mark_sync_test
243254
def test_parallel_result_iteration(method, invert_fetch):
@@ -266,7 +277,8 @@ def test_parallel_result_iteration(method, invert_fetch):
266277
)
267278

268279

269-
@pytest.mark.parametrize("method", ("for loop", "next", "new iter"))
280+
@pytest.mark.parametrize("method",
281+
("for loop", "next", "one iter", "new iter"))
270282
@pytest.mark.parametrize("invert_fetch", (True, False))
271283
@mark_sync_test
272284
def test_interwoven_result_iteration(method, invert_fetch):

0 commit comments

Comments
 (0)