@@ -9,7 +9,7 @@ const { toHex, fromHex } = BSON.onDemand.ByteUtils;
9
9
type VectorHexType = '0x03' | '0x27' | '0x10' ;
10
10
type VectorTest = {
11
11
description : string ;
12
- vector ?: ( number | string ) [ ] ;
12
+ vector ?: number [ ] ;
13
13
valid : boolean ;
14
14
dtype_hex : VectorHexType ;
15
15
padding ?: number ;
@@ -46,7 +46,22 @@ function fixBits(f: number | string): number {
46
46
return f ;
47
47
}
48
48
49
- function make ( vector : ( number | string ) [ ] , dtype_hex : VectorHexType , padding ?: number ) : Binary {
49
+ function dtypeToHelper ( dtype_hex : string ) {
50
+ switch ( dtype_hex ) {
51
+ case '0x10' /* packed_bit */ :
52
+ return 'fromPackedBits' ;
53
+ case '0x03' /* int8 */ :
54
+ return 'fromInt8Array' ;
55
+ break ;
56
+ case '0x27' /* float32 */ :
57
+ return 'fromFloat32Array' ;
58
+ break ;
59
+ default :
60
+ throw new Error ( `Unknown dtype_hex: ${ dtype_hex } ` ) ;
61
+ }
62
+ }
63
+
64
+ function make ( vector : number [ ] , dtype_hex : VectorHexType , padding ?: number ) : Binary {
50
65
let binary : Binary ;
51
66
switch ( dtype_hex ) {
52
67
case '0x10' /* packed_bit */ :
@@ -88,17 +103,18 @@ const invalidTestExpectedError = new Map()
88
103
'Insufficient vector data FLOAT32' ,
89
104
'Invalid Vector: Float32 vector must contain a multiple of 4 bytes'
90
105
)
91
- // skipped
92
- . set ( 'Overflow Vector PACKED_BIT' , false )
93
- . set ( 'Underflow Vector PACKED_BIT' , false )
94
- . set ( 'Overflow Vector INT8' , false )
95
- . set ( 'Underflow Vector INT8' , false )
96
- . set ( 'INT8 with float inputs' , false )
97
- . set ( 'Vector with float values PACKED_BIT' , false ) ;
98
-
99
- function testVectorBuilding ( test : VectorTest , expectedErrorMessage : string ) {
100
- describe ( 'creating an instance of a Binary class using parameters' , ( ) => {
101
- it ( `bson: ${ test . description } ` , function ( ) {
106
+ // These are not possible given the constraints of the input types allowed:
107
+ // our helpers will throw an "unsupported_error" for these
108
+ . set ( 'Overflow Vector PACKED_BIT' , 'unsupported_error' )
109
+ . set ( 'Underflow Vector PACKED_BIT' , 'unsupported_error' )
110
+ . set ( 'Overflow Vector INT8' , 'unsupported_error' )
111
+ . set ( 'Underflow Vector INT8' , 'unsupported_error' )
112
+ . set ( 'INT8 with float inputs' , 'unsupported_error' )
113
+ . set ( 'Vector with float values PACKED_BIT' , 'unsupported_error' ) ;
114
+
115
+ function testVectorInvalidInputValues ( test : VectorTest , expectedErrorMessage : string ) {
116
+ describe ( 'when creating a BSON Vector given invalid input values' , ( ) => {
117
+ it ( `either BSON.serialize() or Binary.${ dtypeToHelper ( test . dtype_hex ) } () throws a BSONError` , function ( ) {
102
118
let thrownError : Error | undefined ;
103
119
try {
104
120
const bin = make ( test . vector ! , test . dtype_hex , test . padding ) ;
@@ -111,15 +127,14 @@ function testVectorBuilding(test: VectorTest, expectedErrorMessage: string) {
111
127
expect (
112
128
expectedErrorMessage ,
113
129
'We expect a certain error message but got an unsupported error'
114
- ) . to . be . false ;
115
- this . skip ( ) ;
130
+ ) . to . equal ( 'unsupported_error' ) ;
131
+ } else {
132
+ expect ( thrownError , thrownError ?. stack ) . to . be . instanceOf ( BSONError ) ;
133
+ expect ( thrownError ?. message ) . to . match ( new RegExp ( expectedErrorMessage ) ) ;
116
134
}
117
-
118
- expect ( thrownError , thrownError ?. stack ) . to . be . instanceOf ( BSONError ) ;
119
- expect ( thrownError ?. message ) . to . match ( new RegExp ( expectedErrorMessage ) ) ;
120
135
} ) ;
121
136
122
- it ( `ejson: ${ test . description } ` , function ( ) {
137
+ it ( `either EJSON.stringify() or Binary. ${ dtypeToHelper ( test . dtype_hex ) } () throws a BSONError ` , function ( ) {
123
138
let thrownError : Error | undefined ;
124
139
try {
125
140
const bin = make ( test . vector ! , test . dtype_hex , test . padding ) ;
@@ -132,19 +147,18 @@ function testVectorBuilding(test: VectorTest, expectedErrorMessage: string) {
132
147
expect (
133
148
expectedErrorMessage ,
134
149
'We expect a certain error message but got an unsupported error'
135
- ) . to . be . false ;
136
- this . skip ( ) ;
150
+ ) . to . equal ( 'unsupported_error' ) ;
151
+ } else {
152
+ expect ( thrownError , thrownError ?. stack ) . to . be . instanceOf ( BSONError ) ;
153
+ expect ( thrownError ?. message ) . to . match ( new RegExp ( expectedErrorMessage ) ) ;
137
154
}
138
-
139
- expect ( thrownError , thrownError ?. stack ) . to . be . instanceOf ( BSONError ) ;
140
- expect ( thrownError ?. message ) . to . match ( new RegExp ( expectedErrorMessage ) ) ;
141
155
} ) ;
142
156
} ) ;
143
157
}
144
158
145
- function testVectorReserializing ( test : VectorTest , expectedErrorMessage : string ) {
146
- describe ( 'creating an instance of a Binary class using canonical_bson ' , ( ) => {
147
- it ( `bson deserialize: ${ test . description } ` , function ( ) {
159
+ function testVectorInvalidBSONBytes ( test : VectorTest , expectedErrorMessage : string ) {
160
+ describe ( 'when creating a Binary Vector instance from invalid bytes ' , ( ) => {
161
+ it ( `BSON.serialize() throw a BSONError ` , function ( ) {
148
162
let thrownError : Error | undefined ;
149
163
const bin = BSON . deserialize ( Buffer . from ( test . canonical_bson ! , 'hex' ) ) ;
150
164
@@ -158,7 +172,7 @@ function testVectorReserializing(test: VectorTest, expectedErrorMessage: string)
158
172
expect ( thrownError ?. message ) . to . match ( new RegExp ( expectedErrorMessage ) ) ;
159
173
} ) ;
160
174
161
- it ( `ejson stringify: ${ test . description } ` , function ( ) {
175
+ it ( `EJSON. stringify() throw a BSONError ` , function ( ) {
162
176
let thrownError : Error | undefined ;
163
177
const bin = BSON . deserialize ( Buffer . from ( test . canonical_bson ! , 'hex' ) ) ;
164
178
@@ -174,7 +188,7 @@ function testVectorReserializing(test: VectorTest, expectedErrorMessage: string)
174
188
} ) ;
175
189
}
176
190
177
- describe ( 'BSON Binary Vector spec tests' , ( ) => {
191
+ describe . only ( 'BSON Binary Vector spec tests' , ( ) => {
178
192
const tests : Record < string , VectorSuite > = Object . create ( null ) ;
179
193
180
194
for ( const file of fs . readdirSync ( path . join ( __dirname , 'specs/bson-binary-vector' ) ) ) {
@@ -197,20 +211,22 @@ describe('BSON Binary Vector spec tests', () => {
197
211
* > MUST assert that the input float array is the same after encoding and decoding.
198
212
*/
199
213
for ( const test of valid ) {
200
- it ( `encode ${ test . description } ` , function ( ) {
201
- const bin = make ( test . vector ! , test . dtype_hex , test . padding ) ;
214
+ describe ( test . description , ( ) => {
215
+ it ( `calling Binary.${ dtypeToHelper ( test . dtype_hex ) } () with input numbers and serializing it does not throw` , function ( ) {
216
+ const bin = make ( test . vector ! , test . dtype_hex , test . padding ) ;
202
217
203
- const buffer = BSON . serialize ( { [ suite . test_key ] : bin } ) ;
204
- expect ( toHex ( buffer ) ) . to . equal ( test . canonical_bson ! . toLowerCase ( ) ) ;
205
- } ) ;
218
+ const buffer = BSON . serialize ( { [ suite . test_key ] : bin } ) ;
219
+ expect ( toHex ( buffer ) ) . to . equal ( test . canonical_bson ! . toLowerCase ( ) ) ;
220
+ } ) ;
206
221
207
- it ( `decode ${ test . description } ` , function ( ) {
208
- const canonical_bson = fromHex ( test . canonical_bson ! . toLowerCase ( ) ) ;
209
- const doc = BSON . deserialize ( canonical_bson ) ;
222
+ it ( `creating a Binary instance from BSON bytes does not throw ` , function ( ) {
223
+ const canonical_bson = fromHex ( test . canonical_bson ! . toLowerCase ( ) ) ;
224
+ const doc = BSON . deserialize ( canonical_bson ) ;
210
225
211
- expect ( doc [ suite . test_key ] . sub_type ) . to . equal ( 0x09 ) ;
212
- expect ( doc [ suite . test_key ] . buffer [ 0 ] ) . to . equal ( + test . dtype_hex ) ;
213
- expect ( doc [ suite . test_key ] . buffer [ 1 ] ) . to . equal ( test . padding ) ;
226
+ expect ( doc [ suite . test_key ] . sub_type ) . to . equal ( 0x09 ) ;
227
+ expect ( doc [ suite . test_key ] . buffer [ 0 ] ) . to . equal ( + test . dtype_hex ) ;
228
+ expect ( doc [ suite . test_key ] . buffer [ 1 ] ) . to . equal ( test . padding ) ;
229
+ } ) ;
214
230
} ) ;
215
231
}
216
232
} ) ;
@@ -224,22 +240,19 @@ describe('BSON Binary Vector spec tests', () => {
224
240
for ( const test of invalid ) {
225
241
const expectedErrorMessage = invalidTestExpectedError . get ( test . description ) ;
226
242
227
- if ( test . vector != null && test . canonical_bson != null ) {
228
- describe ( 'both manual vector building and re-serializing' , ( ) => {
229
- testVectorBuilding ( test , expectedErrorMessage ) ;
230
- testVectorReserializing ( test , expectedErrorMessage ) ;
231
- } ) ;
232
- } else if ( test . canonical_bson != null ) {
233
- describe ( 'vector re-serializing' , ( ) => {
234
- testVectorReserializing ( test , expectedErrorMessage ) ;
235
- } ) ;
236
- } else if ( test . vector != null ) {
237
- describe ( 'manual vector building' , ( ) => {
238
- testVectorBuilding ( test , expectedErrorMessage ) ;
239
- } ) ;
240
- } else {
241
- throw new Error ( 'not testing anything for: ' + util . inspect ( test ) ) ;
242
- }
243
+ describe ( test . description , ( ) => {
244
+ if ( test . canonical_bson != null ) {
245
+ testVectorInvalidBSONBytes ( test , expectedErrorMessage ) ;
246
+ }
247
+
248
+ if ( test . vector != null ) {
249
+ testVectorInvalidInputValues ( test , expectedErrorMessage ) ;
250
+ }
251
+
252
+ if ( test . vector == null && test . canonical_bson == null ) {
253
+ throw new Error ( 'not testing anything for: ' + util . inspect ( test ) ) ;
254
+ }
255
+ } ) ;
243
256
}
244
257
} ) ;
245
258
} ) ;
0 commit comments