|
1 | 1 | 'use strict';
|
2 |
| -const { assert: test } = require('../shared'); |
| 2 | +const { assert: test, filterForCommands } = require('../shared'); |
3 | 3 | const { expect } = require('chai');
|
4 | 4 | const sinon = require('sinon');
|
5 | 5 | const { setTimeout } = require('timers');
|
6 |
| -const { Code, ObjectId, Long, Binary, ReturnDocument, CursorResponse } = require('../../mongodb'); |
| 6 | +const { |
| 7 | + Code, |
| 8 | + ObjectId, |
| 9 | + Long, |
| 10 | + Binary, |
| 11 | + ReturnDocument, |
| 12 | + CursorResponse, |
| 13 | + MongoServerError |
| 14 | +} = require('../../mongodb'); |
7 | 15 |
|
8 | 16 | describe('Find', function () {
|
| 17 | + /** @type(import('../../mongodb').MongoClient */ |
9 | 18 | let client;
|
10 | 19 |
|
11 | 20 | beforeEach(async function () {
|
@@ -496,70 +505,40 @@ describe('Find', function () {
|
496 | 505 | }
|
497 | 506 | });
|
498 | 507 |
|
499 |
| - it('shouldCorrectlyPerformFindsWithHintTurnedOn', { |
500 |
| - metadata: { |
501 |
| - requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } |
502 |
| - }, |
| 508 | + it('shouldCorrectlyPerformFindsWithHintTurnedOn', async function () { |
| 509 | + const configuration = this.configuration; |
| 510 | + client = configuration.newClient(configuration.writeConcernMax(), { |
| 511 | + monitorCommands: true |
| 512 | + }); |
503 | 513 |
|
504 |
| - test: function (done) { |
505 |
| - var configuration = this.configuration; |
506 |
| - var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); |
507 |
| - client.connect(function (err, client) { |
508 |
| - var db = client.db(configuration.db); |
509 |
| - db.createCollection('test_hint', function (err, collection) { |
510 |
| - collection.insert({ a: 1 }, configuration.writeConcernMax(), function (err) { |
511 |
| - expect(err).to.not.exist; |
512 |
| - db.createIndex( |
513 |
| - collection.collectionName, |
514 |
| - 'a', |
515 |
| - configuration.writeConcernMax(), |
516 |
| - function (err) { |
517 |
| - expect(err).to.not.exist; |
518 |
| - collection.find({ a: 1 }, { hint: 'a' }).toArray(function (err) { |
519 |
| - test.ok(err != null); |
| 514 | + const finds = []; |
| 515 | + client.on('commandStarted', filterForCommands('find', finds)); |
520 | 516 |
|
521 |
| - collection.find({ a: 1 }, { hint: ['a'] }).toArray(function (err, items) { |
522 |
| - expect(err).to.not.exist; |
523 |
| - test.equal(1, items.length); |
524 |
| - |
525 |
| - collection.find({ a: 1 }, { hint: { a: 1 } }).toArray(function (err, items) { |
526 |
| - test.equal(1, items.length); |
527 |
| - |
528 |
| - // Modify hints |
529 |
| - collection.hint = 'a_1'; |
530 |
| - test.equal('a_1', collection.hint); |
531 |
| - collection.find({ a: 1 }).toArray(function (err, items) { |
532 |
| - test.equal(1, items.length); |
533 |
| - |
534 |
| - collection.hint = ['a']; |
535 |
| - test.equal(1, collection.hint['a']); |
536 |
| - collection.find({ a: 1 }).toArray(function (err, items) { |
537 |
| - test.equal(1, items.length); |
538 |
| - |
539 |
| - collection.hint = { a: 1 }; |
540 |
| - test.equal(1, collection.hint['a']); |
541 |
| - collection.find({ a: 1 }).toArray(function (err, items) { |
542 |
| - test.equal(1, items.length); |
543 |
| - |
544 |
| - collection.hint = null; |
545 |
| - test.ok(collection.hint == null); |
546 |
| - collection.find({ a: 1 }).toArray(function (err, items) { |
547 |
| - test.equal(1, items.length); |
548 |
| - // Let's close the db |
549 |
| - client.close(done); |
550 |
| - }); |
551 |
| - }); |
552 |
| - }); |
553 |
| - }); |
554 |
| - }); |
555 |
| - }); |
556 |
| - }); |
557 |
| - } |
558 |
| - ); |
559 |
| - }); |
560 |
| - }); |
561 |
| - }); |
562 |
| - } |
| 517 | + await client.connect(); |
| 518 | + |
| 519 | + const db = client.db(configuration.db); |
| 520 | + const collection = await db.createCollection('test_hint'); |
| 521 | + |
| 522 | + await collection.deleteMany({}); |
| 523 | + await collection.insert({ a: 1 }, configuration.writeConcernMax()); |
| 524 | + |
| 525 | + await db.createIndex(collection.collectionName, 'a', configuration.writeConcernMax()); |
| 526 | + |
| 527 | + expect( |
| 528 | + await collection |
| 529 | + .find({ a: 1 }, { hint: 'a' }) |
| 530 | + .toArray() |
| 531 | + .catch(e => e) |
| 532 | + ).to.be.instanceOf(MongoServerError); |
| 533 | + expect(finds[0].command.hint).to.equal('a'); |
| 534 | + |
| 535 | + // Test with hint as array |
| 536 | + expect(await collection.find({ a: 1 }, { hint: ['a'] }).toArray()).to.have.lengthOf(1); |
| 537 | + expect(finds[1].command.hint).to.deep.equal({ a: 1 }); |
| 538 | + |
| 539 | + // Test with hint as object |
| 540 | + expect(await collection.find({ a: 1 }, { hint: { a: 1 } }).toArray()).to.have.lengthOf(1); |
| 541 | + expect(finds[2].command.hint).to.deep.equal({ a: 1 }); |
563 | 542 | });
|
564 | 543 |
|
565 | 544 | it('shouldCorrectlyPerformFindByObjectId', {
|
|
0 commit comments