-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathbsonutil_test.go
More file actions
185 lines (170 loc) · 4.33 KB
/
bsonutil_test.go
File metadata and controls
185 lines (170 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package bsonutil
import (
"math"
"testing"
"time"
"github.com/mongodb/mongo-tools/common/testtype"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func TestBson2Float64(t *testing.T) {
testtype.SkipUnlessTestType(t, testtype.UnitTestType)
assert := assert.New(t)
decimalVal, _ := primitive.ParseDecimal128("-1")
tests := []struct {
in interface{}
expected float64
isSuccess bool
description string
}{
{int32(1), 1.0, true, "int32"},
{int64(1), 1.0, true, "int64"},
{1.0, 1.0, true, "float"},
{decimalVal, float64(-1), true, "decimal128"},
{"invalid value", 0, false, "invalid float value"},
}
for _, test := range tests {
result, ok := Bson2Float64(test.in)
if test.isSuccess {
assert.True(ok, "%s converted to float64", test.description)
} else {
assert.False(ok, "%s did not convert to float64", test.description)
}
assert.Equal(test.expected, result, test.description)
}
}
// It'd be good to test the case where IsEqual returns an error, but it's not
// clear if this can actually happen in practice. Internally, these errors can
// only occur when the call to `bson.Marshal()` fails. But the type signature
// for IsEqual means that we are always passing `bson.D` values to
// `bson.Marshal()`, and I don't think those can cause marshaling errors.
func TestIsEqual(t *testing.T) {
testtype.SkipUnlessTestType(t, testtype.UnitTestType)
assert := assert.New(t)
tests := []struct {
left bson.D
right bson.D
isEqual bool
description string
}{
{
bson.D{{"hello", int64(42)}},
bson.D{{"hello", int64(42)}},
true,
"identical documents with int64 keys",
},
{
bson.D{{"hello", int64(42)}},
bson.D{{"hello", int32(42)}},
false,
"documents have same keys but values have different types",
},
{
bson.D{{"foo", "bar"}, {"baz", "buz"}},
bson.D{{"baz", "buz"}, {"foo", "bar"}},
false,
"document has same key/value pairs but in different order",
},
{
bson.D{{"hello", primitive.DateTime(42)}},
bson.D{{"hello", primitive.DateTime(42)}},
true,
"identical documents with datetime keys",
},
{
bson.D{{"hello", primitive.DateTime(42)}},
bson.D{{"hello", primitive.DateTime(999)}},
false,
"same key but different datetime value",
},
}
for _, test := range tests {
isEq, err := IsEqual(test.left, test.right)
if assert.NoError(err) {
if test.isEqual {
assert.True(isEq, test.description)
} else {
assert.False(isEq)
}
}
}
}
func TestMarshalExtJSONReversible(t *testing.T) {
testtype.SkipUnlessTestType(t, testtype.UnitTestType)
tests := []struct {
val any
reversible bool
expectedJSON string
}{
{
bson.M{"field1": bson.M{"$date": 1257894000000}},
true,
`{"field1":{"$date":{"$numberLong":"1257894000000"}}}`,
},
{
bson.M{"field1": time.Unix(1257894000, 0)},
true,
`{"field1":{"$date":{"$numberLong":"1257894000000"}}}`,
},
{
bson.M{"field1": bson.M{"$date": "invalid"}},
false,
``,
},
}
for _, test := range tests {
json, err := MarshalExtJSONReversible(
test.val,
true, /* canonical */
false, /* escapeHTML */
)
if !test.reversible {
assert.ErrorContains(t, err, "marshal is not reversible")
} else {
assert.NoError(t, err)
}
assert.Equal(t, test.expectedJSON, string(json))
}
}
func TestMarshalExtJSONWithBSONRoundtripConsistency(t *testing.T) {
testtype.SkipUnlessTestType(t, testtype.UnitTestType)
tests := []struct {
val any
consistentAfterRoundtripping bool
expectedJSON string
}{
{
bson.M{"field1": bson.M{"grapes": int64(123)}},
true,
`{"field1":{"grapes":{"$numberLong":"123"}}}`,
},
{
bson.M{"field1": bson.M{"$date": 1257894000000}},
false,
``,
},
{
bson.M{"field1": bson.M{"nanField": math.NaN()}},
true,
`{"field1":{"nanField":{"$numberDouble":"NaN"}}}`,
},
}
for _, test := range tests {
json, err := MarshalExtJSONWithBSONRoundtripConsistency(
test.val,
true, /* canonical */
false, /* escapeHTML */
)
if !test.consistentAfterRoundtripping {
assert.ErrorContains(
t,
err,
"marshaling BSON to ExtJSON and back resulted in discrepancies",
)
} else {
assert.NoError(t, err)
}
assert.Equal(t, test.expectedJSON, string(json))
}
}