@@ -6,94 +6,39 @@ const { expect } = require('chai');
6
6
const mongodbDriver = require ( 'mongodb' ) ;
7
7
const mongodbLegacy = require ( '../..' ) ;
8
8
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' ) ;
74
11
75
12
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
-
86
13
for ( const {
87
14
className,
88
- instance,
89
15
method,
90
16
possibleCallbackPositions,
17
+ functionLength,
91
18
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 ) {
96
20
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
+
97
42
afterEach ( async function ( ) {
98
43
if ( className === 'ClientSession' && method !== 'endSession' ) {
99
44
await instance . endSession ( ) ;
@@ -104,6 +49,8 @@ describe('wrapper API', () => {
104
49
if ( className === 'GridFSBucketWriteStream' && method !== 'end' ) {
105
50
await instance . end ( ) ;
106
51
}
52
+
53
+ sinon . restore ( ) ;
107
54
} ) ;
108
55
const resolveSuite = [ ] ;
109
56
const rejectsSuite = [ ] ;
@@ -129,7 +76,7 @@ describe('wrapper API', () => {
129
76
let stubbedMethod ;
130
77
const expectedResult = { message : 'success!' } ;
131
78
132
- before ( 'setup success stub for callback case' , function ( ) {
79
+ beforeEach ( 'setup success stub for callback case' , function ( ) {
133
80
superPromise = Promise . resolve ( expectedResult ) ;
134
81
stubbedMethod = makeStub ( className , method , superPromise ) ;
135
82
callback = sinon . spy ( ) ;
@@ -159,7 +106,7 @@ describe('wrapper API', () => {
159
106
let actualError ;
160
107
const expectedError = new Error ( 'error!' ) ;
161
108
162
- before ( 'setup error stub for callback case' , function ( ) {
109
+ beforeEach ( 'setup error stub for callback case' , function ( ) {
163
110
superPromise = Promise . reject ( expectedError ) ;
164
111
stubbedMethod = makeStub ( className , method , superPromise ) ;
165
112
callback = sinon . spy ( ) ;
@@ -194,7 +141,7 @@ describe('wrapper API', () => {
194
141
let stubbedMethod ;
195
142
let expectedResult = { message : 'success!' } ;
196
143
197
- before ( 'setup success stub for promise case' , function ( ) {
144
+ beforeEach ( 'setup success stub for promise case' , function ( ) {
198
145
superPromise = Promise . resolve ( expectedResult ) ;
199
146
stubbedMethod = makeStub ( className , method , superPromise ) ;
200
147
actualReturnValue = instance [ method ] ( ...args ) ;
@@ -223,7 +170,7 @@ describe('wrapper API', () => {
223
170
let actualError ;
224
171
const expectedError = new Error ( 'error!' ) ;
225
172
226
- before ( 'setup error stub for promise case' , function ( ) {
173
+ beforeEach ( 'setup error stub for promise case' , function ( ) {
227
174
superPromise = Promise . reject ( expectedError ) ;
228
175
stubbedMethod = makeStub ( className , method , superPromise ) ;
229
176
actualReturnValue = instance [ method ] ( ...args ) ;
@@ -257,3 +204,33 @@ describe('wrapper API', () => {
257
204
} ) ;
258
205
}
259
206
} ) ;
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