Skip to content

Commit

Permalink
add test cases for cross chain apps
Browse files Browse the repository at this point in the history
  • Loading branch information
yutianwu committed Jul 28, 2023
1 parent 48b87a2 commit 09e36fb
Show file tree
Hide file tree
Showing 4 changed files with 658 additions and 1 deletion.
156 changes: 156 additions & 0 deletions x/storage/keeper/cross_app_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,162 @@ func (s *TestSuite) TestSynCreateBucket() {
s.Require().NoError(res.Err)
}

func (s *TestSuite) TestSynMirrorBucket() {
ctrl := gomock.NewController(s.T())
storageKeeper := types.NewMockStorageKeeper(ctrl)
storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes()

app := keeper.NewBucketApp(storageKeeper)
synPackage := types.MirrorBucketSynPackage{
Owner: sample.RandAccAddress(),
Id: big.NewInt(10),
}

serializedSynPack, err := synPackage.Serialize()
s.Require().NoError(err)
serializedSynPack = append([]byte{types.OperationMirrorBucket}, serializedSynPack...)

// case 1: normal case
res := app.ExecuteSynPackage(s.ctx, nil, serializedSynPack)
s.Require().NoError(res.Err)
}

func (s *TestSuite) TestAckMirrorBucket() {
ctrl := gomock.NewController(s.T())
storageKeeper := types.NewMockStorageKeeper(ctrl)
storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes()

app := keeper.NewBucketApp(storageKeeper)
ackPackage := types.MirrorBucketAckPackage{
Status: types.StatusSuccess,
Id: big.NewInt(10),
}

serializedAckPack, err := ackPackage.Serialize()
s.Require().NoError(err)
serializedAckPack = append([]byte{types.OperationMirrorBucket}, serializedAckPack...)

// case 1: bucket not found
storageKeeper.EXPECT().GetBucketInfoById(gomock.Any(), gomock.Any()).Return(nil, false)

res := app.ExecuteAckPackage(s.ctx, nil, serializedAckPack)
s.Require().ErrorIs(res.Err, types.ErrNoSuchBucket)

// case 2: success case
storageKeeper.EXPECT().GetBucketInfoById(gomock.Any(), gomock.Any()).Return(&types.BucketInfo{}, true)
storageKeeper.EXPECT().SetBucketInfo(gomock.Any(), gomock.Any()).Return()

res = app.ExecuteAckPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedAckPack)
s.Require().NoError(res.Err)
}

func (s *TestSuite) TestAckCreateBucket() {
ctrl := gomock.NewController(s.T())
storageKeeper := types.NewMockStorageKeeper(ctrl)
storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes()

app := keeper.NewBucketApp(storageKeeper)
ackPackage := types.CreateBucketAckPackage{
Status: types.StatusSuccess,
Id: big.NewInt(10),
Creator: sample.RandAccAddress(),
ExtraData: []byte("extra data"),
}

serializedAckPack := ackPackage.MustSerialize()
serializedAckPack = append([]byte{types.OperationCreateBucket}, serializedAckPack...)

// case 1: normal case
res := app.ExecuteAckPackage(s.ctx, nil, serializedAckPack)
s.Require().NoError(res.Err)
}

func (s *TestSuite) TestAckDeleteBucket() {
ctrl := gomock.NewController(s.T())
storageKeeper := types.NewMockStorageKeeper(ctrl)
storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes()

app := keeper.NewBucketApp(storageKeeper)
ackPackage := types.DeleteBucketAckPackage{
Status: types.StatusSuccess,
Id: big.NewInt(10),
ExtraData: []byte("extra data"),
}

serializedAckPack := ackPackage.MustSerialize()
serializedAckPack = append([]byte{types.OperationDeleteBucket}, serializedAckPack...)

// case 1: normal case
res := app.ExecuteAckPackage(s.ctx, nil, serializedAckPack)
s.Require().NoError(res.Err)
}

func (s *TestSuite) TestFailAckMirrorBucket() {
ctrl := gomock.NewController(s.T())
storageKeeper := types.NewMockStorageKeeper(ctrl)
storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes()

app := keeper.NewBucketApp(storageKeeper)
ackPackage := types.MirrorBucketSynPackage{
Id: big.NewInt(10),
Owner: sample.RandAccAddress(),
}

serializedAckPack, err := ackPackage.Serialize()
s.Require().NoError(err)
serializedAckPack = append([]byte{types.OperationMirrorBucket}, serializedAckPack...)

// case 1: bucket not found
storageKeeper.EXPECT().GetBucketInfoById(gomock.Any(), gomock.Any()).Return(&types.BucketInfo{}, false)

res := app.ExecuteFailAckPackage(s.ctx, nil, serializedAckPack)
s.Require().ErrorIs(res.Err, types.ErrNoSuchBucket)

// case 2: normal case
storageKeeper.EXPECT().GetBucketInfoById(gomock.Any(), gomock.Any()).Return(&types.BucketInfo{}, true)
storageKeeper.EXPECT().SetBucketInfo(gomock.Any(), gomock.Any()).Return()

res = app.ExecuteFailAckPackage(s.ctx, &sdk.CrossChainAppContext{}, serializedAckPack)
s.Require().NoError(res.Err)
}

func (s *TestSuite) TestFailAckCreateBucket() {
ctrl := gomock.NewController(s.T())
storageKeeper := types.NewMockStorageKeeper(ctrl)
storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes()

app := keeper.NewBucketApp(storageKeeper)
createSynPackage := types.CreateBucketSynPackage{
Creator: sample.RandAccAddress(),
BucketName: "bucketname",
ExtraData: []byte("extra data"),
PaymentAddress: sample.RandAccAddress(),
PrimarySpAddress: sample.RandAccAddress(),
}
serializedSynPackage := createSynPackage.MustSerialize()
serializedSynPackage = append([]byte{types.OperationCreateBucket}, serializedSynPackage...)

// case 1: normal case
res := app.ExecuteFailAckPackage(s.ctx, nil, serializedSynPackage)
s.Require().NoError(res.Err)
}

func (s *TestSuite) TestFailAckDeleteBucket() {
ctrl := gomock.NewController(s.T())
storageKeeper := types.NewMockStorageKeeper(ctrl)
storageKeeper.EXPECT().Logger(gomock.Any()).Return(s.ctx.Logger()).AnyTimes()

app := keeper.NewBucketApp(storageKeeper)
deleteSynPackage := types.DeleteBucketSynPackage{
Operator: sample.RandAccAddress(),
Id: big.NewInt(10),
ExtraData: []byte("extra data"),
}

serializedSynPackage := deleteSynPackage.MustSerialize()
serializedSynPackage = append([]byte{types.OperationDeleteBucket}, serializedSynPackage...)

// case 1: normal case
res := app.ExecuteFailAckPackage(s.ctx, nil, serializedSynPackage)
s.Require().NoError(res.Err)
}
Loading

0 comments on commit 09e36fb

Please sign in to comment.