Skip to content

Conversation

@blink1073
Copy link
Member

@blink1073 blink1073 commented Aug 13, 2024

  • Add tests

@blink1073 blink1073 marked this pull request as ready for review August 13, 2024 12:39
Comment on lines 403 to 409
res: list[_DocumentType] = []
remaining = length
while self.alive:
if not await self._next_batch(res):
if remaining != 0 and not await self._next_batch(res, remaining):
break
if length != -1:
remaining = length - len(res)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I think someone can pass -2 and it be valid. Should we also throw a ValueError here as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to switch to Optional[int], per Shane's recommendation.

c = self.db.test.aggregate([{"$changeStream": {}}], maxAwaitTimeMS=1)
self.addCleanup(c.close)
docs = c.to_list(2)
self.assertEqual(len(docs), 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How long does this test take on a sharded cluster?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't work on sharded, I'll make a separate test for CommandCursor

iter = self.__aiter__()
ret = []
for _ in range(length):
ret.append(await iter.next())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call self.next() instead of using __iter__?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

await self.close()

async def to_list(self) -> list[_DocumentType]:
async def to_list(self, length: int = -1) -> list[_DocumentType]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use -1? I was thinking we'd use None to mean no limit to match Motor. Is it for nicer typing (since there's no need for Optional)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll switch to Optional[int].

self.addCleanup(coll.drop)
c = coll.find()
docs = c.to_list(3)
self.assertEqual(len(docs), 3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add another test with a small batchSize? like:

        c = coll.find(batch_size=2)
        docs = c.to_list(3)
        self.assertEqual(len(docs), 3)
        docs = c.to_list(3)
        self.assertEqual(len(docs), 2)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

docs = c.to_list()
self.assertEqual([], docs)

def test_to_list_length(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you create an identical test for CommandCursor?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a test_command_cursor_to_list_length

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use aggregate

"""Get all or some available documents from the cursor."""
if not len(self._data) and not self._killed:
await self._refresh()
print("hi", len(self._data)) # noqa: T201
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to remove a print here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@blink1073 blink1073 requested a review from ShaneHarvey August 14, 2024 00:14
Copy link
Member

@ShaneHarvey ShaneHarvey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some test issues:

FAILED test/asynchronous/test_cursor.py::TestCursor::test_to_list_length - TypeError: object of type 'coroutine' has no len()
FAILED test/asynchronous/test_session.py::TestSession::test_end_sessions - pytest.PytestUnraisableExceptionWarning: Exception ignored in: <coroutine object AsyncCursor.to_list at 0x7f99a0b51ec0>

Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/warnings.py", line 506, in _warn_unawaited_coroutine
    warn(msg, category=RuntimeWarning, stacklevel=2, source=coro)
RuntimeWarning: coroutine 'AsyncCursor.to_list' was never awaited
Coroutine created at (most recent call last)
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/unittest/async_case.py", line 158, in run
    return super().run(result)
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/unittest/case.py", line 676, in run
    self._callTestMethod(testMethod)
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/unittest/async_case.py", line 65, in _callTestMethod
    self._callMaybeAsync(method)
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/unittest/async_case.py", line 88, in _callMaybeAsync
    return self._asyncioTestLoop.run_until_complete(fut)
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/asyncio/base_events.py", line 603, in run_until_complete
    self.run_forever()
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/asyncio/base_events.py", line 570, in run_forever
    self._run_once()
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/asyncio/base_events.py", line 1851, in _run_once
    handle._run()
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/asyncio/events.py", line 81, in _run
    self._context.run(self._callback, *self._args)
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/unittest/async_case.py", line 102, in _asyncioLoopRunner
    ret = await awaitable
  File "/home/runner/work/mongo-python-driver/mongo-python-driver/test/asynchronous/test_cursor.py", line 1413, in test_to_list_length
    docs = c.to_list(3)
= 2 failed, 2347 passed, 2083 skipped, 667 deselected, 26 xfailed, 38 warnings in 189.81s (0:03:09) =

@blink1073
Copy link
Member Author

Fixed the tests.

@blink1073 blink1073 requested a review from ShaneHarvey August 14, 2024 02:34
@blink1073 blink1073 merged commit adf8817 into mongodb:master Aug 14, 2024
@blink1073 blink1073 deleted the PYTHON-4584 branch August 14, 2024 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants