From 048ee81836fa477e4f4052bea5b8e6ae58483c48 Mon Sep 17 00:00:00 2001 From: Prashant Mital <5883388+prashantmital@users.noreply.github.com> Date: Mon, 17 May 2021 13:58:06 -0700 Subject: [PATCH] PYTHON-2719 RawBatchCursor must raise StopIteration instead of returning empty bytes when the cursor contains no results (#624) --- pymongo/message.py | 4 +++- test/test_cursor.py | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pymongo/message.py b/pymongo/message.py index 25de5a72f..d08636ff1 100644 --- a/pymongo/message.py +++ b/pymongo/message.py @@ -1526,7 +1526,9 @@ class _OpReply(object): error_object.get("$err"), error_object.get("code"), error_object) - return [self.documents] + if self.documents: + return [self.documents] + return [] def unpack_response(self, cursor_id=None, codec_options=_UNICODE_REPLACE_CODEC_OPTIONS, diff --git a/test/test_cursor.py b/test/test_cursor.py index 582d0641e..fb657c3f6 100644 --- a/test/test_cursor.py +++ b/test/test_cursor.py @@ -1471,7 +1471,14 @@ class TestRawBatchCursor(IntegrationTest): explanation = c.find_raw_batches().explain() self.assertIsInstance(explanation, dict) + def test_empty(self): + self.db.test.drop() + cursor = self.db.test.find_raw_batches() + with self.assertRaises(StopIteration): + next(cursor) + def test_clone(self): + self.db.test.insert_one({}) cursor = self.db.test.find_raw_batches() # Copy of a RawBatchCursor is also a RawBatchCursor, not a Cursor. self.assertIsInstance(next(cursor.clone()), bytes)