PYTHON-4533 - Convert test/test_cursor.py to async (#1731)

This commit is contained in:
Noah Stapp 2024-07-16 13:55:11 -07:00 committed by GitHub
parent b6f72adb21
commit f0e025a127
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 2013 additions and 191 deletions

View File

@ -424,4 +424,4 @@ class AsyncRawBatchCommandCursor(AsyncCommandCursor[_DocumentType]):
return raw_response # type: ignore[return-value]
def __getitem__(self, index: int) -> NoReturn:
raise InvalidOperation("Cannot call __getitem__ on RawBatchCursor")
raise InvalidOperation("Cannot call __getitem__ on AsyncRawBatchCommandCursor")

View File

@ -242,6 +242,7 @@ class AsyncCursor(Generic[_DocumentType]):
self._exhaust_checked = True
self._supports_exhaust() # type: ignore[unused-coroutine]
else:
self._exhaust = cursor_type == CursorType.EXHAUST
self._exhaust_checked = False
async def _supports_exhaust(self) -> None:
@ -1318,4 +1319,4 @@ class AsyncRawBatchCursor(AsyncCursor, Generic[_DocumentType]):
return await clone.explain()
def __getitem__(self, index: Any) -> NoReturn:
raise InvalidOperation("Cannot call __getitem__ on RawBatchCursor")
raise InvalidOperation("Cannot call __getitem__ on AsyncRawBatchCursor")

View File

@ -424,4 +424,4 @@ class RawBatchCommandCursor(CommandCursor[_DocumentType]):
return raw_response # type: ignore[return-value]
def __getitem__(self, index: int) -> NoReturn:
raise InvalidOperation("Cannot call __getitem__ on RawBatchCursor")
raise InvalidOperation("Cannot call __getitem__ on RawBatchCommandCursor")

View File

@ -242,6 +242,7 @@ class Cursor(Generic[_DocumentType]):
self._exhaust_checked = True
self._supports_exhaust() # type: ignore[unused-coroutine]
else:
self._exhaust = cursor_type == CursorType.EXHAUST
self._exhaust_checked = False
def _supports_exhaust(self) -> None:

File diff suppressed because it is too large Load Diff

View File

@ -48,8 +48,11 @@ from pymongo.operations import _IndexList
from pymongo.read_concern import ReadConcern
from pymongo.read_preferences import ReadPreference
from pymongo.synchronous.cursor import Cursor, CursorType
from pymongo.synchronous.helpers import next
from pymongo.write_concern import WriteConcern
_IS_SYNC = True
class TestCursor(IntegrationTest):
def test_deepcopy_cursor_littered_with_regexes(self):
@ -142,7 +145,8 @@ class TestCursor(IntegrationTest):
db.pymongo_test.drop()
coll = db.pymongo_test
self.assertRaises(TypeError, coll.find().allow_disk_use, "baz")
with self.assertRaises(TypeError):
coll.find().allow_disk_use("baz") # type: ignore[arg-type]
cursor = coll.find().allow_disk_use(True)
self.assertEqual(True, cursor._allow_disk_use)
@ -153,7 +157,8 @@ class TestCursor(IntegrationTest):
db = self.db
db.pymongo_test.drop()
coll = db.pymongo_test
self.assertRaises(TypeError, coll.find().max_time_ms, "foo")
with self.assertRaises(TypeError):
coll.find().max_time_ms("foo") # type: ignore[arg-type]
coll.insert_one({"amalia": 1})
coll.insert_one({"amalia": 2})
@ -185,7 +190,8 @@ class TestCursor(IntegrationTest):
pass
else:
self.fail("ExecutionTimeout not raised")
self.assertRaises(ExecutionTimeout, coll.find_one, max_time_ms=1)
with self.assertRaises(ExecutionTimeout):
coll.find_one(max_time_ms=1)
finally:
client.admin.command("configureFailPoint", "maxTimeAlwaysTimeOut", mode="off")
@ -194,7 +200,8 @@ class TestCursor(IntegrationTest):
db.pymongo_test.drop()
coll = db.create_collection("pymongo_test", capped=True, size=4096)
self.assertRaises(TypeError, coll.find().max_await_time_ms, "foo")
with self.assertRaises(TypeError):
coll.find().max_await_time_ms("foo") # type: ignore[arg-type]
coll.insert_one({"amalia": 1})
coll.insert_one({"amalia": 2})
@ -223,10 +230,10 @@ class TestCursor(IntegrationTest):
self.assertEqual(90, cursor._max_await_time_ms)
listener = AllowListEventListener("find", "getMore")
coll = rs_or_single_client(event_listeners=[listener])[self.db.name].pymongo_test
coll = (rs_or_single_client(event_listeners=[listener]))[self.db.name].pymongo_test
# Tailable_await defaults.
list(coll.find(cursor_type=CursorType.TAILABLE_AWAIT))
coll.find(cursor_type=CursorType.TAILABLE_AWAIT).to_list()
# find
self.assertFalse("maxTimeMS" in listener.started_events[0].command)
# getMore
@ -234,7 +241,7 @@ class TestCursor(IntegrationTest):
listener.reset()
# Tailable_await with max_await_time_ms set.
list(coll.find(cursor_type=CursorType.TAILABLE_AWAIT).max_await_time_ms(99))
coll.find(cursor_type=CursorType.TAILABLE_AWAIT).max_await_time_ms(99).to_list()
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertFalse("maxTimeMS" in listener.started_events[0].command)
@ -244,8 +251,11 @@ class TestCursor(IntegrationTest):
self.assertEqual(99, listener.started_events[1].command["maxTimeMS"])
listener.reset()
# Tailable_await with max_time_ms
list(coll.find(cursor_type=CursorType.TAILABLE_AWAIT).max_time_ms(99))
# Tailable_await with max_time_ms and make sure list() works on synchronous cursors
if _IS_SYNC:
list(coll.find(cursor_type=CursorType.TAILABLE_AWAIT).max_time_ms(99)) # type: ignore[call-overload]
else:
coll.find(cursor_type=CursorType.TAILABLE_AWAIT).max_time_ms(99).to_list()
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertTrue("maxTimeMS" in listener.started_events[0].command)
@ -256,7 +266,12 @@ class TestCursor(IntegrationTest):
listener.reset()
# Tailable_await with both max_time_ms and max_await_time_ms
list(coll.find(cursor_type=CursorType.TAILABLE_AWAIT).max_time_ms(99).max_await_time_ms(99))
(
coll.find(cursor_type=CursorType.TAILABLE_AWAIT)
.max_time_ms(99)
.max_await_time_ms(99)
.to_list()
)
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertTrue("maxTimeMS" in listener.started_events[0].command)
@ -268,7 +283,7 @@ class TestCursor(IntegrationTest):
listener.reset()
# Non tailable_await with max_await_time_ms
list(coll.find(batch_size=1).max_await_time_ms(99))
coll.find(batch_size=1).max_await_time_ms(99).to_list()
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertFalse("maxTimeMS" in listener.started_events[0].command)
@ -278,7 +293,7 @@ class TestCursor(IntegrationTest):
listener.reset()
# Non tailable_await with max_time_ms
list(coll.find(batch_size=1).max_time_ms(99))
coll.find(batch_size=1).max_time_ms(99).to_list()
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertTrue("maxTimeMS" in listener.started_events[0].command)
@ -288,7 +303,7 @@ class TestCursor(IntegrationTest):
self.assertFalse("maxTimeMS" in listener.started_events[1].command)
# Non tailable_await with both max_time_ms and max_await_time_ms
list(coll.find(batch_size=1).max_time_ms(99).max_await_time_ms(88))
coll.find(batch_size=1).max_time_ms(99).max_await_time_ms(88).to_list()
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertTrue("maxTimeMS" in listener.started_events[0].command)
@ -311,7 +326,7 @@ class TestCursor(IntegrationTest):
try:
try:
# Iterate up to first getmore.
list(cursor)
cursor.to_list()
except ExecutionTimeout:
pass
else:
@ -340,19 +355,16 @@ class TestCursor(IntegrationTest):
def test_hint(self):
db = self.db
self.assertRaises(TypeError, db.test.find().hint, 5.5)
with self.assertRaises(TypeError):
db.test.find().hint(5.5) # type: ignore[arg-type]
db.test.drop()
db.test.insert_many([{"num": i, "foo": i} for i in range(100)])
self.assertRaises(
OperationFailure,
db.test.find({"num": 17, "foo": 17}).hint([("num", ASCENDING)]).explain,
)
self.assertRaises(
OperationFailure,
db.test.find({"num": 17, "foo": 17}).hint([("foo", ASCENDING)]).explain,
)
with self.assertRaises(OperationFailure):
db.test.find({"num": 17, "foo": 17}).hint([("num", ASCENDING)]).explain()
with self.assertRaises(OperationFailure):
db.test.find({"num": 17, "foo": 17}).hint([("foo", ASCENDING)]).explain()
spec: list[Any] = [("num", DESCENDING)]
_ = db.test.create_index(spec)
@ -361,10 +373,8 @@ class TestCursor(IntegrationTest):
self.assertEqual(0, first.get("num"))
first = next(db.test.find().hint(spec))
self.assertEqual(99, first.get("num"))
self.assertRaises(
OperationFailure,
db.test.find({"num": 17, "foo": 17}).hint([("foo", ASCENDING)]).explain,
)
with self.assertRaises(OperationFailure):
db.test.find({"num": 17, "foo": 17}).hint([("foo", ASCENDING)]).explain()
a = db.test.find({"num": 17})
a.hint(spec)
@ -402,10 +412,13 @@ class TestCursor(IntegrationTest):
def test_limit(self):
db = self.db
self.assertRaises(TypeError, db.test.find().limit, None)
self.assertRaises(TypeError, db.test.find().limit, "hello")
self.assertRaises(TypeError, db.test.find().limit, 5.5)
self.assertTrue(db.test.find().limit(5))
with self.assertRaises(TypeError):
db.test.find().limit(None) # type: ignore[arg-type]
with self.assertRaises(TypeError):
db.test.find().limit("hello") # type: ignore[arg-type]
with self.assertRaises(TypeError):
db.test.find().limit(5.5) # type: ignore[arg-type]
self.assertTrue((db.test.find()).limit(5))
db.test.drop()
db.test.insert_many([{"x": i} for i in range(100)])
@ -444,7 +457,8 @@ class TestCursor(IntegrationTest):
a.limit(10)
for _ in a:
break
self.assertRaises(InvalidOperation, a.limit, 5)
with self.assertRaises(InvalidOperation):
a.limit(5)
def test_max(self):
db = self.db
@ -458,28 +472,31 @@ class TestCursor(IntegrationTest):
return db.test.find().max(max_spec).hint(expected_index)
cursor = find([("j", 3)], j_index)
self.assertEqual(len(list(cursor)), 3)
self.assertEqual(len(cursor.to_list()), 3)
# Tuple.
cursor = find((("j", 3),), j_index)
self.assertEqual(len(list(cursor)), 3)
self.assertEqual(len(cursor.to_list()), 3)
# Compound index.
index_keys = [("j", ASCENDING), ("k", ASCENDING)]
db.test.create_index(index_keys)
cursor = find([("j", 3), ("k", 3)], index_keys)
self.assertEqual(len(list(cursor)), 3)
self.assertEqual(len(cursor.to_list()), 3)
# Wrong order.
cursor = find([("k", 3), ("j", 3)], index_keys)
self.assertRaises(OperationFailure, list, cursor)
with self.assertRaises(OperationFailure):
cursor.to_list()
# No such index.
cursor = find([("k", 3)], "k")
self.assertRaises(OperationFailure, list, cursor)
self.assertRaises(TypeError, db.test.find().max, 10)
self.assertRaises(TypeError, db.test.find().max, {"j": 10})
with self.assertRaises(OperationFailure):
cursor.to_list()
with self.assertRaises(TypeError):
db.test.find().max(10) # type: ignore[arg-type]
with self.assertRaises(TypeError):
db.test.find().max({"j": 10}) # type: ignore[arg-type]
def test_min(self):
db = self.db
@ -493,28 +510,32 @@ class TestCursor(IntegrationTest):
return db.test.find().min(min_spec).hint(expected_index)
cursor = find([("j", 3)], j_index)
self.assertEqual(len(list(cursor)), 7)
self.assertEqual(len(cursor.to_list()), 7)
# Tuple.
cursor = find((("j", 3),), j_index)
self.assertEqual(len(list(cursor)), 7)
self.assertEqual(len(cursor.to_list()), 7)
# Compound index.
index_keys = [("j", ASCENDING), ("k", ASCENDING)]
db.test.create_index(index_keys)
cursor = find([("j", 3), ("k", 3)], index_keys)
self.assertEqual(len(list(cursor)), 7)
self.assertEqual(len(cursor.to_list()), 7)
# Wrong order.
cursor = find([("k", 3), ("j", 3)], index_keys)
self.assertRaises(OperationFailure, list, cursor)
with self.assertRaises(OperationFailure):
cursor.to_list()
# No such index.
cursor = find([("k", 3)], "k")
self.assertRaises(OperationFailure, list, cursor)
with self.assertRaises(OperationFailure):
cursor.to_list()
self.assertRaises(TypeError, db.test.find().min, 10)
self.assertRaises(TypeError, db.test.find().min, {"j": 10})
with self.assertRaises(TypeError):
db.test.find().min(10) # type: ignore[arg-type]
with self.assertRaises(TypeError):
db.test.find().min({"j": 10}) # type: ignore[arg-type]
def test_min_max_without_hint(self):
coll = self.db.test
@ -522,20 +543,24 @@ class TestCursor(IntegrationTest):
coll.create_index(j_index)
with self.assertRaises(InvalidOperation):
list(coll.find().min([("j", 3)]))
coll.find().min([("j", 3)]).to_list()
with self.assertRaises(InvalidOperation):
list(coll.find().max([("j", 3)]))
coll.find().max([("j", 3)]).to_list()
def test_batch_size(self):
db = self.db
db.test.drop()
db.test.insert_many([{"x": x} for x in range(200)])
self.assertRaises(TypeError, db.test.find().batch_size, None)
self.assertRaises(TypeError, db.test.find().batch_size, "hello")
self.assertRaises(TypeError, db.test.find().batch_size, 5.5)
self.assertRaises(ValueError, db.test.find().batch_size, -1)
self.assertTrue(db.test.find().batch_size(5))
with self.assertRaises(TypeError):
db.test.find().batch_size(None) # type: ignore[arg-type]
with self.assertRaises(TypeError):
db.test.find().batch_size("hello") # type: ignore[arg-type]
with self.assertRaises(TypeError):
db.test.find().batch_size(5.5) # type: ignore[arg-type]
with self.assertRaises(ValueError):
db.test.find().batch_size(-1)
self.assertTrue((db.test.find()).batch_size(5))
a = db.test.find()
for _ in a:
break
@ -547,26 +572,26 @@ class TestCursor(IntegrationTest):
count += 1
self.assertEqual(expected_count, count)
cursor_count(db.test.find().batch_size(0), 200)
cursor_count(db.test.find().batch_size(1), 200)
cursor_count(db.test.find().batch_size(2), 200)
cursor_count(db.test.find().batch_size(5), 200)
cursor_count(db.test.find().batch_size(100), 200)
cursor_count(db.test.find().batch_size(500), 200)
cursor_count((db.test.find()).batch_size(0), 200)
cursor_count((db.test.find()).batch_size(1), 200)
cursor_count((db.test.find()).batch_size(2), 200)
cursor_count((db.test.find()).batch_size(5), 200)
cursor_count((db.test.find()).batch_size(100), 200)
cursor_count((db.test.find()).batch_size(500), 200)
cursor_count(db.test.find().batch_size(0).limit(1), 1)
cursor_count(db.test.find().batch_size(1).limit(1), 1)
cursor_count(db.test.find().batch_size(2).limit(1), 1)
cursor_count(db.test.find().batch_size(5).limit(1), 1)
cursor_count(db.test.find().batch_size(100).limit(1), 1)
cursor_count(db.test.find().batch_size(500).limit(1), 1)
cursor_count((db.test.find()).batch_size(0).limit(1), 1)
cursor_count((db.test.find()).batch_size(1).limit(1), 1)
cursor_count((db.test.find()).batch_size(2).limit(1), 1)
cursor_count((db.test.find()).batch_size(5).limit(1), 1)
cursor_count((db.test.find()).batch_size(100).limit(1), 1)
cursor_count((db.test.find()).batch_size(500).limit(1), 1)
cursor_count(db.test.find().batch_size(0).limit(10), 10)
cursor_count(db.test.find().batch_size(1).limit(10), 10)
cursor_count(db.test.find().batch_size(2).limit(10), 10)
cursor_count(db.test.find().batch_size(5).limit(10), 10)
cursor_count(db.test.find().batch_size(100).limit(10), 10)
cursor_count(db.test.find().batch_size(500).limit(10), 10)
cursor_count((db.test.find()).batch_size(0).limit(10), 10)
cursor_count((db.test.find()).batch_size(1).limit(10), 10)
cursor_count((db.test.find()).batch_size(2).limit(10), 10)
cursor_count((db.test.find()).batch_size(5).limit(10), 10)
cursor_count((db.test.find()).batch_size(100).limit(10), 10)
cursor_count((db.test.find()).batch_size(500).limit(10), 10)
cur = db.test.find().batch_size(1)
next(cur)
@ -651,11 +676,15 @@ class TestCursor(IntegrationTest):
def test_skip(self):
db = self.db
self.assertRaises(TypeError, db.test.find().skip, None)
self.assertRaises(TypeError, db.test.find().skip, "hello")
self.assertRaises(TypeError, db.test.find().skip, 5.5)
self.assertRaises(ValueError, db.test.find().skip, -5)
self.assertTrue(db.test.find().skip(5))
with self.assertRaises(TypeError):
db.test.find().skip(None) # type: ignore[arg-type]
with self.assertRaises(TypeError):
db.test.find().skip("hello") # type: ignore[arg-type]
with self.assertRaises(TypeError):
db.test.find().skip(5.5) # type: ignore[arg-type]
with self.assertRaises(ValueError):
db.test.find().skip(-5)
self.assertTrue((db.test.find()).skip(5))
db.drop_collection("test")
@ -685,7 +714,7 @@ class TestCursor(IntegrationTest):
self.assertEqual(i["x"], 10)
break
for i in db.test.find().skip(1000):
for _ in db.test.find().skip(1000):
self.fail()
a = db.test.find()
@ -697,10 +726,14 @@ class TestCursor(IntegrationTest):
def test_sort(self):
db = self.db
self.assertRaises(TypeError, db.test.find().sort, 5)
self.assertRaises(ValueError, db.test.find().sort, [])
self.assertRaises(TypeError, db.test.find().sort, [], ASCENDING)
self.assertRaises(TypeError, db.test.find().sort, [("hello", DESCENDING)], DESCENDING)
with self.assertRaises(TypeError):
db.test.find().sort(5) # type: ignore[arg-type]
with self.assertRaises(ValueError):
db.test.find().sort([]) # type: ignore[arg-type]
with self.assertRaises(TypeError):
db.test.find().sort([], ASCENDING) # type: ignore[arg-type]
with self.assertRaises(TypeError):
db.test.find().sort([("hello", DESCENDING)], DESCENDING) # type: ignore[arg-type]
db.test.drop()
@ -750,28 +783,31 @@ class TestCursor(IntegrationTest):
db.test.drop()
a = db.test.find()
self.assertRaises(TypeError, a.where, 5)
self.assertRaises(TypeError, a.where, None)
self.assertRaises(TypeError, a.where, {})
with self.assertRaises(TypeError):
a.where(5) # type: ignore[arg-type]
with self.assertRaises(TypeError):
a.where(None) # type: ignore[arg-type]
with self.assertRaises(TypeError):
a.where({}) # type: ignore[arg-type]
db.test.insert_many([{"x": i} for i in range(10)])
self.assertEqual(3, len(list(db.test.find().where("this.x < 3"))))
self.assertEqual(3, len(list(db.test.find().where(Code("this.x < 3")))))
self.assertEqual(3, len(db.test.find().where("this.x < 3").to_list()))
self.assertEqual(3, len(db.test.find().where(Code("this.x < 3")).to_list()))
code_with_scope = Code("this.x < i", {"i": 3})
if client_context.version.at_least(4, 3, 3):
# MongoDB 4.4 removed support for Code with scope.
with self.assertRaises(OperationFailure):
list(db.test.find().where(code_with_scope))
db.test.find().where(code_with_scope).to_list()
code_with_empty_scope = Code("this.x < 3", {})
with self.assertRaises(OperationFailure):
list(db.test.find().where(code_with_empty_scope))
db.test.find().where(code_with_empty_scope).to_list()
else:
self.assertEqual(3, len(list(db.test.find().where(code_with_scope))))
self.assertEqual(3, len(db.test.find().where(code_with_scope).to_list()))
self.assertEqual(10, len(list(db.test.find())))
self.assertEqual(10, len(db.test.find().to_list()))
self.assertEqual([0, 1, 2], [a["x"] for a in db.test.find().where("this.x < 3")])
self.assertEqual([], [a["x"] for a in db.test.find({"x": 5}).where("this.x < 3")])
self.assertEqual([5], [a["x"] for a in db.test.find({"x": 5}).where("this.x > 3")])
@ -856,24 +892,26 @@ class TestCursor(IntegrationTest):
self.assertNotEqual(cursor, cursor.clone())
# Just test attributes
cursor = self.db.test.find(
{"x": re.compile("^hello.*")},
projection={"_id": False},
skip=1,
no_cursor_timeout=True,
cursor_type=CursorType.TAILABLE_AWAIT,
sort=[("x", 1)],
allow_partial_results=True,
oplog_replay=True,
batch_size=123,
collation={"locale": "en_US"},
hint=[("_id", 1)],
max_scan=100,
max_time_ms=1000,
return_key=True,
show_record_id=True,
snapshot=True,
allow_disk_use=True,
cursor = (
self.db.test.find(
{"x": re.compile("^hello.*")},
projection={"_id": False},
skip=1,
no_cursor_timeout=True,
cursor_type=CursorType.TAILABLE_AWAIT,
sort=[("x", 1)],
allow_partial_results=True,
oplog_replay=True,
batch_size=123,
collation={"locale": "en_US"},
hint=[("_id", 1)],
max_scan=100,
max_time_ms=1000,
return_key=True,
show_record_id=True,
snapshot=True,
allow_disk_use=True,
)
).limit(2)
cursor.min([("a", 1)]).max([("b", 3)])
cursor.add_option(128)
@ -915,6 +953,7 @@ class TestCursor(IntegrationTest):
self.assertTrue(isinstance(cursor2._hint, dict))
self.assertEqual(cursor._hint, cursor2._hint)
@client_context.require_sync
def test_clone_empty(self):
self.db.test.delete_many({})
self.db.test.insert_many([{"x": i} for i in range(1, 4)])
@ -923,11 +962,15 @@ class TestCursor(IntegrationTest):
self.assertRaises(StopIteration, cursor.next)
self.assertRaises(StopIteration, cursor2.next)
# Cursors don't support slicing
@client_context.require_sync
def test_bad_getitem(self):
self.assertRaises(TypeError, lambda x: self.db.test.find()[x], "hello")
self.assertRaises(TypeError, lambda x: self.db.test.find()[x], 5.5)
self.assertRaises(TypeError, lambda x: self.db.test.find()[x], None)
# Cursors don't support slicing
@client_context.require_sync
def test_getitem_slice_index(self):
self.db.drop_collection("test")
self.db.test.insert_many([{"i": i} for i in range(100)])
@ -937,51 +980,53 @@ class TestCursor(IntegrationTest):
self.assertRaises(IndexError, lambda: self.db.test.find()[-1:])
self.assertRaises(IndexError, lambda: self.db.test.find()[1:2:2])
for a, b in zip(count(0), self.db.test.find()):
for a, b in zip(count(0), self.db.test.find()): # type: ignore[call-overload]
self.assertEqual(a, b["i"])
self.assertEqual(100, len(list(self.db.test.find()[0:])))
for a, b in zip(count(0), self.db.test.find()[0:]):
self.assertEqual(100, len(list(self.db.test.find()[0:]))) # type: ignore[call-overload]
for a, b in zip(count(0), self.db.test.find()[0:]): # type: ignore[call-overload]
self.assertEqual(a, b["i"])
self.assertEqual(80, len(list(self.db.test.find()[20:])))
for a, b in zip(count(20), self.db.test.find()[20:]):
self.assertEqual(80, len(list(self.db.test.find()[20:]))) # type: ignore[call-overload]
for a, b in zip(count(20), self.db.test.find()[20:]): # type: ignore[call-overload]
self.assertEqual(a, b["i"])
for a, b in zip(count(99), self.db.test.find()[99:]):
for a, b in zip(count(99), self.db.test.find()[99:]): # type: ignore[call-overload]
self.assertEqual(a, b["i"])
for _i in self.db.test.find()[1000:]:
self.fail()
self.assertEqual(5, len(list(self.db.test.find()[20:25])))
self.assertEqual(5, len(list(self.db.test.find()[20:25])))
for a, b in zip(count(20), self.db.test.find()[20:25]):
self.assertEqual(5, len(list(self.db.test.find()[20:25]))) # type: ignore[call-overload]
self.assertEqual(5, len(list(self.db.test.find()[20:25]))) # type: ignore[call-overload]
for a, b in zip(count(20), self.db.test.find()[20:25]): # type: ignore[call-overload]
self.assertEqual(a, b["i"])
self.assertEqual(80, len(list(self.db.test.find()[40:45][20:])))
for a, b in zip(count(20), self.db.test.find()[40:45][20:]):
self.assertEqual(80, len(list(self.db.test.find()[40:45][20:]))) # type: ignore[call-overload]
for a, b in zip(count(20), self.db.test.find()[40:45][20:]): # type: ignore[call-overload]
self.assertEqual(a, b["i"])
self.assertEqual(80, len(list(self.db.test.find()[40:45].limit(0).skip(20))))
for a, b in zip(count(20), self.db.test.find()[40:45].limit(0).skip(20)):
self.assertEqual(80, len(list(self.db.test.find()[40:45].limit(0).skip(20)))) # type: ignore[call-overload]
for a, b in zip(count(20), self.db.test.find()[40:45].limit(0).skip(20)): # type: ignore[call-overload]
self.assertEqual(a, b["i"])
self.assertEqual(80, len(list(self.db.test.find().limit(10).skip(40)[20:])))
for a, b in zip(count(20), self.db.test.find().limit(10).skip(40)[20:]):
self.assertEqual(80, len(list(self.db.test.find().limit(10).skip(40)[20:]))) # type: ignore[call-overload]
for a, b in zip(count(20), self.db.test.find().limit(10).skip(40)[20:]): # type: ignore[call-overload]
self.assertEqual(a, b["i"])
self.assertEqual(1, len(list(self.db.test.find()[:1])))
self.assertEqual(5, len(list(self.db.test.find()[:5])))
self.assertEqual(1, len(list(self.db.test.find()[:1]))) # type: ignore[call-overload]
self.assertEqual(5, len(list(self.db.test.find()[:5]))) # type: ignore[call-overload]
self.assertEqual(1, len(list(self.db.test.find()[99:100])))
self.assertEqual(1, len(list(self.db.test.find()[99:1000])))
self.assertEqual(0, len(list(self.db.test.find()[10:10])))
self.assertEqual(0, len(list(self.db.test.find()[:0])))
self.assertEqual(80, len(list(self.db.test.find()[10:10].limit(0).skip(20))))
self.assertEqual(1, len(list(self.db.test.find()[99:100]))) # type: ignore[call-overload]
self.assertEqual(1, len(list(self.db.test.find()[99:1000]))) # type: ignore[call-overload]
self.assertEqual(0, len(list(self.db.test.find()[10:10]))) # type: ignore[call-overload]
self.assertEqual(0, len(list(self.db.test.find()[:0]))) # type: ignore[call-overload]
self.assertEqual(80, len(list(self.db.test.find()[10:10].limit(0).skip(20)))) # type: ignore[call-overload]
self.assertRaises(IndexError, lambda: self.db.test.find()[10:8])
# Cursors don't support slicing
@client_context.require_sync
def test_getitem_numeric_index(self):
self.db.drop_collection("test")
self.db.test.insert_many([{"i": i} for i in range(100)])
@ -997,22 +1042,30 @@ class TestCursor(IntegrationTest):
self.assertRaises(IndexError, lambda x: self.db.test.find()[x], 100)
self.assertRaises(IndexError, lambda x: self.db.test.find().skip(50)[x], 50)
@client_context.require_sync
def test_iteration_with_list(self):
self.db.drop_collection("test")
self.db.test.insert_many([{"i": i} for i in range(100)])
cur = self.db.test.find().batch_size(10)
self.assertEqual(100, len(list(cur))) # type: ignore[call-overload]
def test_len(self):
self.assertRaises(TypeError, len, self.db.test.find())
with self.assertRaises(TypeError):
len(self.db.test.find()) # type: ignore[arg-type]
def test_properties(self):
self.assertEqual(self.db.test, self.db.test.find().collection)
def set_coll():
with self.assertRaises(AttributeError):
self.db.test.find().collection = "hello" # type: ignore
self.assertRaises(AttributeError, set_coll)
def test_get_more(self):
db = self.db
db.drop_collection("test")
db.test.insert_many([{"i": i} for i in range(10)])
self.assertEqual(10, len(list(db.test.find().batch_size(5))))
self.assertEqual(10, len(db.test.find().batch_size(5).to_list()))
def test_tailable(self):
db = self.db
@ -1046,7 +1099,7 @@ class TestCursor(IntegrationTest):
# have more than 3 documents. Just make sure
# this doesn't raise...
db.test.insert_many([{"x": i} for i in range(4, 7)])
self.assertEqual(0, len(list(cursor)))
self.assertEqual(0, len(cursor.to_list()))
# and that the cursor doesn't think it's still alive.
self.assertFalse(cursor.alive)
@ -1054,27 +1107,30 @@ class TestCursor(IntegrationTest):
self.assertEqual(3, db.test.count_documents({}))
# __getitem__(index)
for cursor in (
db.test.find(cursor_type=CursorType.TAILABLE),
db.test.find(cursor_type=CursorType.TAILABLE_AWAIT),
):
self.assertEqual(4, cursor[0]["x"])
self.assertEqual(5, cursor[1]["x"])
self.assertEqual(6, cursor[2]["x"])
if _IS_SYNC:
for cursor in (
db.test.find(cursor_type=CursorType.TAILABLE),
db.test.find(cursor_type=CursorType.TAILABLE_AWAIT),
):
self.assertEqual(4, cursor[0]["x"])
self.assertEqual(5, cursor[1]["x"])
self.assertEqual(6, cursor[2]["x"])
cursor.rewind()
self.assertEqual([4], [doc["x"] for doc in cursor[0:1]])
cursor.rewind()
self.assertEqual([5], [doc["x"] for doc in cursor[1:2]])
cursor.rewind()
self.assertEqual([6], [doc["x"] for doc in cursor[2:3]])
cursor.rewind()
self.assertEqual([4, 5], [doc["x"] for doc in cursor[0:2]])
cursor.rewind()
self.assertEqual([5, 6], [doc["x"] for doc in cursor[1:3]])
cursor.rewind()
self.assertEqual([4, 5, 6], [doc["x"] for doc in cursor[0:3]])
cursor.rewind()
self.assertEqual([4], [doc["x"] for doc in cursor[0:1]])
cursor.rewind()
self.assertEqual([5], [doc["x"] for doc in cursor[1:2]])
cursor.rewind()
self.assertEqual([6], [doc["x"] for doc in cursor[2:3]])
cursor.rewind()
self.assertEqual([4, 5], [doc["x"] for doc in cursor[0:2]])
cursor.rewind()
self.assertEqual([5, 6], [doc["x"] for doc in cursor[1:3]])
cursor.rewind()
self.assertEqual([4, 5, 6], [doc["x"] for doc in cursor[0:3]])
# The Async API does not support threading
@client_context.require_sync
def test_concurrent_close(self):
"""Ensure a tailable can be closed from another thread."""
db = self.db
@ -1127,9 +1183,9 @@ class TestCursor(IntegrationTest):
self.db.drop_collection("test")
self.db.test.insert_many([{} for _ in range(100)])
self.assertEqual(100, len(list(self.db.test.find())))
self.assertEqual(50, len(list(self.db.test.find().max_scan(50))))
self.assertEqual(50, len(list(self.db.test.find().max_scan(90).max_scan(50))))
self.assertEqual(100, len(self.db.test.find().to_list()))
self.assertEqual(50, len(self.db.test.find().max_scan(50).to_list()))
self.assertEqual(50, len(self.db.test.find().max_scan(90).max_scan(50).to_list()))
def test_with_statement(self):
self.db.drop_collection("test")
@ -1141,7 +1197,7 @@ class TestCursor(IntegrationTest):
self.assertFalse(c2.alive)
with self.db.test.find() as c2:
self.assertEqual(100, len(list(c2)))
self.assertEqual(100, len(c2.to_list()))
self.assertFalse(c2.alive)
self.assertTrue(c1.alive)
@ -1150,7 +1206,7 @@ class TestCursor(IntegrationTest):
self.client.drop_database(self.db)
self.db.command("profile", 2) # Profile ALL commands.
try:
list(self.db.test.find().comment("foo"))
self.db.test.find().comment("foo").to_list()
count = self.db.system.profile.count_documents(
{"ns": "pymongo_test.test", "op": "query", "command.comment": "foo"}
)
@ -1233,7 +1289,7 @@ class TestCursor(IntegrationTest):
self.assertEqual(0, len(listener.started_events))
@client_context.require_failCommand_appName
def test_timeout_kills_cursor_asynchronously(self):
def test_timeout_kills_cursor_synchronously(self):
listener = AllowListEventListener("killCursors")
client = rs_or_single_client(event_listeners=[listener])
self.addCleanup(client.close)
@ -1304,7 +1360,7 @@ class TestCursor(IntegrationTest):
coll.insert_many([{} for _ in range(5)])
self.addCleanup(coll.drop)
list(coll.find(batch_size=3))
coll.find(batch_size=3).to_list()
started = listener.started_events
self.assertEqual(2, len(started))
self.assertEqual("find", started[0].command_name)
@ -1322,7 +1378,7 @@ class TestRawBatchCursor(IntegrationTest):
c.drop()
docs = [{"_id": i, "x": 3.0 * i} for i in range(10)]
c.insert_many(docs)
batches = list(c.find_raw_batches().sort("_id"))
batches = (c.find_raw_batches()).sort("_id").to_list()
self.assertEqual(1, len(batches))
self.assertEqual(docs, decode_all(batches[0]))
@ -1337,9 +1393,9 @@ class TestRawBatchCursor(IntegrationTest):
client = rs_or_single_client(event_listeners=[listener])
with client.start_session() as session:
with session.start_transaction():
batches = list(
client[self.db.name].test.find_raw_batches(session=session).sort("_id")
)
batches = (
(client[self.db.name].test.find_raw_batches(session=session)).sort("_id")
).to_list()
cmd = listener.started_events[0]
self.assertEqual(cmd.command_name, "find")
self.assertIn("$clusterTime", cmd.command)
@ -1368,7 +1424,7 @@ class TestRawBatchCursor(IntegrationTest):
with self.fail_point(
{"mode": {"times": 1}, "data": {"failCommands": ["find"], "closeConnection": True}}
):
batches = list(client[self.db.name].test.find_raw_batches().sort("_id"))
batches = (client[self.db.name].test.find_raw_batches()).sort("_id").to_list()
self.assertEqual(1, len(batches))
self.assertEqual(docs, decode_all(batches[0]))
@ -1389,7 +1445,7 @@ class TestRawBatchCursor(IntegrationTest):
db = client[self.db.name]
with client.start_session(snapshot=True) as session:
db.test.distinct("x", {}, session=session)
batches = list(db.test.find_raw_batches(session=session).sort("_id"))
batches = (db.test.find_raw_batches(session=session)).sort("_id").to_list()
self.assertEqual(1, len(batches))
self.assertEqual(docs, decode_all(batches[0]))
@ -1400,7 +1456,7 @@ class TestRawBatchCursor(IntegrationTest):
def test_explain(self):
c = self.db.test
c.insert_one({})
explanation = c.find_raw_batches().explain()
explanation = (c.find_raw_batches()).explain()
self.assertIsInstance(explanation, dict)
def test_empty(self):
@ -1421,7 +1477,7 @@ class TestRawBatchCursor(IntegrationTest):
c = self.db.test
c.drop()
c.insert_many({"_id": i} for i in range(200))
result = b"".join(c.find_raw_batches(cursor_type=CursorType.EXHAUST))
result = b"".join((c.find_raw_batches(cursor_type=CursorType.EXHAUST)).to_list())
self.assertEqual([{"_id": i} for i in range(200)], decode_all(result))
def test_server_error(self):
@ -1433,7 +1489,7 @@ class TestRawBatchCursor(IntegrationTest):
def test_get_item(self):
with self.assertRaises(InvalidOperation):
self.db.test.find_raw_batches()[0]
(self.db.test.find_raw_batches())[0]
def test_collation(self):
next(self.db.test.find_raw_batches(collation=Collation("en_US")))
@ -1487,20 +1543,20 @@ class TestRawBatchCursor(IntegrationTest):
self.assertEqual(decode_all(csr["nextBatch"][0]), [{"_id": i} for i in range(4, 8)])
finally:
# Finish the cursor.
tuple(cursor)
cursor.close()
class TestRawBatchCommandCursor(IntegrationTest):
@classmethod
def setUpClass(cls):
super().setUpClass()
def _setup_class(cls):
super()._setup_class()
def test_aggregate_raw(self):
c = self.db.test
c.drop()
docs = [{"_id": i, "x": 3.0 * i} for i in range(10)]
c.insert_many(docs)
batches = list(c.aggregate_raw_batches([{"$sort": {"_id": 1}}]))
batches = (c.aggregate_raw_batches([{"$sort": {"_id": 1}}])).to_list()
self.assertEqual(1, len(batches))
self.assertEqual(docs, decode_all(batches[0]))
@ -1515,11 +1571,11 @@ class TestRawBatchCommandCursor(IntegrationTest):
client = rs_or_single_client(event_listeners=[listener])
with client.start_session() as session:
with session.start_transaction():
batches = list(
batches = (
client[self.db.name].test.aggregate_raw_batches(
[{"$sort": {"_id": 1}}], session=session
)
)
).to_list()
cmd = listener.started_events[0]
self.assertEqual(cmd.command_name, "aggregate")
self.assertIn("$clusterTime", cmd.command)
@ -1547,7 +1603,9 @@ class TestRawBatchCommandCursor(IntegrationTest):
with self.fail_point(
{"mode": {"times": 1}, "data": {"failCommands": ["aggregate"], "closeConnection": True}}
):
batches = list(client[self.db.name].test.aggregate_raw_batches([{"$sort": {"_id": 1}}]))
batches = (
client[self.db.name].test.aggregate_raw_batches([{"$sort": {"_id": 1}}])
).to_list()
self.assertEqual(1, len(batches))
self.assertEqual(docs, decode_all(batches[0]))
@ -1569,7 +1627,9 @@ class TestRawBatchCommandCursor(IntegrationTest):
db = client[self.db.name]
with client.start_session(snapshot=True) as session:
db.test.distinct("x", {}, session=session)
batches = list(db.test.aggregate_raw_batches([{"$sort": {"_id": 1}}], session=session))
batches = (
db.test.aggregate_raw_batches([{"$sort": {"_id": 1}}], session=session)
).to_list()
self.assertEqual(1, len(batches))
self.assertEqual(docs, decode_all(batches[0]))
@ -1585,7 +1645,7 @@ class TestRawBatchCommandCursor(IntegrationTest):
c.insert_one({"_id": 10, "x": "not a number"})
with self.assertRaises(OperationFailure) as exc:
list(
(
self.db.test.aggregate_raw_batches(
[
{
@ -1595,14 +1655,14 @@ class TestRawBatchCommandCursor(IntegrationTest):
],
batchSize=4,
)
)
).to_list()
# The server response was decoded, not left raw.
self.assertIsInstance(exc.exception.details, dict)
def test_get_item(self):
with self.assertRaises(InvalidOperation):
self.db.test.aggregate_raw_batches([])[0]
(self.db.test.aggregate_raw_batches([]))[0]
def test_collation(self):
next(self.db.test.aggregate_raw_batches([], collation=Collation("en_US")))
@ -1661,7 +1721,7 @@ class TestRawBatchCommandCursor(IntegrationTest):
listener.reset()
result = list(c.find({}, cursor_type=pymongo.CursorType.EXHAUST, batch_size=1))
result = c.find({}, cursor_type=pymongo.CursorType.EXHAUST, batch_size=1).to_list()
self.assertEqual(len(result), 3)

View File

@ -27,6 +27,7 @@ import threading
import time
import unittest
import warnings
from asyncio import iscoroutinefunction
from collections import abc, defaultdict
from functools import partial
from test import client_context, db_pwd, db_user
@ -963,11 +964,18 @@ def _ignore_deprecations():
def ignore_deprecations(wrapped=None):
"""A context manager or a decorator."""
if wrapped:
if iscoroutinefunction(wrapped):
@functools.wraps(wrapped)
def wrapper(*args, **kwargs):
with _ignore_deprecations():
return wrapped(*args, **kwargs)
@functools.wraps(wrapped)
async def wrapper(*args, **kwargs):
with _ignore_deprecations():
return await wrapped(*args, **kwargs)
else:
@functools.wraps(wrapped)
def wrapper(*args, **kwargs):
with _ignore_deprecations():
return wrapped(*args, **kwargs)
return wrapper

View File

@ -146,6 +146,7 @@ converted_tests = [
"utils_spec_runner.py",
"test_client.py",
"test_collection.py",
"test_cursor.py",
"test_database.py",
"test_session.py",
"test_transactions.py",