test: chain getmessage cli command

I also added some helper functions for mocking in the types/mock pkg
This commit is contained in:
Nikola Divic
2022-02-09 17:29:29 +01:00
parent a923d7c884
commit e797ec138d
3 changed files with 67 additions and 10 deletions
+3 -1
View File
@@ -301,6 +301,8 @@ var ChainGetMsgCmd = &cli.Command{
Usage: "Get and print a message by its cid",
ArgsUsage: "[messageCid]",
Action: func(cctx *cli.Context) error {
afmt := NewAppFmt(cctx.App)
if !cctx.Args().Present() {
return fmt.Errorf("must pass a cid of a message to get")
}
@@ -339,7 +341,7 @@ var ChainGetMsgCmd = &cli.Command{
return err
}
fmt.Println(string(enc))
afmt.Println(string(enc))
return nil
},
}
+35
View File
@@ -150,6 +150,7 @@ func TestChainDeleteObj(t *testing.T) {
})
}
// TestChainStatObj checks if "chain delete-obj" prints size and IPLD link counts for object, respecting the --base flag
func TestChainStatObj(t *testing.T) {
cmd := WithCategory("chain", ChainStatObjCmd)
block := mock.MkBlock(nil, 0, 0)
@@ -199,3 +200,37 @@ func TestChainStatObj(t *testing.T) {
checkOutput(buf)
})
}
// TestChainGetMsg checks if "chain getmessage" properly decodes and serializes as JSON a Message fetched from the IPLD store
func TestChainGetMsg(t *testing.T) {
app, mockApi, buf, done := newMockAppWithFullAPI(t, WithCategory("chain", ChainGetMsgCmd))
defer done()
from, err := mock.RandomActorAddress()
assert.NoError(t, err)
to, err := mock.RandomActorAddress()
assert.NoError(t, err)
msg := mock.UnsignedMessage(*from, *to, 0)
obj := new(bytes.Buffer)
err = msg.MarshalCBOR(obj)
assert.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
gomock.InOrder(
mockApi.EXPECT().ChainReadObj(ctx, msg.Cid()).Return(obj.Bytes(), nil),
)
err = app.Run([]string{"chain", "getmessage", msg.Cid().String()})
assert.NoError(t, err)
var out types.Message
err = json.Unmarshal(buf.Bytes(), &out)
assert.NoError(t, err)
assert.Equal(t, *msg, out)
}