Skip to content

Commit

Permalink
Add ballot types test and remove redundant test data file
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Mar 28, 2024
1 parent 363af6b commit 6e6edf7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 31 deletions.
8 changes: 5 additions & 3 deletions x/observer/keeper/params_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package keeper
package keeper_test

import (
"fmt"
Expand All @@ -9,11 +9,13 @@ import (
"github.com/tendermint/tendermint/crypto"

"github.com/stretchr/testify/require"
testutilkeeper "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/x/observer/keeper"
"github.com/zeta-chain/zetacore/x/observer/types"
)

func TestGetParams(t *testing.T) {
k, ctx := SetupKeeper(t)
k, ctx := keeper.SetupKeeper(t)
params := types.DefaultParams()

k.SetParams(ctx, params)
Expand All @@ -22,7 +24,7 @@ func TestGetParams(t *testing.T) {
}

func TestGenerateAddress(t *testing.T) {
types.SetConfig(false)
testutilkeeper.SetConfig(false)
addr := sdk.AccAddress(crypto.AddressHash([]byte("Output1" + strconv.Itoa(1))))
addrString := addr.String()
fmt.Println(addrString)
Expand Down
6 changes: 6 additions & 0 deletions x/observer/types/ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ func (m Ballot) AddVote(address string, vote VoteType) (Ballot, error) {
// `index` is the index of the `address` in the `VoterList`
// `index` is used to set the vote in the `Votes` array
index := m.GetVoterIndex(address)
if index == -1 {
return m, errors.Wrap(ErrUnableToAddVote, fmt.Sprintf("Voter %s not in voter list", address))
}
m.Votes[index] = vote
return m, nil
}

func (m Ballot) HasVoted(address string) bool {
index := m.GetVoterIndex(address)
if index == -1 {
return false
}
return m.Votes[index] != VoteType_NotYetVoted
}

Expand Down
19 changes: 18 additions & 1 deletion x/observer/types/ballot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestBallot_AddVote(t *testing.T) {
finalVotes []VoteType
finalStatus BallotStatus
isFinalized bool
wantErr bool
}{
{
name: "All success",
Expand Down Expand Up @@ -188,6 +189,18 @@ func TestBallot_AddVote(t *testing.T) {
finalStatus: BallotStatus_BallotInProgress,
isFinalized: false,
},
{
name: "Voter not in voter list",
threshold: sdk.MustNewDecFromStr("0.66"),
voterList: []string{},
votes: []votes{
{"Observer5", VoteType_SuccessObservation},
},
wantErr: true,
finalVotes: []VoteType{},
finalStatus: BallotStatus_BallotInProgress,
isFinalized: false,
},
}
for _, test := range tt {
test := test
Expand All @@ -202,7 +215,11 @@ func TestBallot_AddVote(t *testing.T) {
BallotStatus: BallotStatus_BallotInProgress,
}
for _, vote := range test.votes {
ballot, _ = ballot.AddVote(vote.address, vote.vote)
b, err := ballot.AddVote(vote.address, vote.vote)
if test.wantErr {
require.Error(t, err)
}
ballot = b
}

finalBallot, isFinalized := ballot.IsFinalizingVote()
Expand Down
27 changes: 0 additions & 27 deletions x/observer/types/test_data.go

This file was deleted.

0 comments on commit 6e6edf7

Please sign in to comment.