Skip to content

Commit a76de92

Browse files
authored
GODRIVER-3481 Improve JSON tests for Binary Vector Subtype. (#1942)
1 parent 3ab40de commit a76de92

File tree

4 files changed

+46
-112
lines changed

4 files changed

+46
-112
lines changed

bson/bson_binary_vector_spec_test.go

Lines changed: 24 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package bson
99
import (
1010
"encoding/hex"
1111
"encoding/json"
12-
"fmt"
1312
"math"
1413
"os"
1514
"path"
@@ -64,53 +63,13 @@ func TestBsonBinaryVectorSpec(t *testing.T) {
6463
})
6564
}
6665

67-
t.Run("FLOAT32 with padding", func(t *testing.T) {
68-
t.Parallel()
69-
70-
t.Run("Unmarshaling", func(t *testing.T) {
71-
val := D{{"vector", Binary{Subtype: TypeBinaryVector, Data: []byte{Float32Vector, 3}}}}
72-
b, err := Marshal(val)
73-
require.NoError(t, err, "marshaling test BSON")
74-
var got struct {
75-
Vector Vector
76-
}
77-
err = Unmarshal(b, &got)
78-
require.ErrorContains(t, err, errNonZeroVectorPadding.Error())
79-
})
80-
})
81-
82-
t.Run("INT8 with padding", func(t *testing.T) {
83-
t.Parallel()
84-
85-
t.Run("Unmarshaling", func(t *testing.T) {
86-
val := D{{"vector", Binary{Subtype: TypeBinaryVector, Data: []byte{Int8Vector, 3}}}}
87-
b, err := Marshal(val)
88-
require.NoError(t, err, "marshaling test BSON")
89-
var got struct {
90-
Vector Vector
91-
}
92-
err = Unmarshal(b, &got)
93-
require.ErrorContains(t, err, errNonZeroVectorPadding.Error())
94-
})
95-
})
96-
9766
t.Run("Padding specified with no vector data PACKED_BIT", func(t *testing.T) {
9867
t.Parallel()
9968

10069
t.Run("Marshaling", func(t *testing.T) {
10170
_, err := NewPackedBitVector(nil, 1)
10271
require.EqualError(t, err, errNonZeroVectorPadding.Error())
10372
})
104-
t.Run("Unmarshaling", func(t *testing.T) {
105-
val := D{{"vector", Binary{Subtype: TypeBinaryVector, Data: []byte{PackedBitVector, 1}}}}
106-
b, err := Marshal(val)
107-
require.NoError(t, err, "marshaling test BSON")
108-
var got struct {
109-
Vector Vector
110-
}
111-
err = Unmarshal(b, &got)
112-
require.ErrorContains(t, err, errNonZeroVectorPadding.Error())
113-
})
11473
})
11574

11675
t.Run("Exceeding maximum padding PACKED_BIT", func(t *testing.T) {
@@ -120,47 +79,9 @@ func TestBsonBinaryVectorSpec(t *testing.T) {
12079
_, err := NewPackedBitVector(nil, 8)
12180
require.EqualError(t, err, errVectorPaddingTooLarge.Error())
12281
})
123-
t.Run("Unmarshaling", func(t *testing.T) {
124-
val := D{{"vector", Binary{Subtype: TypeBinaryVector, Data: []byte{PackedBitVector, 8}}}}
125-
b, err := Marshal(val)
126-
require.NoError(t, err, "marshaling test BSON")
127-
var got struct {
128-
Vector Vector
129-
}
130-
err = Unmarshal(b, &got)
131-
require.ErrorContains(t, err, errVectorPaddingTooLarge.Error())
132-
})
13382
})
13483
}
13584

136-
// TODO: This test may be added into the spec tests.
137-
func TestFloat32VectorWithInsufficientData(t *testing.T) {
138-
t.Parallel()
139-
140-
val := Binary{Subtype: TypeBinaryVector}
141-
142-
for _, tc := range [][]byte{
143-
{Float32Vector, 0, 42},
144-
{Float32Vector, 0, 42, 42},
145-
{Float32Vector, 0, 42, 42, 42},
146-
147-
{Float32Vector, 0, 42, 42, 42, 42, 42},
148-
{Float32Vector, 0, 42, 42, 42, 42, 42, 42},
149-
{Float32Vector, 0, 42, 42, 42, 42, 42, 42, 42},
150-
} {
151-
t.Run(fmt.Sprintf("marshaling %d bytes", len(tc)-2), func(t *testing.T) {
152-
val.Data = tc
153-
b, err := Marshal(D{{"vector", val}})
154-
require.NoError(t, err, "marshaling test BSON")
155-
var got struct {
156-
Vector Vector
157-
}
158-
err = Unmarshal(b, &got)
159-
require.ErrorContains(t, err, errInsufficientVectorData.Error())
160-
})
161-
}
162-
}
163-
16485
func convertSlice[T int8 | float32 | byte](s []interface{}) []T {
16586
v := make([]T, len(s))
16687
for i, e := range s {
@@ -208,33 +129,44 @@ func runBsonBinaryVectorTest(t *testing.T, testKey string, test bsonBinaryVector
208129

209130
t.Run("Unmarshaling", func(t *testing.T) {
210131
skipCases := map[string]string{
211-
"FLOAT32 with padding": "run in alternative case",
212-
"Overflow Vector INT8": "compile-time restriction",
213-
"Underflow Vector INT8": "compile-time restriction",
214-
"INT8 with padding": "run in alternative case",
215-
"INT8 with float inputs": "compile-time restriction",
216-
"Overflow Vector PACKED_BIT": "compile-time restriction",
217-
"Underflow Vector PACKED_BIT": "compile-time restriction",
218-
"Vector with float values PACKED_BIT": "compile-time restriction",
219-
"Padding specified with no vector data PACKED_BIT": "run in alternative case",
220-
"Exceeding maximum padding PACKED_BIT": "run in alternative case",
221-
"Negative padding PACKED_BIT": "compile-time restriction",
132+
"Overflow Vector INT8": "compile-time restriction",
133+
"Underflow Vector INT8": "compile-time restriction",
134+
"INT8 with float inputs": "compile-time restriction",
135+
"Overflow Vector PACKED_BIT": "compile-time restriction",
136+
"Underflow Vector PACKED_BIT": "compile-time restriction",
137+
"Vector with float values PACKED_BIT": "compile-time restriction",
138+
"Negative padding PACKED_BIT": "compile-time restriction",
222139
}
223140
if reason, ok := skipCases[test.Description]; ok {
224141
t.Skipf("skip test case %s: %s", test.Description, reason)
225142
}
226143

144+
errMap := map[string]string{
145+
"FLOAT32 with padding": "padding must be 0",
146+
"INT8 with padding": "padding must be 0",
147+
"Padding specified with no vector data PACKED_BIT": "padding must be 0",
148+
"Exceeding maximum padding PACKED_BIT": "padding cannot be larger than 7",
149+
}
150+
227151
t.Parallel()
228152

229153
var got map[string]Vector
230154
err := Unmarshal(testBSON, &got)
231-
require.NoError(t, err)
232-
require.Equal(t, testVector, got)
155+
if test.Valid {
156+
require.NoError(t, err)
157+
require.Equal(t, testVector, got)
158+
} else if errMsg, ok := errMap[test.Description]; ok {
159+
require.ErrorContains(t, err, errMsg)
160+
} else {
161+
require.Error(t, err)
162+
}
233163
})
234164

235165
t.Run("Marshaling", func(t *testing.T) {
236166
skipCases := map[string]string{
237167
"FLOAT32 with padding": "private padding field",
168+
"Insufficient vector data with 3 bytes FLOAT32": "invalid case",
169+
"Insufficient vector data with 5 bytes FLOAT32": "invalid case",
238170
"Overflow Vector INT8": "compile-time restriction",
239171
"Underflow Vector INT8": "compile-time restriction",
240172
"INT8 with padding": "private padding field",

testdata/bson-binary-vector/float32.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,22 @@
4444
"vector": [127.0, 7.0],
4545
"dtype_hex": "0x27",
4646
"dtype_alias": "FLOAT32",
47-
"padding": 3
47+
"padding": 3,
48+
"canonical_bson": "1C00000005766563746F72000A0000000927030000FE420000E04000"
49+
},
50+
{
51+
"description": "Insufficient vector data with 3 bytes FLOAT32",
52+
"valid": false,
53+
"dtype_hex": "0x27",
54+
"dtype_alias": "FLOAT32",
55+
"canonical_bson": "1700000005766563746F7200050000000927002A2A2A00"
56+
},
57+
{
58+
"description": "Insufficient vector data with 5 bytes FLOAT32",
59+
"valid": false,
60+
"dtype_hex": "0x27",
61+
"dtype_alias": "FLOAT32",
62+
"canonical_bson": "1900000005766563746F7200070000000927002A2A2A2A2A00"
4863
}
4964
]
5065
}

testdata/bson-binary-vector/int8.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"vector": [127, 7],
4343
"dtype_hex": "0x03",
4444
"dtype_alias": "INT8",
45-
"padding": 3
45+
"padding": 3,
46+
"canonical_bson": "1600000005766563746F7200040000000903037F0700"
4647
},
4748
{
4849
"description": "INT8 with float inputs",

testdata/bson-binary-vector/packed_bit.json

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"vector": [],
99
"dtype_hex": "0x10",
1010
"dtype_alias": "PACKED_BIT",
11-
"padding": 1
11+
"padding": 1,
12+
"canonical_bson": "1400000005766563746F72000200000009100100"
1213
},
1314
{
1415
"description": "Simple Vector PACKED_BIT",
@@ -61,21 +62,14 @@
6162
"dtype_alias": "PACKED_BIT",
6263
"padding": 0
6364
},
64-
{
65-
"description": "Padding specified with no vector data PACKED_BIT",
66-
"valid": false,
67-
"vector": [],
68-
"dtype_hex": "0x10",
69-
"dtype_alias": "PACKED_BIT",
70-
"padding": 1
71-
},
7265
{
7366
"description": "Exceeding maximum padding PACKED_BIT",
7467
"valid": false,
7568
"vector": [1],
7669
"dtype_hex": "0x10",
7770
"dtype_alias": "PACKED_BIT",
78-
"padding": 8
71+
"padding": 8,
72+
"canonical_bson": "1500000005766563746F7200030000000910080100"
7973
},
8074
{
8175
"description": "Negative padding PACKED_BIT",
@@ -84,14 +78,6 @@
8478
"dtype_hex": "0x10",
8579
"dtype_alias": "PACKED_BIT",
8680
"padding": -1
87-
},
88-
{
89-
"description": "Vector with float values PACKED_BIT",
90-
"valid": false,
91-
"vector": [127.5],
92-
"dtype_hex": "0x10",
93-
"dtype_alias": "PACKED_BIT",
94-
"padding": 0
9581
}
9682
]
9783
}

0 commit comments

Comments
 (0)