Skip to content

Commit

Permalink
don't require mongodb on browser tests
Browse files Browse the repository at this point in the history
(part of the fix to #303)
  • Loading branch information
justingrant committed Feb 9, 2019
1 parent 2e08392 commit 3dc2cc1
Showing 1 changed file with 62 additions and 4 deletions.
66 changes: 62 additions & 4 deletions test/node/extended_json_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@ const Long = BSON.Long;
const MaxKey = BSON.MaxKey;
const MinKey = BSON.MinKey;
const ObjectID = BSON.ObjectID;
const ObjectId = BSON.ObjectId;
const BSONRegExp = BSON.BSONRegExp;
const BSONSymbol = BSON.BSONSymbol;
const Timestamp = BSON.Timestamp;

// support old ObjectID class because MongoDB drivers still return it
const OldObjectID = (function() {
try {
return require('mongodb').ObjectID;
}
catch {
return ObjectId; // if mongo is unavailable, e.g. browsers, just re-use BSON's
}
})();

describe('Extended JSON', function() {
let doc = {};

Expand All @@ -41,7 +52,9 @@ describe('Extended JSON', function() {
long: Long.fromNumber(200),
maxKey: new MaxKey(),
minKey: new MinKey(),
objectId: ObjectID.createFromHexString('111111111111111111111111'),
objectId: ObjectId.createFromHexString('111111111111111111111111'),
objectID: ObjectID.createFromHexString('111111111111111111111111'),
oldObjectID: OldObjectID.createFromHexString('111111111111111111111111'),
regexp: new BSONRegExp('hello world', 'i'),
symbol: new BSONSymbol('symbol'),
timestamp: Timestamp.fromNumber(1000),
Expand All @@ -55,7 +68,7 @@ describe('Extended JSON', function() {
it('should correctly extend an existing mongodb module', function() {
// Serialize the document
var json =
'{"_id":{"$numberInt":"100"},"gh":{"$numberInt":"1"},"binary":{"$binary":{"base64":"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+Pw==","subType":"00"}},"date":{"$date":{"$numberLong":"1488372056737"}},"code":{"$code":"function() {}","$scope":{"a":{"$numberInt":"1"}}},"dbRef":{"$ref":"tests","$id":{"$numberInt":"1"},"$db":"test"},"decimal":{"$numberDecimal":"100"},"double":{"$numberDouble":"10.1"},"int32":{"$numberInt":"10"},"long":{"$numberLong":"200"},"maxKey":{"$maxKey":1},"minKey":{"$minKey":1},"objectId":{"$oid":"111111111111111111111111"},"regexp":{"$regularExpression":{"pattern":"hello world","options":"i"}},"symbol":{"$symbol":"symbol"},"timestamp":{"$timestamp":{"t":0,"i":1000}},"int32Number":{"$numberInt":"300"},"doubleNumber":{"$numberDouble":"200.2"},"longNumberIntFit":{"$numberLong":"7036874417766400"},"doubleNumberIntFit":{"$numberLong":"19007199250000000"}}';
'{"_id":{"$numberInt":"100"},"gh":{"$numberInt":"1"},"binary":{"$binary":{"base64":"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+Pw==","subType":"00"}},"date":{"$date":{"$numberLong":"1488372056737"}},"code":{"$code":"function() {}","$scope":{"a":{"$numberInt":"1"}}},"dbRef":{"$ref":"tests","$id":{"$numberInt":"1"},"$db":"test"},"decimal":{"$numberDecimal":"100"},"double":{"$numberDouble":"10.1"},"int32":{"$numberInt":"10"},"long":{"$numberLong":"200"},"maxKey":{"$maxKey":1},"minKey":{"$minKey":1},"objectId":{"$oid":"111111111111111111111111"},"objectID":{"$oid":"111111111111111111111111"},"oldObjectID":{"$oid":"111111111111111111111111"},"regexp":{"$regularExpression":{"pattern":"hello world","options":"i"}},"symbol":{"$symbol":"symbol"},"timestamp":{"$timestamp":{"t":0,"i":1000}},"int32Number":{"$numberInt":"300"},"doubleNumber":{"$numberDouble":"200.2"},"longNumberIntFit":{"$numberLong":"7036874417766400"},"doubleNumberIntFit":{"$numberLong":"19007199250000000"}}';

assert.equal(json, EJSON.stringify(doc, null, 0, { relaxed: false }));
});
Expand Down Expand Up @@ -98,17 +111,36 @@ describe('Extended JSON', function() {
});

it('should correctly serialize bson types when they are values', function() {
var serialized = EJSON.stringify(new ObjectID('591801a468f9e7024b6235ea'), { relaxed: false });
var serialized = EJSON.stringify(new ObjectId('591801a468f9e7024b6235ea'), { relaxed: false });
expect(serialized).to.equal('{"$oid":"591801a468f9e7024b6235ea"}');
serialized = EJSON.stringify(new ObjectID('591801a468f9e7024b6235ea'), { relaxed: false });
expect(serialized).to.equal('{"$oid":"591801a468f9e7024b6235ea"}');
serialized = EJSON.stringify(new OldObjectID('591801a468f9e7024b6235ea'), { relaxed: false });
expect(serialized).to.equal('{"$oid":"591801a468f9e7024b6235ea"}');

serialized = EJSON.stringify(new Int32(42), { relaxed: false });
expect(serialized).to.equal('{"$numberInt":"42"}');
serialized = EJSON.stringify(
{
_id: { $nin: [new ObjectId('591801a468f9e7024b6235ea')] }
},
{ relaxed: false }
);
expect(serialized).to.equal('{"_id":{"$nin":[{"$oid":"591801a468f9e7024b6235ea"}]}}');
serialized = EJSON.stringify(
{
_id: { $nin: [new ObjectID('591801a468f9e7024b6235ea')] }
},
{ relaxed: false }
);
expect(serialized).to.equal('{"_id":{"$nin":[{"$oid":"591801a468f9e7024b6235ea"}]}}');
serialized = EJSON.stringify(
{
_id: { $nin: [new OldObjectID('591801a468f9e7024b6235ea')] }
},
{ relaxed: false }
);
expect(serialized).to.equal('{"_id":{"$nin":[{"$oid":"591801a468f9e7024b6235ea"}]}}');

serialized = EJSON.stringify(new Binary(new Uint8Array([1, 2, 3, 4, 5])), { relaxed: false });
expect(serialized).to.equal('{"$binary":{"base64":"AQIDBAU=","subType":"00"}}');
Expand All @@ -122,7 +154,7 @@ describe('Extended JSON', function() {
var parsed = EJSON.parse(input);

expect(parsed).to.deep.equal({
result: [{ _id: new ObjectID('591801a468f9e7024b623939'), emptyField: null }]
result: [{ _id: new ObjectId('591801a468f9e7024b623939'), emptyField: null }]
});
});

Expand Down Expand Up @@ -170,7 +202,9 @@ describe('Extended JSON', function() {
long: new Long(234),
maxKey: new MaxKey(),
minKey: new MinKey(),
objectId: ObjectId.createFromHexString('111111111111111111111111'),
objectID: ObjectID.createFromHexString('111111111111111111111111'),
oldObjectID: OldObjectID.createFromHexString('111111111111111111111111'),
bsonRegExp: new BSONRegExp('hello world', 'i'),
symbol: new BSONSymbol('symbol'),
timestamp: new Timestamp()
Expand All @@ -187,7 +221,9 @@ describe('Extended JSON', function() {
long: { $numberLong: '234' },
maxKey: { $maxKey: 1 },
minKey: { $minKey: 1 },
objectId: { $oid: '111111111111111111111111' },
objectID: { $oid: '111111111111111111111111' },
oldObjectID: { $oid: '111111111111111111111111' },
bsonRegExp: { $regularExpression: { pattern: 'hello world', options: 'i' } },
symbol: { $symbol: 'symbol' },
timestamp: { $timestamp: { t: 0, i: 0 } }
Expand All @@ -205,7 +241,9 @@ describe('Extended JSON', function() {
long: { $numberLong: '234' },
maxKey: { $maxKey: 1 },
minKey: { $minKey: 1 },
objectId: { $oid: '111111111111111111111111' },
objectID: { $oid: '111111111111111111111111' },
oldObjectID: { $oid: '111111111111111111111111' },
bsonRegExp: { $regularExpression: { pattern: 'hello world', options: 'i' } },
symbol: { $symbol: 'symbol' },
timestamp: { $timestamp: { t: 0, i: 0 } }
Expand Down Expand Up @@ -237,7 +275,9 @@ describe('Extended JSON', function() {
// minKey
expect(result.minKey).to.be.an.instanceOf(BSON.MinKey);
// objectID
expect(result.objectId.toString()).to.equal('111111111111111111111111');
expect(result.objectID.toString()).to.equal('111111111111111111111111');
expect(result.oldObjectID.toString()).to.equal('111111111111111111111111');
//bsonRegExp
expect(result.bsonRegExp).to.be.an.instanceOf(BSON.BSONRegExp);
expect(result.bsonRegExp.pattern).to.equal('hello world');
Expand All @@ -253,4 +293,22 @@ describe('Extended JSON', function() {
expect(result.test).to.equal(34.12);
expect(result.test).to.be.a('number');
});

it('should work for function-valued and array-valued replacer parameters', function() {
const doc = { a: new Int32(10), b: new Int32(10) };

var replacerArray = ['a', '$numberInt'];
var serialized = EJSON.stringify(doc, replacerArray, 0, { relaxed: false });
expect(serialized).to.equal('{"a":{"$numberInt":"10"}}');

serialized = EJSON.stringify(doc, replacerArray);
expect(serialized).to.equal('{"a":10}');

var replacerFunc = function (key, value) { return key === 'b' ? undefined : value; }
serialized = EJSON.stringify(doc, replacerFunc, 0, { relaxed: false });
expect(serialized).to.equal('{"a":{"$numberInt":"10"}}');

serialized = EJSON.stringify(doc, replacerFunc);
expect(serialized).to.equal('{"a":10}');
});
});

0 comments on commit 3dc2cc1

Please sign in to comment.