Skip to content

Commit f30efd2

Browse files
authored
GODRIVER-3307 Simplify MarshalValue and UnmarshalValue test case setup. (#1914)
1 parent b296809 commit f30efd2

File tree

3 files changed

+195
-99
lines changed

3 files changed

+195
-99
lines changed

bson/marshal_value_cases_test.go

Lines changed: 159 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,169 @@ package bson
88

99
import (
1010
"io"
11-
"testing"
1211

13-
"go.mongodb.org/mongo-driver/v2/internal/assert"
1412
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
1513
)
1614

15+
var marshalValueTestCases = []marshalValueTestCase{
16+
{
17+
name: "double",
18+
val: 3.14,
19+
bsontype: TypeDouble,
20+
bytes: bsoncore.AppendDouble(nil, 3.14),
21+
},
22+
{
23+
name: "string",
24+
val: "hello world",
25+
bsontype: TypeString,
26+
bytes: bsoncore.AppendString(nil, "hello world"),
27+
},
28+
{
29+
name: "binary",
30+
val: Binary{1, []byte{1, 2}},
31+
bsontype: TypeBinary,
32+
bytes: bsoncore.AppendBinary(nil, 1, []byte{1, 2}),
33+
},
34+
{
35+
name: "undefined",
36+
val: Undefined{},
37+
bsontype: TypeUndefined,
38+
bytes: []byte{},
39+
},
40+
{
41+
name: "object id",
42+
val: ObjectID{103, 116, 166, 161, 70, 33, 67, 139, 164, 144, 255, 112},
43+
bsontype: TypeObjectID,
44+
bytes: bsoncore.AppendObjectID(nil, ObjectID{103, 116, 166, 161, 70, 33, 67, 139, 164, 144, 255, 112}),
45+
},
46+
{
47+
name: "boolean",
48+
val: true,
49+
bsontype: TypeBoolean,
50+
bytes: bsoncore.AppendBoolean(nil, true),
51+
},
52+
{
53+
name: "datetime",
54+
val: DateTime(5),
55+
bsontype: TypeDateTime,
56+
bytes: bsoncore.AppendDateTime(nil, 5),
57+
},
58+
{
59+
name: "null",
60+
val: Null{},
61+
bsontype: TypeNull,
62+
bytes: []byte{},
63+
},
64+
{
65+
name: "regex",
66+
val: Regex{Pattern: "pattern", Options: "imx"},
67+
bsontype: TypeRegex,
68+
bytes: bsoncore.AppendRegex(nil, "pattern", "imx"),
69+
},
70+
{
71+
name: "dbpointer",
72+
val: DBPointer{
73+
DB: "db",
74+
Pointer: ObjectID{103, 116, 166, 161, 70, 33, 67, 139, 164, 144, 255, 112},
75+
},
76+
bsontype: TypeDBPointer,
77+
bytes: bsoncore.AppendDBPointer(
78+
nil,
79+
"db",
80+
ObjectID{103, 116, 166, 161, 70, 33, 67, 139, 164, 144, 255, 112},
81+
),
82+
},
83+
{
84+
name: "javascript",
85+
val: JavaScript("js"),
86+
bsontype: TypeJavaScript,
87+
bytes: bsoncore.AppendJavaScript(nil, "js"),
88+
},
89+
{
90+
name: "symbol",
91+
val: Symbol("symbol"),
92+
bsontype: TypeSymbol,
93+
bytes: bsoncore.AppendSymbol(nil, "symbol"),
94+
},
95+
{
96+
name: "code with scope",
97+
val: CodeWithScope{Code: "code", Scope: D{{"a", "b"}}},
98+
bsontype: TypeCodeWithScope,
99+
bytes: bsoncore.AppendCodeWithScope(
100+
nil,
101+
"code",
102+
bsoncore.NewDocumentBuilder().
103+
AppendString("a", "b").
104+
Build()),
105+
},
106+
{
107+
name: "int32",
108+
val: 5,
109+
bsontype: TypeInt32,
110+
bytes: bsoncore.AppendInt32(nil, 5),
111+
},
112+
{
113+
name: "int64",
114+
val: int64(5),
115+
bsontype: TypeInt64,
116+
bytes: bsoncore.AppendInt64(nil, 5),
117+
},
118+
{
119+
name: "timestamp",
120+
val: Timestamp{T: 1, I: 5},
121+
bsontype: TypeTimestamp,
122+
bytes: bsoncore.AppendTimestamp(nil, 1, 5),
123+
},
124+
{
125+
name: "decimal128",
126+
val: NewDecimal128(5, 10),
127+
bsontype: TypeDecimal128,
128+
bytes: bsoncore.AppendDecimal128(nil, 5, 10),
129+
},
130+
{
131+
name: "min key",
132+
val: MinKey{},
133+
bsontype: TypeMinKey,
134+
bytes: []byte{},
135+
},
136+
{
137+
name: "max key",
138+
val: MaxKey{},
139+
bsontype: TypeMaxKey,
140+
bytes: []byte{},
141+
},
142+
{
143+
name: "struct",
144+
val: marshalValueStruct{Foo: 10},
145+
bsontype: TypeEmbeddedDocument,
146+
bytes: bsoncore.NewDocumentBuilder().
147+
AppendInt32("foo", 10).
148+
Build(),
149+
},
150+
{
151+
name: "D",
152+
val: D{{"foo", int32(10)}},
153+
bsontype: TypeEmbeddedDocument,
154+
bytes: bsoncore.NewDocumentBuilder().
155+
AppendInt32("foo", 10).
156+
Build(),
157+
},
158+
{
159+
name: "M",
160+
val: M{"foo": int32(10)},
161+
bsontype: TypeEmbeddedDocument,
162+
bytes: bsoncore.NewDocumentBuilder().
163+
AppendInt32("foo", 10).
164+
Build(),
165+
},
166+
{
167+
name: "ValueMarshaler",
168+
val: marshalValueMarshaler{Foo: 10},
169+
bsontype: TypeInt32,
170+
bytes: bsoncore.AppendInt32(nil, 10),
171+
},
172+
}
173+
17174
// helper type for testing MarshalValue that implements io.Reader
18175
type marshalValueInterfaceInner struct {
19176
Foo int
@@ -59,70 +216,3 @@ type marshalValueTestCase struct {
59216
bsontype Type
60217
bytes []byte
61218
}
62-
63-
func newMarshalValueTestCases(t *testing.T) []marshalValueTestCase {
64-
t.Helper()
65-
66-
var (
67-
oid = NewObjectID()
68-
regex = Regex{Pattern: "pattern", Options: "imx"}
69-
dbPointer = DBPointer{DB: "db", Pointer: NewObjectID()}
70-
codeWithScope = CodeWithScope{Code: "code", Scope: D{{"a", "b"}}}
71-
decimal128h, decimal128l = NewDecimal128(5, 10).GetBytes()
72-
structTest = marshalValueStruct{Foo: 10}
73-
)
74-
idx, scopeCore := bsoncore.AppendDocumentStart(nil)
75-
scopeCore = bsoncore.AppendStringElement(scopeCore, "a", "b")
76-
scopeCore, err := bsoncore.AppendDocumentEnd(scopeCore, idx)
77-
assert.Nil(t, err, "Document error: %v", err)
78-
structCore, err := Marshal(structTest)
79-
assert.Nil(t, err, "Marshal error: %v", err)
80-
81-
return []marshalValueTestCase{
82-
{"double", 3.14, TypeDouble, bsoncore.AppendDouble(nil, 3.14)},
83-
{"string", "hello world", TypeString, bsoncore.AppendString(nil, "hello world")},
84-
{"binary", Binary{1, []byte{1, 2}}, TypeBinary, bsoncore.AppendBinary(nil, 1, []byte{1, 2})},
85-
{"undefined", Undefined{}, TypeUndefined, []byte{}},
86-
{"object id", oid, TypeObjectID, bsoncore.AppendObjectID(nil, oid)},
87-
{"boolean", true, TypeBoolean, bsoncore.AppendBoolean(nil, true)},
88-
{"datetime", DateTime(5), TypeDateTime, bsoncore.AppendDateTime(nil, 5)},
89-
{"null", Null{}, TypeNull, []byte{}},
90-
{"regex", regex, TypeRegex, bsoncore.AppendRegex(nil, regex.Pattern, regex.Options)},
91-
{"dbpointer", dbPointer, TypeDBPointer, bsoncore.AppendDBPointer(nil, dbPointer.DB, dbPointer.Pointer)},
92-
{"javascript", JavaScript("js"), TypeJavaScript, bsoncore.AppendJavaScript(nil, "js")},
93-
{"symbol", Symbol("symbol"), TypeSymbol, bsoncore.AppendSymbol(nil, "symbol")},
94-
{"code with scope", codeWithScope, TypeCodeWithScope, bsoncore.AppendCodeWithScope(nil, "code", scopeCore)},
95-
{"int32", 5, TypeInt32, bsoncore.AppendInt32(nil, 5)},
96-
{"int64", int64(5), TypeInt64, bsoncore.AppendInt64(nil, 5)},
97-
{"timestamp", Timestamp{T: 1, I: 5}, TypeTimestamp, bsoncore.AppendTimestamp(nil, 1, 5)},
98-
{"decimal128", NewDecimal128(decimal128h, decimal128l), TypeDecimal128, bsoncore.AppendDecimal128(nil, decimal128h, decimal128l)},
99-
{"min key", MinKey{}, TypeMinKey, []byte{}},
100-
{"max key", MaxKey{}, TypeMaxKey, []byte{}},
101-
{"struct", structTest, TypeEmbeddedDocument, structCore},
102-
{"D", D{{"foo", int32(10)}}, TypeEmbeddedDocument, structCore},
103-
{"M", M{"foo": int32(10)}, TypeEmbeddedDocument, structCore},
104-
{"ValueMarshaler", marshalValueMarshaler{Foo: 10}, TypeInt32, bsoncore.AppendInt32(nil, 10)},
105-
}
106-
107-
}
108-
109-
func newMarshalValueTestCasesWithInterfaceCore(t *testing.T) []marshalValueTestCase {
110-
t.Helper()
111-
112-
marshalValueTestCases := newMarshalValueTestCases(t)
113-
114-
interfaceTest := marshalValueInterfaceOuter{
115-
Reader: marshalValueInterfaceInner{
116-
Foo: 10,
117-
},
118-
}
119-
interfaceCore, err := Marshal(interfaceTest)
120-
assert.Nil(t, err, "Marshal error: %v", err)
121-
122-
marshalValueTestCases = append(
123-
marshalValueTestCases,
124-
marshalValueTestCase{"interface", interfaceTest, TypeEmbeddedDocument, interfaceCore},
125-
)
126-
127-
return marshalValueTestCases
128-
}

bson/marshal_value_test.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,45 @@
77
package bson
88

99
import (
10+
"slices"
1011
"strings"
1112
"testing"
1213

1314
"go.mongodb.org/mongo-driver/v2/internal/assert"
1415
"go.mongodb.org/mongo-driver/v2/internal/require"
16+
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
1517
)
1618

1719
func TestMarshalValue(t *testing.T) {
1820
t.Parallel()
1921

20-
marshalValueTestCases := newMarshalValueTestCasesWithInterfaceCore(t)
21-
22-
t.Run("MarshalValue", func(t *testing.T) {
23-
t.Parallel()
22+
testCases := slices.Clone(marshalValueTestCases)
23+
testCases = append(testCases, marshalValueTestCase{
24+
name: "interface",
25+
val: marshalValueInterfaceOuter{
26+
Reader: marshalValueInterfaceInner{
27+
Foo: 10,
28+
},
29+
},
30+
bsontype: TypeEmbeddedDocument,
31+
bytes: bsoncore.NewDocumentBuilder().
32+
AppendDocument("reader", bsoncore.NewDocumentBuilder().
33+
AppendInt32("foo", 10).
34+
Build()).
35+
Build(),
36+
})
2437

25-
for _, tc := range marshalValueTestCases {
26-
tc := tc
38+
for _, tc := range testCases {
39+
tc := tc
2740

28-
t.Run(tc.name, func(t *testing.T) {
29-
t.Parallel()
41+
t.Run(tc.name, func(t *testing.T) {
42+
t.Parallel()
3043

31-
valueType, valueBytes, err := MarshalValue(tc.val)
32-
assert.Nil(t, err, "MarshalValue error: %v", err)
33-
compareMarshalValueResults(t, tc, valueType, valueBytes)
34-
})
35-
}
36-
})
44+
valueType, valueBytes, err := MarshalValue(tc.val)
45+
assert.Nil(t, err, "MarshalValue error: %v", err)
46+
compareMarshalValueResults(t, tc, valueType, valueBytes)
47+
})
48+
}
3749

3850
t.Run("returns distinct address ranges", func(t *testing.T) {
3951
// Call MarshalValue in a loop with the same large value (make sure to

bson/unmarshal_value_test.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,18 @@ import (
1919
func TestUnmarshalValue(t *testing.T) {
2020
t.Parallel()
2121

22-
unmarshalValueTestCases := newMarshalValueTestCases(t)
22+
for _, tc := range marshalValueTestCases {
23+
tc := tc
2324

24-
t.Run("UnmarshalValue", func(t *testing.T) {
25-
t.Parallel()
25+
t.Run(tc.name, func(t *testing.T) {
26+
t.Parallel()
2627

27-
for _, tc := range unmarshalValueTestCases {
28-
tc := tc
29-
30-
t.Run(tc.name, func(t *testing.T) {
31-
t.Parallel()
32-
33-
gotValue := reflect.New(reflect.TypeOf(tc.val))
34-
err := UnmarshalValue(tc.bsontype, tc.bytes, gotValue.Interface())
35-
assert.Nil(t, err, "UnmarshalValueWithRegistry error: %v", err)
36-
assert.Equal(t, tc.val, gotValue.Elem().Interface(), "value mismatch; expected %s, got %s", tc.val, gotValue.Elem())
37-
})
38-
}
39-
})
28+
gotValue := reflect.New(reflect.TypeOf(tc.val))
29+
err := UnmarshalValue(tc.bsontype, tc.bytes, gotValue.Interface())
30+
assert.Nil(t, err, "UnmarshalValueWithRegistry error: %v", err)
31+
assert.Equal(t, tc.val, gotValue.Elem().Interface(), "value mismatch; expected %s, got %s", tc.val, gotValue.Elem())
32+
})
33+
}
4034
}
4135

4236
// tests covering GODRIVER-2779

0 commit comments

Comments
 (0)