1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
// Copyright (C) MongoDB, Inc. 2023-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package bson
import (
"reflect"
"strings"
"testing"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/internal/assert"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
func TestUnmarshalValue(t *testing.T) {
t.Parallel()
unmarshalValueTestCases := newMarshalValueTestCases(t)
t.Run("UnmarshalValue", func(t *testing.T) {
t.Parallel()
for _, tc := range unmarshalValueTestCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
gotValue := reflect.New(reflect.TypeOf(tc.val))
err := UnmarshalValue(tc.bsontype, tc.bytes, gotValue.Interface())
assert.Nil(t, err, "UnmarshalValueWithRegistry error: %v", err)
assert.Equal(t, tc.val, gotValue.Elem().Interface(), "value mismatch; expected %s, got %s", tc.val, gotValue.Elem())
})
}
})
t.Run("UnmarshalValueWithRegistry with DefaultRegistry", func(t *testing.T) {
t.Parallel()
for _, tc := range unmarshalValueTestCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
gotValue := reflect.New(reflect.TypeOf(tc.val))
err := UnmarshalValueWithRegistry(DefaultRegistry, tc.bsontype, tc.bytes, gotValue.Interface())
assert.Nil(t, err, "UnmarshalValueWithRegistry error: %v", err)
assert.Equal(t, tc.val, gotValue.Elem().Interface(), "value mismatch; expected %s, got %s", tc.val, gotValue.Elem())
})
}
})
// tests covering GODRIVER-2779
t.Run("UnmarshalValueWithRegistry with custom registry", func(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
val interface{}
bsontype bsontype.Type
bytes []byte
}{
{
name: "SliceCodec binary",
val: []byte("hello world"),
bsontype: bsontype.Binary,
bytes: bsoncore.AppendBinary(nil, bsontype.BinaryGeneric, []byte("hello world")),
},
{
name: "SliceCodec string",
val: []byte("hello world"),
bsontype: bsontype.String,
bytes: bsoncore.AppendString(nil, "hello world"),
},
}
rb := NewRegistryBuilder().RegisterTypeDecoder(reflect.TypeOf([]byte{}), bsoncodec.NewSliceCodec()).Build()
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
gotValue := reflect.New(reflect.TypeOf(tc.val))
err := UnmarshalValueWithRegistry(rb, tc.bsontype, tc.bytes, gotValue.Interface())
assert.Nil(t, err, "UnmarshalValueWithRegistry error: %v", err)
assert.Equal(t, tc.val, gotValue.Elem().Interface(), "value mismatch; expected %s, got %s", tc.val, gotValue.Elem())
})
}
})
}
// tests covering GODRIVER-2779
func BenchmarkSliceCodecUnmarshal(b *testing.B) {
benchmarks := []struct {
name string
bsontype bsontype.Type
bytes []byte
}{
{
name: "SliceCodec binary",
bsontype: bsontype.Binary,
bytes: bsoncore.AppendBinary(nil, bsontype.BinaryGeneric, []byte(strings.Repeat("t", 4096))),
},
{
name: "SliceCodec string",
bsontype: bsontype.String,
bytes: bsoncore.AppendString(nil, strings.Repeat("t", 4096)),
},
}
rb := NewRegistryBuilder().RegisterTypeDecoder(reflect.TypeOf([]byte{}), bsoncodec.NewSliceCodec()).Build()
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := UnmarshalValueWithRegistry(rb, bm.bsontype, bm.bytes, &[]byte{})
if err != nil {
b.Fatal(err)
}
}
})
})
}
}
|