Skip to content

Commit b7e89de

Browse files
committed
test(ext-json): add manual tests for extended JSON
1 parent d6b71ab commit b7e89de

File tree

1 file changed

+250
-0
lines changed

1 file changed

+250
-0
lines changed

test/node/extended_json_tests.js

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const expect = require('chai').expect;
5+
const BSON = require('../../lib/bson');
6+
const EJSON = BSON.EJSON;
7+
8+
// BSON types
9+
const Binary = BSON.Binary;
10+
const Code = BSON.Code;
11+
const DBRef = BSON.DBRef;
12+
const Decimal128 = BSON.Decimal128;
13+
const Double = BSON.Double;
14+
const Int32 = BSON.Int32;
15+
const Long = BSON.Long;
16+
const MaxKey = BSON.MaxKey;
17+
const MinKey = BSON.MinKey;
18+
const ObjectID = BSON.ObjectID;
19+
const BSONRegExp = BSON.BSONRegExp;
20+
const Symbol = BSON.Symbol;
21+
const Timestamp = BSON.Timestamp;
22+
23+
describe('Extended JSON', function() {
24+
let doc = {};
25+
26+
before(function() {
27+
const buffer = new Buffer(64);
28+
for (var i = 0; i < buffer.length; i++) buffer[i] = i;
29+
const date = new Date();
30+
date.setTime(1488372056737);
31+
doc = {
32+
_id: new Int32(100),
33+
gh: new Int32(1),
34+
binary: new Binary(buffer),
35+
date: date,
36+
code: new Code('function() {}', { a: new Int32(1) }),
37+
dbRef: new DBRef('tests', new Int32(1), 'test'),
38+
decimal: Decimal128.fromString('100'),
39+
double: new Double(10.1),
40+
int32: new Int32(10),
41+
long: Long.fromNumber(200),
42+
maxKey: new MaxKey(),
43+
minKey: new MinKey(),
44+
objectId: ObjectID.createFromHexString('111111111111111111111111'),
45+
regexp: new BSONRegExp('hello world', 'i'),
46+
symbol: new Symbol('symbol'),
47+
timestamp: Timestamp.fromNumber(1000),
48+
int32Number: 300,
49+
doubleNumber: 200.2,
50+
longNumberIntFit: 0x19000000000000,
51+
doubleNumberIntFit: 19007199250000000.12
52+
};
53+
});
54+
55+
it('should correctly extend an existing mongodb module', function() {
56+
// Serialize the document
57+
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"}}';
59+
60+
assert.equal(json, EJSON.stringify(doc, null, 0, { relaxed: false }));
61+
});
62+
63+
it('should correctly deserialize using the default relaxed mode', function() {
64+
// Deserialize the document using non strict mode
65+
var doc1 = EJSON.parse(EJSON.stringify(doc, null, 0));
66+
67+
// Validate the values
68+
assert.equal(300, doc1.int32Number);
69+
assert.equal(200.2, doc1.doubleNumber);
70+
assert.equal(0x19000000000000, doc1.longNumberIntFit);
71+
assert.equal(19007199250000000.12, doc1.doubleNumberIntFit);
72+
73+
// Deserialize the document using strict mode
74+
doc1 = EJSON.parse(EJSON.stringify(doc, null, 0), { relaxed: false });
75+
76+
// Validate the values
77+
expect(doc1.int32Number._bsontype).to.equal('Int32');
78+
expect(doc1.doubleNumber._bsontype).to.equal('Double');
79+
expect(doc1.longNumberIntFit._bsontype).to.equal('Long');
80+
expect(doc1.doubleNumberIntFit._bsontype).to.equal('Long');
81+
});
82+
83+
it('should correctly serialize, and deserialize using built-in BSON', function() {
84+
// Create a doc
85+
var doc1 = {
86+
int32: new Int32(10)
87+
};
88+
89+
// Serialize the document
90+
var text = EJSON.stringify(doc1, null, 0, { relaxed: false });
91+
expect(text).to.equal('{"int32":{"$numberInt":"10"}}');
92+
93+
// Deserialize the json in strict and non strict mode
94+
var doc2 = EJSON.parse(text, { relaxed: false });
95+
expect(doc2.int32._bsontype).to.equal('Int32');
96+
doc2 = EJSON.parse(text);
97+
expect(doc2.int32).to.equal(10);
98+
});
99+
100+
it('should correctly serialize bson types when they are values', function() {
101+
var serialized = EJSON.stringify(new ObjectID('591801a468f9e7024b6235ea'), { relaxed: false });
102+
expect(serialized).to.equal('{"$oid":"591801a468f9e7024b6235ea"}');
103+
serialized = EJSON.stringify(new Int32(42), { relaxed: false });
104+
expect(serialized).to.equal('{"$numberInt":"42"}');
105+
serialized = EJSON.stringify(
106+
{
107+
_id: { $nin: [new ObjectID('591801a468f9e7024b6235ea')] }
108+
},
109+
{ relaxed: false }
110+
);
111+
expect(serialized).to.equal('{"_id":{"$nin":[{"$oid":"591801a468f9e7024b6235ea"}]}}');
112+
113+
serialized = EJSON.stringify(new Binary(new Uint8Array([1, 2, 3, 4, 5])), { relaxed: false });
114+
expect(serialized).to.equal('{"$binary":{"base64":"AQIDBAU=","subType":"00"}}');
115+
});
116+
117+
it('should correctly parse null values', function() {
118+
expect(EJSON.parse('null')).to.be.null;
119+
expect(EJSON.parse('[null]')[0]).to.be.null;
120+
121+
var input = '{"result":[{"_id":{"$oid":"591801a468f9e7024b623939"},"emptyField":null}]}';
122+
var parsed = EJSON.parse(input);
123+
124+
expect(parsed).to.deep.equal({
125+
result: [{ _id: new ObjectID('591801a468f9e7024b623939'), emptyField: null }]
126+
});
127+
});
128+
129+
it('should correctly throw when passed a non-string to parse', function() {
130+
expect(() => {
131+
EJSON.parse({});
132+
}).to.throw;
133+
});
134+
135+
it('should allow relaxed parsing by default', function() {
136+
const dt = new Date(1452124800000);
137+
const inputObject = {
138+
int: { $numberInt: '500' },
139+
long: { $numberLong: '42' },
140+
double: { $numberDouble: '24' },
141+
date: { $date: { $numberLong: '1452124800000' } }
142+
};
143+
144+
const parsed = EJSON.parse(JSON.stringify(inputObject));
145+
expect(parsed).to.eql({
146+
int: 500,
147+
long: 42,
148+
double: 24,
149+
date: dt
150+
});
151+
});
152+
153+
it('should allow regexp', function() {
154+
const parsedRegExp = EJSON.stringify({ test: /some-regex/i });
155+
const parsedBSONRegExp = EJSON.stringify(
156+
{ test: new BSONRegExp('some-regex', 'i') },
157+
{ relaxed: true }
158+
);
159+
expect(parsedRegExp).to.eql(parsedBSONRegExp);
160+
});
161+
162+
it('should serialize from BSON object to EJSON object', function() {
163+
const doc = {
164+
binary: new Binary(''),
165+
code: new Code('function() {}'),
166+
dbRef: new DBRef('tests', new Int32(1), 'test'),
167+
decimal128: new Decimal128(128),
168+
double: new Double(10.1),
169+
int32: new Int32(10),
170+
long: new Long(234),
171+
maxKey: new MaxKey(),
172+
minKey: new MinKey(),
173+
objectID: ObjectID.createFromHexString('111111111111111111111111'),
174+
bsonRegExp: new BSONRegExp('hello world', 'i'),
175+
symbol: new Symbol('symbol'),
176+
timestamp: new Timestamp()
177+
};
178+
179+
const result = EJSON.serialize(doc, { relaxed: false });
180+
expect(result).to.deep.equal({
181+
binary: { $binary: { base64: '', subType: '00' } },
182+
code: { $code: 'function() {}' },
183+
dbRef: { $ref: 'tests', $id: { $numberInt: '1' }, $db: 'test' },
184+
decimal128: { $numberDecimal: '0E-6176' },
185+
double: { $numberDouble: '10.1' },
186+
int32: { $numberInt: '10' },
187+
long: { $numberLong: '234' },
188+
maxKey: { $maxKey: 1 },
189+
minKey: { $minKey: 1 },
190+
objectID: { $oid: '111111111111111111111111' },
191+
bsonRegExp: { $regularExpression: { pattern: 'hello world', options: 'i' } },
192+
symbol: { $symbol: 'symbol' },
193+
timestamp: { $timestamp: { t: 0, i: 0 } }
194+
});
195+
});
196+
197+
it('should deserialize from EJSON object to BSON object', function() {
198+
const doc = {
199+
binary: { $binary: { base64: '', subType: '00' } },
200+
code: { $code: 'function() {}' },
201+
dbRef: { $ref: 'tests', $id: { $numberInt: '1' }, $db: 'test' },
202+
decimal128: { $numberDecimal: '0E-6176' },
203+
double: { $numberDouble: '10.1' },
204+
int32: { $numberInt: '10' },
205+
long: { $numberLong: '234' },
206+
maxKey: { $maxKey: 1 },
207+
minKey: { $minKey: 1 },
208+
objectID: { $oid: '111111111111111111111111' },
209+
bsonRegExp: { $regularExpression: { pattern: 'hello world', options: 'i' } },
210+
symbol: { $symbol: 'symbol' },
211+
timestamp: { $timestamp: { t: 0, i: 0 } }
212+
};
213+
214+
const result = EJSON.deserialize(doc, { relaxed: false });
215+
216+
// binary
217+
expect(result.binary).to.be.an.instanceOf(BSON.Binary);
218+
// code
219+
expect(result.code).to.be.an.instanceOf(BSON.Code);
220+
expect(result.code.code).to.equal('function() {}');
221+
// dbRef
222+
expect(result.dbRef).to.be.an.instanceOf(BSON.DBRef);
223+
expect(result.dbRef.collection).to.equal('tests');
224+
expect(result.dbRef.db).to.equal('test');
225+
// decimal128
226+
expect(result.decimal128).to.be.an.instanceOf(BSON.Decimal128);
227+
// double
228+
expect(result.double).to.be.an.instanceOf(BSON.Double);
229+
expect(result.double.value).to.equal(10.1);
230+
// int32
231+
expect(result.int32).to.be.an.instanceOf(BSON.Int32);
232+
expect(result.int32.value).to.equal('10');
233+
//long
234+
expect(result.long).to.be.an.instanceOf(BSON.Long);
235+
// maxKey
236+
expect(result.maxKey).to.be.an.instanceOf(BSON.MaxKey);
237+
// minKey
238+
expect(result.minKey).to.be.an.instanceOf(BSON.MinKey);
239+
// objectID
240+
expect(result.objectID.toString()).to.equal('111111111111111111111111');
241+
//bsonRegExp
242+
expect(result.bsonRegExp).to.be.an.instanceOf(BSON.BSONRegExp);
243+
expect(result.bsonRegExp.pattern).to.equal('hello world');
244+
expect(result.bsonRegExp.options).to.equal('i');
245+
// symbol
246+
expect(result.symbol.toString()).to.equal('symbol');
247+
// timestamp
248+
expect(result.timestamp).to.be.an.instanceOf(BSON.Timestamp);
249+
});
250+
});

0 commit comments

Comments
 (0)