From 1a29a7f17bb095c67457d4f537c7421bbad13977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 28 Aug 2024 23:43:02 +0100 Subject: [PATCH] api: add test to show existing protoFormat behavior --- api/helpers_test.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/api/helpers_test.go b/api/helpers_test.go index 02038a659..21eb82820 100644 --- a/api/helpers_test.go +++ b/api/helpers_test.go @@ -3,11 +3,16 @@ package api import ( "bytes" "encoding/json" + "strings" "testing" "github.com/ethereum/go-ethereum/common" + qt "github.com/frankban/quicktest" "github.com/google/go-cmp/cmp" "go.vocdoni.io/dvote/types" + "go.vocdoni.io/proto/build/go/models" + "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/proto" ) func TestAPIHelpers_encodeEVMResultsArgs(t *testing.T) { @@ -149,3 +154,31 @@ func TestConvertKeysToCamel(t *testing.T) { t.Fatal(diff) } } + +func TestProtoFormat(t *testing.T) { + wantJSON := strings.TrimSpace(` +{ + "setProcess": { + "txtype": "SET_PROCESS_CENSUS", + "nonce": 1, + "processId": "sx3/YYFNq5DWw6m2XWyQgwSA5Lda0y50eUICAAAAAAA=", + "censusRoot": "zUU9BcTLBCnuXuGu/tAW9VO4AmtM7VsMNSkFv6U8foE=", + "censusURI": "ipfs://bafybeicyfukarcryrvy5oe37ligmxwf55sbfiojori4t25wencma4ymxfa", + "censusSize": "1000" + } +} +`) + var ptx models.Tx + err := protojson.Unmarshal([]byte(wantJSON), &ptx) + qt.Assert(t, err, qt.IsNil) + + asProto, err := proto.Marshal(&ptx) + qt.Assert(t, err, qt.IsNil) + + var dst bytes.Buffer + err = json.Indent(&dst, []byte(protoFormat(asProto)), "", "\t") + qt.Assert(t, err, qt.IsNil) + gotJSON := dst.String() + + qt.Assert(t, gotJSON, qt.Equals, wantJSON) +}