Skip to content

Commit 0ab3e05

Browse files
fix
1 parent 43f415b commit 0ab3e05

File tree

1 file changed

+61
-84
lines changed

1 file changed

+61
-84
lines changed

test/unit/api.test.js

Lines changed: 61 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -6,94 +6,39 @@ const { expect } = require('chai');
66
const mongodbDriver = require('mongodb');
77
const mongodbLegacy = require('../..');
88
const { MongoDBNamespace } = require('mongodb/lib/utils');
9-
const { classNameToMethodList, unitTestableAPI } = require('../tools/api');
10-
const { byStrings, sorted, runMicroTask } = require('../tools/utils');
11-
12-
// Dummy data to help with testing
13-
const iLoveJs = 'mongodb://iLoveJavascript';
14-
const client = new mongodbLegacy.MongoClient(iLoveJs);
15-
const db = new mongodbLegacy.Db(client, 'animals');
16-
const collection = new mongodbLegacy.Collection(db, 'pets', {});
17-
const namespace = MongoDBNamespace.fromString('animals.pets');
18-
19-
const state = { client, db, collection, namespace };
20-
client.connect();
21-
22-
function makeInstance({ client, db, namespace, collection }, className) {
23-
const CLASS_FACTORY = new Map([
24-
['Admin', () => new mongodbLegacy.Admin(db)],
25-
['AggregationCursor', () => new mongodbLegacy.AggregationCursor(client, namespace)],
26-
['ChangeStream', () => new mongodbLegacy.ChangeStream(client)],
27-
['ClientSession', () => client.startSession()],
28-
['Collection', () => new mongodbLegacy.Collection(db, 'pets')],
29-
['Db', () => new mongodbLegacy.Db(client, 'animals')],
30-
['FindCursor', () => new mongodbLegacy.FindCursor(client, namespace)],
31-
['GridFSBucket', () => new mongodbLegacy.GridFSBucket(db)],
32-
['GridFSBucketWriteStream', () => new mongodbLegacy.GridFSBucket(db).openUploadStream('file')],
33-
['ListCollectionsCursor', () => new mongodbLegacy.ListCollectionsCursor(db, {})],
34-
['ListIndexesCursor', () => new mongodbLegacy.ListIndexesCursor(collection)],
35-
['MongoClient', () => new mongodbLegacy.MongoClient(iLoveJs)],
36-
['OrderedBulkOperation', () => collection.initializeOrderedBulkOp()],
37-
['UnorderedBulkOperation', () => collection.initializeUnorderedBulkOp()]
38-
]);
39-
40-
const factory =
41-
CLASS_FACTORY.get(className) ??
42-
(() => {
43-
throw new Error('Unsupported classname: ' + className);
44-
});
45-
46-
return factory();
47-
}
48-
49-
function makeStub(className, method, superPromise) {
50-
return sinon.stub(mongodbDriver[className].prototype, method).returns(superPromise);
51-
}
52-
53-
/**
54-
* A generator that yields all programmatically-testable methods from the mongodb driver. We do this in two steps:
55-
* First, we load the exhaustive list of all methods we need to test from api.js. Second, we load the legacy driver
56-
* and use the methods loaded from api.js to find references to each class and method we need to test. The
57-
* generator can then yield the references to the legacy classes, so that we can programmatically test them. The
58-
* generator also yields all possible callback positions for each function.
59-
*/
60-
function* generateTests() {
61-
for (const object of unitTestableAPI) {
62-
const { method, className, possibleCallbackPositions } = object;
63-
64-
const instance = makeInstance(state, className);
65-
66-
yield {
67-
className,
68-
method,
69-
instance,
70-
possibleCallbackPositions
71-
};
72-
}
73-
}
9+
const { unitTestableAPI } = require('../tools/api');
10+
const { runMicroTask } = require('../tools/utils');
7411

