frostfs-api-go/rpc/message/test/message.go
Airat Arifullin bc16a32c24
All checks were successful
Tests and linters / Tests (1.19) (pull_request) Successful in 45s
Tests and linters / Lint (pull_request) Successful in 56s
Tests and linters / Tests (1.20) (pull_request) Successful in 2m3s
Tests and linters / Tests with -race (pull_request) Successful in 2m13s
[#40] types: Generate StableMarshaler/StableSize methods for protobufs
* Add plugin option for protogen in Makefile
* Fix the generator for the plugin in util/protogen
* Erase convertable types, move helpful methods to gRPC protobufs
* Erase helpers for convertations
* Generate StableMarshlal/StableSize for protobufs by the protoc plugin

Signed-off-by: Airat Arifullin a.arifullin@yadro.com
2023-07-10 12:08:48 +03:00

46 lines
1.1 KiB
Go

package messagetest
import (
"fmt"
"testing"
utilproto "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
func TestRPCMessage(t *testing.T, msgGens ...func(empty bool) proto.Message) {
for _, msgGen := range msgGens {
msg1 := msgGen(false)
t.Run("encoding", func(t *testing.T) {
t.Run(fmt.Sprintf("JSON_%T", msg1), func(t *testing.T) {
data, err := protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
msg1,
)
require.NoError(t, err)
msg2 := msgGen(true)
require.NoError(t, protojson.Unmarshal(data, msg2))
require.True(t, proto.Equal(msg1, msg2))
})
if bm, ok := msg1.(utilproto.StableMarshaller); ok {
t.Run(fmt.Sprintf("Stable_%T", msg1), func(t *testing.T) {
data := bm.StableMarshal(nil)
require.Len(t, data, bm.StableSize())
msg2 := msgGen(true)
require.NoError(t, proto.Unmarshal(data, msg2))
require.True(t, proto.Equal(msg1, msg2))
})
}
})
}
}