1
- import * as SPYABLE_BSON from 'bson' ;
2
1
import { expect } from 'chai' ;
3
2
import * as sinon from 'sinon' ;
4
3
4
+ // to spy on the bson module, we must import it from the driver
5
+ // eslint-disable-next-line @typescript-eslint/no-restricted-imports
6
+ import * as mdb from '../../../../src/bson' ;
5
7
import {
6
- BSON ,
7
8
CursorResponse ,
8
9
Int32 ,
9
10
MongoDBResponse ,
10
11
MongoUnexpectedServerResponseError ,
11
- OnDemandDocument
12
+ OnDemandDocument ,
13
+ serialize
12
14
} from '../../../mongodb' ;
13
15
14
16
describe ( 'class MongoDBResponse' , ( ) => {
15
17
it ( 'is a subclass of OnDemandDocument' , ( ) => {
16
- expect ( new MongoDBResponse ( BSON . serialize ( { ok : 1 } ) ) ) . to . be . instanceOf ( OnDemandDocument ) ;
18
+ expect ( new MongoDBResponse ( serialize ( { ok : 1 } ) ) ) . to . be . instanceOf ( OnDemandDocument ) ;
17
19
} ) ;
18
20
19
21
context ( 'utf8 validation' , ( ) => {
20
- let deseriailzeSpy : sinon . SinonSpy ;
22
+ let deseriailzeSpy : sinon . SinonStub < Parameters < typeof mdb . deserialize > > ;
21
23
beforeEach ( function ( ) {
22
- // @ts -expect-error accessing internal property.
23
- OnDemandDocument . BSON = SPYABLE_BSON ;
24
-
25
- deseriailzeSpy = sinon . spy ( SPYABLE_BSON , 'deserialize' ) ;
24
+ const deserialize = mdb . deserialize ;
25
+ deseriailzeSpy = sinon . stub < Parameters < typeof deserialize > > ( ) . callsFake ( deserialize ) ;
26
+ sinon . stub ( mdb , 'deserialize' ) . get ( ( ) => {
27
+ return deseriailzeSpy ;
28
+ } ) ;
26
29
} ) ;
27
30
afterEach ( function ( ) {
28
31
sinon . restore ( ) ;
@@ -31,7 +34,7 @@ describe('class MongoDBResponse', () => {
31
34
context ( 'when enableUtf8Validation is not specified' , ( ) => {
32
35
const options = { enableUtf8Validation : undefined } ;
33
36
it ( 'calls BSON deserialize with writeErrors validation turned off' , ( ) => {
34
- const res = new MongoDBResponse ( BSON . serialize ( { } ) ) ;
37
+ const res = new MongoDBResponse ( serialize ( { } ) ) ;
35
38
res . toObject ( options ) ;
36
39
37
40
expect ( deseriailzeSpy ) . to . have . been . called ;
@@ -49,7 +52,7 @@ describe('class MongoDBResponse', () => {
49
52
context ( 'when enableUtf8Validation is true' , ( ) => {
50
53
const options = { enableUtf8Validation : true } ;
51
54
it ( 'calls BSON deserialize with writeErrors validation turned off' , ( ) => {
52
- const res = new MongoDBResponse ( BSON . serialize ( { } ) ) ;
55
+ const res = new MongoDBResponse ( serialize ( { } ) ) ;
53
56
res . toObject ( options ) ;
54
57
55
58
expect ( deseriailzeSpy ) . to . have . been . called ;
@@ -67,7 +70,7 @@ describe('class MongoDBResponse', () => {
67
70
context ( 'when enableUtf8Validation is false' , ( ) => {
68
71
const options = { enableUtf8Validation : false } ;
69
72
it ( 'calls BSON deserialize with all validation disabled' , ( ) => {
70
- const res = new MongoDBResponse ( BSON . serialize ( { } ) ) ;
73
+ const res = new MongoDBResponse ( serialize ( { } ) ) ;
71
74
res . toObject ( options ) ;
72
75
73
76
expect ( deseriailzeSpy ) . to . have . been . called ;
@@ -87,7 +90,7 @@ describe('class MongoDBResponse', () => {
87
90
describe ( 'class CursorResponse' , ( ) => {
88
91
describe ( 'get cursor()' , ( ) => {
89
92
it ( 'throws if input does not contain cursor embedded document' , ( ) => {
90
- expect ( ( ) => new CursorResponse ( BSON . serialize ( { ok : 1 } ) ) . cursor ) . to . throw (
93
+ expect ( ( ) => new CursorResponse ( serialize ( { ok : 1 } ) ) . cursor ) . to . throw (
91
94
MongoUnexpectedServerResponseError ,
92
95
/ " c u r s o r " i s m i s s i n g /
93
96
) ;
@@ -96,7 +99,7 @@ describe('class CursorResponse', () => {
96
99
97
100
describe ( 'get id()' , ( ) => {
98
101
it ( 'throws if input does not contain cursor.id int64' , ( ) => {
99
- expect ( ( ) => new CursorResponse ( BSON . serialize ( { ok : 1 , cursor : { } } ) ) . id ) . to . throw (
102
+ expect ( ( ) => new CursorResponse ( serialize ( { ok : 1 , cursor : { } } ) ) . id ) . to . throw (
100
103
MongoUnexpectedServerResponseError ,
101
104
/ " i d " i s m i s s i n g /
102
105
) ;
@@ -107,22 +110,22 @@ describe('class CursorResponse', () => {
107
110
it ( 'throws if input does not contain firstBatch nor nextBatch' , ( ) => {
108
111
expect (
109
112
// @ts -expect-error: testing private getter
110
- ( ) => new CursorResponse ( BSON . serialize ( { ok : 1 , cursor : { id : 0n , batch : [ ] } } ) ) . batch
113
+ ( ) => new CursorResponse ( serialize ( { ok : 1 , cursor : { id : 0n , batch : [ ] } } ) ) . batch
111
114
) . to . throw ( MongoUnexpectedServerResponseError , / d i d n o t c o n t a i n a b a t c h / ) ;
112
115
} ) ;
113
116
} ) ;
114
117
115
118
describe ( 'get ns()' , ( ) => {
116
119
it ( 'sets namespace to null if input does not contain cursor.ns' , ( ) => {
117
- expect ( new CursorResponse ( BSON . serialize ( { ok : 1 , cursor : { id : 0n , firstBatch : [ ] } } ) ) . ns )
118
- . to . be . null ;
120
+ expect ( new CursorResponse ( serialize ( { ok : 1 , cursor : { id : 0n , firstBatch : [ ] } } ) ) . ns ) . to . be
121
+ . null ;
119
122
} ) ;
120
123
} ) ;
121
124
122
125
describe ( 'get batchSize()' , ( ) => {
123
126
it ( 'reports the returned batch size' , ( ) => {
124
127
const response = new CursorResponse (
125
- BSON . serialize ( { ok : 1 , cursor : { id : 0n , nextBatch : [ { } , { } , { } ] } } )
128
+ serialize ( { ok : 1 , cursor : { id : 0n , nextBatch : [ { } , { } , { } ] } } )
126
129
) ;
127
130
expect ( response . batchSize ) . to . equal ( 3 ) ;
128
131
expect ( response . shift ( ) ) . to . deep . equal ( { } ) ;
@@ -133,7 +136,7 @@ describe('class CursorResponse', () => {
133
136
describe ( 'get length()' , ( ) => {
134
137
it ( 'reports number of documents remaining in the batch' , ( ) => {
135
138
const response = new CursorResponse (
136
- BSON . serialize ( { ok : 1 , cursor : { id : 0n , nextBatch : [ { } , { } , { } ] } } )
139
+ serialize ( { ok : 1 , cursor : { id : 0n , nextBatch : [ { } , { } , { } ] } } )
137
140
) ;
138
141
expect ( response ) . to . have . lengthOf ( 3 ) ;
139
142
expect ( response . shift ( ) ) . to . deep . equal ( { } ) ;
@@ -146,7 +149,7 @@ describe('class CursorResponse', () => {
146
149
147
150
beforeEach ( async function ( ) {
148
151
response = new CursorResponse (
149
- BSON . serialize ( {
152
+ serialize ( {
150
153
ok : 1 ,
151
154
cursor : { id : 0n , nextBatch : [ { _id : 1 } , { _id : 2 } , { _id : 3 } ] }
152
155
} )
@@ -173,7 +176,7 @@ describe('class CursorResponse', () => {
173
176
174
177
beforeEach ( async function ( ) {
175
178
response = new CursorResponse (
176
- BSON . serialize ( {
179
+ serialize ( {
177
180
ok : 1 ,
178
181
cursor : { id : 0n , nextBatch : [ { _id : 1 } , { _id : 2 } , { _id : 3 } ] }
179
182
} )
0 commit comments