|
| 1 | +// Copyright (C) MongoDB, Inc. 2024-present. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +// not use this file except in compliance with the License. You may obtain |
| 5 | +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + |
| 7 | +package bson |
| 8 | + |
| 9 | +import ( |
| 10 | + "encoding/hex" |
| 11 | + "encoding/json" |
| 12 | + "fmt" |
| 13 | + "math" |
| 14 | + "os" |
| 15 | + "path" |
| 16 | + "testing" |
| 17 | + |
| 18 | + "go.mongodb.org/mongo-driver/v2/internal/require" |
| 19 | +) |
| 20 | + |
| 21 | +const bsonBinaryVectorDir = "../testdata/bson-binary-vector/" |
| 22 | + |
| 23 | +type bsonBinaryVectorTests struct { |
| 24 | + Description string `json:"description"` |
| 25 | + TestKey string `json:"test_key"` |
| 26 | + Tests []bsonBinaryVectorTestCase `json:"tests"` |
| 27 | +} |
| 28 | + |
| 29 | +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"` |
| 37 | +} |
| 38 | + |
| 39 | +func TestBsonBinaryVectorSpec(t *testing.T) { |
| 40 | + t.Parallel() |
| 41 | + |
| 42 | + jsonFiles, err := findJSONFilesInDir(bsonBinaryVectorDir) |
| 43 | + require.NoErrorf(t, err, "error finding JSON files in %s: %v", bsonBinaryVectorDir, err) |
| 44 | + |
| 45 | + for _, file := range jsonFiles { |
| 46 | + filepath := path.Join(bsonBinaryVectorDir, file) |
| 47 | + content, err := os.ReadFile(filepath) |
| 48 | + require.NoErrorf(t, err, "reading test file %s", filepath) |
| 49 | + |
| 50 | + var tests bsonBinaryVectorTests |
| 51 | + require.NoErrorf(t, json.Unmarshal(content, &tests), "parsing test file %s", filepath) |
| 52 | + |
| 53 | + t.Run(tests.Description, func(t *testing.T) { |
| 54 | + t.Parallel() |
| 55 | + |
| 56 | + for _, test := range tests.Tests { |
| 57 | + test := test |
| 58 | + t.Run(test.Description, func(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + |
| 61 | + runBsonBinaryVectorTest(t, tests.TestKey, test) |
| 62 | + }) |
| 63 | + } |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 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 | + |
| 97 | + t.Run("Padding specified with no vector data PACKED_BIT", func(t *testing.T) { |
| 98 | + t.Parallel() |
| 99 | + |
| 100 | + t.Run("Marshaling", func(t *testing.T) { |
| 101 | + _, err := NewPackedBitVector(nil, 1) |
| 102 | + require.EqualError(t, err, errNonZeroVectorPadding.Error()) |
| 103 | + }) |
| 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 | + }) |
| 114 | + }) |
| 115 | + |
| 116 | + t.Run("Exceeding maximum padding PACKED_BIT", func(t *testing.T) { |
| 117 | + t.Parallel() |
| 118 | + |
| 119 | + t.Run("Marshaling", func(t *testing.T) { |
| 120 | + _, err := NewPackedBitVector(nil, 8) |
| 121 | + require.EqualError(t, err, errVectorPaddingTooLarge.Error()) |
| 122 | + }) |
| 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 | + }) |
| 133 | + }) |
| 134 | +} |
| 135 | + |
| 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 | + |
| 164 | +func convertSlice[T int8 | float32 | byte](s []interface{}) []T { |
| 165 | + v := make([]T, len(s)) |
| 166 | + for i, e := range s { |
| 167 | + f := math.NaN() |
| 168 | + switch val := e.(type) { |
| 169 | + case float64: |
| 170 | + f = val |
| 171 | + case string: |
| 172 | + if val == "inf" { |
| 173 | + f = math.Inf(0) |
| 174 | + } else if val == "-inf" { |
| 175 | + f = math.Inf(-1) |
| 176 | + } |
| 177 | + } |
| 178 | + v[i] = T(f) |
| 179 | + } |
| 180 | + return v |
| 181 | +} |
| 182 | + |
| 183 | +func runBsonBinaryVectorTest(t *testing.T, testKey string, test bsonBinaryVectorTestCase) { |
| 184 | + testVector := make(map[string]Vector) |
| 185 | + switch alias := test.DtypeHex; alias { |
| 186 | + case "0x03": |
| 187 | + testVector[testKey] = Vector{ |
| 188 | + dType: Int8Vector, |
| 189 | + int8Data: convertSlice[int8](test.Vector), |
| 190 | + } |
| 191 | + case "0x27": |
| 192 | + testVector[testKey] = Vector{ |
| 193 | + dType: Float32Vector, |
| 194 | + float32Data: convertSlice[float32](test.Vector), |
| 195 | + } |
| 196 | + case "0x10": |
| 197 | + testVector[testKey] = Vector{ |
| 198 | + dType: PackedBitVector, |
| 199 | + bitData: convertSlice[byte](test.Vector), |
| 200 | + bitPadding: uint8(test.Padding), |
| 201 | + } |
| 202 | + default: |
| 203 | + t.Fatalf("unsupported vector type: %s", alias) |
| 204 | + } |
| 205 | + |
| 206 | + testBSON, err := hex.DecodeString(test.CanonicalBson) |
| 207 | + require.NoError(t, err, "decoding canonical BSON") |
| 208 | + |
| 209 | + t.Run("Unmarshaling", func(t *testing.T) { |
| 210 | + 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", |
| 222 | + } |
| 223 | + if reason, ok := skipCases[test.Description]; ok { |
| 224 | + t.Skipf("skip test case %s: %s", test.Description, reason) |
| 225 | + } |
| 226 | + |
| 227 | + t.Parallel() |
| 228 | + |
| 229 | + var got map[string]Vector |
| 230 | + err := Unmarshal(testBSON, &got) |
| 231 | + require.NoError(t, err) |
| 232 | + require.Equal(t, testVector, got) |
| 233 | + }) |
| 234 | + |
| 235 | + t.Run("Marshaling", func(t *testing.T) { |
| 236 | + skipCases := map[string]string{ |
| 237 | + "FLOAT32 with padding": "private padding field", |
| 238 | + "Overflow Vector INT8": "compile-time restriction", |
| 239 | + "Underflow Vector INT8": "compile-time restriction", |
| 240 | + "INT8 with padding": "private padding field", |
| 241 | + "INT8 with float inputs": "compile-time restriction", |
| 242 | + "Overflow Vector PACKED_BIT": "compile-time restriction", |
| 243 | + "Underflow Vector PACKED_BIT": "compile-time restriction", |
| 244 | + "Vector with float values PACKED_BIT": "compile-time restriction", |
| 245 | + "Padding specified with no vector data PACKED_BIT": "run in alternative case", |
| 246 | + "Exceeding maximum padding PACKED_BIT": "run in alternative case", |
| 247 | + "Negative padding PACKED_BIT": "compile-time restriction", |
| 248 | + } |
| 249 | + if reason, ok := skipCases[test.Description]; ok { |
| 250 | + t.Skipf("skip test case %s: %s", test.Description, reason) |
| 251 | + } |
| 252 | + |
| 253 | + t.Parallel() |
| 254 | + |
| 255 | + got, err := Marshal(testVector) |
| 256 | + require.NoError(t, err) |
| 257 | + require.Equal(t, testBSON, got) |
| 258 | + }) |
| 259 | +} |
0 commit comments