Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add unit tests #47

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 66 additions & 65 deletions keeper/vote_extension_test.go
Original file line number Diff line number Diff line change
@@ -1,77 +1,78 @@
package keeper_test

// import (
// abci "github.com/cometbft/cometbft/abci/types"
// store "github.com/vitwit/avail-da-module/keeper"
// )
import (
abci "github.com/cometbft/cometbft/abci/types"
store "github.com/vitwit/avail-da-module/keeper"
)

// func (s *TestSuite) TestExtendVoteHandler() {
func (s *TestSuite) TestExtendVoteHandler() {
testCases := []struct {
name string
startHeight uint64
endHeight uint64
availHeight uint64
blobStatus uint32
currentHeight int64
voteEndHeight uint64
isLastVoting bool
expectErr bool
}{
{
"blob status is not in voting state",
uint64(1),
uint64(20),
uint64(30),
uint32(3),
int64(22),
uint64(20),
false,
false,
},
{
"blob status is in voting state",
uint64(1),
uint64(20),
uint64(30),
uint32(2),
int64(30),
uint64(31),
true,
false,
},
}

// testCases := []struct {
// name string
// startHeight uint64
// endHeight uint64
// availHeight uint64
// blobStatus uint32
// currentHeight int64
// voteEndHeight uint64
// expectErr bool
// }{
// {
// "blob status is not in voting state",
// uint64(1),
// uint64(20),
// uint64(30),
// uint32(1),
// int64(22),
// uint64(20),
// false,
// },
// {
// "blob status is in voting state",
// uint64(1),
// uint64(20),
// uint64(30),
// uint32(2),
// int64(30),
// uint64(31),
// false,
// },
// }
for _, tc := range testCases {
s.Run(tc.name, func() {
s.ctx = s.ctx.WithBlockHeight(tc.currentHeight)

// for _, tc := range testCases {
// s.Run(tc.name, func() {
// s.ctx = s.ctx.WithBlockHeight(tc.currentHeight)
err := store.UpdateStartHeight(s.ctx, s.store, tc.startHeight)
s.Require().NoError(err)

// err := store.UpdateStartHeight(s.ctx, s.store, tc.startHeight)
// s.Require().NoError(err)
err = store.UpdateEndHeight(s.ctx, s.store, tc.endHeight)
s.Require().NoError(err)

// err = store.UpdateEndHeight(s.ctx, s.store, tc.endHeight)
// s.Require().NoError(err)
err = store.UpdateAvailHeight(s.ctx, s.store, tc.availHeight)
s.Require().NoError(err)

// err = store.UpdateAvailHeight(s.ctx, s.store, tc.availHeight)
// s.Require().NoError(err)
err = store.UpdateBlobStatus(s.ctx, s.store, tc.blobStatus)
s.Require().NoError(err)

// err = store.UpdateBlobStatus(s.ctx, s.store, tc.blobStatus)
// s.Require().NoError(err)
err = store.UpdateVotingEndHeight(s.ctx, s.store, tc.voteEndHeight, tc.isLastVoting)
s.Require().NoError(err)

// err = store.UpdateVotingEndHeight(s.ctx, s.store, tc.voteEndHeight)
// s.Require().NoError(err)
extendVoteHandler := s.voteExtensionHandler.ExtendVoteHandler()

// extendVoteHandler := s.voteExtensionHandler.ExtendVoteHandler()
req := &abci.RequestExtendVote{
ProposerAddress: s.addrs[1],
}

// req := &abci.RequestExtendVote{
// ProposerAddress: s.addrs[1],
// }

// res, err := extendVoteHandler(s.ctx, req)
// if tc.expectErr {
// s.Require().Error(err)
// } else {
// s.Require().NoError(err)
// s.Require().NotNil(res)
// }
// })
// }

// }
res, err := extendVoteHandler(s.ctx, req)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().NotNil(res)
}
})
}
}
87 changes: 87 additions & 0 deletions relayer/avail/light_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package avail_test

import (
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
avail "github.com/vitwit/avail-da-module/relayer/avail"
httpClient "github.com/vitwit/avail-da-module/relayer/http"
)

func newMockLightClient(serverURL string) *avail.LightClient {
httpClient := httpClient.NewHandler()
return avail.NewLightClient(serverURL, httpClient)
}

// Test IsDataAvailable
func TestLightClient_IsDataAvailable(t *testing.T) {
expectedBlockHeight := 100
expectedBase64Data := base64.StdEncoding.EncodeToString([]byte("cosmos-block-data"))

mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
block := avail.Block{
Block: 100,
Extrinsics: []avail.Extrinsics{
{Data: expectedBase64Data},
},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(block)
}))
defer mockServer.Close()

lightClient := newMockLightClient(mockServer.URL)

isAvailable, err := lightClient.IsDataAvailable([]byte("cosmos-block-data"), expectedBlockHeight)
assert.NoError(t, err, "expected no error")
assert.True(t, isAvailable, "expected data to be available")
}

func TestLightClient_GetBlock(t *testing.T) {
expectedBlockHeight := 100
expectedExtrinsics := []avail.Extrinsics{
{Data: base64.StdEncoding.EncodeToString([]byte("cosmos-block-data"))},
}

mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
block := avail.Block{
Block: int64(expectedBlockHeight),
Extrinsics: expectedExtrinsics,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(block)
}))
defer mockServer.Close()

lightClient := newMockLightClient(mockServer.URL)

block, err := lightClient.GetBlock(expectedBlockHeight)
assert.NoError(t, err, "expected no error")
assert.Equal(t, expectedBlockHeight, int(block.Block), "expected block height to match")
assert.Equal(t, expectedExtrinsics, block.Extrinsics, "expected extrinsics to match")
}

func TestLightClient_Submit(t *testing.T) {
expectedBlockMetaData := avail.BlockMetaData{
BlockNumber: 100,
BlockHash: "testhash",
Hash: "datahash",
Index: 1,
}

mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(expectedBlockMetaData)
}))
defer mockServer.Close()

lightClient := newMockLightClient(mockServer.URL)

blockMeta, err := lightClient.Submit([]byte("testdata"))
assert.NoError(t, err, "expected no error")
assert.Equal(t, expectedBlockMetaData, blockMeta, "expected block metadata to match")
}
111 changes: 111 additions & 0 deletions relayer/avail/mocks/DA.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading