|
| 1 | +// Copyright (C) MongoDB, Inc. 2017-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 | + "io" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "go.mongodb.org/mongo-driver/bson/bsoncodec" |
| 14 | + "go.mongodb.org/mongo-driver/bson/bsontype" |
| 15 | + "go.mongodb.org/mongo-driver/bson/primitive" |
| 16 | + "go.mongodb.org/mongo-driver/internal/testutil/assert" |
| 17 | + "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" |
| 18 | +) |
| 19 | + |
| 20 | +// helper type for testing MarshalValue that implements io.Reader |
| 21 | +type marshalValueInterfaceInner struct { |
| 22 | + Foo int |
| 23 | +} |
| 24 | + |
| 25 | +var _ io.Reader = marshalValueInterfaceInner{} |
| 26 | + |
| 27 | +func (marshalValueInterfaceInner) Read([]byte) (int, error) { |
| 28 | + return 0, nil |
| 29 | +} |
| 30 | + |
| 31 | +// helper type for testing MarshalValue that contains an interface |
| 32 | +type marshalValueInterfaceOuter struct { |
| 33 | + Reader io.Reader |
| 34 | +} |
| 35 | + |
| 36 | +// helper type for testing MarshalValue that implements ValueMarshaler |
| 37 | +type marshalValueMarshaler struct { |
| 38 | + Foo int |
| 39 | +} |
| 40 | + |
| 41 | +var _ ValueMarshaler = marshalValueMarshaler{} |
| 42 | + |
| 43 | +func (mvi marshalValueMarshaler) MarshalBSONValue() (bsontype.Type, []byte, error) { |
| 44 | + return bsontype.Int32, bsoncore.AppendInt32(nil, int32(mvi.Foo)), nil |
| 45 | +} |
| 46 | + |
| 47 | +type marshalValueStruct struct { |
| 48 | + Foo int |
| 49 | +} |
| 50 | + |
| 51 | +type marshalValueTestCase struct { |
| 52 | + name string |
| 53 | + val interface{} |
| 54 | + expectedType bsontype.Type |
| 55 | + expectedBytes []byte |
| 56 | +} |
| 57 | + |
| 58 | +func TestMarshalValue(t *testing.T) { |
| 59 | + oid := primitive.NewObjectID() |
| 60 | + regex := primitive.Regex{Pattern: "pattern", Options: "imx"} |
| 61 | + dbPointer := primitive.DBPointer{DB: "db", Pointer: primitive.NewObjectID()} |
| 62 | + codeWithScope := primitive.CodeWithScope{Code: "code", Scope: D{{"a", "b"}}} |
| 63 | + idx, scopeCore := bsoncore.AppendDocumentStart(nil) |
| 64 | + scopeCore = bsoncore.AppendStringElement(scopeCore, "a", "b") |
| 65 | + scopeCore, _ = bsoncore.AppendDocumentEnd(scopeCore, idx) |
| 66 | + decimal128 := primitive.NewDecimal128(5, 10) |
| 67 | + interfaceTest := marshalValueInterfaceOuter{ |
| 68 | + Reader: marshalValueInterfaceInner{ |
| 69 | + Foo: 10, |
| 70 | + }, |
| 71 | + } |
| 72 | + interfaceCore, err := Marshal(interfaceTest) |
| 73 | + assert.Nil(t, err, "Marshal error: %v", err) |
| 74 | + structTest := marshalValueStruct{Foo: 10} |
| 75 | + structCore, err := Marshal(structTest) |
| 76 | + assert.Nil(t, err, "Marshal error: %v", err) |
| 77 | + |
| 78 | + marshalValueTestCases := []marshalValueTestCase{ |
| 79 | + {"double", 3.14, bsontype.Double, bsoncore.AppendDouble(nil, 3.14)}, |
| 80 | + {"string", "hello world", bsontype.String, bsoncore.AppendString(nil, "hello world")}, |
| 81 | + {"binary", primitive.Binary{1, []byte{1, 2}}, bsontype.Binary, bsoncore.AppendBinary(nil, 1, []byte{1, 2})}, |
| 82 | + {"undefined", primitive.Undefined{}, bsontype.Undefined, []byte{}}, |
| 83 | + {"object id", oid, bsontype.ObjectID, bsoncore.AppendObjectID(nil, oid)}, |
| 84 | + {"boolean", true, bsontype.Boolean, bsoncore.AppendBoolean(nil, true)}, |
| 85 | + {"datetime", primitive.DateTime(5), bsontype.DateTime, bsoncore.AppendDateTime(nil, 5)}, |
| 86 | + {"null", primitive.Null{}, bsontype.Null, []byte{}}, |
| 87 | + {"regex", regex, bsontype.Regex, bsoncore.AppendRegex(nil, regex.Pattern, regex.Options)}, |
| 88 | + {"dbpointer", dbPointer, bsontype.DBPointer, bsoncore.AppendDBPointer(nil, dbPointer.DB, dbPointer.Pointer)}, |
| 89 | + {"javascript", primitive.JavaScript("js"), bsontype.JavaScript, bsoncore.AppendJavaScript(nil, "js")}, |
| 90 | + {"symbol", primitive.Symbol("symbol"), bsontype.Symbol, bsoncore.AppendSymbol(nil, "symbol")}, |
| 91 | + {"code with scope", codeWithScope, bsontype.CodeWithScope, bsoncore.AppendCodeWithScope(nil, "code", scopeCore)}, |
| 92 | + {"int32", 5, bsontype.Int32, bsoncore.AppendInt32(nil, 5)}, |
| 93 | + {"int64", int64(5), bsontype.Int64, bsoncore.AppendInt64(nil, 5)}, |
| 94 | + {"timestamp", primitive.Timestamp{T: 1, I: 5}, bsontype.Timestamp, bsoncore.AppendTimestamp(nil, 1, 5)}, |
| 95 | + {"decimal128", decimal128, bsontype.Decimal128, bsoncore.AppendDecimal128(nil, decimal128)}, |
| 96 | + {"min key", primitive.MinKey{}, bsontype.MinKey, []byte{}}, |
| 97 | + {"max key", primitive.MaxKey{}, bsontype.MaxKey, []byte{}}, |
| 98 | + {"struct", structTest, bsontype.EmbeddedDocument, structCore}, |
| 99 | + {"interface", interfaceTest, bsontype.EmbeddedDocument, interfaceCore}, |
| 100 | + {"D", D{{"foo", 10}}, bsontype.EmbeddedDocument, structCore}, |
| 101 | + {"M", M{"foo": 10}, bsontype.EmbeddedDocument, structCore}, |
| 102 | + {"ValueMarshaler", marshalValueMarshaler{Foo: 10}, bsontype.Int32, bsoncore.AppendInt32(nil, 10)}, |
| 103 | + } |
| 104 | + |
| 105 | + t.Run("MarshalValue", func(t *testing.T) { |
| 106 | + for _, tc := range marshalValueTestCases { |
| 107 | + t.Run(tc.name, func(t *testing.T) { |
| 108 | + valueType, valueBytes, err := MarshalValue(tc.val) |
| 109 | + assert.Nil(t, err, "MarshalValue error: %v", err) |
| 110 | + compareMarshalValueResults(t, tc, valueType, valueBytes) |
| 111 | + }) |
| 112 | + } |
| 113 | + }) |
| 114 | + t.Run("MarshalValueAppend", func(t *testing.T) { |
| 115 | + for _, tc := range marshalValueTestCases { |
| 116 | + t.Run(tc.name, func(t *testing.T) { |
| 117 | + valueType, valueBytes, err := MarshalValueAppend(nil, tc.val) |
| 118 | + assert.Nil(t, err, "MarshalValueAppend error: %v", err) |
| 119 | + compareMarshalValueResults(t, tc, valueType, valueBytes) |
| 120 | + }) |
| 121 | + } |
| 122 | + }) |
| 123 | + t.Run("MarshalValueWithRegistry", func(t *testing.T) { |
| 124 | + for _, tc := range marshalValueTestCases { |
| 125 | + t.Run(tc.name, func(t *testing.T) { |
| 126 | + valueType, valueBytes, err := MarshalValueWithRegistry(DefaultRegistry, tc.val) |
| 127 | + assert.Nil(t, err, "MarshalValueWithRegistry error: %v", err) |
| 128 | + compareMarshalValueResults(t, tc, valueType, valueBytes) |
| 129 | + }) |
| 130 | + } |
| 131 | + }) |
| 132 | + t.Run("MarshalValueWithContext", func(t *testing.T) { |
| 133 | + ec := bsoncodec.EncodeContext{Registry: DefaultRegistry} |
| 134 | + for _, tc := range marshalValueTestCases { |
| 135 | + t.Run(tc.name, func(t *testing.T) { |
| 136 | + valueType, valueBytes, err := MarshalValueWithContext(ec, tc.val) |
| 137 | + assert.Nil(t, err, "MarshalValueWithContext error: %v", err) |
| 138 | + compareMarshalValueResults(t, tc, valueType, valueBytes) |
| 139 | + }) |
| 140 | + } |
| 141 | + }) |
| 142 | + t.Run("MarshalValueAppendWithRegistry", func(t *testing.T) { |
| 143 | + for _, tc := range marshalValueTestCases { |
| 144 | + t.Run(tc.name, func(t *testing.T) { |
| 145 | + valueType, valueBytes, err := MarshalValueAppendWithRegistry(DefaultRegistry, nil, tc.val) |
| 146 | + assert.Nil(t, err, "MarshalValueAppendWithRegistry error: %v", err) |
| 147 | + compareMarshalValueResults(t, tc, valueType, valueBytes) |
| 148 | + }) |
| 149 | + } |
| 150 | + }) |
| 151 | + t.Run("MarshalValueAppendWithContext", func(t *testing.T) { |
| 152 | + ec := bsoncodec.EncodeContext{Registry: DefaultRegistry} |
| 153 | + for _, tc := range marshalValueTestCases { |
| 154 | + t.Run(tc.name, func(t *testing.T) { |
| 155 | + valueType, valueBytes, err := MarshalValueAppendWithContext(ec, nil, tc.val) |
| 156 | + assert.Nil(t, err, "MarshalValueWithContext error: %v", err) |
| 157 | + compareMarshalValueResults(t, tc, valueType, valueBytes) |
| 158 | + }) |
| 159 | + } |
| 160 | + }) |
| 161 | +} |
| 162 | + |
| 163 | +func compareMarshalValueResults(t *testing.T, tc marshalValueTestCase, gotType bsontype.Type, gotBytes []byte) { |
| 164 | + t.Helper() |
| 165 | + expectedValue := RawValue{Type: tc.expectedType, Value: tc.expectedBytes} |
| 166 | + gotValue := RawValue{Type: gotType, Value: gotBytes} |
| 167 | + assert.Equal(t, expectedValue, gotValue, "value mismatch; expected %s, got %s", expectedValue, gotValue) |
| 168 | +} |
0 commit comments