Skip to content

Commit

Permalink
refactor: fixed create tests
Browse files Browse the repository at this point in the history
Yashk767 committed Apr 10, 2024
1 parent 1a93d6c commit 9510190
Showing 5 changed files with 228 additions and 12 deletions.
120 changes: 120 additions & 0 deletions accounts/mocks/account_manager_interface.go

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

100 changes: 100 additions & 0 deletions cmd/create_test.go
Original file line number Diff line number Diff line change
@@ -2,7 +2,10 @@ package cmd

import (
"errors"
accountsPkgMocks "razor/accounts/mocks"
"razor/core/types"
pathPkgMocks "razor/path/mocks"
utilsPkgMocks "razor/utils/mocks"
"testing"

"github.com/ethereum/go-ethereum/accounts"
@@ -11,6 +14,103 @@ import (
"github.com/stretchr/testify/mock"
)

func TestCreate(t *testing.T) {
var password string

nilAccount := accounts.Account{Address: common.Address{0x00},
URL: accounts.URL{Scheme: "TestKeyScheme", Path: "test/key/path"},
}

type args struct {
path string
pathErr error
accountManagerErr error
account accounts.Account
}
tests := []struct {
name string
args args
want accounts.Account
wantErr error
}{
{
name: "Test 1: When create function executes successfully",
args: args{
path: "/home/local",
pathErr: nil,
account: accounts.Account{Address: common.HexToAddress("0x000000000000000000000000000000000000dea1"),
URL: accounts.URL{Scheme: "TestKeyScheme", Path: "test/key/path"},
},
},
want: accounts.Account{Address: common.HexToAddress("0x000000000000000000000000000000000000dea1"),
URL: accounts.URL{Scheme: "TestKeyScheme", Path: "test/key/path"},
},
wantErr: nil,
},
{
name: "Test 2: When create fails due to path error",
args: args{
path: "/home/local",
pathErr: errors.New("path error"),
account: accounts.Account{Address: common.HexToAddress("0x000000000000000000000000000000000000dea1"),
URL: accounts.URL{Scheme: "TestKeyScheme", Path: "test/key/path"},
},
},
want: nilAccount,
wantErr: errors.New("path error"),
},
{
name: "Test 3: When there is an error in getting account manager",
args: args{
path: "/home/local",
pathErr: nil,
accountManagerErr: errors.New("account manager error"),
account: accounts.Account{Address: common.HexToAddress("0x000000000000000000000000000000000000dea1"),
URL: accounts.URL{Scheme: "TestKeyScheme", Path: "test/key/path"},
},
},
want: nilAccount,
wantErr: errors.New("account manager error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
accountsMock := new(accountsPkgMocks.AccountManagerInterface)

var pathMock *pathPkgMocks.PathInterface
var utilsMock *utilsPkgMocks.Utils

pathMock = new(pathPkgMocks.PathInterface)
pathUtils = pathMock

utilsMock = new(utilsPkgMocks.Utils)
razorUtils = utilsMock

pathMock.On("GetDefaultPath").Return(tt.args.path, tt.args.pathErr)
utilsMock.On("AccountManagerForKeystore").Return(accountsMock, tt.args.accountManagerErr)

accountsMock.On("CreateAccount", mock.Anything, mock.Anything).Return(tt.args.account)

utils := &UtilsStruct{}
got, err := utils.Create(password)

if got.Address != tt.want.Address {
t.Errorf("New address created, got = %v, want %v", got, tt.want.Address)
}

if err == nil || tt.wantErr == nil {
if err != tt.wantErr {
t.Errorf("Error for Create function, got = %v, want %v", got, tt.wantErr)
}
} else {
if err.Error() != tt.wantErr.Error() {
t.Errorf("Error for Create function, got = %v, want %v", got, tt.wantErr)
}
}
})
}
}

func TestExecuteCreate(t *testing.T) {
var flagSet *pflag.FlagSet

4 changes: 0 additions & 4 deletions cmd/test_utils_test.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/crypto"
"math/big"
//accountsPkgMocks "razor/accounts/mocks"
"razor/cmd/mocks"
"razor/path"
pathPkgMocks "razor/path/mocks"
@@ -51,7 +50,6 @@ var (
osMock *mocks.OSInterface
pathMock *pathPkgMocks.PathInterface
osPathMock *pathPkgMocks.OSInterface
//accountsMock *accountsPkgMocks.AccountManagerInterface
)

func SetUpMockInterfaces() {
@@ -165,8 +163,6 @@ func SetUpMockInterfaces() {

osPathMock = new(pathPkgMocks.OSInterface)
path.OSUtilsInterface = osPathMock

//accountsMock = new(accountsPkgMocks.AccountManagerInterface)
}

var privateKey, _ = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
2 changes: 2 additions & 0 deletions core/types/account.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@ import (
"github.com/ethereum/go-ethereum/accounts"
)

//go:generate mockery --name=AccountManagerInterface --output=../../accounts/mocks --case=underscore

type Account struct {
Address string
Password string
14 changes: 6 additions & 8 deletions utils/mocks/utils.go

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

0 comments on commit 9510190

Please sign in to comment.