Skip to content

Commit

Permalink
refactor: fix utils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashk767 committed Apr 24, 2024
1 parent cef547d commit 89dbc2a
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 199 deletions.
22 changes: 12 additions & 10 deletions cmd/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ func TestCommit(t *testing.T) {

func TestHandleCommitState(t *testing.T) {
var (
client *ethclient.Client
epoch uint32
seed []byte
client *ethclient.Client
epoch uint32
seed []byte
commitParams types.CommitParams
)

rogueValue := big.NewInt(1111)
Expand Down Expand Up @@ -228,11 +229,11 @@ func TestHandleCommitState(t *testing.T) {
utilsMock.On("GetNumActiveCollections", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.numActiveCollections, tt.args.numActiveCollectionsErr)
utilsMock.On("GetAssignedCollections", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything).Return(tt.args.assignedCollections, tt.args.seqAllottedCollections, tt.args.assignedCollectionsErr)
utilsMock.On("GetCollectionIdFromIndex", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(tt.args.collectionId, tt.args.collectionIdErr)
utilsMock.On("GetAggregatedDataOfCollection", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything, mock.Anything).Return(tt.args.collectionData, tt.args.collectionDataErr)
utilsMock.On("GetAggregatedDataOfCollection", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.collectionData, tt.args.collectionDataErr)
utilsMock.On("GetRogueRandomValue", mock.Anything).Return(rogueValue)

utils := &UtilsStruct{}
got, err := utils.HandleCommitState(client, epoch, seed, tt.args.rogueData)
got, err := utils.HandleCommitState(client, epoch, seed, commitParams, tt.args.rogueData)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Data from HandleCommitState function, got = %v, want = %v", got, tt.want)
}
Expand Down Expand Up @@ -370,9 +371,10 @@ func TestGetSalt(t *testing.T) {

func BenchmarkHandleCommitState(b *testing.B) {
var (
client *ethclient.Client
epoch uint32
seed []byte
client *ethclient.Client
epoch uint32
seed []byte
commitParams types.CommitParams
)

rogueValue := big.NewInt(1111)
Expand All @@ -393,11 +395,11 @@ func BenchmarkHandleCommitState(b *testing.B) {
utilsMock.On("GetNumActiveCollections", mock.AnythingOfType("*ethclient.Client")).Return(v.numActiveCollections, nil)
utilsMock.On("GetAssignedCollections", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything).Return(v.assignedCollections, nil, nil)
utilsMock.On("GetCollectionIdFromIndex", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(uint16(1), nil)
utilsMock.On("GetAggregatedDataOfCollection", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything, mock.Anything).Return(big.NewInt(1000), nil)
utilsMock.On("GetAggregatedDataOfCollection", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(big.NewInt(1000), nil)
utilsMock.On("GetRogueRandomValue", mock.Anything).Return(rogueValue)

ut := &UtilsStruct{}
_, err := ut.HandleCommitState(client, epoch, seed, types.Rogue{IsRogue: false})
_, err := ut.HandleCommitState(client, epoch, seed, commitParams, types.Rogue{IsRogue: false})
if err != nil {
log.Fatal(err)
}
Expand Down
81 changes: 59 additions & 22 deletions cmd/mocks/utils_cmd_interface.go

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

24 changes: 14 additions & 10 deletions cmd/vote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path"
"path/filepath"
"razor/cache"
"razor/core/types"
"razor/pkg/bindings"
"razor/utils"
Expand Down Expand Up @@ -134,8 +135,9 @@ func TestExecuteVote(t *testing.T) {
utilsMock.On("ConnectToClient", mock.AnythingOfType("string")).Return(client)
flagSetMock.On("GetBoolRogue", mock.AnythingOfType("*pflag.FlagSet")).Return(tt.args.rogueStatus, tt.args.rogueErr)
flagSetMock.On("GetStringSliceRogueMode", mock.AnythingOfType("*pflag.FlagSet")).Return(tt.args.rogueMode, tt.args.rogueModeErr)
cmdUtilsMock.On("InitAssetCache", mock.Anything).Return(&cache.JobsCache{}, &cache.CollectionsCache{}, nil)
cmdUtilsMock.On("HandleExit").Return()
cmdUtilsMock.On("Vote", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.voteErr)
cmdUtilsMock.On("Vote", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.voteErr)
osMock.On("Exit", mock.AnythingOfType("int")).Return()

utils := &UtilsStruct{}
Expand Down Expand Up @@ -296,11 +298,12 @@ func TestCalculateSecret(t *testing.T) {

func TestInitiateCommit(t *testing.T) {
var (
client *ethclient.Client
config types.Configurations
account types.Account
stakerId uint32
rogueData types.Rogue
client *ethclient.Client
config types.Configurations
account types.Account
stakerId uint32
commitParams types.CommitParams
rogueData types.Rogue
)
type args struct {
staker bindings.StructsStaker
Expand Down Expand Up @@ -501,14 +504,14 @@ func TestInitiateCommit(t *testing.T) {
utilsMock.On("GetEpochLastCommitted", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint32")).Return(tt.args.lastCommit, tt.args.lastCommitErr)
cmdUtilsMock.On("CalculateSecret", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.signature, tt.args.secret, tt.args.secretErr)
cmdUtilsMock.On("GetSalt", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(tt.args.salt, tt.args.saltErr)
cmdUtilsMock.On("HandleCommitState", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.commitData, tt.args.commitDataErr)
cmdUtilsMock.On("HandleCommitState", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.commitData, tt.args.commitDataErr)
pathMock.On("GetDefaultPath").Return(tt.args.path, tt.args.pathErr)
cmdUtilsMock.On("Commit", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.commitTxn, tt.args.commitTxnErr)
utilsMock.On("WaitForBlockCompletion", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string")).Return(tt.args.waitForBlockCompletionErr)
pathMock.On("GetCommitDataFileName", mock.AnythingOfType("string")).Return(tt.args.fileName, tt.args.fileNameErr)
fileUtilsMock.On("SaveDataToCommitJsonFile", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.saveErr)
ut := &UtilsStruct{}
if err := ut.InitiateCommit(client, config, account, tt.args.epoch, stakerId, rogueData); (err != nil) != tt.wantErr {
if err := ut.InitiateCommit(client, config, account, tt.args.epoch, stakerId, commitParams, rogueData); (err != nil) != tt.wantErr {
t.Errorf("InitiateCommit() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down Expand Up @@ -856,6 +859,7 @@ func TestHandleBlock(t *testing.T) {
client *ethclient.Client
account types.Account
blockNumber *big.Int
commitParams types.CommitParams
rogueData types.Rogue
backupNodeActionsToIgnore []string
)
Expand Down Expand Up @@ -1208,7 +1212,7 @@ func TestHandleBlock(t *testing.T) {
clientUtilsMock.On("BalanceAtWithRetry", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(tt.args.ethBalance, tt.args.ethBalanceErr)
utilsMock.On("GetStakerSRZRBalance", mock.Anything, mock.Anything).Return(tt.args.sRZRBalance, tt.args.sRZRBalanceErr)
osMock.On("Exit", mock.AnythingOfType("int")).Return()
cmdUtilsMock.On("InitiateCommit", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.initiateCommitErr)
cmdUtilsMock.On("InitiateCommit", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.initiateCommitErr)
cmdUtilsMock.On("InitiateReveal", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.initiateRevealErr)
cmdUtilsMock.On("InitiatePropose", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.initiateProposeErr)
cmdUtilsMock.On("HandleDispute", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.handleDisputeErr)
Expand All @@ -1220,7 +1224,7 @@ func TestHandleBlock(t *testing.T) {
utilsMock.On("WaitTillNextNSecs", mock.AnythingOfType("int32")).Return()
lastVerification = tt.args.lastVerification
ut := &UtilsStruct{}
ut.HandleBlock(client, account, blockNumber, tt.args.config, rogueData, backupNodeActionsToIgnore)
ut.HandleBlock(client, account, blockNumber, tt.args.config, commitParams, rogueData, backupNodeActionsToIgnore)
})
}
}
72 changes: 36 additions & 36 deletions utils/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,42 +102,42 @@ func TestGetDataFromAPI(t *testing.T) {
want: nil,
wantErr: true,
},
//{
// name: "Post request to fetch chainId",
// args: args{
// urlStruct: types.DataSourceURL{
// Type: "POST",
// URL: "https://rpc.ankr.com/polygon_mumbai",
// Body: map[string]interface{}{"jsonrpc": "2.0", "method": "eth_chainId", "params": nil, "id": 0},
// Header: map[string]string{"content-type": "application/json"},
// },
// },
// want: sampleChainId,
//},
//{
// name: "Header requires API_KEY from environment but its not present",
// args: args{
// urlStruct: types.DataSourceURL{
// Type: "POST",
// URL: "https://rpc.ankr.com/polygon_mumbai",
// Body: map[string]interface{}{"jsonrpc": "2.0", "method": "eth_chainId", "params": nil, "id": 0},
// Header: map[string]string{"auth": "${API_KEY}", "content-type": "application/json"},
// },
// },
// want: sampleChainId,
//},
//{
// name: "Body for POST request is incorrect",
// args: args{
// urlStruct: types.DataSourceURL{
// Type: "POST",
// URL: "https://rpc.ankr.com/polygon_mumbai",
// Body: map[string]interface{}{"fail": func() {}, "jsonrpc": 1},
// },
// },
// want: nil,
// wantErr: true,
//},
{
name: "Post request to fetch chainId",
args: args{
urlStruct: types.DataSourceURL{
Type: "POST",
URL: "https://rpc.ankr.com/polygon_mumbai",
Body: map[string]interface{}{"jsonrpc": "2.0", "method": "eth_chainId", "params": nil, "id": 0},
Header: map[string]string{"content-type": "application/json"},
},
},
want: sampleChainId,

Check failure on line 115 in utils/api_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: sampleChainId
},
{
name: "Header requires API_KEY from environment but its not present",
args: args{
urlStruct: types.DataSourceURL{
Type: "POST",
URL: "https://rpc.ankr.com/polygon_mumbai",
Body: map[string]interface{}{"jsonrpc": "2.0", "method": "eth_chainId", "params": nil, "id": 0},
Header: map[string]string{"auth": "${API_KEY}", "content-type": "application/json"},
},
},
want: sampleChainId,

Check failure on line 127 in utils/api_test.go

View workflow job for this annotation

GitHub Actions / test

undefined: sampleChainId (typecheck)
},
{
name: "Body for POST request is incorrect",
args: args{
urlStruct: types.DataSourceURL{
Type: "POST",
URL: "https://rpc.ankr.com/polygon_mumbai",
Body: map[string]interface{}{"fail": func() {}, "jsonrpc": 1},
},
},
want: nil,
wantErr: true,
},
{
name: "Invalid request type",
args: args{
Expand Down
Loading

0 comments on commit 89dbc2a

Please sign in to comment.