diff --git a/test/integration/node-specific/bson-options/bsonRegExp.test.js b/test/integration/node-specific/bson-options/bsonRegExp.test.ts similarity index 90% rename from test/integration/node-specific/bson-options/bsonRegExp.test.js rename to test/integration/node-specific/bson-options/bsonRegExp.test.ts index 5c3ddd9f18a..452e0a60456 100644 --- a/test/integration/node-specific/bson-options/bsonRegExp.test.js +++ b/test/integration/node-specific/bson-options/bsonRegExp.test.ts @@ -1,12 +1,11 @@ -'use strict'; +import { expect } from 'chai'; -const { expect } = require('chai'); -const { BSONRegExp } = require('../../../mongodb'); +import { BSONRegExp, type MongoClient } from '../../../../src'; describe('BSONRegExp', () => { describe('bsonRegExp option', () => { // define client and option for tests to use - let client; + let client: MongoClient; const option = { bsonRegExp: true }; for (const passOptionTo of ['client', 'db', 'collection', 'operation']) { it(`should respond with BSONRegExp class with option passed to ${passOptionTo}`, async function () { diff --git a/test/integration/node-specific/bson-options/ignore_undefined.test.js b/test/integration/node-specific/bson-options/ignore_undefined.test.js deleted file mode 100644 index 98008382b85..00000000000 --- a/test/integration/node-specific/bson-options/ignore_undefined.test.js +++ /dev/null @@ -1,371 +0,0 @@ -'use strict'; -const { expect } = require('chai'); -const { assert: test, setupDatabase } = require('../../shared'); -const { ObjectId } = require('../../../mongodb'); - -describe('Ignore Undefined', function () { - before(function () { - return setupDatabase(this.configuration); - }); - - let client; - - beforeEach(async function () { - client = this.configuration.newClient(); - }); - - afterEach(async function () { - await client.close(); - }); - - it('Should correctly insert document ignoring undefined field', { - metadata: { requires: { topology: ['single'] } }, - - test: function (done) { - var configuration = this.configuration; - var client = configuration.newClient(configuration.writeConcernMax(), { - maxPoolSize: 1, - ignoreUndefined: true - }); - - client.connect(function (err, client) { - var db = client.db(configuration.db); - var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue'); - - // Ignore the undefined field - collection.insert({ a: 1, b: undefined }, configuration.writeConcernMax(), function (err) { - expect(err).to.not.exist; - - // Locate the doument - collection.findOne(function (err, item) { - test.equal(1, item.a); - test.ok(item.b === undefined); - client.close(done); - }); - }); - }); - } - }); - - it( - 'Should correctly connect using MongoClient and perform insert document ignoring undefined field', - { - metadata: { requires: { topology: ['single'] } }, - - test: function (done) { - var configuration = this.configuration; - const client = configuration.newClient( - {}, - { - ignoreUndefined: true - } - ); - - client.connect(function (err, client) { - var db = client.db(configuration.db); - var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue1'); - collection.insert({ a: 1, b: undefined }, function (err) { - expect(err).to.not.exist; - - collection.findOne(function (err, item) { - test.equal(1, item.a); - test.ok(item.b === undefined); - - collection.insertOne({ a: 2, b: undefined }, function (err) { - expect(err).to.not.exist; - - collection.findOne({ a: 2 }, function (err, item) { - test.equal(2, item.a); - test.ok(item.b === undefined); - - collection.insertMany([{ a: 3, b: undefined }], function (err) { - expect(err).to.not.exist; - - collection.findOne({ a: 3 }, function (err, item) { - test.equal(3, item.a); - test.ok(item.b === undefined); - client.close(done); - }); - }); - }); - }); - }); - }); - }); - } - } - ); - - it('Should correctly update document ignoring undefined field', { - metadata: { requires: { topology: ['single'] } }, - - test: function (done) { - var configuration = this.configuration; - var client = configuration.newClient(configuration.writeConcernMax(), { - maxPoolSize: 1, - ignoreUndefined: true - }); - - client.connect(function (err, client) { - var db = client.db(configuration.db); - var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue2'); - var id = new ObjectId(); - - collection.updateOne( - { _id: id, a: 1, b: undefined }, - { $set: { a: 1, b: undefined } }, - { upsert: true }, - function (err) { - expect(err).to.not.exist; - collection.findOne({ _id: id }, function (err, item) { - test.equal(1, item.a); - test.ok(item.b === undefined); - var id = new ObjectId(); - - collection.updateMany( - { _id: id, a: 1, b: undefined }, - { $set: { a: 1, b: undefined } }, - { upsert: true }, - function (err) { - expect(err).to.not.exist; - collection.findOne({ _id: id }, function (err, item) { - test.equal(1, item.a); - test.ok(item.b === undefined); - var id = new ObjectId(); - - collection.update( - { _id: id, a: 1, b: undefined }, - { $set: { a: 1, b: undefined } }, - { upsert: true }, - function (err) { - expect(err).to.not.exist; - collection.findOne({ _id: id }, function (err, item) { - test.equal(1, item.a); - test.ok(item.b === undefined); - client.close(done); - }); - } - ); - }); - } - ); - }); - } - ); - }); - } - }); - - it('Should correctly inherit ignore undefined field from db during insert', async function () { - const configuration = this.configuration; - const client = configuration.newClient(configuration.writeConcernMax(), { - maxPoolSize: 1, - ignoreUndefined: false - }); - - const db = client.db(configuration.db, { ignoreUndefined: true }); - const collection = db.collection('shouldCorrectlyIgnoreUndefinedValue3'); - - await collection.insert({ a: 1, b: undefined }); - const item = await collection.findOne(); - expect(item).to.have.property('a', 1); - expect(item).to.not.have.property('b'); - - await client.close(); - }); - - it('Should correctly inherit ignore undefined field from collection during insert', function (done) { - const db = client.db('shouldCorrectlyIgnoreUndefinedValue4', { ignoreUndefined: false }); - const collection = db.collection('shouldCorrectlyIgnoreUndefinedValue4', { - ignoreUndefined: true - }); - - // Ignore the undefined field - collection.insert({ a: 1, b: undefined }, err => { - expect(err).to.not.exist; - - // Locate the doument - collection.findOne((err, item) => { - expect(err).to.not.exist; - expect(item).to.have.property('a', 1); - expect(item).to.not.have.property('b'); - done(); - }); - }); - }); - - it('Should correctly inherit ignore undefined field from operation during insert', function (done) { - const db = client.db('shouldCorrectlyIgnoreUndefinedValue5'); - const collection = db.collection('shouldCorrectlyIgnoreUndefinedValue5', { - ignoreUndefined: false - }); - - // Ignore the undefined field - collection.insert({ a: 1, b: undefined }, { ignoreUndefined: true }, err => { - expect(err).to.not.exist; - - // Locate the doument - collection.findOne({}, (err, item) => { - expect(err).to.not.exist; - expect(item).to.have.property('a', 1); - expect(item).to.not.have.property('b'); - done(); - }); - }); - }); - - it('Should correctly inherit ignore undefined field from operation during findOneAndReplace', function (done) { - const db = client.db('shouldCorrectlyIgnoreUndefinedValue6'); - const collection = db.collection('shouldCorrectlyIgnoreUndefinedValue6', { - ignoreUndefined: false - }); - - collection.insert({ a: 1, b: 2 }, err => { - expect(err).to.not.exist; - - // Replace the doument, ignoring undefined fields - collection.findOneAndReplace({}, { a: 1, b: undefined }, { ignoreUndefined: true }, err => { - expect(err).to.not.exist; - - // Locate the doument - collection.findOne((err, item) => { - expect(err).to.not.exist; - expect(item).to.have.property('a', 1); - expect(item).to.not.have.property('b'); - done(); - }); - }); - }); - }); - - it('Should correctly ignore undefined field during bulk write', function (done) { - const db = client.db('shouldCorrectlyIgnoreUndefinedValue7'); - const collection = db.collection('shouldCorrectlyIgnoreUndefinedValue7'); - - // Ignore the undefined field - collection.bulkWrite( - [{ insertOne: { a: 1, b: undefined } }], - { ignoreUndefined: true }, - err => { - expect(err).to.not.exist; - - // Locate the doument - collection.findOne((err, item) => { - expect(err).to.not.exist; - expect(item).to.have.property('a', 1); - expect(item).to.not.have.property('b'); - done(); - }); - } - ); - }); - - describe('ignoreUndefined A server', function () { - it('should correctly execute insert culling undefined', { - metadata: { requires: { mongodb: '>=3.2' } }, - test: function (done) { - const coll = client.db().collection('insert1'); - coll.drop(() => { - const objectId = new ObjectId(); - coll.insertOne( - { _id: objectId, a: 1, b: undefined }, - { ignoreUndefined: true }, - (err, res) => { - expect(err).to.not.exist; - expect(res).property('insertedId').to.exist; - - const cursor = coll.find({ _id: objectId }); - this.defer(() => cursor.close()); - - cursor.next((err, doc) => { - expect(err).to.not.exist; - expect(doc).to.not.have.property('b'); - done(); - }); - } - ); - }); - } - }); - - it('should correctly execute update culling undefined', { - metadata: { requires: { mongodb: '>=3.2' } }, - test: function (done) { - const coll = client.db().collection('update1'); - coll.drop(() => { - const objectId = new ObjectId(); - coll.updateOne( - { _id: objectId, a: 1, b: undefined }, - { $set: { a: 1, b: undefined } }, - { ignoreUndefined: true, upsert: true }, - (err, res) => { - expect(err).to.not.exist; - expect(res).property('upsertedCount').to.equal(1); - - const cursor = coll.find({ _id: objectId }); - this.defer(() => cursor.close()); - - cursor.next((err, doc) => { - expect(err).to.not.exist; - expect(doc).to.not.have.property('b'); - done(); - }); - } - ); - }); - } - }); - - it('should correctly execute remove culling undefined', { - metadata: { requires: { mongodb: '>=3.2' } }, - test: function (done) { - const coll = client.db().collection('remove1'); - coll.drop(() => { - const objectId = new ObjectId(); - coll.insertMany( - [ - { id: objectId, a: 1, b: undefined }, - { id: objectId, a: 2, b: 1 } - ], - (err, res) => { - expect(err).to.not.exist; - expect(res).property('insertedCount').to.equal(2); - - coll.deleteMany({ b: undefined }, { ignoreUndefined: true }, (err, res) => { - expect(err).to.not.exist; - expect(res).property('deletedCount').to.equal(2); - done(); - }); - } - ); - }); - } - }); - - it('should correctly execute remove not culling undefined', { - metadata: { requires: { mongodb: '>=3.2' } }, - test: function (done) { - const coll = client.db().collection('remove1'); - coll.drop(() => { - const objectId = new ObjectId(); - coll.insertMany( - [ - { id: objectId, a: 1, b: undefined }, - { id: objectId, a: 2, b: 1 } - ], - (err, res) => { - expect(err).to.not.exist; - expect(res).property('insertedCount').to.equal(2); - - coll.deleteMany({ b: null }, (err, res) => { - expect(err).to.not.exist; - expect(res).property('deletedCount').to.equal(1); - done(); - }); - } - ); - }); - } - }); - }); -}); diff --git a/test/integration/node-specific/bson-options/ignore_undefined.test.ts b/test/integration/node-specific/bson-options/ignore_undefined.test.ts new file mode 100644 index 00000000000..fd7c5cc18d4 --- /dev/null +++ b/test/integration/node-specific/bson-options/ignore_undefined.test.ts @@ -0,0 +1,243 @@ +import { expect } from 'chai'; + +import { type MongoClient, ObjectId } from '../../../../src'; +import { assert as test, ignoreNsNotFound, setupDatabase } from '../../shared'; + +describe('Ignore Undefined', function () { + before(function () { + return setupDatabase(this.configuration); + }); + + let client: MongoClient; + + beforeEach(async function () { + client = this.configuration.newClient(); + }); + + afterEach(async function () { + await client.close(); + }); + + it('should correctly insert document ignoring undefined field', async function () { + const configuration = this.configuration; + const client = configuration.newClient(configuration.writeConcernMax(), { + maxPoolSize: 1, + ignoreUndefined: true + }); + + const db = client.db(configuration.db); + const collection = db.collection('shouldCorrectlyIgnoreUndefinedValue'); + + // Ignore the undefined field + await collection.insertOne({ a: 1, b: undefined }, configuration.writeConcernMax()); + + const item = await collection.findOne(); + test.equal(1, item.a); + test.ok(item.b === undefined); + await client.close(); + }); + + // TODO(NODE-7192): remove as it duplicates "should correctly insert document ignoring undefined field" + // it('Should correctly connect using MongoClient and perform insert document ignoring undefined field', async function () { + // const configuration = this.configuration; + // const client = configuration.newClient( + // {}, + // { + // ignoreUndefined: true + // } + // ); + // + // const db = client.db(configuration.db); + // const collection = db.collection('shouldCorrectlyIgnoreUndefinedValue1'); + // await collection.insertOne({ a: 1, b: undefined }); + // + // const item = await collection.findOne(); + // test.equal(1, item.a); + // test.ok(item.b === undefined); + // + // await collection.insertOne({ a: 2, b: undefined }); + // + // const item1 = await collection.findOne({ a: 2 }); + // test.equal(2, item1.a); + // test.ok(item1.b === undefined); + // + // await collection.insertMany([{ a: 3, b: undefined }]); + // + // const item2 = await collection.findOne({ a: 3 }); + // test.equal(3, item2.a); + // test.ok(item2.b === undefined); + // await client.close(); + // }); + + it('Should correctly update document ignoring undefined field', async function () { + const configuration = this.configuration; + const client = configuration.newClient(configuration.writeConcernMax(), { + maxPoolSize: 1, + ignoreUndefined: true + }); + + const db = client.db(configuration.db); + const collection = db.collection('shouldCorrectlyIgnoreUndefinedValue2'); + + const id1 = new ObjectId(); + await collection.updateOne( + { _id: id1, a: 1, b: undefined }, + { $set: { a: 1, b: undefined } }, + { upsert: true } + ); + const item1 = await collection.findOne({ _id: id1 }); + test.equal(1, item1.a); + test.ok(item1.b === undefined); + + const id2 = new ObjectId(); + await collection.updateMany( + { _id: id2, a: 1, b: undefined }, + { $set: { a: 1, b: undefined } }, + { upsert: true } + ); + const item2 = await collection.findOne({ _id: id2 }); + test.equal(1, item2.a); + test.ok(item2.b === undefined); + + await client.close(); + }); + + it('should correctly inherit ignore undefined field from db during insert', async function () { + const configuration = this.configuration; + const client = configuration.newClient(configuration.writeConcernMax(), { + maxPoolSize: 1, + ignoreUndefined: false + }); + + const db = client.db(configuration.db, { ignoreUndefined: true }); + const collection = db.collection('shouldCorrectlyIgnoreUndefinedValue3'); + + await collection.insertOne({ a: 1, b: undefined }); + const item = await collection.findOne(); + expect(item).to.have.property('a', 1); + expect(item).to.not.have.property('b'); + + await client.close(); + }); + + it('should correctly inherit ignore undefined field from collection during insert', async function () { + const db = client.db('shouldCorrectlyIgnoreUndefinedValue4', { ignoreUndefined: false }); + const collection = db.collection('shouldCorrectlyIgnoreUndefinedValue4', { + ignoreUndefined: true + }); + + // Ignore the undefined field + await collection.insertOne({ a: 1, b: undefined }); + + const item = await collection.findOne(); + expect(item).to.have.property('a', 1); + expect(item).to.not.have.property('b'); + }); + + it('Should correctly inherit ignore undefined field from operation during insert', async function () { + const db = client.db('shouldCorrectlyIgnoreUndefinedValue5'); + const collection = db.collection('shouldCorrectlyIgnoreUndefinedValue5', { + ignoreUndefined: false + }); + + // Ignore the undefined field + await collection.insertOne({ a: 1, b: undefined }, { ignoreUndefined: true }); + + const item = await collection.findOne({}); + expect(item).to.have.property('a', 1); + expect(item).to.not.have.property('b'); + }); + + it('Should correctly inherit ignore undefined field from operation during findOneAndReplace', async function () { + const db = client.db('shouldCorrectlyIgnoreUndefinedValue6'); + const collection = db.collection('shouldCorrectlyIgnoreUndefinedValue6', { + ignoreUndefined: false + }); + + await collection.insertOne({ a: 1, b: 2 }); + + // Replace the doument, ignoring undefined fields + await collection.findOneAndReplace({}, { a: 1, b: undefined }, { ignoreUndefined: true }); + + const item = await collection.findOne(); + expect(item).to.have.property('a', 1); + expect(item).to.not.have.property('b'); + }); + + it('Should correctly ignore undefined field during bulk write', async function () { + const db = client.db('shouldCorrectlyIgnoreUndefinedValue7'); + const collection = db.collection('shouldCorrectlyIgnoreUndefinedValue7'); + + // Ignore the undefined field + await collection.bulkWrite([{ insertOne: { document: { a: 1, b: undefined } } }], { + ignoreUndefined: true + }); + + const item = await collection.findOne(); + expect(item).to.have.property('a', 1); + expect(item).to.not.have.property('b'); + }); + + describe('ignoreUndefined A server', function () { + it('should correctly execute insert culling undefined', async function () { + const coll = client.db().collection('insert1'); + await coll.drop().catch(ignoreNsNotFound); + const objectId = new ObjectId(); + const res = await coll.insertOne( + { _id: objectId, a: 1, b: undefined }, + { ignoreUndefined: true } + ); + expect(res).property('insertedId').to.exist; + + const cursor = coll.find({ _id: objectId }); + + const doc = await cursor.next(); + expect(doc).to.not.have.property('b'); + }); + + it('should correctly execute update culling undefined', async function () { + const coll = client.db().collection('update1'); + await coll.drop().catch(ignoreNsNotFound); + const objectId = new ObjectId(); + const res = await coll.updateOne( + { _id: objectId, a: 1, b: undefined }, + { $set: { a: 1, b: undefined } }, + { ignoreUndefined: true, upsert: true } + ); + expect(res).property('upsertedCount').to.equal(1); + + const cursor = coll.find({ _id: objectId }); + + const doc = await cursor.next(); + expect(doc).to.not.have.property('b'); + }); + + it('should correctly execute remove culling undefined', async function () { + const coll = client.db().collection('remove1'); + await coll.drop().catch(ignoreNsNotFound); + const objectId = new ObjectId(); + const res = await coll.insertMany([ + { id: objectId, a: 1, b: undefined }, + { id: objectId, a: 2, b: 1 } + ]); + expect(res).property('insertedCount').to.equal(2); + + const res2 = await coll.deleteMany({ b: undefined }, { ignoreUndefined: true }); + expect(res2).property('deletedCount').to.equal(2); + }); + + it('should correctly execute remove not culling undefined', async function () { + const coll = client.db().collection('remove1'); + await coll.drop().catch(ignoreNsNotFound); + const objectId = new ObjectId(); + const res = await coll.insertMany([ + { id: objectId, a: 1, b: undefined }, + { id: objectId, a: 2, b: 1 } + ]); + expect(res).property('insertedCount').to.equal(2); + + const res2 = await coll.deleteMany({ b: null }); + expect(res2).property('deletedCount').to.equal(1); + }); + }); +}); diff --git a/test/integration/node-specific/bson-options/promote_buffers.test.js b/test/integration/node-specific/bson-options/promote_buffers.test.js deleted file mode 100644 index dbc46bda665..00000000000 --- a/test/integration/node-specific/bson-options/promote_buffers.test.js +++ /dev/null @@ -1,183 +0,0 @@ -'use strict'; -const { expect } = require('chai'); - -const { assert: test, setupDatabase } = require('../../shared'); - -describe('Promote Buffers', function () { - before(function () { - return setupDatabase(this.configuration); - }); - - it('should correctly honor promoteBuffers when creating an instance using Db', { - // Add a tag that our runner can trigger on - // in this case we are setting that node needs to be higher than 0.10.X to run - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } - }, - - test: function (done) { - var configuration = this.configuration; - var client = configuration.newClient(configuration.writeConcernMax(), { - maxPoolSize: 1, - promoteBuffers: true - }); - - client.connect(function (err, client) { - var db = client.db(configuration.db); - db.collection('shouldCorrectlyHonorPromoteBuffer1').insert( - { - doc: Buffer.alloc(256) - }, - function (err) { - expect(err).to.not.exist; - - db.collection('shouldCorrectlyHonorPromoteBuffer1').findOne(function (err, doc) { - expect(err).to.not.exist; - test.ok(doc.doc instanceof Buffer); - - client.close(done); - }); - } - ); - }); - } - }); - - it('should correctly honor promoteBuffers when creating an instance using MongoClient', { - // Add a tag that our runner can trigger on - // in this case we are setting that node needs to be higher than 0.10.X to run - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } - }, - - test: function (done) { - var configuration = this.configuration; - - const client = configuration.newClient({}, { promoteBuffers: true }); - client.connect(function (err, client) { - var db = client.db(configuration.db); - - db.collection('shouldCorrectlyHonorPromoteBuffer2').insert( - { - doc: Buffer.alloc(256) - }, - function (err) { - expect(err).to.not.exist; - - db.collection('shouldCorrectlyHonorPromoteBuffer2').findOne(function (err, doc) { - expect(err).to.not.exist; - test.ok(doc.doc instanceof Buffer); - - client.close(done); - }); - } - ); - }); - } - }); - - it('should correctly honor promoteBuffers at cursor level', { - // Add a tag that our runner can trigger on - // in this case we are setting that node needs to be higher than 0.10.X to run - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } - }, - - test: function (done) { - var configuration = this.configuration; - - const client = configuration.newClient({}, { promoteBuffers: true }); - client.connect(function (err, client) { - var db = client.db(configuration.db); - - db.collection('shouldCorrectlyHonorPromoteBuffer3').insert( - { - doc: Buffer.alloc(256) - }, - function (err) { - expect(err).to.not.exist; - - db.collection('shouldCorrectlyHonorPromoteBuffer3') - .find() - .next(function (err, doc) { - expect(err).to.not.exist; - test.ok(doc.doc instanceof Buffer); - - client.close(done); - }); - } - ); - }); - } - }); - - it('should correctly honor promoteBuffers at cursor find level', { - // Add a tag that our runner can trigger on - // in this case we are setting that node needs to be higher than 0.10.X to run - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } - }, - - test: function (done) { - var configuration = this.configuration; - - const client = configuration.newClient(); - client.connect(function (err, client) { - var db = client.db(configuration.db); - db.collection('shouldCorrectlyHonorPromoteBuffer4').insert( - { - doc: Buffer.alloc(256) - }, - function (err) { - expect(err).to.not.exist; - - db.collection('shouldCorrectlyHonorPromoteBuffer4') - .find({}, { promoteBuffers: true }) - .next(function (err, doc) { - expect(err).to.not.exist; - test.ok(doc.doc instanceof Buffer); - - client.close(done); - }); - } - ); - }); - } - }); - - it('should correctly honor promoteBuffers at aggregate level', { - // Add a tag that our runner can trigger on - // in this case we are setting that node needs to be higher than 0.10.X to run - metadata: { - requires: { - topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] - } - }, - - test: function (done) { - var configuration = this.configuration; - - const client = configuration.newClient(); - client.connect(function (err, client) { - var db = client.db(configuration.db); - db.collection('shouldCorrectlyHonorPromoteBuffer5').insert( - { - doc: Buffer.alloc(256) - }, - function (err) { - expect(err).to.not.exist; - - db.collection('shouldCorrectlyHonorPromoteBuffer5') - .aggregate([{ $match: {} }], { promoteBuffers: true }) - .next(function (err, doc) { - expect(err).to.not.exist; - test.ok(doc.doc instanceof Buffer); - - client.close(done); - }); - } - ); - }); - } - }); -}); diff --git a/test/integration/node-specific/bson-options/promote_buffers.test.ts b/test/integration/node-specific/bson-options/promote_buffers.test.ts new file mode 100644 index 00000000000..e71311c9dae --- /dev/null +++ b/test/integration/node-specific/bson-options/promote_buffers.test.ts @@ -0,0 +1,90 @@ +import { expect } from 'chai'; + +import { setupDatabase } from '../../shared'; + +describe('Promote Buffers', function () { + before(function () { + return setupDatabase(this.configuration); + }); + + it('should correctly honor promoteBuffers when creating an instance using Db', async function () { + const configuration = this.configuration; + const client = configuration.newClient(configuration.writeConcernMax(), { + maxPoolSize: 1, + promoteBuffers: true + }); + + const db = client.db(configuration.db); + await db.collection('shouldCorrectlyHonorPromoteBuffer1').insertOne({ + doc: Buffer.alloc(256) + }); + + const doc = await db.collection('shouldCorrectlyHonorPromoteBuffer1').findOne(); + expect(doc.doc).to.be.instanceof(Buffer); + await client.close(); + }); + + it('should correctly honor promoteBuffers when creating an instance using MongoClient', async function () { + const configuration = this.configuration; + + const client = configuration.newClient({}, { promoteBuffers: true }); + const db = client.db(configuration.db); + + await db.collection('shouldCorrectlyHonorPromoteBuffer2').insertOne({ + doc: Buffer.alloc(256) + }); + + const doc = await db.collection('shouldCorrectlyHonorPromoteBuffer2').findOne(); + expect(doc.doc).to.be.instanceof(Buffer); + await client.close(); + }); + + it('should correctly honor promoteBuffers at cursor level', async function () { + const configuration = this.configuration; + + const client = configuration.newClient({}, { promoteBuffers: true }); + const db = client.db(configuration.db); + + await db.collection('shouldCorrectlyHonorPromoteBuffer3').insertOne({ + doc: Buffer.alloc(256) + }); + + const doc = await db.collection('shouldCorrectlyHonorPromoteBuffer3').find().next(); + expect(doc.doc).to.be.instanceof(Buffer); + await client.close(); + }); + + it('should correctly honor promoteBuffers at cursor find level', async function () { + const configuration = this.configuration; + + const client = configuration.newClient(); + const db = client.db(configuration.db); + await db.collection('shouldCorrectlyHonorPromoteBuffer4').insertOne({ + doc: Buffer.alloc(256) + }); + + const doc = await db + .collection('shouldCorrectlyHonorPromoteBuffer4') + .find({}, { promoteBuffers: true }) + .next(); + expect(doc.doc).to.be.instanceof(Buffer); + await client.close(); + }); + + it('should correctly honor promoteBuffers at aggregate level', async function () { + const configuration = this.configuration; + + const client = configuration.newClient(); + const db = client.db(configuration.db); + await db.collection('shouldCorrectlyHonorPromoteBuffer5').insertOne({ + doc: Buffer.alloc(256) + }); + + const doc = await db + .collection('shouldCorrectlyHonorPromoteBuffer5') + .aggregate([{ $match: {} }], { promoteBuffers: true }) + .next(); + expect(doc.doc).to.be.instanceof(Buffer); + await client.close(); + }); +}); diff --git a/test/integration/node-specific/bson-options/promote_values.test.js b/test/integration/node-specific/bson-options/promote_values.test.js deleted file mode 100644 index 81a41b27a7a..00000000000 --- a/test/integration/node-specific/bson-options/promote_values.test.js +++ /dev/null @@ -1,209 +0,0 @@ -'use strict'; -const { expect } = require('chai'); -const { Long, Int32, Double } = require('../../../mongodb'); -const { assert: test, setupDatabase } = require('../../shared'); - -describe('Promote Values', function () { - before(function () { - return setupDatabase(this.configuration); - }); - - it('should correctly honor promoteValues when creating an instance using Db', { - // Add a tag that our runner can trigger on - // in this case we are setting that node needs to be higher than 0.10.X to run - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } - }, - - test: async function () { - let configuration = this.configuration; - let client = configuration.newClient(configuration.writeConcernMax(), { - maxPoolSize: 1, - promoteValues: false - }); - await client.connect(); - let db = client.db(configuration.db); - - await db.collection('shouldCorrectlyHonorPromoteValues').insertOne({ - doc: Long.fromNumber(10), - int: 10, - double: 2.2222, - array: [[Long.fromNumber(10)]] - }); - let doc = await db.collection('shouldCorrectlyHonorPromoteValues').findOne(); - expect(Long.fromNumber(10)).deep.equals(doc.doc); - expect(new Int32(10)).deep.equals(doc.int); - expect(new Double(2.2222)).deep.equals(doc.double); - - await client.close(); - } - }); - - it('should correctly honor promoteValues when creating an instance using MongoClient', { - // Add a tag that our runner can trigger on - // in this case we are setting that node needs to be higher than 0.10.X to run - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } - }, - - test: async function () { - var configuration = this.configuration; - const client = configuration.newClient({}, { promoteValues: false }); - await client.connect(); - const db = client.db(configuration.db); - - await db.collection('shouldCorrectlyHonorPromoteValues').insertOne({ - doc: Long.fromNumber(10), - int: 10, - double: 2.2222, - array: [[Long.fromNumber(10)]] - }); - - const doc = await db.collection('shouldCorrectlyHonorPromoteValues').findOne(); - expect(Long.fromNumber(10)).deep.equals(doc.doc); - expect(new Int32(10)).deep.equals(doc.int); - expect(new Double(2.2222)).deep.equals(doc.double); - - await client.close(); - } - }); - - it('should correctly honor promoteValues at cursor level', { - // Add a tag that our runner can trigger on - // in this case we are setting that node needs to be higher than 0.10.X to run - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } - }, - - test: async function () { - const configuration = this.configuration; - const client = configuration.newClient({}, { promoteValues: false }); - await client.connect(); - const db = client.db(configuration.db); - await db.collection('shouldCorrectlyHonorPromoteValues').insertOne({ - doc: Long.fromNumber(10), - int: 10, - double: 2.2222, - array: [[Long.fromNumber(10)]] - }); - - const doc = await db.collection('shouldCorrectlyHonorPromoteValues').find().next(); - expect(doc.doc).to.deep.equal(Long.fromNumber(10)); - expect(doc.int).to.deep.equal(new Int32(10)); - expect(doc.double).to.deep.equal(new Double(2.2222)); - - await client.close(); - } - }); - - it('should correctly honor promoteValues at cursor find level', { - // Add a tag that our runner can trigger on - // in this case we are setting that node needs to be higher than 0.10.X to run - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } - }, - - test: async function () { - const configuration = this.configuration; - const client = configuration.newClient(); - await client.connect(); - const db = client.db(configuration.db); - - await db.collection('shouldCorrectlyHonorPromoteValues').insertOne({ - doc: Long.fromNumber(10), - int: 10, - double: 2.2222, - array: [[Long.fromNumber(10)]] - }); - - const doc = await db - .collection('shouldCorrectlyHonorPromoteValues') - .find({}, { promoteValues: false }) - .next(); - expect(doc.doc).to.deep.equal(Long.fromNumber(10)); - expect(doc.int).to.deep.equal(new Int32(10)); - expect(doc.double).to.deep.equal(new Double(2.2222)); - - await client.close(); - } - }); - - it('should correctly honor promoteValues at aggregate level', { - // Add a tag that our runner can trigger on - // in this case we are setting that node needs to be higher than 0.10.X to run - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } - }, - - test: async function () { - const configuration = this.configuration; - const client = configuration.newClient(); - await client.connect(); - const db = client.db(configuration.db); - await db.collection('shouldCorrectlyHonorPromoteValues2').insertOne({ - doc: Long.fromNumber(10), - int: 10, - double: 2.2222, - array: [[Long.fromNumber(10)]] - }); - const doc = await db - .collection('shouldCorrectlyHonorPromoteValues2') - .aggregate([{ $match: {} }], { promoteValues: false }) - .next(); - expect(doc.doc, Long.fromNumber(10)); - expect(doc.int, new Int32(10)); - expect(doc.double, new Double(2.2222)); - - await client.close(); - } - }); - - it('Should correctly promoteValues when calling getMore on queries', { - metadata: { - requires: { - topology: ['single', 'ssl', 'wiredtiger'] - } - }, - - test: function (done) { - var configuration = this.configuration; - const client = configuration.newClient(); - client.connect(function (err, client) { - var docs = new Array(150).fill(0).map(function (_, i) { - return { - _id: 'needle_' + i, - is_even: i % 2, - long: Long.fromString('1234567890'), - double: 0.23456, - int: 1234 - }; - }); - - var db = client.db(configuration.db); - - db.collection('haystack').insertMany(docs, function (errInsert) { - if (errInsert) throw errInsert; - // change limit from 102 to 101 and this test passes. - // seems to indicate that the promoteValues flag is used for the - // initial find, but not for subsequent getMores - db.collection('haystack') - .find({}, { limit: 102, promoteValues: false }) - .stream() - .on('data', function (doc) { - test.equal(typeof doc.int, 'object'); - test.equal(doc.int._bsontype, 'Int32'); - test.equal(typeof doc.long, 'object'); - test.equal(doc.long._bsontype, 'Long'); - test.equal(typeof doc.double, 'object'); - test.equal(doc.double._bsontype, 'Double'); - }) - .on('end', function () { - db.dropCollection('haystack', function () { - client.close(done); - }); - }); - }); - }); - } - }); -}); diff --git a/test/integration/node-specific/bson-options/promote_values.test.ts b/test/integration/node-specific/bson-options/promote_values.test.ts new file mode 100644 index 00000000000..f92c32d4aa6 --- /dev/null +++ b/test/integration/node-specific/bson-options/promote_values.test.ts @@ -0,0 +1,172 @@ +import { expect } from 'chai'; + +import { Double, Int32, Long } from '../../../../src'; +import { assert as test, setupDatabase } from '../../shared'; + +describe('Promote Values', function () { + before(function () { + return setupDatabase(this.configuration); + }); + + it('should correctly honor promoteValues when creating an instance using Db', async function () { + const configuration = this.configuration; + const client = configuration.newClient(configuration.writeConcernMax(), { + maxPoolSize: 1, + promoteValues: false + }); + await client.connect(); + const db = client.db(configuration.db); + + await db.collection('shouldCorrectlyHonorPromoteValues').insertOne({ + doc: Long.fromNumber(10), + int: 10, + double: 2.2222, + array: [[Long.fromNumber(10)]] + }); + const doc = await db.collection('shouldCorrectlyHonorPromoteValues').findOne(); + expect(Long.fromNumber(10)).deep.equals(doc.doc); + expect(new Int32(10)).deep.equals(doc.int); + expect(new Double(2.2222)).deep.equals(doc.double); + + await client.close(); + }); + + // TODO(NODE-7192): remove as it duplicates "should correctly honor promoteValues when creating an instance using Db" + // it('should correctly honor promoteValues when creating an instance using MongoClient', { + // // Add a tag that our runner can trigger on + // // in this case we are setting that node needs to be higher than 0.10.X to run + // metadata: { + // requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } + // }, + // + // test: async function () { + // const configuration = this.configuration; + // const client = configuration.newClient({}, { promoteValues: false }); + // await client.connect(); + // const db = client.db(configuration.db); + // + // await db.collection('shouldCorrectlyHonorPromoteValues').insertOne({ + // doc: Long.fromNumber(10), + // int: 10, + // double: 2.2222, + // array: [[Long.fromNumber(10)]] + // }); + // + // const doc = await db.collection('shouldCorrectlyHonorPromoteValues').findOne(); + // expect(Long.fromNumber(10)).deep.equals(doc.doc); + // expect(new Int32(10)).deep.equals(doc.int); + // expect(new Double(2.2222)).deep.equals(doc.double); + // + // await client.close(); + // } + // }); + + // TODO(NODE-7192): remove as it duplicates "should correctly honor promoteValues when creating an instance using Db" + // it('should correctly honor promoteValues at cursor level', { + // // Add a tag that our runner can trigger on + // // in this case we are setting that node needs to be higher than 0.10.X to run + // metadata: { + // requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } + // }, + // + // test: async function () { + // const configuration = this.configuration; + // const client = configuration.newClient({}, { promoteValues: false }); + // await client.connect(); + // const db = client.db(configuration.db); + // await db.collection('shouldCorrectlyHonorPromoteValues').insertOne({ + // doc: Long.fromNumber(10), + // int: 10, + // double: 2.2222, + // array: [[Long.fromNumber(10)]] + // }); + // + // const doc = await db.collection('shouldCorrectlyHonorPromoteValues').find().next(); + // expect(doc.doc).to.deep.equal(Long.fromNumber(10)); + // expect(doc.int).to.deep.equal(new Int32(10)); + // expect(doc.double).to.deep.equal(new Double(2.2222)); + // + // await client.close(); + // } + // }); + + it('should correctly honor promoteValues at cursor find level', async function () { + const configuration = this.configuration; + const client = configuration.newClient(); + await client.connect(); + const db = client.db(configuration.db); + + await db.collection('shouldCorrectlyHonorPromoteValues').insertOne({ + doc: Long.fromNumber(10), + int: 10, + double: 2.2222, + array: [[Long.fromNumber(10)]] + }); + + const doc = await db + .collection('shouldCorrectlyHonorPromoteValues') + .find({}, { promoteValues: false }) + .next(); + expect(doc.doc).to.deep.equal(Long.fromNumber(10)); + expect(doc.int).to.deep.equal(new Int32(10)); + expect(doc.double).to.deep.equal(new Double(2.2222)); + + await client.close(); + }); + + it('should correctly honor promoteValues at aggregate level', async function () { + const configuration = this.configuration; + const client = configuration.newClient(); + await client.connect(); + const db = client.db(configuration.db); + await db.collection('shouldCorrectlyHonorPromoteValues2').insertOne({ + doc: Long.fromNumber(10), + int: 10, + double: 2.2222, + array: [[Long.fromNumber(10)]] + }); + const doc = await db + .collection('shouldCorrectlyHonorPromoteValues2') + .aggregate([{ $match: {} }], { promoteValues: false }) + .next(); + expect(doc.doc).to.deep.equal(Long.fromNumber(10)); + expect(doc.int).to.deep.equal(new Int32(10)); + expect(doc.double).to.deep.equal(new Double(2.2222)); + + await client.close(); + }); + + it('Should correctly promoteValues when calling getMore on queries', async function () { + const configuration = this.configuration; + const client = configuration.newClient(); + const docs = new Array(150).fill(0).map(function (_, i) { + return { + _id: 'needle_' + i, + is_even: i % 2, + long: Long.fromString('1234567890'), + double: 0.23456, + int: 1234 + }; + }); + + const db = client.db(configuration.db); + + await db.collection<{ _id: string }>('haystack').insertMany(docs); + + const stream = db + .collection('haystack') + .find({}, { batchSize: 50, promoteValues: false }) + .stream(); + + for await (const doc of stream) { + test.equal(typeof doc.int, 'object'); + test.equal(doc.int._bsontype, 'Int32'); + test.equal(typeof doc.long, 'object'); + test.equal(doc.long._bsontype, 'Long'); + test.equal(typeof doc.double, 'object'); + test.equal(doc.double._bsontype, 'Double'); + } + + await client.close(); + }); +});