Skip to content

Add Result.__next__ + clean-up docs #647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,8 @@ A :class:`neo4j.Result` is attached to an active connection, through a :class:`n

.. describe:: iter(result)

.. describe:: next(result)

.. automethod:: keys

.. automethod:: consume
Expand Down
6 changes: 5 additions & 1 deletion docs/source/async_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,11 @@ A :class:`neo4j.AsyncResult` is attached to an active connection, through a :cla

.. autoclass:: neo4j.AsyncResult()

.. describe:: iter(result)
.. method:: result.__aiter__()
:async:

.. method:: result.__anext__()
:async:

.. automethod:: keys

Expand Down
110 changes: 0 additions & 110 deletions docs/source/results.rst

This file was deleted.

3 changes: 3 additions & 0 deletions neo4j/_async/work/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ async def __aiter__(self):

self._closed = True

async def __anext__(self):
return await self.__aiter__().__anext__()

async def _attach(self):
"""Sets the Result object in an attached state by fetching messages from
the connection to the buffer.
Expand Down
3 changes: 3 additions & 0 deletions neo4j/_sync/work/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ def __iter__(self):

self._closed = True

def __next__(self):
return self.__iter__().__next__()

def _attach(self):
"""Sets the Result object in an attached state by fetching messages from
the connection to the buffer.
Expand Down
18 changes: 15 additions & 3 deletions tests/unit/async_/work/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ async def fetch_and_compare_all_records(
if limit is None:
assert result._closed
elif method == "next":
n = len(expected_records) if limit is None else limit
for _ in range(n):
record = await AsyncUtil.next(result)
received_records.append([record.get(key, None)])
if limit is None:
with pytest.raises(StopAsyncIteration):
await AsyncUtil.next(result)
assert result._closed
elif method == "one iter":
iter_ = AsyncUtil.iter(result)
n = len(expected_records) if limit is None else limit
for _ in range(n):
Expand All @@ -223,7 +232,8 @@ async def fetch_and_compare_all_records(
assert received_records == expected_records


@pytest.mark.parametrize("method", ("for loop", "next", "new iter"))
@pytest.mark.parametrize("method",
("for loop", "next", "one iter", "new iter"))
@pytest.mark.parametrize("records", (
[],
[[42]],
Expand All @@ -237,7 +247,8 @@ async def test_result_iteration(method, records):
await fetch_and_compare_all_records(result, "x", records, method)


@pytest.mark.parametrize("method", ("for loop", "next", "new iter"))
@pytest.mark.parametrize("method",
("for loop", "next", "one iter", "new iter"))
@pytest.mark.parametrize("invert_fetch", (True, False))
@mark_async_test
async def test_parallel_result_iteration(method, invert_fetch):
Expand Down Expand Up @@ -266,7 +277,8 @@ async def test_parallel_result_iteration(method, invert_fetch):
)


@pytest.mark.parametrize("method", ("for loop", "next", "new iter"))
@pytest.mark.parametrize("method",
("for loop", "next", "one iter", "new iter"))
@pytest.mark.parametrize("invert_fetch", (True, False))
@mark_async_test
async def test_interwoven_result_iteration(method, invert_fetch):
Expand Down
18 changes: 15 additions & 3 deletions tests/unit/sync/work/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ def fetch_and_compare_all_records(
if limit is None:
assert result._closed
elif method == "next":
n = len(expected_records) if limit is None else limit
for _ in range(n):
record = Util.next(result)
received_records.append([record.get(key, None)])
if limit is None:
with pytest.raises(StopIteration):
Util.next(result)
assert result._closed
elif method == "one iter":
iter_ = Util.iter(result)
n = len(expected_records) if limit is None else limit
for _ in range(n):
Expand All @@ -223,7 +232,8 @@ def fetch_and_compare_all_records(
assert received_records == expected_records


@pytest.mark.parametrize("method", ("for loop", "next", "new iter"))
@pytest.mark.parametrize("method",
("for loop", "next", "one iter", "new iter"))
@pytest.mark.parametrize("records", (
[],
[[42]],
Expand All @@ -237,7 +247,8 @@ def test_result_iteration(method, records):
fetch_and_compare_all_records(result, "x", records, method)


@pytest.mark.parametrize("method", ("for loop", "next", "new iter"))
@pytest.mark.parametrize("method",
("for loop", "next", "one iter", "new iter"))
@pytest.mark.parametrize("invert_fetch", (True, False))
@mark_sync_test
def test_parallel_result_iteration(method, invert_fetch):
Expand Down Expand Up @@ -266,7 +277,8 @@ def test_parallel_result_iteration(method, invert_fetch):
)


@pytest.mark.parametrize("method", ("for loop", "next", "new iter"))
@pytest.mark.parametrize("method",
("for loop", "next", "one iter", "new iter"))
@pytest.mark.parametrize("invert_fetch", (True, False))
@mark_sync_test
def test_interwoven_result_iteration(method, invert_fetch):
Expand Down