-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodecs_test.go
More file actions
129 lines (112 loc) · 3.43 KB
/
Copy pathcodecs_test.go
File metadata and controls
129 lines (112 loc) · 3.43 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
package codecs
import (
"bytes"
"fmt"
"reflect"
"testing"
"time"
"github.com/gogo/protobuf/types"
"github.com/golang/protobuf/jsonpb"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/bit-one/mgd-proto/pmongo"
"github.com/bit-one/mgd-proto/test"
)
func TestCodecs(t *testing.T) {
rb := bson.NewRegistryBuilder()
r := Register(rb).Build()
tm := time.Now()
// BSON accuracy is in milliseconds
tm = time.Date(tm.Year(), tm.Month(), tm.Day(), tm.Hour(), tm.Minute(), tm.Second(),
(tm.Nanosecond()/1000000)*1000000, tm.Location())
ts, err := types.TimestampProto(tm)
if err != nil {
t.Errorf("ptypes.TimestampProto error = %v", err)
return
}
objectID := primitive.NewObjectID()
id := pmongo.NewObjectId(objectID)
t.Run("primitive object id", func(t *testing.T) {
resultID, err := id.GetObjectID()
if err != nil {
t.Errorf("mongodb.ObjectId.GetPrimitiveObjectID() error = %v", err)
return
}
if !reflect.DeepEqual(objectID, resultID) {
t.Errorf("failed: primitive object ID=%#v, ID=%#v", objectID, id)
return
}
})
in := test.Data{
BoolValue: &types.BoolValue{Value: true},
BytesValue: &types.BytesValue{Value: make([]byte, 5)},
DoubleValue: &types.DoubleValue{Value: 1.2},
FloatValue: &types.FloatValue{Value: 1.3},
Int32Value: &types.Int32Value{Value: -12345},
Int64Value: &types.Int64Value{Value: -123456789},
StringValue: &types.StringValue{Value: "qwerty"},
Uint32Value: &types.UInt32Value{Value: 12345},
Uint64Value: &types.UInt64Value{Value: 123456789},
Timestamp: ts,
Id: id,
Listvalue: &types.ListValue{
Values: []*types.Value{
&types.Value{Kind: &types.Value_StringValue{StringValue: "a"}},
&types.Value{Kind: &types.Value_NumberValue{NumberValue: 1.0}},
&types.Value{Kind: &types.Value_BoolValue{BoolValue: false}},
&types.Value{Kind: &types.Value_NullValue{NullValue: types.NullValue_NULL_VALUE}},
&types.Value{Kind: &types.Value_StructValue{
StructValue: &types.Struct{
Fields: map[string]*types.Value{
"helo": &types.Value{Kind: &types.Value_StringValue{StringValue: "world"}},
"gee": &types.Value{Kind: &types.Value_NumberValue{NumberValue: 3.0}},
},
},
}},
&types.Value{Kind: &types.Value_ListValue{
ListValue: &types.ListValue{
Values: []*types.Value{
&types.Value{Kind: &types.Value_StringValue{StringValue: "b"}},
&types.Value{Kind: &types.Value_NumberValue{NumberValue: 2.0}},
},
},
}},
},
},
}
fmt.Println(in)
t.Run("marshal/unmarshal", func(t *testing.T) {
b, err := bson.MarshalWithRegistry(r, &in)
if err != nil {
t.Errorf("bson.MarshalWithRegistry error = %v", err)
return
}
var out test.Data
if err = bson.UnmarshalWithRegistry(r, b, &out); err != nil {
t.Errorf("bson.UnmarshalWithRegistry error = %v", err)
return
}
fmt.Println(out)
if !reflect.DeepEqual(in, out) {
t.Errorf("failed: in=%#v, out=%#v", in, out)
return
}
})
t.Run("marshal-jsonpb/unmarshal-jsonpb", func(t *testing.T) {
var b bytes.Buffer
m := &jsonpb.Marshaler{}
if err := m.Marshal(&b, &in); err != nil {
t.Errorf("jsonpb.Marshaler.Marshal error = %v", err)
return
}
var out test.Data
if err = jsonpb.Unmarshal(&b, &out); err != nil {
t.Errorf("jsonpb.Unmarshal error = %v", err)
return
}
if !reflect.DeepEqual(in, out) {
t.Errorf("failed: in=%#v, out=%#v", in, out)
return
}
})
}