Skip to content

Commit a12eb32

Browse files
committed
chore(mocha): convert all tests to use mocha and chai
1 parent 2e573a7 commit a12eb32

11 files changed

+5268
-5159
lines changed

test/node/bson_compliance_test.js

+95-112
Original file line numberDiff line numberDiff line change
@@ -8,130 +8,113 @@ var BSON = require('../..'),
88
ObjectID = BSON.ObjectID,
99
DBRef = BSON.DBRef,
1010
MinKey = BSON.MinKey,
11-
MaxKey = BSON.MaxKey;
11+
MaxKey = BSON.MaxKey,
12+
expect = require('chai').expect;
1213

1314
var createBSON = require('../utils');
1415

15-
/**
16-
* Retrieve the server information for the current
17-
* instance of the db client
18-
*
19-
* @ignore
20-
*/
21-
exports.setUp = function(callback) {
22-
callback();
23-
};
16+
describe('BSON Compliance', function() {
17+
/**
18+
* @ignore
19+
*/
20+
it('Pass all corrupt BSON scenarios ./compliance/corrupt.json', function(done) {
21+
// Read and parse the json file
22+
var scenarios = require(__dirname + '/compliance/corrupt');
2423

25-
/**
26-
* Retrieve the server information for the current
27-
* instance of the db client
28-
*
29-
* @ignore
30-
*/
31-
exports.tearDown = function(callback) {
32-
callback();
33-
};
24+
// Create a new BSON instance
25+
var bson = createBSON();
3426

35-
/**
36-
* @ignore
37-
*/
38-
exports['Pass all corrupt BSON scenarios ./compliance/corrupt.json'] = function(test) {
39-
// Read and parse the json file
40-
var scenarios = require(__dirname + '/compliance/corrupt');
27+
for (var i = 0; i < scenarios.documents.length; i++) {
28+
var doc = scenarios.documents[i];
29+
if (doc.skip) continue;
4130

42-
// Create a new BSON instance
43-
var bson = createBSON();
44-
45-
for (var i = 0; i < scenarios.documents.length; i++) {
46-
var doc = scenarios.documents[i];
47-
if (doc.skip) continue;
48-
49-
try {
50-
// Create a buffer containing the payload
51-
var buffer = new Buffer(doc.encoded, 'hex');
52-
// Attempt to deserialize
53-
bson.deserialize(buffer);
54-
test.ok(false);
55-
} catch (err) {
56-
test.ok(true);
31+
try {
32+
// Create a buffer containing the payload
33+
var buffer = new Buffer(doc.encoded, 'hex');
34+
// Attempt to deserialize
35+
bson.deserialize(buffer);
36+
expect(false).to.be.ok;
37+
} catch (err) {
38+
expect(true).to.be.ok;
39+
}
5740
}
58-
}
5941

60-
test.done();
61-
};
42+
done();
43+
});
6244

63-
/**
64-
* @ignore
65-
*/
66-
exports['Pass all valid BSON serialization scenarios ./compliance/valid.json'] = function(test) {
67-
// Read and parse the json file
68-
var scenarios = require(__dirname + '/compliance/valid');
45+
/**
46+
* @ignore
47+
*/
48+
it('Pass all valid BSON serialization scenarios ./compliance/valid.json', function(done) {
49+
// Read and parse the json file
50+
var scenarios = require(__dirname + '/compliance/valid');
6951

70-
// Create a new BSON instance
71-
var bson = createBSON();
52+
// Create a new BSON instance
53+
var bson = createBSON();
7254

73-
// Translate extended json to correctly typed doc
74-
var translate = function(doc, object) {
75-
for (var name in doc) {
76-
if (
77-
typeof doc[name] === 'number' ||
78-
typeof doc[name] === 'string' ||
79-
typeof doc[name] === 'boolean'
80-
) {
81-
object[name] = doc[name];
82-
} else if (Array.isArray(doc[name])) {
83-
object[name] = translate(doc[name], []);
84-
} else if (doc[name]['$numberLong']) {
85-
object[name] = Long.fromString(doc[name]['$numberLong']);
86-
} else if (doc[name]['$undefined']) {
87-
object[name] = null;
88-
} else if (doc[name]['$date']) {
89-
var date = new Date();
90-
date.setTime(parseInt(doc[name]['$date']['$numberLong'], 10));
91-
object[name] = date;
92-
} else if (doc[name]['$regexp']) {
93-
object[name] = new RegExp(doc[name]['$regexp'], doc[name]['$options'] || '');
94-
} else if (doc[name]['$oid']) {
95-
object[name] = new ObjectID(doc[name]['$oid']);
96-
} else if (doc[name]['$binary']) {
97-
object[name] = new Binary(doc[name]['$binary'], doc[name]['$type'] || 1);
98-
} else if (doc[name]['$timestamp']) {
99-
object[name] = Timestamp.fromBits(
100-
parseInt(doc[name]['$timestamp']['t'], 10),
101-
parseInt(doc[name]['$timestamp']['i'])
102-
);
103-
} else if (doc[name]['$ref']) {
104-
object[name] = new DBRef(doc[name]['$ref'], doc[name]['$id'], doc[name]['$db']);
105-
} else if (doc[name]['$minKey']) {
106-
object[name] = new MinKey();
107-
} else if (doc[name]['$maxKey']) {
108-
object[name] = new MaxKey();
109-
} else if (doc[name]['$code']) {
110-
object[name] = new Code(doc[name]['$code'], doc[name]['$scope'] || {});
111-
} else if (doc[name] != null && typeof doc[name] === 'object') {
112-
object[name] = translate(doc[name], {});
55+
// Translate extended json to correctly typed doc
56+
var translate = function(doc, object) {
57+
for (var name in doc) {
58+
if (
59+
typeof doc[name] === 'number' ||
60+
typeof doc[name] === 'string' ||
61+
typeof doc[name] === 'boolean'
62+
) {
63+
object[name] = doc[name];
64+
} else if (Array.isArray(doc[name])) {
65+
object[name] = translate(doc[name], []);
66+
} else if (doc[name]['$numberLong']) {
67+
object[name] = Long.fromString(doc[name]['$numberLong']);
68+
} else if (doc[name]['$undefined']) {
69+
object[name] = null;
70+
} else if (doc[name]['$date']) {
71+
var date = new Date();
72+
date.setTime(parseInt(doc[name]['$date']['$numberLong'], 10));
73+
object[name] = date;
74+
} else if (doc[name]['$regexp']) {
75+
object[name] = new RegExp(doc[name]['$regexp'], doc[name]['$options'] || '');
76+
} else if (doc[name]['$oid']) {
77+
object[name] = new ObjectID(doc[name]['$oid']);
78+
} else if (doc[name]['$binary']) {
79+
object[name] = new Binary(doc[name]['$binary'], doc[name]['$type'] || 1);
80+
} else if (doc[name]['$timestamp']) {
81+
object[name] = Timestamp.fromBits(
82+
parseInt(doc[name]['$timestamp']['t'], 10),
83+
parseInt(doc[name]['$timestamp']['i'])
84+
);
85+
} else if (doc[name]['$ref']) {
86+
object[name] = new DBRef(doc[name]['$ref'], doc[name]['$id'], doc[name]['$db']);
87+
} else if (doc[name]['$minKey']) {
88+
object[name] = new MinKey();
89+
} else if (doc[name]['$maxKey']) {
90+
object[name] = new MaxKey();
91+
} else if (doc[name]['$code']) {
92+
object[name] = new Code(doc[name]['$code'], doc[name]['$scope'] || {});
93+
} else if (doc[name] != null && typeof doc[name] === 'object') {
94+
object[name] = translate(doc[name], {});
95+
}
11396
}
114-
}
11597

116-
return object;
117-
};
98+
return object;
99+
};
118100

119-
// Iterate over all the results
120-
scenarios.documents.forEach(function(doc) {
121-
if (doc.skip) return;
122-
// Create a buffer containing the payload
123-
var expectedData = new Buffer(doc.encoded, 'hex');
124-
// Get the expectedDocument
125-
var expectedDocument = translate(doc.document, {});
126-
// Serialize to buffer
127-
var buffer = bson.serialize(expectedDocument);
128-
// Validate the output
129-
test.equal(expectedData.toString('hex'), buffer.toString('hex'));
130-
// Attempt to deserialize
131-
var object = bson.deserialize(buffer, { promoteLongs: false });
132-
// // Validate the object
133-
test.deepEqual(JSON.stringify(expectedDocument), JSON.stringify(object));
134-
});
101+
// Iterate over all the results
102+
scenarios.documents.forEach(function(doc) {
103+
if (doc.skip) return;
104+
// Create a buffer containing the payload
105+
var expectedData = new Buffer(doc.encoded, 'hex');
106+
// Get the expectedDocument
107+
var expectedDocument = translate(doc.document, {});
108+
// Serialize to buffer
109+
var buffer = bson.serialize(expectedDocument);
110+
// Validate the output
111+
expect(expectedData.toString('hex')).to.equal(buffer.toString('hex'));
112+
// Attempt to deserialize
113+
var object = bson.deserialize(buffer, { promoteLongs: false });
114+
// // Validate the object
115+
expect(JSON.stringify(expectedDocument)).to.deep.equal(JSON.stringify(object));
116+
});
135117

136-
test.done();
137-
};
118+
done();
119+
});
120+
});

test/node/bson_corpus_tests.js

+58-80
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
var BSON = require('../..'),
44
Decimal128 = BSON.Decimal128,
5-
f = require('util').format,
65
fs = require('fs'),
76
expect = require('chai').expect,
87
path = require('path'),
@@ -26,88 +25,67 @@ var skip = {
2625
'passing this would require building a custom type to store the NaN payload data.'
2726
};
2827

29-
function testScenario(scenario) {
30-
if (scenario.valid) {
31-
console.log(' * Starting valid scenario tests');
32-
33-
scenario.valid.forEach(v => {
34-
if (skip.hasOwnProperty(v.description)) return;
35-
36-
console.log(
37-
f(
38-
' - valid scenario [%s] with \n bson: [%s] \n ext-json: [%s]',
39-
v.description,
40-
v.canonical_bson,
41-
v.canonical_extjson
42-
)
43-
);
44-
45-
var cB = new Buffer(v.canonical_bson, 'hex');
46-
if (v.degenerate_bson) var dB = new Buffer(v.degenerate_bson, 'hex');
47-
if (v.converted_bson) var convB = new Buffer(v.converted_bson, 'hex');
48-
49-
var roundTripped = bson.serialize(bson.deserialize(cB, deserializeOptions), serializeOptions);
50-
51-
if (scenario.deprecated) expect(convB).to.deep.equal(roundTripped);
52-
else expect(cB).to.deep.equal(roundTripped);
53-
54-
if (dB)
55-
expect(cB).to.deep.equal(
56-
bson.serialize(bson.deserialize(dB, deserializeOptions), serializeOptions)
57-
);
58-
});
59-
}
60-
61-
if (scenario.decodeErrors) {
62-
console.log(' * Starting decode error scenario tests');
63-
scenario.decodeErrors.forEach(d => {
64-
console.log(f(' - decode error [%s] with \n bson: [%s]', d.description, d.bson));
65-
var B = new Buffer(d.bson, 'hex');
66-
expect(() => bson.deserialize(B, deserializeOptions)).to.throw();
67-
});
68-
}
69-
70-
if (scenario.parseErrors) {
71-
console.log(' * Starting parse error scenario tests');
72-
scenario.parseErrors.forEach(scenario => {
73-
console.log(
74-
f(' - parse error [%s] with \n string: [%s]', scenario.description, scenario.string)
75-
);
76-
expect(() => Decimal128.fromString(scenario.string)).to.throw();
77-
});
78-
}
79-
}
80-
81-
function printScenarioInformation(scenario) {
82-
console.log(
83-
f(
84-
'= Starting %s for bson_type %s with test key %s',
85-
scenario[1].description,
86-
scenario[1].bson_type,
87-
scenario[1].test_key
88-
)
89-
);
90-
}
91-
9228
function findScenarios() {
9329
return fs
9430
.readdirSync(path.join(__dirname, 'specs/bson-corpus'))
95-
.filter(x => {
96-
return x.indexOf('json') !== -1;
97-
})
98-
.map(x => {
99-
return [x, fs.readFileSync(path.join(__dirname, 'specs/bson-corpus', x), 'utf8')];
100-
})
101-
.map(x => {
102-
return [path.basename(x[0], '.json'), JSON.parse(x[1])];
103-
});
31+
.filter(x => x.indexOf('json') !== -1)
32+
.map(x => JSON.parse(fs.readFileSync(path.join(__dirname, 'specs/bson-corpus', x), 'utf8')));
10433
}
10534

106-
findScenarios().forEach(scenario => {
107-
var exportName = f('Pass all BSON corpus ./specs/bson-corpus/%s.json', scenario[0]);
108-
exports[exportName] = function(test) {
109-
printScenarioInformation(scenario);
110-
testScenario(scenario[1]);
111-
test.done();
112-
};
35+
describe('BSON Corpus', function() {
36+
findScenarios().forEach(scenario => {
37+
describe(scenario.description, function() {
38+
if (scenario.valid) {
39+
describe('valid', function() {
40+
scenario.valid.forEach(v => {
41+
if (skip.hasOwnProperty(v.description)) {
42+
it.skip(v.description, () => {});
43+
return;
44+
}
45+
46+
it(v.description, function() {
47+
var cB = new Buffer(v.canonical_bson, 'hex');
48+
if (v.degenerate_bson) var dB = new Buffer(v.degenerate_bson, 'hex');
49+
if (v.converted_bson) var convB = new Buffer(v.converted_bson, 'hex');
50+
51+
var roundTripped = bson.serialize(
52+
bson.deserialize(cB, deserializeOptions),
53+
serializeOptions
54+
);
55+
56+
if (scenario.deprecated) expect(convB).to.deep.equal(roundTripped);
57+
else expect(cB).to.deep.equal(roundTripped);
58+
59+
if (dB) {
60+
expect(cB).to.deep.equal(
61+
bson.serialize(bson.deserialize(dB, deserializeOptions), serializeOptions)
62+
);
63+
}
64+
});
65+
});
66+
});
67+
}
68+
69+
if (scenario.decodeErrors) {
70+
describe('decodeErrors', function() {
71+
scenario.decodeErrors.forEach(d => {
72+
it(d.description, function() {
73+
var B = new Buffer(d.bson, 'hex');
74+
expect(() => bson.deserialize(B, deserializeOptions)).to.throw();
75+
});
76+
});
77+
});
78+
}
79+
80+
if (scenario.parseErrors) {
81+
describe('parseErrors', function() {
82+
scenario.parseErrors.forEach(p => {
83+
it(p.description, function() {
84+
expect(() => Decimal128.fromString(scenario.string)).to.throw();
85+
});
86+
});
87+
});
88+
}
89+
});
90+
});
11391
});

0 commit comments

Comments
 (0)