Skip to content

GODRIVER-3478 Use ExtJSON for BSON binary vector spec tests. #2003

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 21 additions & 24 deletions bson/bson_binary_vector_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package bson
import (
"encoding/hex"
"encoding/json"
"math"
"os"
"path"
"testing"
Expand All @@ -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) {
Expand Down Expand Up @@ -82,21 +81,19 @@ func TestBsonBinaryVectorSpec(t *testing.T) {
})
}

func convertSlice[T int8 | float32 | byte](s []interface{}) []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)
require.NoError(t, err)

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
}
Expand All @@ -107,17 +104,17 @@ func runBsonBinaryVectorTest(t *testing.T, testKey string, test bsonBinaryVector
case "0x03":
testVector[testKey] = Vector{
dType: Int8Vector,
int8Data: convertSlice[int8](test.Vector),
int8Data: decodeTestSlice[int8](t, test.Vector),
}
case "0x27":
testVector[testKey] = Vector{
dType: Float32Vector,
float32Data: convertSlice[float32](test.Vector),
float32Data: decodeTestSlice[float32](t, test.Vector),
}
case "0x10":
testVector[testKey] = Vector{
dType: PackedBitVector,
bitData: convertSlice[byte](test.Vector),
bitData: decodeTestSlice[byte](t, test.Vector),
bitPadding: uint8(test.Padding),
}
default:
Expand Down
2 changes: 1 addition & 1 deletion testdata/bson-binary-vector/float32.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading