From 8f519c5932ecf54e690598293c687bf11e8b05f5 Mon Sep 17 00:00:00 2001 From: Qingyang Hu Date: Fri, 28 Mar 2025 17:01:44 -0400 Subject: [PATCH 1/2] GODRIVER-3478 Use ExtJSON for BSON binary vector spec tests. --- bson/bson_binary_vector_spec_test.go | 44 +++++++++++------------- testdata/bson-binary-vector/float32.json | 2 +- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/bson/bson_binary_vector_spec_test.go b/bson/bson_binary_vector_spec_test.go index dcd987b3e5..a645c374ba 100644 --- a/bson/bson_binary_vector_spec_test.go +++ b/bson/bson_binary_vector_spec_test.go @@ -9,7 +9,6 @@ package bson import ( "encoding/hex" "encoding/json" - "math" "os" "path" "testing" @@ -26,13 +25,13 @@ type bsonBinaryVectorTests struct { } type bsonBinaryVectorTestCase struct { - Description string `json:"description"` - Valid bool `json:"valid"` - Vector []interface{} `json:"vector"` - DtypeHex string `json:"dtype_hex"` - DtypeAlias string `json:"dtype_alias"` - Padding int `json:"padding"` - CanonicalBson string `json:"canonical_bson"` + Description string `json:"description"` + Valid bool `json:"valid"` + Vector json.RawMessage `json:"vector"` + DtypeHex string `json:"dtype_hex"` + DtypeAlias string `json:"dtype_alias"` + Padding int `json:"padding"` + CanonicalBson string `json:"canonical_bson"` } func TestBsonBinaryVectorSpec(t *testing.T) { @@ -82,21 +81,18 @@ func TestBsonBinaryVectorSpec(t *testing.T) { }) } -func convertSlice[T int8 | float32 | byte](s []interface{}) []T { +func convertSlice[T int8 | float32 | byte](t *testing.T, data []byte) []T { + if len(data) == 0 { + return nil + } + var s []float64 + err := UnmarshalExtJSON(data, true, &s) + if err != nil { + t.Fatalf("got %q while handling %s", err, string(data)) + } v := make([]T, len(s)) for i, e := range s { - f := math.NaN() - switch val := e.(type) { - case float64: - f = val - case string: - if val == "inf" { - f = math.Inf(0) - } else if val == "-inf" { - f = math.Inf(-1) - } - } - v[i] = T(f) + v[i] = T(e) } return v } @@ -107,17 +103,17 @@ func runBsonBinaryVectorTest(t *testing.T, testKey string, test bsonBinaryVector case "0x03": testVector[testKey] = Vector{ dType: Int8Vector, - int8Data: convertSlice[int8](test.Vector), + int8Data: convertSlice[int8](t, test.Vector), } case "0x27": testVector[testKey] = Vector{ dType: Float32Vector, - float32Data: convertSlice[float32](test.Vector), + float32Data: convertSlice[float32](t, test.Vector), } case "0x10": testVector[testKey] = Vector{ dType: PackedBitVector, - bitData: convertSlice[byte](test.Vector), + bitData: convertSlice[byte](t, test.Vector), bitPadding: uint8(test.Padding), } default: diff --git a/testdata/bson-binary-vector/float32.json b/testdata/bson-binary-vector/float32.json index 845f504ff3..0bc88fc65a 100644 --- a/testdata/bson-binary-vector/float32.json +++ b/testdata/bson-binary-vector/float32.json @@ -32,7 +32,7 @@ { "description": "Infinity Vector FLOAT32", "valid": true, - "vector": ["-inf", 0.0, "inf"], + "vector": [{"$numberDouble": "-Infinity"}, 0.0, {"$numberDouble": "Infinity"}], "dtype_hex": "0x27", "dtype_alias": "FLOAT32", "padding": 0, From 9905268894c87f8c2a150172e2ff972b514257c2 Mon Sep 17 00:00:00 2001 From: Qingyang Hu Date: Tue, 8 Apr 2025 09:23:25 -0400 Subject: [PATCH 2/2] updates --- bson/bson_binary_vector_spec_test.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/bson/bson_binary_vector_spec_test.go b/bson/bson_binary_vector_spec_test.go index a645c374ba..37529a4686 100644 --- a/bson/bson_binary_vector_spec_test.go +++ b/bson/bson_binary_vector_spec_test.go @@ -81,15 +81,16 @@ func TestBsonBinaryVectorSpec(t *testing.T) { }) } -func convertSlice[T int8 | float32 | byte](t *testing.T, data []byte) []T { +func decodeTestSlice[T int8 | float32 | byte](t *testing.T, data []byte) []T { + t.Helper() + if len(data) == 0 { return nil } var s []float64 err := UnmarshalExtJSON(data, true, &s) - if err != nil { - t.Fatalf("got %q while handling %s", err, string(data)) - } + require.NoError(t, err) + v := make([]T, len(s)) for i, e := range s { v[i] = T(e) @@ -103,17 +104,17 @@ func runBsonBinaryVectorTest(t *testing.T, testKey string, test bsonBinaryVector case "0x03": testVector[testKey] = Vector{ dType: Int8Vector, - int8Data: convertSlice[int8](t, test.Vector), + int8Data: decodeTestSlice[int8](t, test.Vector), } case "0x27": testVector[testKey] = Vector{ dType: Float32Vector, - float32Data: convertSlice[float32](t, test.Vector), + float32Data: decodeTestSlice[float32](t, test.Vector), } case "0x10": testVector[testKey] = Vector{ dType: PackedBitVector, - bitData: convertSlice[byte](t, test.Vector), + bitData: decodeTestSlice[byte](t, test.Vector), bitPadding: uint8(test.Padding), } default: