@@ -31,7 +31,7 @@ struct TestFile {
3131struct Test {
3232 description : String ,
3333 valid : bool ,
34- vector : Vec < Number > ,
34+ vector : Option < Vec < Number > > ,
3535 #[ serde(
3636 rename = "dtype_hex" ,
3737 deserialize_with = "deserialize_u8_from_hex_string"
@@ -131,51 +131,68 @@ fn vector_from_numbers(
131131 }
132132}
133133
134+ // Only return the binary if it represents a valid vector; otherwise, return an error.
135+ fn binary_from_bytes ( bson : & str , test_key : & str , description : & str ) -> Result < Binary , String > {
136+ let bytes = hex:: decode ( bson) . expect ( description) ;
137+ let mut test_document = Document :: from_reader ( bytes. as_slice ( ) ) . expect ( description) ;
138+ let bson = test_document. remove ( test_key) . expect ( description) ;
139+ let binary = match bson {
140+ Bson :: Binary ( binary) => binary,
141+ other => panic ! ( "{}: expected binary, got {}" , description, other) ,
142+ } ;
143+ if let Err ( error) = Vector :: try_from ( & binary) {
144+ Err ( error. to_string ( ) )
145+ } else {
146+ Ok ( binary)
147+ }
148+ }
149+
134150fn run_test_file ( test_file : TestFile ) {
135151 for test in test_file. tests {
136152 let description = format ! ( "{} ({})" , test. description, test_file. description) ;
137153
138- let test_vector = match (
139- vector_from_numbers ( test. vector , test. d_type , test. padding ) ,
140- test. valid ,
141- ) {
142- ( Ok ( vector) , true ) => vector,
143- ( Err ( _) , false ) => return ,
144- ( Ok ( vector) , false ) => panic ! (
145- "{}: valid was false but successfully constructed vector {:?}" ,
146- description, vector
147- ) ,
148- ( Err ( error) , true ) => panic ! (
149- "{}: valid was true but vector construction failed with error {}" ,
150- description, error
151- ) ,
154+ let test_vector = match test. vector {
155+ Some ( vector) => match vector_from_numbers ( vector, test. d_type , test. padding ) {
156+ Ok ( vector) => {
157+ assert ! ( test. valid, "{}" , description) ;
158+ Some ( vector)
159+ }
160+ Err ( error) => {
161+ assert ! ( !test. valid, "{}: {}" , description, error) ;
162+ None
163+ }
164+ } ,
165+ None => None ,
166+ } ;
167+
168+ let test_binary = match test. canonical_bson {
169+ Some ( bson) => match binary_from_bytes ( & bson, & test_file. test_key , & description) {
170+ Ok ( vector) => {
171+ assert ! ( test. valid, "{}" , description) ;
172+ Some ( vector)
173+ }
174+ Err ( error) => {
175+ assert ! ( !test. valid, "{}: {}" , description, error) ;
176+ None
177+ }
178+ } ,
179+ None => None ,
152180 } ;
153181
154- let Some ( canonical_bson ) = test . canonical_bson else {
182+ let ( Some ( test_vector ) , Some ( test_binary ) ) = ( test_vector , test_binary ) else {
155183 return ;
156184 } ;
157185
158- let bytes = hex:: decode ( canonical_bson) . expect ( & description) ;
159- let mut test_document = Document :: from_reader ( bytes. as_slice ( ) ) . expect ( & description) ;
160- // Rename the field to match the name used in the struct below.
161- let vector = test_document
162- . remove ( & test_file. test_key )
163- . expect ( & description) ;
164- test_document. insert ( "vector" , vector) ;
165- let bson = test_document. get ( "vector" ) . expect ( & description) ;
166- let test_binary = match bson {
167- Bson :: Binary ( binary) => binary,
168- other => panic ! ( "{}: expected binary, got {}" , description, other) ,
169- } ;
186+ let test_document = doc ! { "vector" : & test_binary } ;
170187
171188 // TryFrom<Binary> for Vector
172- let parsed_vector = Vector :: try_from ( test_binary) . expect ( & description) ;
189+ let parsed_vector = Vector :: try_from ( & test_binary) . expect ( & description) ;
173190 assert_eq ! ( parsed_vector, test_vector) ;
174191
175192 // From<Vector> for Binary
176193 let binary = Binary :: from ( & test_vector) ;
177194 assert_eq ! ( binary. subtype, BinarySubtype :: Vector ) ;
178- assert_eq ! ( & binary, test_binary) ;
195+ assert_eq ! ( binary, test_binary) ;
179196
180197 // From<Vector> for Bson
181198 let document = doc ! { "vector" : & test_vector } ;
0 commit comments