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
24 changes: 20 additions & 4 deletions packages/shell-api/src/shell-bson.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ describe('Shell BSON', () => {
expect(s._bsontype).to.equal('ObjectID');
expect(s.toHexString()).to.equal('5ebbe8e2905bb493d6981b6b');
});
it('works with an integer argument', () => {
const s = new (shellBson.ObjectId as any)(0x12345678);
expect(s._bsontype).to.equal('ObjectID');
expect(s.toHexString().slice(0, 8)).to.equal('12345678');
});
it('has help and other metadata', async() => {
const s = shellBson.ObjectId();
expect((await toShellResult(s.help)).type).to.equal('Help');
Expand All @@ -119,9 +124,9 @@ describe('Shell BSON', () => {
});
it('errors for wrong type of arg 1', () => {
try {
(shellBson.ObjectId as any)(1);
(shellBson.ObjectId as any)(Symbol('foo'));
} catch (e) {
return expect(e.message).to.contain('string, got number');
return expect(e.message).to.contain('object, got symbol');
}
expect.fail('Expecting error, nothing thrown');
});
Expand Down Expand Up @@ -152,6 +157,11 @@ describe('Shell BSON', () => {
const s = new (shellBson.Timestamp as any)(0, 100);
expect(s._bsontype).to.equal('Timestamp');
});
it('with a long argument', () => {
const s = shellBson.Timestamp(shellBson.Long(1, 2));
expect(s._bsontype).to.equal('Timestamp');
expect(s.toExtendedJSON()).to.deep.equal({ $timestamp: { t: 2, i: 1 } });
});
it('has help and other metadata', async() => {
const s = shellBson.Timestamp(0, 100);
expect((await toShellResult(s.help)).type).to.equal('Help');
Expand All @@ -162,7 +172,7 @@ describe('Shell BSON', () => {
try {
(shellBson.Timestamp as any)('1');
} catch (e) {
return expect(e.message).to.contain('number, got string');
return expect(e.message).to.contain('object, got string');
}
expect.fail('Expecting error, nothing thrown');
});
Expand Down Expand Up @@ -191,6 +201,12 @@ describe('Shell BSON', () => {
expect(code.code).to.equal('code');
expect(code.scope).to.deep.equal({ k: 'v' });
});
it('works with a function argument', () => {
const fn = function() { expect.fail(); };
const code = shellBson.Code(fn, { k: 'v' });
expect(code.code).to.equal(fn);
expect(code.scope).to.deep.equal({ k: 'v' });
});
it('has help and other metadata', async() => {
const s = shellBson.Code('code', { k: 'v' });
expect((await toShellResult(s.help)).type).to.equal('Help');
Expand All @@ -201,7 +217,7 @@ describe('Shell BSON', () => {
try {
(shellBson.Code as any)(1);
} catch (e) {
return expect(e.message).to.contain('string, got number');
return expect(e.message).to.contain('function, got number');
}
expect.fail('Expecting error, nothing thrown');
});
Expand Down
22 changes: 11 additions & 11 deletions packages/shell-api/src/shell-bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,32 @@ export default function constructShellBson(bson: typeof BSON, printWarning: (msg
(bson.BSONSymbol as any).prototype.deprecated = true;

const bsonPkg = {
DBRef: Object.assign(function(namespace: string, oid: any, db?: string): any {
DBRef: Object.assign(function(namespace: string, oid: any, db?: string): typeof bson.DBRef.prototype {
assertArgsDefinedType([namespace, oid, db], ['string', true, [undefined, 'string']], 'DBRef');
return new bson.DBRef(namespace, oid, db);
}, { prototype: bson.DBRef.prototype }),
// DBPointer not available in the bson 1.x library, but depreciated since 1.6
Map: bson.Map,
bsonsize: function(object: any): any {
bsonsize: function(object: any): number {
assertArgsDefinedType([object], ['object'], 'bsonsize');
return bson.calculateObjectSize(object);
},
MaxKey: Object.assign(function(): any {
MaxKey: Object.assign(function(): typeof bson.MaxKey.prototype {
return new bson.MaxKey();
}, { prototype: bson.MaxKey.prototype }),
MinKey: Object.assign(function(): any {
MinKey: Object.assign(function(): typeof bson.MinKey.prototype {
return new bson.MinKey();
}, { prototype: bson.MinKey.prototype }),
ObjectId: Object.assign(function(id?: string): any {
assertArgsDefinedType([id], [[undefined, 'string']], 'ObjectId');
ObjectId: Object.assign(function(id?: string | number | typeof bson.ObjectId.prototype | Buffer): typeof bson.ObjectId.prototype {
assertArgsDefinedType([id], [[undefined, 'string', 'number', 'object']], 'ObjectId');
return new bson.ObjectId(id);
}, { prototype: bson.ObjectId.prototype }),
Timestamp: Object.assign(function(low = 0, high = 0): any {
assertArgsDefinedType([low, high], ['number', 'number'], 'Timestamp');
return new bson.Timestamp(low, high);
Timestamp: Object.assign(function(low?: number | typeof bson.Long.prototype, high?: number): typeof bson.Timestamp.prototype {
assertArgsDefinedType([low, high], [['number', 'object', undefined], [undefined, 'number']], 'Timestamp');
return new bson.Timestamp(low as number, high as number);
}, { prototype: bson.Timestamp.prototype }),
Code: Object.assign(function(c: any = '', s?: any): any {
assertArgsDefinedType([c, s], [[undefined, 'string'], [undefined, 'object']], 'Code');
Code: Object.assign(function(c: string | Function = '', s?: any): typeof bson.Code.prototype {
assertArgsDefinedType([c, s], [[undefined, 'string', 'function'], [undefined, 'object']], 'Code');
return new bson.Code(c, s);
}, { prototype: bson.Code.prototype }),
NumberDecimal: Object.assign(function(s = '0'): any {
Expand Down