MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / test_cursor

Method test_cursor

test/test_session.py:445–506  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

443 clone.close()
444
445 def test_cursor(self):
446 listener = self.listener
447 client = self.client
448 coll = client.pymongo_test.collection
449 coll.insert_many([{} for _ in range(1000)])
450
451 # Test all cursor methods.
452 if _IS_SYNC:
453 # getitem is only supported in the synchronous API
454 ops = [
455 ("find", lambda session: coll.find(session=session).to_list()),
456 ("getitem", lambda session: coll.find(session=session)[0]),
457 ("distinct", lambda session: coll.find(session=session).distinct("a")),
458 ("explain", lambda session: coll.find(session=session).explain()),
459 ]
460 else:
461 ops = [
462 ("find", lambda session: coll.find(session=session).to_list()),
463 ("distinct", lambda session: coll.find(session=session).distinct("a")),
464 ("explain", lambda session: coll.find(session=session).explain()),
465 ]
466
467 for name, f in ops:
468 with client.start_session() as s:
469 listener.reset()
470 f(session=s)
471 self.assertGreaterEqual(len(listener.started_events), 1)
472 for event in listener.started_events:
473 self.assertIn(
474 "lsid",
475 event.command,
476 f"{name} sent no lsid with {event.command_name}",
477 )
478
479 self.assertEqual(
480 s.session_id,
481 event.command["lsid"],
482 f"{name} sent wrong lsid with {event.command_name}",
483 )
484
485 with self.assertRaisesRegex(InvalidOperation, "ended session"):
486 f(session=s)
487
488 # No explicit session.
489 for name, f in ops:
490 listener.reset()
491 f(session=None)
492 event0 = listener.first_command_started()
493 self.assertIn("lsid", event0.command, f"{name} sent no lsid with {event0.command_name}")
494
495 lsid = event0.command["lsid"]
496
497 for event in listener.started_events[1:]:
498 self.assertIn(
499 "lsid", event.command, f"{name} sent no lsid with {event.command_name}"
500 )
501
502 self.assertEqual(

Callers

nothing calls this directly

Calls 8

insert_manyMethod · 0.45
to_listMethod · 0.45
findMethod · 0.45
distinctMethod · 0.45
explainMethod · 0.45
start_sessionMethod · 0.45
resetMethod · 0.45
first_command_startedMethod · 0.45

Tested by

no test coverage detected