1
+ import * as util from 'util' ;
1
2
import * as fs from 'fs' ;
2
3
import * as path from 'path' ;
3
- import { BSON , BSONError , Binary } from '../register-bson' ;
4
+ import { BSON , BSONError , Binary , EJSON } from '../register-bson' ;
4
5
import { expect } from 'chai' ;
5
6
6
7
const { toHex, fromHex } = BSON . onDemand . ByteUtils ;
7
8
8
9
type VectorHexType = '0x03' | '0x27' | '0x10' ;
9
10
type VectorTest = {
10
11
description : string ;
11
- vector : ( number | string ) [ ] ;
12
+ vector ? : ( number | string ) [ ] ;
12
13
valid : boolean ;
13
14
dtype_hex : VectorHexType ;
14
15
padding ?: number ;
@@ -87,6 +88,10 @@ const invalidTestExpectedError = new Map()
87
88
'Invalid Vector: padding must be a value between 0 and 7'
88
89
)
89
90
. set ( 'Negative padding PACKED_BIT' , 'Invalid Vector: padding must be a value between 0 and 7' )
91
+ . set (
92
+ 'Insufficient vector data FLOAT32' ,
93
+ 'Invalid Vector: Float32 vector must contain a multiple of 4 bytes'
94
+ )
90
95
// skipped
91
96
. set ( 'Overflow Vector PACKED_BIT' , false )
92
97
. set ( 'Underflow Vector PACKED_BIT' , false )
@@ -97,6 +102,84 @@ const invalidTestExpectedError = new Map()
97
102
. set ( 'Vector with float values PACKED_BIT' , false )
98
103
. set ( 'Vector with float values PACKED_BIT' , false ) ;
99
104
105
+ function testVectorBuilding ( test : VectorTest , expectedErrorMessage : string ) {
106
+ describe ( 'creating an instance of a Binary class using parameters' , ( ) => {
107
+ it ( `bson: ${ test . description } ` , function ( ) {
108
+ let thrownError : Error | undefined ;
109
+ try {
110
+ const bin = make ( test . vector ! , test . dtype_hex , test . padding ) ;
111
+ BSON . serialize ( { bin } ) ;
112
+ } catch ( error ) {
113
+ thrownError = error ;
114
+ }
115
+
116
+ if ( thrownError ?. message . startsWith ( 'unsupported_error' ) ) {
117
+ expect (
118
+ expectedErrorMessage ,
119
+ 'We expect a certain error message but got an unsupported error'
120
+ ) . to . be . false ;
121
+ this . skip ( ) ;
122
+ }
123
+
124
+ expect ( thrownError , thrownError ?. stack ) . to . be . instanceOf ( BSONError ) ;
125
+ expect ( thrownError ?. message ) . to . match ( new RegExp ( expectedErrorMessage ) ) ;
126
+ } ) ;
127
+
128
+ it ( `ejson: ${ test . description } ` , function ( ) {
129
+ let thrownError : Error | undefined ;
130
+ try {
131
+ const bin = make ( test . vector ! , test . dtype_hex , test . padding ) ;
132
+ BSON . EJSON . stringify ( { bin } ) ;
133
+ } catch ( error ) {
134
+ thrownError = error ;
135
+ }
136
+
137
+ if ( thrownError ?. message . startsWith ( 'unsupported_error' ) ) {
138
+ expect (
139
+ expectedErrorMessage ,
140
+ 'We expect a certain error message but got an unsupported error'
141
+ ) . to . be . false ;
142
+ this . skip ( ) ;
143
+ }
144
+
145
+ expect ( thrownError , thrownError ?. stack ) . to . be . instanceOf ( BSONError ) ;
146
+ expect ( thrownError ?. message ) . to . match ( new RegExp ( expectedErrorMessage ) ) ;
147
+ } ) ;
148
+ } ) ;
149
+ }
150
+
151
+ function testVectorReserializing ( test : VectorTest , expectedErrorMessage : string ) {
152
+ describe ( 'creating an instance of a Binary class using canonical_bson' , ( ) => {
153
+ it ( `bson deserialize: ${ test . description } ` , function ( ) {
154
+ let thrownError : Error | undefined ;
155
+ const bin = BSON . deserialize ( Buffer . from ( test . canonical_bson ! , 'hex' ) ) ;
156
+
157
+ try {
158
+ BSON . serialize ( bin ) ;
159
+ } catch ( error ) {
160
+ thrownError = error ;
161
+ }
162
+
163
+ expect ( thrownError , thrownError ?. stack ) . to . be . instanceOf ( BSONError ) ;
164
+ expect ( thrownError ?. message ) . to . match ( new RegExp ( expectedErrorMessage ) ) ;
165
+ } ) ;
166
+
167
+ it ( `ejson stringify: ${ test . description } ` , function ( ) {
168
+ let thrownError : Error | undefined ;
169
+ const bin = BSON . deserialize ( Buffer . from ( test . canonical_bson ! , 'hex' ) ) ;
170
+
171
+ try {
172
+ EJSON . stringify ( bin ) ;
173
+ } catch ( error ) {
174
+ thrownError = error ;
175
+ }
176
+
177
+ expect ( thrownError , thrownError ?. stack ) . to . be . instanceOf ( BSONError ) ;
178
+ expect ( thrownError ?. message ) . to . match ( new RegExp ( expectedErrorMessage ) ) ;
179
+ } ) ;
180
+ } ) ;
181
+ }
182
+
100
183
describe ( 'BSON Binary Vector spec tests' , ( ) => {
101
184
const tests : Record < string , VectorSuite > = Object . create ( null ) ;
102
185
@@ -121,7 +204,7 @@ describe('BSON Binary Vector spec tests', () => {
121
204
*/
122
205
for ( const test of valid ) {
123
206
it ( `encode ${ test . description } ` , function ( ) {
124
- const bin = make ( test . vector , test . dtype_hex , test . padding ) ;
207
+ const bin = make ( test . vector ! , test . dtype_hex , test . padding ) ;
125
208
126
209
const buffer = BSON . serialize ( { [ suite . test_key ] : bin } ) ;
127
210
expect ( toHex ( buffer ) ) . to . equal ( test . canonical_bson ! . toLowerCase ( ) ) ;
@@ -147,47 +230,22 @@ describe('BSON Binary Vector spec tests', () => {
147
230
for ( const test of invalid ) {
148
231
const expectedErrorMessage = invalidTestExpectedError . get ( test . description ) ;
149
232
150
- it ( `bson: ${ test . description } ` , function ( ) {
151
- let thrownError : Error | undefined ;
152
- try {
153
- const bin = make ( test . vector , test . dtype_hex , test . padding ) ;
154
- BSON . serialize ( { bin } ) ;
155
- } catch ( error ) {
156
- thrownError = error ;
157
- }
158
-
159
- if ( thrownError ?. message . startsWith ( 'unsupported_error' ) ) {
160
- expect (
161
- expectedErrorMessage ,
162
- 'We expect a certain error message but got an unsupported error'
163
- ) . to . be . false ;
164
- this . skip ( ) ;
165
- }
166
-
167
- expect ( thrownError ) . to . be . instanceOf ( BSONError ) ;
168
- expect ( thrownError ?. message ) . to . match ( new RegExp ( expectedErrorMessage ) ) ;
169
- } ) ;
170
-
171
- it ( `extended json: ${ test . description } ` , function ( ) {
172
- let thrownError : Error | undefined ;
173
- try {
174
- const bin = make ( test . vector , test . dtype_hex , test . padding ) ;
175
- BSON . EJSON . stringify ( { bin } ) ;
176
- } catch ( error ) {
177
- thrownError = error ;
178
- }
179
-
180
- if ( thrownError ?. message . startsWith ( 'unsupported_error' ) ) {
181
- expect (
182
- expectedErrorMessage ,
183
- 'We expect a certain error message but got an unsupported error'
184
- ) . to . be . false ;
185
- this . skip ( ) ;
186
- }
187
-
188
- expect ( thrownError ) . to . be . instanceOf ( BSONError ) ;
189
- expect ( thrownError ?. message ) . to . match ( new RegExp ( expectedErrorMessage ) ) ;
190
- } ) ;
233
+ if ( test . vector != null && test . canonical_bson != null ) {
234
+ describe ( 'both manual vector building and re-serializing' , ( ) => {
235
+ testVectorBuilding ( test , expectedErrorMessage ) ;
236
+ testVectorReserializing ( test , expectedErrorMessage ) ;
237
+ } ) ;
238
+ } else if ( test . canonical_bson != null ) {
239
+ describe ( 'vector re-serializing' , ( ) => {
240
+ testVectorReserializing ( test , expectedErrorMessage ) ;
241
+ } ) ;
242
+ } else if ( test . vector != null ) {
243
+ describe ( 'manual vector building' , ( ) => {
244
+ testVectorBuilding ( test , expectedErrorMessage ) ;
245
+ } ) ;
246
+ } else {
247
+ throw new Error ( 'not testing anything for: ' + util . inspect ( test ) ) ;
248
+ }
191
249
}
192
250
} ) ;
193
251
} ) ;
0 commit comments