Skip to content

Commit 3dc2cc1

Browse files
authored
don't require mongodb on browser tests
(part of the fix to #303)
1 parent 2e08392 commit 3dc2cc1

File tree

1 file changed

+62
-4
lines changed

1 file changed

+62
-4
lines changed

Diff for: test/node/extended_json_tests.js

+62-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,21 @@ const Long = BSON.Long;
1616
const MaxKey = BSON.MaxKey;
1717
const MinKey = BSON.MinKey;
1818
const ObjectID = BSON.ObjectID;
19+
const ObjectId = BSON.ObjectId;
1920
const BSONRegExp = BSON.BSONRegExp;
2021
const BSONSymbol = BSON.BSONSymbol;
2122
const Timestamp = BSON.Timestamp;
2223

24+
// support old ObjectID class because MongoDB drivers still return it
25+
const OldObjectID = (function() {
26+
try {
27+
return require('mongodb').ObjectID;
28+
}
29+
catch {
30+
return ObjectId; // if mongo is unavailable, e.g. browsers, just re-use BSON's
31+
}
32+
})();
33+
2334
describe('Extended JSON', function() {
2435
let doc = {};
2536

@@ -41,7 +52,9 @@ describe('Extended JSON', function() {
4152
long: Long.fromNumber(200),
4253
maxKey: new MaxKey(),
4354
minKey: new MinKey(),
44-
objectId: ObjectID.createFromHexString('111111111111111111111111'),
55+
objectId: ObjectId.createFromHexString('111111111111111111111111'),
56+
objectID: ObjectID.createFromHexString('111111111111111111111111'),
57+
oldObjectID: OldObjectID.createFromHexString('111111111111111111111111'),
4558
regexp: new BSONRegExp('hello world', 'i'),
4659
symbol: new BSONSymbol('symbol'),
4760
timestamp: Timestamp.fromNumber(1000),
@@ -55,7 +68,7 @@ describe('Extended JSON', function() {
5568
it('should correctly extend an existing mongodb module', function() {
5669
// Serialize the document
5770
var json =
58-
'{"_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"}}';
71+
'{"_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"}}';
5972

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

100113
it('should correctly serialize bson types when they are values', function() {
101-
var serialized = EJSON.stringify(new ObjectID('591801a468f9e7024b6235ea'), { relaxed: false });
114+
var serialized = EJSON.stringify(new ObjectId('591801a468f9e7024b6235ea'), { relaxed: false });
115+
expect(serialized).to.equal('{"$oid":"591801a468f9e7024b6235ea"}');
116+
serialized = EJSON.stringify(new ObjectID('591801a468f9e7024b6235ea'), { relaxed: false });
102117
expect(serialized).to.equal('{"$oid":"591801a468f9e7024b6235ea"}');
118+
serialized = EJSON.stringify(new OldObjectID('591801a468f9e7024b6235ea'), { relaxed: false });
119+
expect(serialized).to.equal('{"$oid":"591801a468f9e7024b6235ea"}');
120+
103121
serialized = EJSON.stringify(new Int32(42), { relaxed: false });
104122
expect(serialized).to.equal('{"$numberInt":"42"}');
123+
serialized = EJSON.stringify(
124+
{
125+
_id: { $nin: [new ObjectId('591801a468f9e7024b6235ea')] }
126+
},
127+
{ relaxed: false }
128+
);
129+
expect(serialized).to.equal('{"_id":{"$nin":[{"$oid":"591801a468f9e7024b6235ea"}]}}');
105130
serialized = EJSON.stringify(
106131
{
107132
_id: { $nin: [new ObjectID('591801a468f9e7024b6235ea')] }
108133
},
109134
{ relaxed: false }
110135
);
111136
expect(serialized).to.equal('{"_id":{"$nin":[{"$oid":"591801a468f9e7024b6235ea"}]}}');
137+
serialized = EJSON.stringify(
138+
{
139+
_id: { $nin: [new OldObjectID('591801a468f9e7024b6235ea')] }
140+
},
141+
{ relaxed: false }
142+
);
143+
expect(serialized).to.equal('{"_id":{"$nin":[{"$oid":"591801a468f9e7024b6235ea"}]}}');
112144

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

124156
expect(parsed).to.deep.equal({
125-
result: [{ _id: new ObjectID('591801a468f9e7024b623939'), emptyField: null }]
157+
result: [{ _id: new ObjectId('591801a468f9e7024b623939'), emptyField: null }]
126158
});
127159
});
128160

@@ -170,7 +202,9 @@ describe('Extended JSON', function() {
170202
long: new Long(234),
171203
maxKey: new MaxKey(),
172204
minKey: new MinKey(),
205+
objectId: ObjectId.createFromHexString('111111111111111111111111'),
173206
objectID: ObjectID.createFromHexString('111111111111111111111111'),
207+
oldObjectID: OldObjectID.createFromHexString('111111111111111111111111'),
174208
bsonRegExp: new BSONRegExp('hello world', 'i'),
175209
symbol: new BSONSymbol('symbol'),
176210
timestamp: new Timestamp()
@@ -187,7 +221,9 @@ describe('Extended JSON', function() {
187221
long: { $numberLong: '234' },
188222
maxKey: { $maxKey: 1 },
189223
minKey: { $minKey: 1 },
224+
objectId: { $oid: '111111111111111111111111' },
190225
objectID: { $oid: '111111111111111111111111' },
226+
oldObjectID: { $oid: '111111111111111111111111' },
191227
bsonRegExp: { $regularExpression: { pattern: 'hello world', options: 'i' } },
192228
symbol: { $symbol: 'symbol' },
193229
timestamp: { $timestamp: { t: 0, i: 0 } }
@@ -205,7 +241,9 @@ describe('Extended JSON', function() {
205241
long: { $numberLong: '234' },
206242
maxKey: { $maxKey: 1 },
207243
minKey: { $minKey: 1 },
244+
objectId: { $oid: '111111111111111111111111' },
208245
objectID: { $oid: '111111111111111111111111' },
246+
oldObjectID: { $oid: '111111111111111111111111' },
209247
bsonRegExp: { $regularExpression: { pattern: 'hello world', options: 'i' } },
210248
symbol: { $symbol: 'symbol' },
211249
timestamp: { $timestamp: { t: 0, i: 0 } }
@@ -237,7 +275,9 @@ describe('Extended JSON', function() {
237275
// minKey
238276
expect(result.minKey).to.be.an.instanceOf(BSON.MinKey);
239277
// objectID
278+
expect(result.objectId.toString()).to.equal('111111111111111111111111');
240279
expect(result.objectID.toString()).to.equal('111111111111111111111111');
280+
expect(result.oldObjectID.toString()).to.equal('111111111111111111111111');
241281
//bsonRegExp
242282
expect(result.bsonRegExp).to.be.an.instanceOf(BSON.BSONRegExp);
243283
expect(result.bsonRegExp.pattern).to.equal('hello world');
@@ -253,4 +293,22 @@ describe('Extended JSON', function() {
253293
expect(result.test).to.equal(34.12);
254294
expect(result.test).to.be.a('number');
255295
});
296+
297+
it('should work for function-valued and array-valued replacer parameters', function() {
298+
const doc = { a: new Int32(10), b: new Int32(10) };
299+
300+
var replacerArray = ['a', '$numberInt'];
301+
var serialized = EJSON.stringify(doc, replacerArray, 0, { relaxed: false });
302+
expect(serialized).to.equal('{"a":{"$numberInt":"10"}}');
303+
304+
serialized = EJSON.stringify(doc, replacerArray);
305+
expect(serialized).to.equal('{"a":10}');
306+
307+
var replacerFunc = function (key, value) { return key === 'b' ? undefined : value; }
308+
serialized = EJSON.stringify(doc, replacerFunc, 0, { relaxed: false });
309+
expect(serialized).to.equal('{"a":{"$numberInt":"10"}}');
310+
311+
serialized = EJSON.stringify(doc, replacerFunc);
312+
expect(serialized).to.equal('{"a":10}');
313+
});
256314
});

0 commit comments

Comments
 (0)