Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Sep 10, 2024
1 parent 0a39fd8 commit 16d21c4
Show file tree
Hide file tree
Showing 6 changed files with 375 additions and 4 deletions.
6 changes: 3 additions & 3 deletions testutil/keeper/mocks/emissions/observer.go

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

190 changes: 190 additions & 0 deletions x/observer/keeper/ballot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,193 @@ func TestKeeper_GetAllBallots(t *testing.T) {
require.Equal(t, 1, len(ballots))
require.Equal(t, b, ballots[0])
}

func TestKeeper_DeleteBallot(t *testing.T) {
t.Run("delete existing ballot", func(t *testing.T) {
//Arrange
k, ctx, _, _ := keepertest.ObserverKeeper(t)
identifier := sample.ZetaIndex(t)
b := &types.Ballot{
BallotIdentifier: identifier,
}
k.SetBallot(ctx, b)
_, found := k.GetBallot(ctx, identifier)
require.True(t, found)

//Act
k.DeleteBallot(ctx, identifier)

//Assert
_, found = k.GetBallot(ctx, identifier)
require.False(t, found)
})

t.Run("delete non-existing ballot,nothing happens", func(t *testing.T) {
//Arrange
k, ctx, _, _ := keepertest.ObserverKeeper(t)
identifier := sample.ZetaIndex(t)
numberOfBallots := 10
for i := 0; i < numberOfBallots; i++ {
k.SetBallot(ctx, &types.Ballot{
BallotIdentifier: sample.ZetaIndex(t),
})
}

require.Len(t, k.GetAllBallots(ctx), numberOfBallots)

//Act
k.DeleteBallot(ctx, identifier)

//Assert
_, found := k.GetBallot(ctx, identifier)
require.False(t, found)
require.Len(t, k.GetAllBallots(ctx), numberOfBallots)
})
}

func TestKeeper_DeleteBallotList(t *testing.T) {
t.Run("delete existing ballot list", func(t *testing.T) {
//Arrange
k, ctx, _, _ := keepertest.ObserverKeeper(t)
numberOfBallotLists := 10
for i := 0; i < numberOfBallotLists; i++ {
k.AddBallotToList(ctx, types.Ballot{
Index: sample.ZetaIndex(t),
BallotCreationHeight: 1,
})
}

_, found := k.GetBallotList(ctx, 1)
require.True(t, found)

//Act
k.DeleteBallotList(ctx, 1)

//Assert
_, found = k.GetBallotList(ctx, 1)
require.False(t, found)
})

t.Run("delete non-existing ballot list, nothing happens", func(t *testing.T) {
//Arrange
k, ctx, _, _ := keepertest.ObserverKeeper(t)
numberOfBallotLists := 10
for i := 0; i < numberOfBallotLists; i++ {
k.AddBallotToList(ctx, types.Ballot{
Index: sample.ZetaIndex(t),
BallotCreationHeight: 1,
})
}

_, found := k.GetBallotList(ctx, 1)
require.True(t, found)

//Act
k.DeleteBallotList(ctx, 2)

//Assert
_, found = k.GetBallotList(ctx, 1)
require.True(t, found)
})
}

