@@ -16,10 +16,26 @@ const Long = BSON.Long;
1616const MaxKey = BSON . MaxKey ;
1717const MinKey = BSON . MinKey ;
1818const ObjectID = BSON . ObjectID ;
19+ const ObjectId = BSON . ObjectId ;
1920const BSONRegExp = BSON . BSONRegExp ;
2021const BSONSymbol = BSON . BSONSymbol ;
2122const Timestamp = BSON . Timestamp ;
2223
24+ // test the old ObjectID class because MongoDB drivers still return it
25+ // fall back to BSON's ObjectId in browser tests
26+ function getOldObjectID ( ) {
27+ try {
28+ // do a dynamic resolve to avoid exception when running browser tests
29+ const file = require . resolve ( 'mongodb' ) ;
30+ return require ( file ) . ObjectID ;
31+ }
32+ catch ( e ) {
33+ return ObjectId ; // if mongo is unavailable, e.g. browsers, just re-use BSON's
34+ }
35+ }
36+
37+ const OldObjectID = getOldObjectID ( ) ;
38+
2339describe ( 'Extended JSON' , function ( ) {
2440 let doc = { } ;
2541
@@ -41,7 +57,9 @@ describe('Extended JSON', function() {
4157 long : Long . fromNumber ( 200 ) ,
4258 maxKey : new MaxKey ( ) ,
4359 minKey : new MinKey ( ) ,
44- objectId : ObjectID . createFromHexString ( '111111111111111111111111' ) ,
60+ objectId : ObjectId . createFromHexString ( '111111111111111111111111' ) ,
61+ objectID : ObjectID . createFromHexString ( '111111111111111111111111' ) ,
62+ oldObjectID : OldObjectID . createFromHexString ( '111111111111111111111111' ) ,
4563 regexp : new BSONRegExp ( 'hello world' , 'i' ) ,
4664 symbol : new BSONSymbol ( 'symbol' ) ,
4765 timestamp : Timestamp . fromNumber ( 1000 ) ,
@@ -55,7 +73,7 @@ describe('Extended JSON', function() {
5573 it ( 'should correctly extend an existing mongodb module' , function ( ) {
5674 // Serialize the document
5775 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"}}' ;
76+ '{"_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"}}' ;
5977
6078 assert . equal ( json , EJSON . stringify ( doc , null , 0 , { relaxed : false } ) ) ;
6179 } ) ;
@@ -98,17 +116,36 @@ describe('Extended JSON', function() {
98116 } ) ;
99117
100118 it ( 'should correctly serialize bson types when they are values' , function ( ) {
101- var serialized = EJSON . stringify ( new ObjectID ( '591801a468f9e7024b6235ea' ) , { relaxed : false } ) ;
119+ var serialized = EJSON . stringify ( new ObjectId ( '591801a468f9e7024b6235ea' ) , { relaxed : false } ) ;
120+ expect ( serialized ) . to . equal ( '{"$oid":"591801a468f9e7024b6235ea"}' ) ;
121+ serialized = EJSON . stringify ( new ObjectID ( '591801a468f9e7024b6235ea' ) , { relaxed : false } ) ;
102122 expect ( serialized ) . to . equal ( '{"$oid":"591801a468f9e7024b6235ea"}' ) ;
123+ serialized = EJSON . stringify ( new OldObjectID ( '591801a468f9e7024b6235ea' ) , { relaxed : false } ) ;
124+ expect ( serialized ) . to . equal ( '{"$oid":"591801a468f9e7024b6235ea"}' ) ;
125+
103126 serialized = EJSON . stringify ( new Int32 ( 42 ) , { relaxed : false } ) ;
104127 expect ( serialized ) . to . equal ( '{"$numberInt":"42"}' ) ;
128+ serialized = EJSON . stringify (
129+ {
130+ _id : { $nin : [ new ObjectId ( '591801a468f9e7024b6235ea' ) ] }
131+ } ,
132+ { relaxed : false }
133+ ) ;
134+ expect ( serialized ) . to . equal ( '{"_id":{"$nin":[{"$oid":"591801a468f9e7024b6235ea"}]}}' ) ;
105135 serialized = EJSON . stringify (
106136 {
107137 _id : { $nin : [ new ObjectID ( '591801a468f9e7024b6235ea' ) ] }
108138 } ,
109139 { relaxed : false }
110140 ) ;
111141 expect ( serialized ) . to . equal ( '{"_id":{"$nin":[{"$oid":"591801a468f9e7024b6235ea"}]}}' ) ;
142+ serialized = EJSON . stringify (
143+ {
144+ _id : { $nin : [ new OldObjectID ( '591801a468f9e7024b6235ea' ) ] }
145+ } ,
146+ { relaxed : false }
147+ ) ;
148+ expect ( serialized ) . to . equal ( '{"_id":{"$nin":[{"$oid":"591801a468f9e7024b6235ea"}]}}' ) ;
112149
113150 serialized = EJSON . stringify ( new Binary ( new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 ] ) ) , { relaxed : false } ) ;
114151 expect ( serialized ) . to . equal ( '{"$binary":{"base64":"AQIDBAU=","subType":"00"}}' ) ;
@@ -122,7 +159,7 @@ describe('Extended JSON', function() {
122159 var parsed = EJSON . parse ( input ) ;
123160
124161 expect ( parsed ) . to . deep . equal ( {
125- result : [ { _id : new ObjectID ( '591801a468f9e7024b623939' ) , emptyField : null } ]
162+ result : [ { _id : new ObjectId ( '591801a468f9e7024b623939' ) , emptyField : null } ]
126163 } ) ;
127164 } ) ;
128165
@@ -170,7 +207,9 @@ describe('Extended JSON', function() {
170207 long : new Long ( 234 ) ,
171208 maxKey : new MaxKey ( ) ,
172209 minKey : new MinKey ( ) ,
210+ objectId : ObjectId . createFromHexString ( '111111111111111111111111' ) ,
173211 objectID : ObjectID . createFromHexString ( '111111111111111111111111' ) ,
212+ oldObjectID : OldObjectID . createFromHexString ( '111111111111111111111111' ) ,
174213 bsonRegExp : new BSONRegExp ( 'hello world' , 'i' ) ,
175214 symbol : new BSONSymbol ( 'symbol' ) ,
176215 timestamp : new Timestamp ( )
@@ -187,7 +226,9 @@ describe('Extended JSON', function() {
187226 long : { $numberLong : '234' } ,
188227 maxKey : { $maxKey : 1 } ,
189228 minKey : { $minKey : 1 } ,
229+ objectId : { $oid : '111111111111111111111111' } ,
190230 objectID : { $oid : '111111111111111111111111' } ,
231+ oldObjectID : { $oid : '111111111111111111111111' } ,
191232 bsonRegExp : { $regularExpression : { pattern : 'hello world' , options : 'i' } } ,
192233 symbol : { $symbol : 'symbol' } ,
193234 timestamp : { $timestamp : { t : 0 , i : 0 } }
@@ -205,7 +246,9 @@ describe('Extended JSON', function() {
205246 long : { $numberLong : '234' } ,
206247 maxKey : { $maxKey : 1 } ,
207248 minKey : { $minKey : 1 } ,
249+ objectId : { $oid : '111111111111111111111111' } ,
208250 objectID : { $oid : '111111111111111111111111' } ,
251+ oldObjectID : { $oid : '111111111111111111111111' } ,
209252 bsonRegExp : { $regularExpression : { pattern : 'hello world' , options : 'i' } } ,
210253 symbol : { $symbol : 'symbol' } ,
211254 timestamp : { $timestamp : { t : 0 , i : 0 } }
@@ -237,7 +280,9 @@ describe('Extended JSON', function() {
237280 // minKey
238281 expect ( result . minKey ) . to . be . an . instanceOf ( BSON . MinKey ) ;
239282 // objectID
283+ expect ( result . objectId . toString ( ) ) . to . equal ( '111111111111111111111111' ) ;
240284 expect ( result . objectID . toString ( ) ) . to . equal ( '111111111111111111111111' ) ;
285+ expect ( result . oldObjectID . toString ( ) ) . to . equal ( '111111111111111111111111' ) ;
241286 //bsonRegExp
242287 expect ( result . bsonRegExp ) . to . be . an . instanceOf ( BSON . BSONRegExp ) ;
243288 expect ( result . bsonRegExp . pattern ) . to . equal ( 'hello world' ) ;
0 commit comments