diff --git a/test/node/object_id.test.ts b/test/node/object_id.test.ts index f9f1b5290..6a8da7e13 100644 --- a/test/node/object_id.test.ts +++ b/test/node/object_id.test.ts @@ -258,91 +258,133 @@ describe('ObjectId', function () { ).to.be.true; }); - it('should correctly use buffer pool for ObjectId creation', function () { - const oldPoolSize = ObjectId.poolSize; - ObjectId.poolSize = 2; - const obj = new ObjectId(); - const obj2 = new ObjectId(); - - expect(obj.offset).to.equal(0); - expect(obj2.offset).to.equal(12); - expect(obj.offset).to.not.equal(obj2.offset); - expect(obj.pool).to.equal(obj2.pool); - - expect(obj.id).to.not.equal(obj2.id); - ObjectId.poolSize = oldPoolSize; - }); - - it('should respect buffer pool size for ObjectId creation', function () { - const oldPoolSize = ObjectId.poolSize; - ObjectId.poolSize = 2; - const test = new ObjectId(); - // Must fill current (large) pool first - const num = (test.pool.byteLength - test.offset) / 12; - for (let i = 0; i < num + 1; i++) { - new ObjectId(); - } + it('ObjectId.poolSize should be 1 by default', function () { + expect(ObjectId.poolSize).to.equal(1); + }); - const obj = new ObjectId(); - const obj2 = new ObjectId(); - const obj3 = new ObjectId(); - - expect(obj.offset).to.equal(0); - expect(obj2.offset).to.equal(12); - expect(obj3.offset).to.equal(0); - expect(obj.pool).to.equal(obj2.pool); - expect(obj2.pool).to.not.equal(obj3.pool); - - expect(obj.id).to.not.equal(obj2.id); - expect(obj2.id).to.not.equal(obj3.id); - ObjectId.poolSize = oldPoolSize; - }); - - it('should allow poolSize of 1', function () { - const oldPoolSize = ObjectId.poolSize; - ObjectId.poolSize = 1; - const test = new ObjectId(); - // Must fill current (large) pool first - const num = (test.pool.byteLength - test.offset) / 12; - for (let i = 0; i < num + 1; i++) { - new ObjectId(); - } + describe('when poolSize is greater than 1', function () { + let oldPoolSize; + before(function () { + oldPoolSize = ObjectId.poolSize; + ObjectId.poolSize = 2; + }); + + after(function () { + ObjectId.poolSize = oldPoolSize; + }); + + it('should correctly use buffer pool for ObjectId creation', function () { + const obj = new ObjectId(); + const obj2 = new ObjectId(); + + expect(obj.offset).to.equal(0); + expect(obj2.offset).to.equal(12); + expect(obj.offset).to.not.equal(obj2.offset); + expect(obj.pool).to.equal(obj2.pool); + + expect(obj.id).to.not.equal(obj2.id); + }); + + it('should respect buffer pool size for ObjectId creation', function () { + const oldPoolSize = ObjectId.poolSize; + ObjectId.poolSize = 2; + const test = new ObjectId(); + // Must fill current (large) pool first + const num = (test.pool.byteLength - test.offset) / 12; + for (let i = 0; i < num + 1; i++) { + new ObjectId(); + } - const obj = new ObjectId(); - const obj2 = new ObjectId(); - const obj3 = new ObjectId(); + const obj = new ObjectId(); + const obj2 = new ObjectId(); + const obj3 = new ObjectId(); - expect(obj.offset).to.equal(undefined); - expect(obj2.offset).to.equal(undefined); - expect(obj3.offset).to.equal(undefined); - expect(obj.pool).to.not.equal(obj2.pool); - expect(obj2.pool).to.not.equal(obj3.pool); + expect(obj.offset).to.equal(0); + expect(obj2.offset).to.equal(12); + expect(obj3.offset).to.equal(0); + expect(obj.pool).to.equal(obj2.pool); + expect(obj2.pool).to.not.equal(obj3.pool); + + expect(obj.id).to.not.equal(obj2.id); + expect(obj2.id).to.not.equal(obj3.id); + ObjectId.poolSize = oldPoolSize; + }); - expect(obj.id).to.not.equal(obj2.id); - expect(obj2.id).to.not.equal(obj3.id); - ObjectId.poolSize = oldPoolSize; + it('two identical ObjectId values are not deep equal', function () { + const oid0 = new ObjectId('00'.repeat(12)); + const oid1 = new ObjectId('00'.repeat(12)); + expect(oid0).to.not.deep.equal(oid1); + }); }); - it('should default to poolSize = 1 when invalid poolSize set', function () { - const oldPoolSize = ObjectId.poolSize; + describe('when poolSize is 1', function () { + let oldPoolSize; + before(function () { + oldPoolSize = ObjectId.poolSize; + ObjectId.poolSize = 1; + }); - ObjectId.poolSize = 0; - expect(ObjectId.poolSize).to.equal(1); - ObjectId.poolSize = -1; - expect(ObjectId.poolSize).to.equal(1); - ObjectId.poolSize = 0n; - expect(ObjectId.poolSize).to.equal(1); - ObjectId.poolSize = ''; - expect(ObjectId.poolSize).to.equal(1); - ObjectId.poolSize = NaN; - expect(ObjectId.poolSize).to.equal(1); - ObjectId.poolSize = {}; - expect(ObjectId.poolSize).to.equal(1); - ObjectId.poolSize = false; - expect(ObjectId.poolSize).to.equal(1); - ObjectId.poolSize = '1'; + after(function () { + ObjectId.poolSize = oldPoolSize; + }); + + it('should allow poolSize of 1', function () { + const test = new ObjectId(); + // Must fill current (large) pool first + const num = (test.pool.byteLength - test.offset) / 12; + for (let i = 0; i < num + 1; i++) { + new ObjectId(); + } + + const obj = new ObjectId(); + const obj2 = new ObjectId(); + const obj3 = new ObjectId(); + + expect(obj.offset).to.equal(undefined); + expect(obj2.offset).to.equal(undefined); + expect(obj3.offset).to.equal(undefined); + expect(obj.pool).to.not.equal(obj2.pool); + expect(obj2.pool).to.not.equal(obj3.pool); + + expect(obj.id).to.not.equal(obj2.id); + expect(obj2.id).to.not.equal(obj3.id); + }); + + it('two identical ObjectId values are deep equal', function () { + const oid0 = new ObjectId('00'.repeat(12)); + const oid1 = new ObjectId('00'.repeat(12)); + expect(oid0).to.deep.equal(oid1); + }); + }); + + describe('when poolSize is modified', function () { + let oldPoolSize; + beforeEach(function () { + oldPoolSize = ObjectId.poolSize; + ObjectId.poolSize = 1; + }); + + afterEach(function () { + ObjectId.poolSize = oldPoolSize; + }); - ObjectId.poolSize = oldPoolSize; + it('should default to poolSize = 1 when invalid poolSize set', function () { + ObjectId.poolSize = 0; + expect(ObjectId.poolSize).to.equal(1); + ObjectId.poolSize = -1; + expect(ObjectId.poolSize).to.equal(1); + ObjectId.poolSize = 0n; + expect(ObjectId.poolSize).to.equal(1); + ObjectId.poolSize = ''; + expect(ObjectId.poolSize).to.equal(1); + ObjectId.poolSize = NaN; + expect(ObjectId.poolSize).to.equal(1); + ObjectId.poolSize = {}; + expect(ObjectId.poolSize).to.equal(1); + ObjectId.poolSize = false; + expect(ObjectId.poolSize).to.equal(1); + ObjectId.poolSize = '1'; + }); }); it('should throw error if non-12 byte non-24 hex string passed in', function () { @@ -534,48 +576,6 @@ describe('ObjectId', function () { expect(oid.toString()).to.not.equal(equalId.toString()); expect(oid.equals(equalId)).to.be.true; }); - - it('should use otherId[kId] Buffer for equality when otherId has _bsontype === ObjectId', () => { - let equalId = { _bsontype: 'ObjectId', [oidKId]: oid.id }; - - const propAccessRecord: string[] = []; - equalId = new Proxy(equalId, { - get(target, prop: string, recv) { - if (prop !== '_bsontype') { - propAccessRecord.push(prop); - } - return Reflect.get(target, prop, recv); - } - }); - - expect(oid.equals(equalId)).to.be.true; - // once for the 11th byte shortcut - // once for the total equality - expect(propAccessRecord).to.deep.equal(['pool', oidKId, oidKId]); - }); - - it('should use otherId[kId] Pool for equality when otherId has _bsontype === ObjectId when using pool', () => { - const oldPoolSize = ObjectId.poolSize; - ObjectId.poolSize = 2; - const oid = new ObjectId(oidString); - let equalId = new ObjectId(oidString); - - const propAccessRecord: string[] = []; - equalId = new Proxy(equalId, { - get(target, prop: string, recv) { - if (prop !== '_bsontype') { - propAccessRecord.push(prop); - } - return Reflect.get(target, prop, recv); - } - }); - - expect(oid.equals(equalId)).to.be.true; - // once for the 11th byte shortcut - // once for the total equality - expect(propAccessRecord).to.contain('pool').contain('offset'); - ObjectId.poolSize = oldPoolSize; - }); }); context('createFromHexString()', () => {