Skip to content

Commit

Permalink
api: add test to show existing protoFormat behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdan authored and p4u committed Sep 3, 2024
1 parent 50c3f98 commit 1a29a7f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions api/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

0 comments on commit 1a29a7f

Please sign in to comment.