Skip to content

Commit 4bc4835

Browse files
prestonvasquezmatthewdaleqingyang-hu
authored
GODRIVER-3544, GODRIVER-3653 Allow Client to Send Client Metadata On-Demand (#2197)
Co-authored-by: Matt Dale <[email protected]> Co-authored-by: Qingyang Hu <[email protected]>
1 parent 8fb0643 commit 4bc4835

19 files changed

+1737
-329
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (C) MongoDB, Inc. 2025-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 assertbson
8+
9+
import (
10+
"go.mongodb.org/mongo-driver/v2/bson"
11+
"go.mongodb.org/mongo-driver/v2/internal/assert"
12+
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
13+
)
14+
15+
type tHelper interface {
16+
Helper()
17+
}
18+
19+
// EqualDocument asserts that the expected and actual BSON documents are equal.
20+
// If the documents are not equal, it prints both the binary diff and Extended
21+
// JSON representation of the BSON documents.
22+
func EqualDocument(t assert.TestingT, expected, actual []byte) bool {
23+
if h, ok := t.(tHelper); ok {
24+
h.Helper()
25+
}
26+
27+
return assert.Equal(t,
28+
expected,
29+
actual,
30+
`expected and actual BSON documents do not match
31+
As Extended JSON:
32+
Expected: %s
33+
Actual : %s`,
34+
bson.Raw(expected),
35+
bson.Raw(actual))
36+
}
37+
38+
// EqualValue asserts that the expected and actual BSON values are equal. If the
39+
// values are not equal, it prints both the binary diff and Extended JSON
40+
// representation of the BSON values.
41+
func EqualValue[T bson.RawValue | bsoncore.Value](t assert.TestingT, expected, actual T) bool {
42+
if h, ok := t.(tHelper); ok {
43+
h.Helper()
44+
}
45+
46+
return assert.Equal(t,
47+
expected,
48+
actual,
49+
`expected and actual BSON values do not match
50+
As Extended JSON:
51+
Expected: %s
52+
Actual : %s`,
53+
expected,
54+
actual)
55+
}
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
// Copyright (C) MongoDB, Inc. 2025-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 assertbson
8+
9+
import (
10+
"testing"
11+
12+
"go.mongodb.org/mongo-driver/v2/bson"
13+
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
14+
)
15+
16+
func TestEqualDocument(t *testing.T) {
17+
t.Parallel()
18+
19+
testCases := []struct {
20+
name string
21+
expected []byte
22+
actual []byte
23+
want bool
24+
}{
25+
{
26+
name: "equal bson.Raw",
27+
expected: bson.Raw{5, 0, 0, 0, 0},
28+
actual: bson.Raw{5, 0, 0, 0, 0},
29+
want: true,
30+
},
31+
{
32+
name: "different bson.Raw",
33+
expected: bson.Raw{8, 0, 0, 0, 10, 120, 0, 0},
34+
actual: bson.Raw{5, 0, 0, 0, 0},
35+
want: false,
36+
},
37+
{
38+
name: "invalid bson.Raw",
39+
expected: bson.Raw{99, 99, 99, 99},
40+
actual: bson.Raw{5, 0, 0, 0, 0},
41+
want: false,
42+
},
43+
{
44+
name: "nil bson.Raw",
45+
expected: bson.Raw(nil),
46+
actual: bson.Raw(nil),
47+
want: true,
48+
},
49+
}
50+
51+
for _, tc := range testCases {
52+
tc := tc // Capture range variable.
53+
54+
t.Run(tc.name, func(t *testing.T) {
55+
t.Parallel()
56+
57+
got := EqualDocument(new(testing.T), tc.expected, tc.actual)
58+
if got != tc.want {
59+
t.Errorf("EqualDocument(%#v, %#v) = %v, want %v", tc.expected, tc.actual, got, tc.want)
60+
}
61+
})
62+
}
63+
}
64+
65+
func TestEqualValue(t *testing.T) {
66+
t.Parallel()
67+
68+
t.Run("bson.RawValue", func(t *testing.T) {
69+
t.Parallel()
70+
71+
testCases := []struct {
72+
name string
73+
expected bson.RawValue
74+
actual bson.RawValue
75+
want bool
76+
}{
77+
{
78+
name: "equal",
79+
expected: bson.RawValue{
80+
Type: bson.TypeInt32,
81+
Value: []byte{1, 0, 0, 0},
82+
},
83+
actual: bson.RawValue{
84+
Type: bson.TypeInt32,
85+
Value: []byte{1, 0, 0, 0},
86+
},
87+
want: true,
88+
},
89+
{
90+
name: "same type, different value",
91+
expected: bson.RawValue{
92+
Type: bson.TypeInt32,
93+
Value: []byte{1, 0, 0, 0},
94+
},
95+
actual: bson.RawValue{
96+
Type: bson.TypeInt32,
97+
Value: []byte{1, 1, 1, 1},
98+
},
99+
want: false,
100+
},
101+
{
102+
name: "same value, different type",
103+
expected: bson.RawValue{
104+
Type: bson.TypeDouble,
105+
Value: []byte{1, 0, 0, 0, 0, 0, 0, 0},
106+
},
107+
actual: bson.RawValue{
108+
Type: bson.TypeInt64,
109+
Value: []byte{1, 0, 0, 0, 0, 0, 0, 0},
110+
},
111+
want: false,
112+
},
113+
{
114+
name: "different value, different type",
115+
expected: bson.RawValue{
116+
Type: bson.TypeInt32,
117+
Value: []byte{1, 0, 0, 0},
118+
},
119+
actual: bson.RawValue{
120+
Type: bson.TypeString,
121+
Value: []byte{1, 1, 1, 1},
122+
},
123+
want: false,
124+
},
125+
{
126+
name: "invalid",
127+
expected: bson.RawValue{
128+
Type: bson.TypeInt64,
129+
Value: []byte{1, 0, 0, 0},
130+
},
131+
actual: bson.RawValue{
132+
Type: bson.TypeInt32,
133+
Value: []byte{1, 0, 0, 0},
134+
},
135+
want: false,
136+
},
137+
{
138+
name: "empty",
139+
expected: bson.RawValue{},
140+
actual: bson.RawValue{},
141+
want: true,
142+
},
143+
}
144+
145+
for _, tc := range testCases {
146+
tc := tc // Capture range variable.
147+
148+
t.Run(tc.name, func(t *testing.T) {
149+
t.Parallel()
150+
151+
got := EqualValue(new(testing.T), tc.expected, tc.actual)
152+
if got != tc.want {
153+
t.Errorf("EqualValue(%#v, %#v) = %v, want %v", tc.expected, tc.actual, got, tc.want)
154+
}
155+
})
156+
}
157+
})
158+
159+
t.Run("bsoncore.Value", func(t *testing.T) {
160+
t.Parallel()
161+
162+
testCases := []struct {
163+
name string
164+
expected bsoncore.Value
165+
actual bsoncore.Value
166+
want bool
167+
}{
168+
{
169+
name: "equal",
170+
expected: bsoncore.Value{
171+
Type: bsoncore.TypeInt32,
172+
Data: []byte{1, 0, 0, 0},
173+
},
174+
actual: bsoncore.Value{
175+
Type: bsoncore.TypeInt32,
176+
Data: []byte{1, 0, 0, 0},
177+
},
178+
want: true,
179+
},
180+
{
181+
name: "same type, different value",
182+
expected: bsoncore.Value{
183+
Type: bsoncore.TypeInt32,
184+
Data: []byte{1, 0, 0, 0},
185+
},
186+
actual: bsoncore.Value{
187+
Type: bsoncore.TypeInt32,
188+
Data: []byte{1, 1, 1, 1},
189+
},
190+
want: false,
191+
},
192+
{
193+
name: "same value, different type",
194+
expected: bsoncore.Value{
195+
Type: bsoncore.TypeDouble,
196+
Data: []byte{1, 0, 0, 0, 0, 0, 0, 0},
197+
},
198+
actual: bsoncore.Value{
199+
Type: bsoncore.TypeInt64,
200+
Data: []byte{1, 0, 0, 0, 0, 0, 0, 0},
201+
},
202+
want: false,
203+
},
204+
{
205+
name: "different value, different type",
206+
expected: bsoncore.Value{
207+
Type: bsoncore.TypeInt32,
208+
Data: []byte{1, 0, 0, 0},
209+
},
210+
actual: bsoncore.Value{
211+
Type: bsoncore.TypeString,
212+
Data: []byte{1, 1, 1, 1},
213+
},
214+
want: false,
215+
},
216+
{
217+
name: "invalid",
218+
expected: bsoncore.Value{
219+
Type: bsoncore.TypeInt64,
220+
Data: []byte{1, 0, 0, 0},
221+
},
222+
actual: bsoncore.Value{
223+
Type: bsoncore.TypeInt32,
224+
Data: []byte{1, 0, 0, 0},
225+
},
226+
want: false,
227+
},
228+
{
229+
name: "empty",
230+
expected: bsoncore.Value{},
231+
actual: bsoncore.Value{},
232+
want: true,
233+
},
234+
}
235+
236+
for _, tc := range testCases {
237+
tc := tc // Capture range variable.
238+
239+
t.Run(tc.name, func(t *testing.T) {
240+
t.Parallel()
241+
242+
got := EqualValue(new(testing.T), tc.expected, tc.actual)
243+
if got != tc.want {
244+
t.Errorf("EqualValue(%#v, %#v) = %v, want %v", tc.expected, tc.actual, got, tc.want)
245+
}
246+
})
247+
}
248+
})
249+
}

internal/assert/assertion_mongo.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ package assert
1111

1212
import (
1313
"context"
14-
"fmt"
1514
"reflect"
1615
"time"
1716
"unsafe"
@@ -71,26 +70,6 @@ func DifferentAddressRanges(t TestingT, a, b []byte) (ok bool) {
7170
return false
7271
}
7372

74-
// EqualBSON asserts that the expected and actual BSON binary values are equal.
75-
// If the values are not equal, it prints both the binary and Extended JSON diff
76-
// of the BSON values. The provided BSON value types must implement the
77-
// fmt.Stringer interface.
78-
func EqualBSON(t TestingT, expected, actual interface{}) bool {
79-
if h, ok := t.(tHelper); ok {
80-
h.Helper()
81-
}
82-
83-
return Equal(t,
84-
expected,
85-
actual,
86-
`expected and actual BSON values do not match
87-
As Extended JSON:
88-
Expected: %s
89-
Actual : %s`,
90-
expected.(fmt.Stringer).String(),
91-
actual.(fmt.Stringer).String())
92-
}
93-
9473
// Soon runs the provided callback and fails the passed-in test if the callback
9574
// does not complete within timeout. The provided callback should respect the
9675
// passed-in context and cease execution when it has expired.

0 commit comments

Comments
 (0)