From f21bacfe973dceb7138263d1770593c10ec12016 Mon Sep 17 00:00:00 2001 From: Warren James Date: Wed, 25 Sep 2024 18:08:34 -0400 Subject: [PATCH 1/4] support new options arguments --- src/legacy_wrappers/gridfs.js | 33 +++++++++++++--- test/unit/legacy_wrappers/gridfs.test.js | 48 ++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/src/legacy_wrappers/gridfs.js b/src/legacy_wrappers/gridfs.js index a5c2f4d..0baf751 100644 --- a/src/legacy_wrappers/gridfs.js +++ b/src/legacy_wrappers/gridfs.js @@ -7,16 +7,37 @@ Object.defineProperty(module.exports, '__esModule', { value: true }); module.exports.makeLegacyGridFSBucket = function (baseClass) { class LegacyGridFSBucket extends baseClass { - delete(id, callback) { - return maybeCallback(super.delete(id), callback); + delete(id, options, callback) { + callback = + typeof callback === 'function' + ? callback + : typeof options === 'function' + ? options + : undefined; + options = typeof options !== 'function' ? options : undefined; + return maybeCallback(super.delete(id, options), callback); } - rename(id, filename, callback) { - return maybeCallback(super.rename(id, filename), callback); + rename(id, filename, options, callback) { + callback = + typeof callback === 'function' + ? callback + : typeof options === 'function' + ? options + : undefined; + options = typeof options !== 'function' ? options : undefined; + return maybeCallback(super.rename(id, filename, options), callback); } - drop(callback) { - return maybeCallback(super.drop(), callback); + drop(options, callback) { + callback = + typeof callback === 'function' + ? callback + : typeof options === 'function' + ? options + : undefined; + options = typeof options !== 'function' ? options : undefined; + return maybeCallback(super.drop(options), callback); } // conversion diff --git a/test/unit/legacy_wrappers/gridfs.test.js b/test/unit/legacy_wrappers/gridfs.test.js index b1c8ebd..1308c08 100644 --- a/test/unit/legacy_wrappers/gridfs.test.js +++ b/test/unit/legacy_wrappers/gridfs.test.js @@ -48,4 +48,52 @@ describe('legacy_wrappers/gridfs.js', () => { LegacyGridFSBucketWriteStream ); }); + + context('delete', function () { + it('correctly handles parameters when options are provided', function () { + const spy = sinon.spy(mongodbDriver.GridFSBucket.prototype, 'delete'); + const opts = { timeoutMS: 10 }; + const oid = new mongodbDriver.ObjectId(); + bucket.delete(oid, opts, () => {}); + expect(spy).to.be.calledWithExactly(oid, opts); + }); + it('correctly handles parameters when options are not provided', function () { + const spy = sinon.spy(mongodbDriver.GridFSBucket.prototype, 'delete'); + const oid = new mongodbDriver.ObjectId(); + bucket.delete(oid, () => {}); + expect(spy).to.be.calledWithExactly(oid, undefined); + }); + }); + + context('rename', function () { + it('correctly handles parameters when options are provided', function () { + const spy = sinon.spy(mongodbDriver.GridFSBucket.prototype, 'rename'); + const opts = { timeoutMS: 10 }; + const oid = new mongodbDriver.ObjectId(); + bucket.rename(oid, 'name', opts, () => {}); + expect(spy).to.be.calledWithExactly(oid, 'name', opts); + }); + + it('correctly handles parameters when options are not provided', function () { + const spy = sinon.spy(mongodbDriver.GridFSBucket.prototype, 'rename'); + const oid = new mongodbDriver.ObjectId(); + bucket.rename(oid, 'name', () => {}); + expect(spy).to.be.calledWithExactly(oid, 'name', undefined); + }); + }); + + context('drop', function () { + it('correctly handles parameters when options are provided', function () { + const spy = sinon.spy(mongodbDriver.GridFSBucket.prototype, 'drop'); + const opts = { timeoutMS: 10 }; + bucket.drop(opts, () => {}); + expect(spy).to.be.calledWithExactly(opts); + }); + + it('correctly handles parameters when options are not provided', function () { + const spy = sinon.spy(mongodbDriver.GridFSBucket.prototype, 'drop'); + bucket.drop(() => {}); + expect(spy).to.be.calledWithExactly(undefined); + }); + }); }); From ec590cd4ad977c54883ea588108e6ca8540799fa Mon Sep 17 00:00:00 2001 From: Warren James Date: Thu, 26 Sep 2024 11:14:49 -0400 Subject: [PATCH 2/4] support options for changesStream.close --- src/legacy_wrappers/change_stream.js | 11 ++++-- .../legacy_wrappers/change_streams.test.js | 36 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 test/unit/legacy_wrappers/change_streams.test.js diff --git a/src/legacy_wrappers/change_stream.js b/src/legacy_wrappers/change_stream.js index 4ac0094..49f582b 100644 --- a/src/legacy_wrappers/change_stream.js +++ b/src/legacy_wrappers/change_stream.js @@ -7,8 +7,15 @@ Object.defineProperty(module.exports, '__esModule', { value: true }); module.exports.makeLegacyChangeStream = function (baseClass) { class LegacyChangeStream extends baseClass { - close(callback) { - return maybeCallback(super.close(), callback); + close(options, callback) { + callback = + typeof callback === 'function' + ? callback + : typeof options === 'function' + ? options + : undefined; + options = typeof options !== 'function' ? options : undefined; + return maybeCallback(super.close(options), callback); } hasNext(callback) { return maybeCallback(super.hasNext(), callback); diff --git a/test/unit/legacy_wrappers/change_streams.test.js b/test/unit/legacy_wrappers/change_streams.test.js new file mode 100644 index 0000000..c577609 --- /dev/null +++ b/test/unit/legacy_wrappers/change_streams.test.js @@ -0,0 +1,36 @@ +'use strict'; + +const { MongoClient: LegacyMongoClient } = require('../../../src/index'); +const mongodbDriver = require('mongodb'); +const sinon = require('sinon'); +const { expect } = require('chai'); + +const iLoveJs = 'mongodb://iLoveJavascript'; + +describe('legacy_wrappers/change_streams.js', () => { + let client; + let changeStream; + beforeEach(async function () { + client = new LegacyMongoClient(iLoveJs); + changeStream = client.db('test').collection('test').watch(); + }); + + afterEach(async function () { + sinon.restore(); + await client.close(); + }); + + context('close', function () { + it('correctly handles parameters when options are provided', function () { + const spy = sinon.spy(mongodbDriver.ChangeStream.prototype, 'close'); + const opts = { timeoutMS: 100 }; + changeStream.close(opts, () => {}); + expect(spy).to.be.calledWithExactly(opts); + }); + it('correctly handles parameters when options are not provided', function () { + const spy = sinon.spy(mongodbDriver.ChangeStream.prototype, 'close'); + changeStream.close(() => {}); + expect(spy).to.be.calledWithExactly(undefined); + }); + }); +}); From e01ad19ee3bc5729a4352b86d6e1cb89f7717271 Mon Sep 17 00:00:00 2001 From: Warren James Date: Fri, 27 Sep 2024 11:44:48 -0400 Subject: [PATCH 3/4] update api tests --- test/tools/api.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/tools/api.js b/test/tools/api.js index be74cf7..26ceee5 100644 --- a/test/tools/api.js +++ b/test/tools/api.js @@ -52,7 +52,7 @@ const api = [ { className: 'OrderedBulkOperation', method: 'execute', returnType: 'Promise' }, { className: 'UnorderedBulkOperation', method: 'execute', returnType: 'Promise' }, - { className: 'ChangeStream', method: 'close', returnType: 'Promise' }, + { className: 'ChangeStream', method: 'close', returnType: 'Promise', possibleCallbackPositions: [1, 2]}, { className: 'ChangeStream', method: 'hasNext', returnType: 'Promise' }, { className: 'ChangeStream', method: 'next', returnType: 'Promise' }, { className: 'ChangeStream', method: 'tryNext', returnType: 'Promise' }, @@ -117,9 +117,9 @@ const api = [ { className: 'FindCursor', method: 'count', returnType: 'Promise' }, { className: 'FindCursor', method: 'explain', returnType: 'Promise' }, - { className: 'GridFSBucket', method: 'delete', returnType: 'Promise', possibleCallbackPositions: [1] }, - { className: 'GridFSBucket', method: 'drop', returnType: 'Promise' }, - { className: 'GridFSBucket', method: 'rename', returnType: 'Promise', possibleCallbackPositions: [1] }, + { className: 'GridFSBucket', method: 'delete', returnType: 'Promise', possibleCallbackPositions: [1, 2] }, + { className: 'GridFSBucket', method: 'drop', returnType: 'Promise', possibleCallbackPositions: [1, 2] }, + { className: 'GridFSBucket', method: 'rename', returnType: 'Promise', possibleCallbackPositions: [1, 2] }, { className: 'GridFSBucket', method: 'openUploadStream', returnType: 'GridFSBucketWriteStream', notAsync: true }, { className: 'GridFSBucket', method: 'openUploadStreamWithId', returnType: 'GridFSBucketWriteStream', notAsync: true }, { className: 'GridFSBucket', method: 'find', returnType: 'FindCursor', notAsync: true }, @@ -141,7 +141,7 @@ const api = [ ]; module.exports.api = api; -module.exports.classNames = new Set(api.map(({className}) => className)) +module.exports.classNames = new Set(api.map(({ className }) => className)) module.exports.classNameToMethodList = new Map(api.map((api, _, array) => [api.className, sorted(Array.from(new Set(Array.from(array.filter(v => v.className === api.className), method => method))), (a, b) => byStrings(a.method, b.method))] )); From 07e8024c2258c859c47fbdda0bb8371b79cee25e Mon Sep 17 00:00:00 2001 From: Warren James Date: Fri, 27 Sep 2024 13:59:08 -0400 Subject: [PATCH 4/4] remove redundant tests --- .../legacy_wrappers/change_streams.test.js | 36 -------------- test/unit/legacy_wrappers/gridfs.test.js | 48 ------------------- 2 files changed, 84 deletions(-) delete mode 100644 test/unit/legacy_wrappers/change_streams.test.js diff --git a/test/unit/legacy_wrappers/change_streams.test.js b/test/unit/legacy_wrappers/change_streams.test.js deleted file mode 100644 index c577609..0000000 --- a/test/unit/legacy_wrappers/change_streams.test.js +++ /dev/null @@ -1,36 +0,0 @@ -'use strict'; - -const { MongoClient: LegacyMongoClient } = require('../../../src/index'); -const mongodbDriver = require('mongodb'); -const sinon = require('sinon'); -const { expect } = require('chai'); - -const iLoveJs = 'mongodb://iLoveJavascript'; - -describe('legacy_wrappers/change_streams.js', () => { - let client; - let changeStream; - beforeEach(async function () { - client = new LegacyMongoClient(iLoveJs); - changeStream = client.db('test').collection('test').watch(); - }); - - afterEach(async function () { - sinon.restore(); - await client.close(); - }); - - context('close', function () { - it('correctly handles parameters when options are provided', function () { - const spy = sinon.spy(mongodbDriver.ChangeStream.prototype, 'close'); - const opts = { timeoutMS: 100 }; - changeStream.close(opts, () => {}); - expect(spy).to.be.calledWithExactly(opts); - }); - it('correctly handles parameters when options are not provided', function () { - const spy = sinon.spy(mongodbDriver.ChangeStream.prototype, 'close'); - changeStream.close(() => {}); - expect(spy).to.be.calledWithExactly(undefined); - }); - }); -}); diff --git a/test/unit/legacy_wrappers/gridfs.test.js b/test/unit/legacy_wrappers/gridfs.test.js index 1308c08..b1c8ebd 100644 --- a/test/unit/legacy_wrappers/gridfs.test.js +++ b/test/unit/legacy_wrappers/gridfs.test.js @@ -48,52 +48,4 @@ describe('legacy_wrappers/gridfs.js', () => { LegacyGridFSBucketWriteStream ); }); - - context('delete', function () { - it('correctly handles parameters when options are provided', function () { - const spy = sinon.spy(mongodbDriver.GridFSBucket.prototype, 'delete'); - const opts = { timeoutMS: 10 }; - const oid = new mongodbDriver.ObjectId(); - bucket.delete(oid, opts, () => {}); - expect(spy).to.be.calledWithExactly(oid, opts); - }); - it('correctly handles parameters when options are not provided', function () { - const spy = sinon.spy(mongodbDriver.GridFSBucket.prototype, 'delete'); - const oid = new mongodbDriver.ObjectId(); - bucket.delete(oid, () => {}); - expect(spy).to.be.calledWithExactly(oid, undefined); - }); - }); - - context('rename', function () { - it('correctly handles parameters when options are provided', function () { - const spy = sinon.spy(mongodbDriver.GridFSBucket.prototype, 'rename'); - const opts = { timeoutMS: 10 }; - const oid = new mongodbDriver.ObjectId(); - bucket.rename(oid, 'name', opts, () => {}); - expect(spy).to.be.calledWithExactly(oid, 'name', opts); - }); - - it('correctly handles parameters when options are not provided', function () { - const spy = sinon.spy(mongodbDriver.GridFSBucket.prototype, 'rename'); - const oid = new mongodbDriver.ObjectId(); - bucket.rename(oid, 'name', () => {}); - expect(spy).to.be.calledWithExactly(oid, 'name', undefined); - }); - }); - - context('drop', function () { - it('correctly handles parameters when options are provided', function () { - const spy = sinon.spy(mongodbDriver.GridFSBucket.prototype, 'drop'); - const opts = { timeoutMS: 10 }; - bucket.drop(opts, () => {}); - expect(spy).to.be.calledWithExactly(opts); - }); - - it('correctly handles parameters when options are not provided', function () { - const spy = sinon.spy(mongodbDriver.GridFSBucket.prototype, 'drop'); - bucket.drop(() => {}); - expect(spy).to.be.calledWithExactly(undefined); - }); - }); });