|
21 | 21 | import sys |
22 | 22 | import time |
23 | 23 | import threading |
| 24 | +import warnings |
24 | 25 |
|
25 | 26 | sys.path[0:0] = [""] |
26 | 27 |
|
@@ -449,66 +450,102 @@ def test_limit(self): |
449 | 450 | break |
450 | 451 | self.assertRaises(InvalidOperation, a.limit, 5) |
451 | 452 |
|
| 453 | + @ignore_deprecations # Ignore max without hint. |
452 | 454 | def test_max(self): |
453 | 455 | db = self.db |
454 | 456 | db.test.drop() |
455 | | - db.test.create_index([("j", ASCENDING)]) |
| 457 | + j_index = [("j", ASCENDING)] |
| 458 | + db.test.create_index(j_index) |
456 | 459 |
|
457 | 460 | db.test.insert_many([{"j": j, "k": j} for j in range(10)]) |
458 | 461 |
|
459 | | - cursor = db.test.find().max([("j", 3)]) |
| 462 | + def find(max_spec, expected_index): |
| 463 | + cursor = db.test.find().max(max_spec) |
| 464 | + if client_context.requires_hint_with_min_max_queries: |
| 465 | + cursor = cursor.hint(expected_index) |
| 466 | + return cursor |
| 467 | + |
| 468 | + cursor = find([("j", 3)], j_index) |
460 | 469 | self.assertEqual(len(list(cursor)), 3) |
461 | 470 |
|
462 | 471 | # Tuple. |
463 | | - cursor = db.test.find().max((("j", 3), )) |
| 472 | + cursor = find((("j", 3),), j_index) |
464 | 473 | self.assertEqual(len(list(cursor)), 3) |
465 | 474 |
|
466 | 475 | # Compound index. |
467 | | - db.test.create_index([("j", ASCENDING), ("k", ASCENDING)]) |
468 | | - cursor = db.test.find().max([("j", 3), ("k", 3)]) |
| 476 | + index_keys = [("j", ASCENDING), ("k", ASCENDING)] |
| 477 | + db.test.create_index(index_keys) |
| 478 | + cursor = find([("j", 3), ("k", 3)], index_keys) |
469 | 479 | self.assertEqual(len(list(cursor)), 3) |
470 | 480 |
|
471 | 481 | # Wrong order. |
472 | | - cursor = db.test.find().max([("k", 3), ("j", 3)]) |
| 482 | + cursor = find([("k", 3), ("j", 3)], index_keys) |
473 | 483 | self.assertRaises(OperationFailure, list, cursor) |
474 | 484 |
|
475 | 485 | # No such index. |
476 | | - cursor = db.test.find().max([("k", 3)]) |
| 486 | + cursor = find([("k", 3)], "k") |
477 | 487 | self.assertRaises(OperationFailure, list, cursor) |
478 | 488 |
|
479 | 489 | self.assertRaises(TypeError, db.test.find().max, 10) |
480 | 490 | self.assertRaises(TypeError, db.test.find().max, {"j": 10}) |
481 | 491 |
|
| 492 | + @ignore_deprecations # Ignore min without hint. |
482 | 493 | def test_min(self): |
483 | 494 | db = self.db |
484 | 495 | db.test.drop() |
485 | | - db.test.create_index([("j", ASCENDING)]) |
| 496 | + j_index = [("j", ASCENDING)] |
| 497 | + db.test.create_index(j_index) |
486 | 498 |
|
487 | 499 | db.test.insert_many([{"j": j, "k": j} for j in range(10)]) |
488 | 500 |
|
489 | | - cursor = db.test.find().min([("j", 3)]) |
| 501 | + def find(min_spec, expected_index): |
| 502 | + cursor = db.test.find().min(min_spec) |
| 503 | + if client_context.requires_hint_with_min_max_queries: |
| 504 | + cursor = cursor.hint(expected_index) |
| 505 | + return cursor |
| 506 | + |
| 507 | + cursor = find([("j", 3)], j_index) |
490 | 508 | self.assertEqual(len(list(cursor)), 7) |
491 | 509 |
|
492 | 510 | # Tuple. |
493 | | - cursor = db.test.find().min((("j", 3), )) |
| 511 | + cursor = find((("j", 3),), j_index) |
494 | 512 | self.assertEqual(len(list(cursor)), 7) |
495 | 513 |
|
496 | 514 | # Compound index. |
497 | | - db.test.create_index([("j", ASCENDING), ("k", ASCENDING)]) |
498 | | - cursor = db.test.find().min([("j", 3), ("k", 3)]) |
| 515 | + index_keys = [("j", ASCENDING), ("k", ASCENDING)] |
| 516 | + db.test.create_index(index_keys) |
| 517 | + cursor = find([("j", 3), ("k", 3)], index_keys) |
499 | 518 | self.assertEqual(len(list(cursor)), 7) |
500 | 519 |
|
501 | 520 | # Wrong order. |
502 | | - cursor = db.test.find().min([("k", 3), ("j", 3)]) |
| 521 | + cursor = find([("k", 3), ("j", 3)], index_keys) |
503 | 522 | self.assertRaises(OperationFailure, list, cursor) |
504 | 523 |
|
505 | 524 | # No such index. |
506 | | - cursor = db.test.find().min([("k", 3)]) |
| 525 | + cursor = find([("k", 3)], "k") |
507 | 526 | self.assertRaises(OperationFailure, list, cursor) |
508 | 527 |
|
509 | 528 | self.assertRaises(TypeError, db.test.find().min, 10) |
510 | 529 | self.assertRaises(TypeError, db.test.find().min, {"j": 10}) |
511 | 530 |
|
| 531 | + @client_context.require_version_max(4, 1, -1) |
| 532 | + def test_min_max_without_hint(self): |
| 533 | + coll = self.db.test |
| 534 | + j_index = [("j", ASCENDING)] |
| 535 | + coll.create_index(j_index) |
| 536 | + |
| 537 | + with warnings.catch_warnings(record=True) as warns: |
| 538 | + warnings.simplefilter("default", DeprecationWarning) |
| 539 | + list(coll.find().min([("j", 3)])) |
| 540 | + self.assertIn('using a min/max query operator', str(warns[0])) |
| 541 | + # Ensure the warning is raised with the proper stack level. |
| 542 | + del warns[:] |
| 543 | + list(coll.find().min([("j", 3)])) |
| 544 | + self.assertIn('using a min/max query operator', str(warns[0])) |
| 545 | + del warns[:] |
| 546 | + list(coll.find().max([("j", 3)])) |
| 547 | + self.assertIn('using a min/max query operator', str(warns[0])) |
| 548 | + |
512 | 549 | def test_batch_size(self): |
513 | 550 | db = self.db |
514 | 551 | db.test.drop() |
|
0 commit comments