2020-03-23 06:01:59 +00:00
|
|
|
package proto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
pb_struct "github.com/golang/protobuf/ptypes/struct"
|
|
|
|
)
|
|
|
|
|
2020-03-30 09:42:22 +00:00
|
|
|
func Test_ToPBStruct(t *testing.T) {
|
|
|
|
type obj struct {
|
2020-04-21 15:00:32 +00:00
|
|
|
AggregateID string
|
|
|
|
Seq uint64
|
2020-03-30 09:42:22 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
obj obj
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
length int
|
|
|
|
result obj
|
2020-03-23 06:01:59 +00:00
|
|
|
}{
|
2020-03-30 09:42:22 +00:00
|
|
|
{
|
|
|
|
name: "to pb stuct",
|
|
|
|
args: args{
|
2020-04-21 15:00:32 +00:00
|
|
|
obj: obj{AggregateID: "AggregateID", Seq: 12345},
|
2020-03-30 09:42:22 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
length: 2,
|
2020-04-21 15:00:32 +00:00
|
|
|
result: obj{AggregateID: "AggregateID", Seq: 12345},
|
2020-03-30 09:42:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty struct",
|
|
|
|
args: args{
|
|
|
|
obj: obj{},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
length: 2,
|
2020-04-21 15:00:32 +00:00
|
|
|
result: obj{AggregateID: "", Seq: 0},
|
2020-03-30 09:42:22 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
fields, err := ToPBStruct(tt.args.obj)
|
|
|
|
if tt.wantErr && err == nil {
|
|
|
|
t.Errorf("got wrong result, should get err: actual: %v ", err)
|
|
|
|
}
|
|
|
|
if !tt.wantErr && len(fields.Fields) != tt.length {
|
|
|
|
t.Errorf("got wrong result length, expecting: %v, actual: %v ", tt.length, len(fields.Fields))
|
|
|
|
}
|
2020-04-21 15:00:32 +00:00
|
|
|
if !tt.wantErr && tt.result.AggregateID != fields.Fields["AggregateID"].GetStringValue() {
|
|
|
|
t.Errorf("got wrong result, AggregateID should be same: expecting: %v, actual: %v ", tt.result.AggregateID, fields.Fields["AggregateID"].GetStringValue())
|
2020-03-30 09:42:22 +00:00
|
|
|
}
|
|
|
|
if !tt.wantErr && int(tt.result.Seq) != int(fields.Fields["Seq"].GetNumberValue()) {
|
|
|
|
t.Errorf("got wrong result, Seq should be same: expecting: %v, actual: %v ", tt.result.Seq, fields.Fields["Seq"].GetStringValue())
|
|
|
|
}
|
|
|
|
})
|
2020-03-23 06:01:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 09:42:22 +00:00
|
|
|
func Test_FromPBStruct(t *testing.T) {
|
|
|
|
type obj struct {
|
2020-04-21 15:00:32 +00:00
|
|
|
AggregateID string
|
|
|
|
Seq uint64
|
2020-03-30 09:42:22 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
obj *obj
|
|
|
|
fields *pb_struct.Struct
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
result obj
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "from pb stuct",
|
|
|
|
args: args{
|
|
|
|
obj: &obj{},
|
|
|
|
fields: &pb_struct.Struct{Fields: map[string]*pb_struct.Value{
|
2020-04-21 15:00:32 +00:00
|
|
|
"AggregateID": &pb_struct.Value{Kind: &pb_struct.Value_StringValue{StringValue: "AggregateID"}},
|
|
|
|
"Seq": &pb_struct.Value{Kind: &pb_struct.Value_NumberValue{NumberValue: 12345}},
|
2020-03-30 09:42:22 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
2020-04-21 15:00:32 +00:00
|
|
|
result: obj{AggregateID: "AggregateID", Seq: 12345},
|
2020-03-30 09:42:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no fields",
|
|
|
|
args: args{
|
2020-04-21 15:00:32 +00:00
|
|
|
obj: &obj{},
|
|
|
|
fields: &pb_struct.Struct{Fields: map[string]*pb_struct.Value{}},
|
2020-03-30 09:42:22 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
2020-04-21 15:00:32 +00:00
|
|
|
result: obj{AggregateID: "", Seq: 0},
|
2020-03-30 09:42:22 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
err := FromPBStruct(tt.args.obj, tt.args.fields)
|
|
|
|
if tt.wantErr && err == nil {
|
|
|
|
t.Errorf("got wrong result, should get err: actual: %v ", err)
|
|
|
|
}
|
2020-04-21 15:00:32 +00:00
|
|
|
if !tt.wantErr && tt.result.AggregateID != tt.args.obj.AggregateID {
|
|
|
|
t.Errorf("got wrong result, AggregateID should be same: expecting: %v, actual: %v ", tt.result.AggregateID, tt.args.obj.AggregateID)
|
2020-03-30 09:42:22 +00:00
|
|
|
}
|
|
|
|
if !tt.wantErr && int(tt.result.Seq) != int(tt.args.obj.Seq) {
|
|
|
|
t.Errorf("got wrong result, Seq should be same: expecting: %v, actual: %v ", tt.result.Seq, tt.args.obj.Seq)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-03-23 06:01:59 +00:00
|
|
|
}
|