...

Source file src/go.mongodb.org/mongo-driver/bson/unmarshal_value_test.go

Documentation: go.mongodb.org/mongo-driver/bson

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

View as plain text