Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #42 from 0xPolygon/use-mockery
Browse files Browse the repository at this point in the history
Use `mockery` to auto generate mocks
  • Loading branch information
goran-ethernal authored Jan 16, 2024
2 parents 0275d18 + 3da4ae8 commit ee5961b
Show file tree
Hide file tree
Showing 17 changed files with 1,188 additions and 350 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ include version.mk
CHECK_GO := $(shell command -v go 2> /dev/null)
CHECK_CURL := $(shell command -v curl 2> /dev/null)
CHECK_DOCKER := $(shell command -v docker 2> /dev/null)
CHECK_MOCKERY := $(shell command -v mockery 2> /dev/null)

check-go:
ifndef CHECK_GO
Expand All @@ -20,6 +21,11 @@ ifndef CHECK_DOCKER
$(error "Docker is not installed. Please install Docker and retry.")
endif

check-mockery:
ifndef CHECK_MOCKERY
$(error "Mockery is not installed. Please install Mockery and retry.")
endif

# Targets that require the checks
build: check-go
build-docker: check-docker
Expand All @@ -32,6 +38,7 @@ install-linter: check-go check-curl
lint: check-go
unit-tests: check-go
e2e-tests: check-go
generate-mocks: check-go check-mockery

ARCH := $(shell uname -m)

Expand Down Expand Up @@ -110,3 +117,7 @@ e2e-tests: ## Runs E2E tests
.PHONY: unit-tests
unit-tests: ## Runs unit tests
go test -v -timeout=5m -race -shuffle=on -coverprofile coverage.out `go list ./... | grep -v test`

.PHONY: generate-mocks
generate-mocks: ## Generates mocks and other autogenerated types
$(GOENVVARS) go generate ./...
7 changes: 4 additions & 3 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
"errors"
"fmt"
"github.com/0xPolygon/beethoven/config"
"math/big"
"time"

"github.com/0xPolygon/beethoven/config"

"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonrollupmanager"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonzkevm"
"github.com/0xPolygonHermez/zkevm-node/log"
Expand All @@ -28,12 +29,12 @@ const (
)

type Etherman struct {
ethClient EthereumClient
ethClient IEthereumClient
auth bind.TransactOpts
config *config.Config
}

