Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/cli-repl/test/e2e-bson.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,19 @@ describe('BSON e2e', function() {
shell.assertNoErrors();
});
});
describe('MaxKey/MinKey special handling', () => {
it('inserts and retrieves MaxKey/MinKey regardless of whether they have been called as functions', async() => {
await shell.executeLine(`use ${dbName}`);
await shell.executeLine(`db.test.insertOne({
maxfn: MaxKey, maxval: MaxKey(), minfn: MinKey, minval: MinKey()
})`);
const output = await shell.executeLine('db.test.findOne()');
expect(output).to.include('maxfn: MaxKey()');
expect(output).to.include('maxval: MaxKey()');
expect(output).to.include('minfn: MinKey()');
expect(output).to.include('minval: MinKey()');
});
});
describe('help methods', () => {
it('ObjectId has help when returned from the server', async() => {
const value = new bson.ObjectId();
Expand Down
24 changes: 24 additions & 0 deletions packages/shell-api/src/shell-bson.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint camelcase: 0, new-cap: 0 */
import { CommonErrors, MongoshInvalidInputError } from '@mongosh/errors';
import { bson } from '@mongosh/service-provider-core';
import { serialize as bsonSerialize, deserialize as bsonDeserialize } from 'bson';
import { expect } from 'chai';
import sinon from 'ts-sinon';
import { ALL_SERVER_VERSIONS } from './enums';
Expand Down Expand Up @@ -77,6 +78,10 @@ describe('Shell BSON', () => {
const s = new (shellBson.MaxKey as any)();
expect(s._bsontype).to.equal('MaxKey');
});
it('using toBSON', () => {
const s = (shellBson.MaxKey as any).toBSON();
expect(s._bsontype).to.equal('MaxKey');
});
it('has help and other metadata', async() => {
const s = shellBson.MaxKey();
expect((await toShellResult(s.help)).type).to.equal('Help');
Expand All @@ -93,13 +98,31 @@ describe('Shell BSON', () => {
const s = new (shellBson.MinKey as any)();
expect(s._bsontype).to.equal('MinKey');
});
it('using toBSON', () => {
const s = (shellBson.MinKey as any).toBSON();
expect(s._bsontype).to.equal('MinKey');
});
it('has help and other metadata', async() => {
const s = shellBson.MinKey();
expect((await toShellResult(s.help)).type).to.equal('Help');
expect((await toShellResult(s.help())).type).to.equal('Help');
expect(s.serverVersions).to.deep.equal(ALL_SERVER_VERSIONS);
});
});
describe('MinKey & MaxKey constructor special handling', () => {
it('round-trips through bson as expected', () => {
const { MinKey, MaxKey } = shellBson as any;
const expected = { a: { $minKey: 1 }, b: { $maxKey: 1 } };
function roundtrip(value: any): any {
return bson.EJSON.serialize(bsonDeserialize(bsonSerialize(value)));
}

expect(roundtrip({ a: new MinKey(), b: new MaxKey() })).to.deep.equal(expected);
expect(roundtrip({ a: MinKey(), b: MaxKey() })).to.deep.equal(expected);
expect(roundtrip({ a: MinKey.toBSON(), b: MaxKey.toBSON() })).to.deep.equal(expected);
expect(roundtrip({ a: MinKey, b: MaxKey })).to.deep.equal(expected);
});
});
describe('ObjectId', () => {
it('without new', () => {
const s = shellBson.ObjectId('5ebbe8e2905bb493d6981b6b');
Expand Down Expand Up @@ -615,6 +638,7 @@ describe('Shell BSON', () => {
delete bsonProperties.length;
delete shellProperties.index; // ObjectId.index is a random number
delete bsonProperties.index; // ObjectId.index is a random number
delete shellProperties.toBSON; // toBSON is something we add for MaxKey/MinKey as a shell-specific extension
try {
expect(shellProperties).to.deep.equal(bsonProperties);
} catch (err) {
Expand Down
5 changes: 3 additions & 2 deletions packages/shell-api/src/shell-bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ export default function constructShellBson(bson: typeof BSON, printWarning: (msg
assertArgsDefinedType([object], ['object'], 'bsonsize');
return bson.calculateObjectSize(object);
},
// See https://jira.mongodb.org/browse/MONGOSH-1024 for context on the toBSON additions
MaxKey: Object.assign(function MaxKey(): typeof bson.MaxKey.prototype {
return new bson.MaxKey();
}, { ...bson.MaxKey, prototype: bson.MaxKey.prototype }),
}, { ...bson.MaxKey, toBSON: () => new bson.MaxKey(), prototype: bson.MaxKey.prototype }),
MinKey: Object.assign(function MinKey(): typeof bson.MinKey.prototype {
return new bson.MinKey();
}, { ...bson.MinKey, prototype: bson.MinKey.prototype }),
}, { ...bson.MinKey, toBSON: () => new bson.MinKey(), prototype: bson.MinKey.prototype }),
ObjectId: Object.assign(function ObjectId(id?: string | number | typeof bson.ObjectId.prototype | Buffer): typeof bson.ObjectId.prototype {
assertArgsDefinedType([id], [[undefined, 'string', 'number', 'object']], 'ObjectId');
return new bson.ObjectId(id);
Expand Down