7512
describe('wrapper API', () => {
76-
it('all subclassed objects are tested', function () {
77-
// const classesWithGetters = sorted(CLASS_FACTORY.keys(), byStrings);
78-
// const listOfClasses = sorted(classNameToMethodList.keys(), byStrings);
79-
// expect(classesWithGetters).to.deep.equal(listOfClasses);
80-
});
81-
82-
afterEach(() => {
83-
sinon.restore();
84-
});
85-
8613
for (const {
8714
className,
88-
instance,
8915
method,
9016
possibleCallbackPositions,
17+
functionLength,
9118
apiName = `${className}.${method}`
92-
} of generateTests()) {
93-
expect(instance, apiName).to.have.property(method).that.is.a('function');
94-
const functionLength = instance[method].length;
95-
19+
} of unitTestableAPI) {
9620
describe(`${apiName}()`, () => {
21+
let instance, client, db, collection, namespace;
22+
23+
beforeEach(function () {
24+
client = new mongodbLegacy.MongoClient('mongodb://iLoveJavascript');
25+
db = new mongodbLegacy.Db(client, 'animals');
26+
collection = new mongodbLegacy.Collection(db, 'pets', {});
27+
namespace = MongoDBNamespace.fromString('animals.pets');
28+
29+
client.connect().catch(_e => {});
30+
31+
instance = makeInstance(
32+
{
33+
client,
34+
db,
35+
namespace,
36+
collection
37+
},
38+
className
39+
);
40+
});
41+
9742
afterEach(async function () {
9843
if (className === 'ClientSession' && method !== 'endSession') {
9944
await instance.endSession();
@@ -104,6 +49,8 @@ describe('wrapper API', () => {
10449
if (className === 'GridFSBucketWriteStream' && method !== 'end') {
10550
await instance.end();
10651
}
52+
53+
sinon.restore();
10754
});
10855
const resolveSuite = [];
10956
const rejectsSuite = [];
@@ -129,7 +76,7 @@ describe('wrapper API', () => {
12976
let stubbedMethod;
13077
const expectedResult = { message: 'success!' };
13178

132-
before('setup success stub for callback case', function () {
79+
beforeEach('setup success stub for callback case', function () {
13380
superPromise = Promise.resolve(expectedResult);
13481
stubbedMethod = makeStub(className, method, superPromise);
13582
callback = sinon.spy();
@@ -159,7 +106,7 @@ describe('wrapper API', () => {
159106
let actualError;
160107
const expectedError = new Error('error!');
161108

162-
before('setup error stub for callback case', function () {
109+
beforeEach('setup error stub for callback case', function () {
163110
superPromise = Promise.reject(expectedError);
164111
stubbedMethod = makeStub(className, method, superPromise);
165112
callback = sinon.spy();
@@ -194,7 +141,7 @@ describe('wrapper API', () => {
194141
let stubbedMethod;
195142
let expectedResult = { message: 'success!' };
196143

197-
before('setup success stub for promise case', function () {
144+
beforeEach('setup success stub for promise case', function () {
198145
superPromise = Promise.resolve(expectedResult);
199146
stubbedMethod = makeStub(className, method, superPromise);
200147
actualReturnValue = instance[method](...args);
@@ -223,7 +170,7 @@ describe('wrapper API', () => {
223170
let actualError;
224171
const expectedError = new Error('error!');
225172

226-
before('setup error stub for promise case', function () {
173+
beforeEach('setup error stub for promise case', function () {
227174
superPromise = Promise.reject(expectedError);
228175
stubbedMethod = makeStub(className, method, superPromise);
229176
actualReturnValue = instance[method](...args);
@@ -257,3 +204,33 @@ describe('wrapper API', () => {
257204
});
258205
}
259206
});
207+
208+
function makeInstance({ client, db, namespace, collection }, className) {
209+
const CLASS_FACTORY = new Map([
210+
['Admin', () => new mongodbLegacy.Admin(db)],
211+
['AggregationCursor', () => new mongodbLegacy.AggregationCursor(client, namespace)],
212+
['ChangeStream', () => new mongodbLegacy.ChangeStream(client)],
213+
['ClientSession', () => client.startSession()],
214+
['Collection', () => new mongodbLegacy.Collection(db, 'pets')],
215+
['Db', () => new mongodbLegacy.Db(client, 'animals')],
216+
['FindCursor', () => new mongodbLegacy.FindCursor(client, namespace)],
217+
['GridFSBucket', () => new mongodbLegacy.GridFSBucket(db)],
218+
['GridFSBucketWriteStream', () => new mongodbLegacy.GridFSBucket(db).openUploadStream('file')],
219+
['ListCollectionsCursor', () => new mongodbLegacy.ListCollectionsCursor(db, {})],
220+
['ListIndexesCursor', () => new mongodbLegacy.ListIndexesCursor(collection)],
221+
['MongoClient', () => new mongodbLegacy.MongoClient('mongodb://iLoveJavascript')],
222+
['OrderedBulkOperation', () => collection.initializeOrderedBulkOp()],
223+
['UnorderedBulkOperation', () => collection.initializeUnorderedBulkOp()]
224+
]);
225+
226+
const _default = () => {
227+
throw new Error('Unsupported classname: ' + className);
228+
};
229+
const factory = CLASS_FACTORY.get(className) ?? _default;
230+
231+
return factory();
232+
}
233+
234+
function makeStub(className, method, superPromise) {
235+
return sinon.stub(mongodbDriver[className].prototype, method).returns(superPromise);
236+
}

0 commit comments

Comments
 (0)