func TestKeeper_ClearMaturedBallots(t *testing.T) {
t.Run("clear matured ballots successfully", func(t *testing.T) {
//Arrange
k, ctx, _, _ := keepertest.ObserverKeeper(t)
numberOfBallots := 10
ballots := make([]types.Ballot, numberOfBallots)
for i := 0; i < numberOfBallots; i++ {
b := types.Ballot{
BallotIdentifier: sample.ZetaIndex(t),
BallotCreationHeight: 1,
}
k.AddBallotToList(ctx, b)
k.SetBallot(ctx, &b)
ballots[i] = b
}
_, found := k.GetBallotList(ctx, 1)
require.True(t, found)
require.Equal(t, numberOfBallots, len(k.GetAllBallots(ctx)))

//Act
k.ClearMaturedBallots(ctx, ballots, 0)

//Assert
for _, b := range ballots {
_, found = k.GetBallot(ctx, b.BallotIdentifier)
require.False(t, found)
}
_, found = k.GetBallotList(ctx, 0)
require.False(t, found)
eventCount := 0
for _, event := range ctx.EventManager().Events() {
if event.Type == "zetachain.zetacore.observer.EventBallotDeleted" {
eventCount++
}
}
require.Equal(t, numberOfBallots, eventCount)
})

t.Run("clear only ballotList if no ballots are found", func(t *testing.T) {
//Arrange
k, ctx, _, _ := keepertest.ObserverKeeper(t)
numberOfBallots := 10
ballots := make([]types.Ballot, numberOfBallots)
for i := 0; i < numberOfBallots; i++ {
b := types.Ballot{
BallotIdentifier: sample.ZetaIndex(t),
BallotCreationHeight: 1,
}
k.AddBallotToList(ctx, b)
ballots[i] = b
}
_, found := k.GetBallotList(ctx, 1)
require.True(t, found)
require.Equal(t, 0, len(k.GetAllBallots(ctx)))

//Act
k.ClearMaturedBallots(ctx, []types.Ballot{}, 0)

//Assert
_, found = k.GetBallotList(ctx, 1)
require.False(t, found)
require.Equal(t, 0, len(k.GetAllBallots(ctx)))
})

t.Run("clear only ballots successfully if ballotList is not found", func(t *testing.T) {
//Arrange
k, ctx, _, _ := keepertest.ObserverKeeper(t)
numberOfBallots := 10
ballots := make([]types.Ballot, numberOfBallots)
for i := 0; i < numberOfBallots; i++ {
b := types.Ballot{
BallotIdentifier: sample.ZetaIndex(t),
BallotCreationHeight: 1,
}
k.SetBallot(ctx, &b)
ballots[i] = b
}
require.Equal(t, numberOfBallots, len(k.GetAllBallots(ctx)))
_, found := k.GetBallotList(ctx, 1)
require.False(t, found)

//Act
k.ClearMaturedBallots(ctx, ballots, 0)

//Assert
for _, b := range ballots {
_, found := k.GetBallot(ctx, b.BallotIdentifier)
require.False(t, found)
}
_, found = k.GetBallotList(ctx, 1)
require.False(t, found)
eventCount := 0
for _, event := range ctx.EventManager().Events() {
if event.Type == "zetachain.zetacore.observer.EventBallotDeleted" {
eventCount++
}
}
require.Equal(t, numberOfBallots, eventCount)
})
}
75 changes: 75 additions & 0 deletions x/observer/keeper/events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package keeper_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
keepertest "github.com/zeta-chain/node/testutil/keeper"
"github.com/zeta-chain/node/testutil/sample"
"github.com/zeta-chain/node/x/observer/keeper"
"github.com/zeta-chain/node/x/observer/types"
)

func TestEmitEventBallotDeleted(t *testing.T) {
tt := []struct {
name string
ballotIdentifier string
ballotType types.ObservationType
voters []string
voteType []types.VoteType
}{
{
name: "successfull votes only",
ballotIdentifier: sample.ZetaIndex(t),
ballotType: types.ObservationType_InboundTx,
voters: []string{"voter1", "voter2"},
voteType: []types.VoteType{types.VoteType_SuccessObservation, types.VoteType_SuccessObservation},
},

{
name: "failed votes only",
ballotIdentifier: sample.ZetaIndex(t),
ballotType: types.ObservationType_InboundTx,
voters: []string{"voter1", "voter2"},
voteType: []types.VoteType{types.VoteType_FailureObservation, types.VoteType_FailureObservation},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
_, ctx, _, _ := keepertest.ObserverKeeper(t)
ballot := types.Ballot{
BallotIdentifier: tc.ballotIdentifier,
ObservationType: tc.ballotType,
VoterList: tc.voters,
Votes: tc.voteType,
}
keeper.EmitEventBallotDeleted(ctx, ballot)
for _, event := range ctx.EventManager().Events() {
for _, attr := range event.Attributes {
if attr.Key == "ballot_identifier" {
require.Equal(t, tc.ballotIdentifier, RemoveQuotes(attr.Value))
}
if attr.Key == "ballot_type" {
require.Equal(t, tc.ballotType.String(), RemoveQuotes(attr.Value))
}
if attr.Key == "voters" {
expectedString := ""
for _, voter := range ballot.GenerateVoterList() {
st := fmt.Sprintf("{\"voter_address\":\"%s\",\"vote_type\":\"%s\"}", voter.VoterAddress, voter.VoteType)
expectedString += st
expectedString += ","
}
expectedString = expectedString[:len(expectedString)-1]
require.Equal(t, expectedString, RemoveQuotes(attr.Value))
}
}
}

})
}
}

func RemoveQuotes(s string) string {
return s[1 : len(s)-1]
}
2 changes: 1 addition & 1 deletion x/observer/keeper/grpc_query_ballot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestKeeper_BallotByIdentifier(t *testing.T) {
require.NoError(t, err)
require.Equal(t, &types.QueryBallotByIdentifierResponse{
BallotIdentifier: ballot.BallotIdentifier,
Voters: []*types.VoterList{
Voters: []types.VoterList{
{
VoterAddress: voter,
VoteType: types.VoteType_SuccessObservation,
Expand Down
1 change: 1 addition & 0 deletions x/observer/types/ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,6 @@ func (m Ballot) GenerateVoterList() []VoterList {
}
votersList[i] = voter
}

return votersList
}
Loading

0 comments on commit 16d21c4

Please sign in to comment.