1
2
3
4
5
6
7 package bson
8
9 import (
10 "errors"
11 "fmt"
12 "reflect"
13 "testing"
14
15 "go.mongodb.org/mongo-driver/bson/bsoncodec"
16 "go.mongodb.org/mongo-driver/bson/bsontype"
17 "go.mongodb.org/mongo-driver/internal/assert"
18 "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
19 )
20
21 func TestRawValue(t *testing.T) {
22 t.Parallel()
23
24 t.Run("Unmarshal", func(t *testing.T) {
25 t.Parallel()
26
27 t.Run("Uses registry attached to value", func(t *testing.T) {
28 t.Parallel()
29
30 reg := bsoncodec.NewRegistryBuilder().Build()
31 val := RawValue{Type: bsontype.String, Value: bsoncore.AppendString(nil, "foobar"), r: reg}
32 var s string
33 want := bsoncodec.ErrNoDecoder{Type: reflect.TypeOf(s)}
34 got := val.Unmarshal(&s)
35 if !compareErrors(got, want) {
36 t.Errorf("Expected errors to match. got %v; want %v", got, want)
37 }
38 })
39 t.Run("Uses default registry if no registry attached", func(t *testing.T) {
40 t.Parallel()
41
42 want := "foobar"
43 val := RawValue{Type: bsontype.String, Value: bsoncore.AppendString(nil, want)}
44 var got string
45 err := val.Unmarshal(&got)
46 noerr(t, err)
47 if got != want {
48 t.Errorf("Expected strings to match. got %s; want %s", got, want)
49 }
50 })
51 })
52 t.Run("UnmarshalWithRegistry", func(t *testing.T) {
53 t.Parallel()
54
55 t.Run("Returns error when registry is nil", func(t *testing.T) {
56 t.Parallel()
57
58 want := ErrNilRegistry
59 var val RawValue
60 got := val.UnmarshalWithRegistry(nil, &D{})
61 if !errors.Is(got, want) {
62 t.Errorf("Expected errors to match. got %v; want %v", got, want)
63 }
64 })
65 t.Run("Returns lookup error", func(t *testing.T) {
66 t.Parallel()
67
68 reg := bsoncodec.NewRegistryBuilder().Build()
69 var val RawValue
70 var s string
71 want := bsoncodec.ErrNoDecoder{Type: reflect.TypeOf(s)}
72 got := val.UnmarshalWithRegistry(reg, &s)
73 if !compareErrors(got, want) {
74 t.Errorf("Expected errors to match. got %v; want %v", got, want)
75 }
76 })
77 t.Run("Returns DecodeValue error", func(t *testing.T) {
78 t.Parallel()
79
80 reg := NewRegistryBuilder().Build()
81 val := RawValue{Type: bsontype.Double, Value: bsoncore.AppendDouble(nil, 3.14159)}
82 var s string
83 want := fmt.Errorf("cannot decode %v into a string type", bsontype.Double)
84 got := val.UnmarshalWithRegistry(reg, &s)
85 if !compareErrors(got, want) {
86 t.Errorf("Expected errors to match. got %v; want %v", got, want)
87 }
88 })
89 t.Run("Success", func(t *testing.T) {
90 t.Parallel()
91
92 reg := NewRegistryBuilder().Build()
93 want := float64(3.14159)
94 val := RawValue{Type: bsontype.Double, Value: bsoncore.AppendDouble(nil, want)}
95 var got float64
96 err := val.UnmarshalWithRegistry(reg, &got)
97 noerr(t, err)
98 if got != want {
99 t.Errorf("Expected results to match. got %g; want %g", got, want)
100 }
101 })
102 })
103 t.Run("UnmarshalWithContext", func(t *testing.T) {
104 t.Parallel()
105
106 t.Run("Returns error when DecodeContext is nil", func(t *testing.T) {
107 t.Parallel()
108
109 want := ErrNilContext
110 var val RawValue
111 got := val.UnmarshalWithContext(nil, &D{})
112 if !errors.Is(got, want) {
113 t.Errorf("Expected errors to match. got %v; want %v", got, want)
114 }
115 })
116 t.Run("Returns lookup error", func(t *testing.T) {
117 t.Parallel()
118
119 dc := bsoncodec.DecodeContext{Registry: bsoncodec.NewRegistryBuilder().Build()}
120 var val RawValue
121 var s string
122 want := bsoncodec.ErrNoDecoder{Type: reflect.TypeOf(s)}
123 got := val.UnmarshalWithContext(&dc, &s)
124 if !compareErrors(got, want) {
125 t.Errorf("Expected errors to match. got %v; want %v", got, want)
126 }
127 })
128 t.Run("Returns DecodeValue error", func(t *testing.T) {
129 t.Parallel()
130
131 dc := bsoncodec.DecodeContext{Registry: NewRegistryBuilder().Build()}
132 val := RawValue{Type: bsontype.Double, Value: bsoncore.AppendDouble(nil, 3.14159)}
133 var s string
134 want := fmt.Errorf("cannot decode %v into a string type", bsontype.Double)
135 got := val.UnmarshalWithContext(&dc, &s)
136 if !compareErrors(got, want) {
137 t.Errorf("Expected errors to match. got %v; want %v", got, want)
138 }
139 })
140 t.Run("Success", func(t *testing.T) {
141 t.Parallel()
142
143 dc := bsoncodec.DecodeContext{Registry: NewRegistryBuilder().Build()}
144 want := float64(3.14159)
145 val := RawValue{Type: bsontype.Double, Value: bsoncore.AppendDouble(nil, want)}
146 var got float64
147 err := val.UnmarshalWithContext(&dc, &got)
148 noerr(t, err)
149 if got != want {
150 t.Errorf("Expected results to match. got %g; want %g", got, want)
151 }
152 })
153 })
154
155 t.Run("IsZero", func(t *testing.T) {
156 t.Parallel()
157
158 tests := []struct {
159 name string
160 val RawValue
161 want bool
162 }{
163 {
164 name: "empty",
165 val: RawValue{},
166 want: true,
167 },
168 {
169 name: "zero type but non-zero value",
170 val: RawValue{
171 Type: 0x00,
172 Value: bsoncore.AppendInt32(nil, 0),
173 },
174 want: false,
175 },
176 {
177 name: "zero type and zero value",
178 val: RawValue{
179 Type: 0x00,
180 Value: bsoncore.AppendInt32(nil, 0),
181 },
182 },
183 {
184 name: "non-zero type and non-zero value",
185 val: RawValue{
186 Type: bsontype.String,
187 Value: bsoncore.AppendString(nil, "foobar"),
188 },
189 want: false,
190 },
191 {
192 name: "non-zero type and zero value",
193 val: RawValue{
194 Type: bsontype.String,
195 Value: bsoncore.AppendString(nil, "foobar"),
196 },
197 },
198 }
199
200 for _, tt := range tests {
201 tt := tt
202 t.Run(tt.name, func(t *testing.T) {
203 t.Parallel()
204
205 assert.Equal(t, tt.want, tt.val.IsZero())
206 })
207 }
208 })
209 }
210
View as plain text