func New(ethClient EthereumClient, auth bind.TransactOpts, cfg *config.Config) (Etherman, error) {
func New(ethClient IEthereumClient, auth bind.TransactOpts, cfg *config.Config) (Etherman, error) {
return Etherman{
ethClient: ethClient,
auth: auth,
Expand Down
61 changes: 31 additions & 30 deletions etherman/etherman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package etherman
import (
"context"
"errors"
"math/big"
"testing"

"github.com/0xPolygon/beethoven/config"
cdkTypes "github.com/0xPolygon/beethoven/rpc/types"
"github.com/0xPolygon/beethoven/tx"
"github.com/ethereum/go-ethereum/crypto"
"math/big"
"testing"

"github.com/0xPolygon/beethoven/mocks"
"github.com/ethereum/go-ethereum"
Expand All @@ -23,7 +24,7 @@ func signer(from common.Address, tx *types.Transaction) (*types.Transaction, err
return tx, nil
}

func getEtherman(ethClientMock EthereumClient) Etherman {
func getEtherman(ethClientMock IEthereumClient) Etherman {
ethman, _ := New(
ethClientMock,
bind.TransactOpts{
Expand All @@ -50,7 +51,7 @@ func TestGetSequencerAddr(t *testing.T) {
t.Run("Returns expected error on 'TrustedSequencer' call (improperly formatted output)", func(t *testing.T) {
t.Parallel()

ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand Down Expand Up @@ -100,7 +101,7 @@ func TestGetSequencerAddr(t *testing.T) {
t.Run("Returns expected error on 'RollupIDToRollupData' call (improperly formatted output)", func(t *testing.T) {
t.Parallel()

ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand Down Expand Up @@ -131,7 +132,7 @@ func TestGetSequencerAddr(t *testing.T) {
t.Run("Returns expected sequencer address", func(t *testing.T) {
t.Parallel()

ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand Down Expand Up @@ -178,7 +179,7 @@ func TestGetSequencerAddr(t *testing.T) {
func TestBuildTrustedVerifyBatches(t *testing.T) {
t.Parallel()
assert := assert.New(t)
ethman := getEtherman(new(mocks.EthereumClientMock))
ethman := getEtherman(mocks.NewEthereumClientMock(t))

// Because we cant mock the ABI dependency is this the only test case that we somehow
// can have here in a unit test. Further test coverage can get achieved with e2e or integration tests.
Expand All @@ -204,7 +205,7 @@ func TestCallContract(t *testing.T) {
assert := assert.New(t)

t.Run("Returns expected value", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand Down Expand Up @@ -247,7 +248,7 @@ func TestCallContract(t *testing.T) {
})

t.Run("Returns expected error", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand Down Expand Up @@ -295,7 +296,7 @@ func TestCheckTxWasMined(t *testing.T) {
assert := assert.New(t)

t.Run("Returns expected error on 'ethereum.NotFound'", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand All @@ -316,7 +317,7 @@ func TestCheckTxWasMined(t *testing.T) {
})

t.Run("Returns expected error", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand All @@ -337,7 +338,7 @@ func TestCheckTxWasMined(t *testing.T) {
})

t.Run("Returns the expected values", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand All @@ -363,7 +364,7 @@ func TestCurrentNonce(t *testing.T) {
assert := assert.New(t)

t.Run("Returns the expected nonce value", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand All @@ -384,7 +385,7 @@ func TestCurrentNonce(t *testing.T) {
})

t.Run("Returns the expected error", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand All @@ -410,7 +411,7 @@ func TestGetTx(t *testing.T) {
assert := assert.New(t)

t.Run("Returns the expected transaction", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand All @@ -432,7 +433,7 @@ func TestGetTx(t *testing.T) {
})

t.Run("Returns the expected error", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand All @@ -459,7 +460,7 @@ func TestGetTxReceipt(t *testing.T) {
assert := assert.New(t)

t.Run("Returns expected receipt", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand All @@ -479,7 +480,7 @@ func TestGetTxReceipt(t *testing.T) {
})

t.Run("Returns expected error", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand Down Expand Up @@ -512,7 +513,7 @@ func TestSendTx(t *testing.T) {
)

t.Run("Returns expected value", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand All @@ -530,7 +531,7 @@ func TestSendTx(t *testing.T) {
})

t.Run("Returns expected error", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand All @@ -553,7 +554,7 @@ func TestSuggestedGasPrice(t *testing.T) {
assert := assert.New(t)

t.Run("Returns expected value", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand All @@ -572,7 +573,7 @@ func TestSuggestedGasPrice(t *testing.T) {
})

t.Run("Returns expected error", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand All @@ -596,7 +597,7 @@ func TestEstimateGas(t *testing.T) {
assert := assert.New(t)

t.Run("Returns the expected value", func(t *testing.T) {
ethclient := new(mocks.EthereumClientMock)
ethclient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethclient)

ethclient.On(
Expand Down Expand Up @@ -627,7 +628,7 @@ func TestEstimateGas(t *testing.T) {
})

t.Run("Returns the expected error", func(t *testing.T) {
ethclient := new(mocks.EthereumClientMock)
ethclient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethclient)

ethclient.On(
Expand Down Expand Up @@ -671,7 +672,7 @@ func TestSignTx(t *testing.T) {
)

t.Run("Returns the expected value", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

transaction, err := ethman.SignTx(context.TODO(), common.Address{}, txData)
Expand All @@ -694,7 +695,7 @@ func TestGetRevertMessage(t *testing.T) {
)

t.Run("Returns an empty string and the expected error", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand All @@ -713,7 +714,7 @@ func TestGetRevertMessage(t *testing.T) {
})

t.Run("Returns an empty string and the error set to nil", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand All @@ -734,7 +735,7 @@ func TestGetRevertMessage(t *testing.T) {
})

t.Run("Returns the expected revert reason string", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

key, _ := crypto.GenerateKey()
Expand Down Expand Up @@ -784,7 +785,7 @@ func TestGetLastBlock(t *testing.T) {
assert := assert.New(t)

t.Run("Returns the expected values", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand All @@ -811,7 +812,7 @@ func TestGetLastBlock(t *testing.T) {
})

t.Run("Returns the expected error", func(t *testing.T) {
ethClient := new(mocks.EthereumClientMock)
ethClient := mocks.NewEthereumClientMock(t)
ethman := getEtherman(ethClient)

ethClient.On(
Expand Down
3 changes: 2 additions & 1 deletion etherman/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
)

type EthereumClient interface {
//go:generate mockery --name IEthereumClient --structname EthereumClientMock --output ../mocks --case=underscore --filename etherman_client.generated.go
type IEthereumClient interface {
ethereum.ChainReader
ethereum.ChainStateReader
ethereum.ContractCaller
Expand Down
14 changes: 7 additions & 7 deletions interop/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ import (
"github.com/jackc/pgx/v4"
)

var _ types.ZkEVMClientClientCreator = (*zkEVMClientCreator)(nil)
var _ types.IZkEVMClientClientCreator = (*zkEVMClientCreator)(nil)

type zkEVMClientCreator struct{}

func (zc *zkEVMClientCreator) NewClient(rpc string) types.ZkEVMClientInterface {
func (zc *zkEVMClientCreator) NewClient(rpc string) types.IZkEVMClient {
return client.NewClient(rpc)
}

type Executor struct {
logger *log.Logger
interopAdminAddr common.Address
config *config.Config
ethTxMan types.EthTxManager
etherman types.EthermanInterface
ZkEVMClientCreator types.ZkEVMClientClientCreator
ethTxMan types.IEthTxManager
etherman types.IEtherman
ZkEVMClientCreator types.IZkEVMClientClientCreator
}

func New(logger *log.Logger, cfg *config.Config,
interopAdminAddr common.Address,
etherman types.EthermanInterface,
ethTxManager types.EthTxManager,
etherman types.IEtherman,
ethTxManager types.IEthTxManager,
) *Executor {
return &Executor{
logger: logger,
Expand Down
Loading

0 comments on commit ee5961b

Please sign in to comment.