Skip to content

Commit 2cd7f73

Browse files
authored
GODRIVER-3478 Use ExtJSON for BSON binary vector spec tests. (#2003)
1 parent c205d4f commit 2cd7f73

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

bson/bson_binary_vector_spec_test.go

+21-24
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package bson
99
import (
1010
"encoding/hex"
1111
"encoding/json"
12-
"math"
1312
"os"
1413
"path"
1514
"testing"
@@ -27,13 +26,13 @@ type bsonBinaryVectorTests struct {
2726
}
2827

2928
type bsonBinaryVectorTestCase struct {
30-
Description string `json:"description"`
31-
Valid bool `json:"valid"`
32-
Vector []interface{} `json:"vector"`
33-
DtypeHex string `json:"dtype_hex"`
34-
DtypeAlias string `json:"dtype_alias"`
35-
Padding int `json:"padding"`
36-
CanonicalBson string `json:"canonical_bson"`
29+
Description string `json:"description"`
30+
Valid bool `json:"valid"`
31+
Vector json.RawMessage `json:"vector"`
32+
DtypeHex string `json:"dtype_hex"`
33+
DtypeAlias string `json:"dtype_alias"`
34+
Padding int `json:"padding"`
35+
CanonicalBson string `json:"canonical_bson"`
3736
}
3837

3938
func TestBsonBinaryVectorSpec(t *testing.T) {
@@ -83,21 +82,19 @@ func TestBsonBinaryVectorSpec(t *testing.T) {
8382
})
8483
}
8584

86-
func convertSlice[T int8 | float32 | byte](s []interface{}) []T {
85+
func decodeTestSlice[T int8 | float32 | byte](t *testing.T, data []byte) []T {
86+
t.Helper()
87+
88+
if len(data) == 0 {
89+
return nil
90+
}
91+
var s []float64
92+
err := UnmarshalExtJSON(data, true, &s)
93+
require.NoError(t, err)
94+
8795
v := make([]T, len(s))
8896
for i, e := range s {
89-
f := math.NaN()
90-
switch val := e.(type) {
91-
case float64:
92-
f = val
93-
case string:
94-
if val == "inf" {
95-
f = math.Inf(0)
96-
} else if val == "-inf" {
97-
f = math.Inf(-1)
98-
}
99-
}
100-
v[i] = T(f)
97+
v[i] = T(e)
10198
}
10299
return v
103100
}
@@ -108,17 +105,17 @@ func runBsonBinaryVectorTest(t *testing.T, testKey string, test bsonBinaryVector
108105
case "0x03":
109106
testVector[testKey] = Vector{
110107
dType: Int8Vector,
111-
int8Data: convertSlice[int8](test.Vector),
108+
int8Data: decodeTestSlice[int8](t, test.Vector),
112109
}
113110
case "0x27":
114111
testVector[testKey] = Vector{
115112
dType: Float32Vector,
116-
float32Data: convertSlice[float32](test.Vector),
113+
float32Data: decodeTestSlice[float32](t, test.Vector),
117114
}
118115
case "0x10":
119116
testVector[testKey] = Vector{
120117
dType: PackedBitVector,
121-
bitData: convertSlice[byte](test.Vector),
118+
bitData: decodeTestSlice[byte](t, test.Vector),
122119
bitPadding: uint8(test.Padding),
123120
}
124121
default:

testdata/bson-binary-vector/float32.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
{
3333
"description": "Infinity Vector FLOAT32",
3434
"valid": true,
35-
"vector": ["-inf", 0.0, "inf"],
35+
"vector": [{"$numberDouble": "-Infinity"}, 0.0, {"$numberDouble": "Infinity"}],
3636
"dtype_hex": "0x27",
3737
"dtype_alias": "FLOAT32",
3838
"padding": 0,

0 commit comments

Comments
 (0)