[#77] protogen: Add tests for JSON format
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
adb7c602d7
commit
7be31eb847
1 changed files with 68 additions and 33 deletions
|
@ -8,6 +8,7 @@ import (
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto/test"
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto/test"
|
||||||
generated "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto/test/custom"
|
generated "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto/test/custom"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"google.golang.org/protobuf/encoding/protojson"
|
||||||
goproto "google.golang.org/protobuf/proto"
|
goproto "google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -51,6 +52,7 @@ func TestStableMarshalSingle(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, tc := range marshalCases {
|
for _, tc := range marshalCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
t.Run("proto", func(t *testing.T) {
|
||||||
r := tc.input.MarshalProtobuf(nil)
|
r := tc.input.MarshalProtobuf(nil)
|
||||||
require.Equal(t, len(r), tc.input.StableSize())
|
require.Equal(t, len(r), tc.input.StableSize())
|
||||||
require.NotEmpty(t, r)
|
require.NotEmpty(t, r)
|
||||||
|
@ -62,22 +64,52 @@ func TestStableMarshalSingle(t *testing.T) {
|
||||||
require.NoError(t, actualFrostfs.UnmarshalProtobuf(r))
|
require.NoError(t, actualFrostfs.UnmarshalProtobuf(r))
|
||||||
require.Equal(t, tc.input, &actualFrostfs)
|
require.Equal(t, tc.input, &actualFrostfs)
|
||||||
|
|
||||||
// Compare each field directly, because proto-generated code has private fields.
|
primitivesEqual(t, tc.input, &actual)
|
||||||
require.Equal(t, tc.input.FieldA, actual.FieldA)
|
})
|
||||||
require.Equal(t, tc.input.FieldB, actual.FieldB)
|
t.Run("json", func(t *testing.T) {
|
||||||
require.Equal(t, tc.input.FieldC, actual.FieldC)
|
r, err := tc.input.MarshalJSON()
|
||||||
require.Equal(t, tc.input.FieldD, actual.FieldD)
|
require.NoError(t, err)
|
||||||
require.Equal(t, tc.input.FieldE, actual.FieldE)
|
require.NotEmpty(t, r)
|
||||||
require.Equal(t, tc.input.FieldF, actual.FieldF)
|
|
||||||
require.Equal(t, tc.input.FieldG, actual.FieldG)
|
var actual test.Primitives
|
||||||
require.Equal(t, tc.input.FieldI, actual.FieldI)
|
require.NoError(t, protojson.Unmarshal(r, &actual))
|
||||||
require.Equal(t, tc.input.FieldJ, actual.FieldJ)
|
|
||||||
require.Equal(t, tc.input.FieldK, actual.FieldK)
|
var actualFrostfs generated.Primitives
|
||||||
require.EqualValues(t, tc.input.FieldH, actual.FieldH)
|
require.NoError(t, actualFrostfs.UnmarshalJSON(r))
|
||||||
|
require.Equal(t, tc.input, &actualFrostfs)
|
||||||
|
|
||||||
|
primitivesEqual(t, tc.input, &actual)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func primitivesEqual(t *testing.T, a *generated.Primitives, b *test.Primitives) {
|
||||||
|
// Compare each field directly, because proto-generated code has private fields.
|
||||||
|
require.Equal(t, a.FieldA, b.FieldA)
|
||||||
|
require.Equal(t, a.FieldB, b.FieldB)
|
||||||
|
require.Equal(t, a.FieldC, b.FieldC)
|
||||||
|
require.Equal(t, a.FieldD, b.FieldD)
|
||||||
|
require.Equal(t, a.FieldE, b.FieldE)
|
||||||
|
require.Equal(t, a.FieldF, b.FieldF)
|
||||||
|
require.Equal(t, a.FieldG, b.FieldG)
|
||||||
|
require.Equal(t, a.FieldI, b.FieldI)
|
||||||
|
require.Equal(t, a.FieldJ, b.FieldJ)
|
||||||
|
require.Equal(t, a.FieldK, b.FieldK)
|
||||||
|
require.EqualValues(t, a.FieldH, b.FieldH)
|
||||||
|
}
|
||||||
|
|
||||||
|
func repPrimitivesEqual(t *testing.T, a *generated.RepPrimitives, b *test.RepPrimitives) {
|
||||||
|
// Compare each field directly, because proto-generated code has private fields.
|
||||||
|
require.Equal(t, a.FieldA, b.FieldA)
|
||||||
|
require.Equal(t, a.FieldB, b.FieldB)
|
||||||
|
require.Equal(t, a.FieldC, b.FieldC)
|
||||||
|
require.Equal(t, a.FieldD, b.FieldD)
|
||||||
|
require.Equal(t, a.FieldE, b.FieldE)
|
||||||
|
require.Equal(t, a.FieldF, b.FieldF)
|
||||||
|
require.Equal(t, a.FieldFu, b.FieldFu)
|
||||||
|
}
|
||||||
|
|
||||||
func randIntSlice[T protoInt](n int, includeZero bool) []T {
|
func randIntSlice[T protoInt](n int, includeZero bool) []T {
|
||||||
r := make([]T, n)
|
r := make([]T, n)
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
|
@ -142,21 +174,24 @@ func TestStableMarshalRep(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, tc := range marshalCases {
|
for _, tc := range marshalCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
t.Run("proto", func(t *testing.T) {
|
||||||
r := tc.input.MarshalProtobuf(nil)
|
r := tc.input.MarshalProtobuf(nil)
|
||||||
require.Equal(t, len(r), tc.input.StableSize())
|
require.Equal(t, len(r), tc.input.StableSize())
|
||||||
require.NotEmpty(t, r)
|
require.NotEmpty(t, r)
|
||||||
|
|
||||||
var actual test.RepPrimitives
|
var actual test.RepPrimitives
|
||||||
require.NoError(t, goproto.Unmarshal(r, &actual))
|
require.NoError(t, goproto.Unmarshal(r, &actual))
|
||||||
|
repPrimitivesEqual(t, tc.input, &actual)
|
||||||
|
})
|
||||||
|
t.Run("json", func(t *testing.T) {
|
||||||
|
r, err := tc.input.MarshalJSON()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotEmpty(t, r)
|
||||||
|
|
||||||
// Compare each field directly, because proto-generated code has private fields.
|
var actual test.RepPrimitives
|
||||||
require.Equal(t, tc.input.FieldA, actual.FieldA)
|
require.NoError(t, protojson.Unmarshal(r, &actual))
|
||||||
require.Equal(t, tc.input.FieldB, actual.FieldB)
|
repPrimitivesEqual(t, tc.input, &actual)
|
||||||
require.Equal(t, tc.input.FieldC, actual.FieldC)
|
})
|
||||||
require.Equal(t, tc.input.FieldD, actual.FieldD)
|
|
||||||
require.Equal(t, tc.input.FieldE, actual.FieldE)
|
|
||||||
require.Equal(t, tc.input.FieldF, actual.FieldF)
|
|
||||||
require.Equal(t, tc.input.FieldFu, actual.FieldFu)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue