-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotomongo_test.go
234 lines (222 loc) · 6.98 KB
/
protomongo_test.go
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package protomongo_test
import (
"context"
"reflect"
"testing"
"github.com/BenBirt/protomongo"
pb "github.com/BenBirt/protomongo/example"
mongodb "github.com/BenBirt/protomongo/mongodb/testing"
"github.com/golang/protobuf/proto"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
var (
simpleMessage = &pb.SimpleMessage{
StringField: "foo",
Int32Field: 32525,
Int64Field: 1531541553141312315,
FloatField: 21541.3242,
DoubleField: 21535215136361617136.543858,
BoolField: true,
EnumField: pb.Enum_VAL_2,
}
tests = []struct {
name string
pb proto.Message
equivalentPbs []proto.Message
}{
{
name: "simple message",
pb: simpleMessage,
equivalentPbs: []proto.Message{
&pb.RepeatedFieldMessage{
StringField: []string{"foo"},
Int32Field: []int32{32525},
Int64Field: []int64{1531541553141312315},
FloatField: []float32{21541.3242},
DoubleField: []float64{21535215136361617136.543858},
BoolField: []bool{true},
EnumField: []pb.Enum{pb.Enum_VAL_2},
},
},
},
{
name: "message with repeated fields",
pb: &pb.RepeatedFieldMessage{
StringField: []string{"foo", "bar"},
Int32Field: []int32{32525, 1958, 435},
Int64Field: []int64{1531541553141312315, 13512516266},
FloatField: []float32{21541.3242, 634214.2233, 3435.322},
DoubleField: []float64{21535215136361617136.543858, 213143343.76767},
BoolField: []bool{true, false, true, true},
EnumField: []pb.Enum{pb.Enum_VAL_2, pb.Enum_VAL_1},
},
equivalentPbs: []proto.Message{
&pb.SimpleMessage{
StringField: "bar",
Int32Field: 435,
Int64Field: 13512516266,
FloatField: 3435.322,
DoubleField: 213143343.76767,
BoolField: true,
EnumField: pb.Enum_VAL_1,
},
},
},
{
name: "message with submessage",
pb: &pb.MessageWithSubMessage{
StringField: "baz",
SimpleMessage: &pb.SimpleMessage{
StringField: "foo",
Int32Field: 32525,
Int64Field: 1531541553141312315,
FloatField: 21541.3242,
DoubleField: 21535215136361617136.543858,
BoolField: true,
EnumField: pb.Enum_VAL_2,
},
},
equivalentPbs: []proto.Message{
&pb.MessageWithRepeatedSubMessage{
StringField: "baz",
SimpleMessage: []*pb.SimpleMessage{
{
StringField: "foo",
Int32Field: 32525,
Int64Field: 1531541553141312315,
FloatField: 21541.3242,
DoubleField: 21535215136361617136.543858,
BoolField: true,
EnumField: pb.Enum_VAL_2,
},
},
},
},
},
{
name: "message with repeated submessage",
pb: &pb.MessageWithRepeatedSubMessage{
StringField: "baz",
SimpleMessage: []*pb.SimpleMessage{
{
StringField: "foo",
Int32Field: 32525,
Int64Field: 1531541553141312315,
FloatField: 21541.3242,
DoubleField: 21535215136361617136.543858,
BoolField: true,
EnumField: pb.Enum_VAL_2,
},
{
StringField: "qux",
Int32Field: 22,
BoolField: false,
},
},
},
equivalentPbs: []proto.Message{
&pb.MessageWithSubMessage{
StringField: "baz",
SimpleMessage: &pb.SimpleMessage{
StringField: "qux",
Int32Field: 22,
Int64Field: 1531541553141312315,
FloatField: 21541.3242,
DoubleField: 21535215136361617136.543858,
// It might be expected that because the last element of the 'SimpleMessage' slice in 'pb' explicitly sets 'BoolField' to false,
// this field should also be false, because the elements of the 'SimpleMessage' slice should be merged in order.
// However, by the rules of proto3, default field values are never serialized. Thus when the second element
// of the 'SimpleMessage' slice is deserialized, that deserialized value contains no value for 'BoolField', and thus
// this field retains the value that was set in the first element of that slice.
BoolField: true,
EnumField: pb.Enum_VAL_2,
},
},
},
},
{
name: "message with oneof",
pb: &pb.MessageWithOneof{
StringField: "baz",
OneofField: &pb.MessageWithOneof_Int32OneofField{3132},
},
equivalentPbs: []proto.Message{},
},
}
)
func TestAgainstRealDatabase(t *testing.T) {
db, stopMongo := startMongoDatabase(t)
defer stopMongo()
coll := db.Collection("test_collection")
if _, err := coll.InsertOne(context.Background(), simpleMessage); err != nil {
t.Errorf("coll.InsertOne(%v) error = %v, want nil", simpleMessage, err)
}
count, err := coll.CountDocuments(context.Background(), bson.D{})
if err != nil {
t.Errorf("coll.CountDocuments() error = %v, want nil", err)
}
if count != 1 {
t.Errorf("coll.CountDocuments() = %v; want 1", count)
}
var found *pb.SimpleMessage
if err := coll.FindOne(context.Background(), bson.D{}).Decode(&found); err != nil {
t.Errorf("coll.FindOne().Decode() error = %v, want nil", err)
}
if !proto.Equal(simpleMessage, found) {
t.Errorf("proto.Equal(%v, %v) = false, want true", simpleMessage, found)
}
}
// TODO: Add a testcase looking up protos by fields/nested fields.
func TestMarshalUnmarshal(t *testing.T) {
rb := bson.NewRegistryBuilder()
rb.RegisterCodec(reflect.TypeOf((*proto.Message)(nil)).Elem(), protomongo.NewProtobufCodec())
reg := rb.Build()
for _, testCase := range tests {
b, err := bson.MarshalWithRegistry(reg, testCase.pb)
if err != nil {
t.Errorf("bson.MarshalWithRegistry(%v) error = %v, want nil", testCase.pb, err)
}
for _, equivalentPb := range append(testCase.equivalentPbs, testCase.pb) {
out := reflect.New(reflect.TypeOf(equivalentPb).Elem()).Interface().(proto.Message)
if err = bson.UnmarshalWithRegistry(reg, b, &out); err != nil {
t.Errorf("bson.UnmarshalWithRegistry(%v) error = %v, want nil", b, err)
}
if !proto.Equal(equivalentPb, out) {
t.Errorf("proto.Equal(%v, %v) = false, want true", equivalentPb, out)
}
}
}
}
func TestMarshalUnmarshalWithPointers(t *testing.T) {
rb := bson.NewRegistryBuilder()
rb.RegisterCodec(reflect.TypeOf((*proto.Message)(nil)).Elem(), protomongo.NewProtobufCodec())
reg := rb.Build()
for _, testCase := range tests {
b, err := bson.MarshalWithRegistry(reg, testCase.pb)
if err != nil {
t.Errorf("bson.MarshalWithRegistry(%v) error = %v, want nil", testCase.pb, err)
}
for _, equivalentPb := range append(testCase.equivalentPbs, testCase.pb) {
out := reflect.New(reflect.TypeOf(equivalentPb).Elem()).Interface().(proto.Message)
if err = bson.UnmarshalWithRegistry(reg, b, &out); err != nil {
t.Errorf("bson.UnmarshalWithRegistry(%v) error = %v, want nil", b, err)
}
if !proto.Equal(equivalentPb, out) {
t.Errorf("proto.Equal(%v, %v) = false, want true", equivalentPb, out)
}
}
}
}
func startMongoDatabase(t *testing.T) (*mongo.Database, func()) {
mongod := &mongodb.Mongod{}
if err := mongod.Start(); err != nil {
t.Fatal(err)
}
m, err := mongod.GetClient()
if err != nil {
t.Fatal(err)
}
d := m.Database("test_db")
return d, func() { mongod.Stop() }
}