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

Method test_hint

test/test_cursor.py:396–438  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

394 assert 1500 < started[0].command["maxTimeMS"] <= 2000
395
396 def test_hint(self):
397 db = self.db
398 with self.assertRaises(TypeError):
399 db.test.find().hint(5.5) # type: ignore[arg-type]
400 db.test.drop()
401
402 db.test.insert_many([{"num": i, "foo": i} for i in range(100)])
403
404 with self.assertRaises(OperationFailure):
405 db.test.find({"num": 17, "foo": 17}).hint([("num", ASCENDING)]).explain()
406 with self.assertRaises(OperationFailure):
407 db.test.find({"num": 17, "foo": 17}).hint([("foo", ASCENDING)]).explain()
408
409 spec: list[Any] = [("num", DESCENDING)]
410 _ = db.test.create_index(spec)
411
412 first = next(db.test.find())
413 self.assertEqual(0, first.get("num"))
414 first = next(db.test.find().hint(spec))
415 self.assertEqual(99, first.get("num"))
416 with self.assertRaises(OperationFailure):
417 db.test.find({"num": 17, "foo": 17}).hint([("foo", ASCENDING)]).explain()
418
419 a = db.test.find({"num": 17})
420 a.hint(spec)
421 for _ in a:
422 break
423 self.assertRaises(InvalidOperation, a.hint, spec)
424
425 db.test.drop()
426 db.test.insert_many([{"num": i, "foo": i} for i in range(100)])
427 spec: _IndexList = ["num", ("foo", DESCENDING)]
428 db.test.create_index(spec)
429 first = next(db.test.find().hint(spec))
430 self.assertEqual(0, first.get("num"))
431 self.assertEqual(0, first.get("foo"))
432
433 db.test.drop()
434 db.test.insert_many([{"num": i, "foo": i} for i in range(100)])
435 spec = ["num"]
436 db.test.create_index(spec)
437 first = next(db.test.find().hint(spec))
438 self.assertEqual(0, first.get("num"))
439
440 def test_hint_by_name(self):
441 db = self.db

Callers

nothing calls this directly

Calls 8

nextFunction · 0.90
hintMethod · 0.45
findMethod · 0.45
dropMethod · 0.45
insert_manyMethod · 0.45
explainMethod · 0.45
create_indexMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected