Consolidate test doubles

- Migrate various mocks of core namespaces to shared version in `fakes` pkg
- Err on the side of making test doubles less sophisticated
- Don't pull over mocks of namespaces that are only used in example code
This commit is contained in:
Rob Mulholand
2018-07-20 11:37:46 -05:00
parent 63434f6bc9
commit a683e45855
46 changed files with 640 additions and 893 deletions
@@ -16,24 +16,25 @@ package every_block
import (
"fmt"
"github.com/vulcanize/vulcanizedb/pkg/core"
"log"
"math/big"
"github.com/vulcanize/vulcanizedb/pkg/core"
)
type ERC20FetcherInterface interface {
FetchSupplyOf(contractAbi string, contractAddress string, blockNumber int64) (big.Int, error)
GetBlockchain() core.Blockchain
GetBlockChain() core.BlockChain
}
func NewFetcher(blockchain core.Blockchain) Fetcher {
func NewFetcher(blockchain core.BlockChain) Fetcher {
return Fetcher{
Blockchain: blockchain,
}
}
type Fetcher struct {
Blockchain core.Blockchain
Blockchain core.BlockChain
ContractAbi string
ContractAddress string
}
@@ -65,6 +66,6 @@ func (f Fetcher) FetchSupplyOf(contractAbi string, contractAddress string, block
return *result, nil
}
func (f Fetcher) GetBlockchain() core.Blockchain {
func (f Fetcher) GetBlockChain() core.BlockChain {
return f.Blockchain
}
@@ -15,68 +15,50 @@
package every_block_test
import (
"math/big"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/examples/constants"
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/every_block"
"github.com/vulcanize/vulcanizedb/examples/mocks"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/geth/client"
rpc2 "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc"
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
"math/big"
)
var _ = Describe("ERC20 Fetcher", func() {
blockNumber := int64(5502914)
infuraIPC := "https://mainnet.infura.io/J5Vd2fRtGsw0zZ0Ov3BL"
var errorFetcher every_block.Fetcher
var realFetcher every_block.Fetcher
var testFetcher every_block.Fetcher
var fakeBlockchain *mocks.Blockchain
var testAbi string
var testContractAddress string
BeforeEach(func() {
rpcClient, err := rpc.Dial(infuraIPC)
Expect(err).NotTo(HaveOccurred())
ethClient := ethclient.NewClient(rpcClient)
blockChainClient := client.NewClient(ethClient)
clientWrapper := node.ClientWrapper{
ContextCaller: rpcClient,
IPCPath: infuraIPC,
}
node := node.MakeNode(clientWrapper)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
realBlockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
realFetcher = every_block.NewFetcher(realBlockChain)
fakeBlockchain = &mocks.Blockchain{}
testFetcher = every_block.NewFetcher(fakeBlockchain)
testAbi = "testAbi"
testContractAddress = "testContractAddress"
errorBlockchain := &mocks.FailureBlockchain{}
errorFetcher = every_block.NewFetcher(errorBlockchain)
})
Describe("FetchSupplyOf", func() {
It("fetches data from the blockchain with the correct arguments", func() {
fakeBlockchain := fakes.NewMockBlockChain()
testFetcher := every_block.NewFetcher(fakeBlockchain)
testAbi := "testAbi"
testContractAddress := "testContractAddress"
_, err := testFetcher.FetchSupplyOf(testAbi, testContractAddress, blockNumber)
Expect(err).NotTo(HaveOccurred())
Expect(fakeBlockchain.FetchedAbi).To(Equal(testAbi))
Expect(fakeBlockchain.FetchedContractAddress).To(Equal(testContractAddress))
Expect(fakeBlockchain.FetchedMethod).To(Equal("totalSupply"))
Expect(fakeBlockchain.FetchedMethodArg).To(BeNil())
expectedResult := big.Int{}
expected := &expectedResult
Expect(fakeBlockchain.FetchedResult).To(Equal(&expected))
Expect(fakeBlockchain.FetchedBlockNumber).To(Equal(blockNumber))
fakeBlockchain.AssertFetchContractDataCalledWith(testAbi, testContractAddress, "totalSupply", nil, &expected, blockNumber)
})
It("fetches a token's total supply at the given block height", func() {
infuraIPC := "https://mainnet.infura.io/J5Vd2fRtGsw0zZ0Ov3BL"
rawRpcClient, err := rpc.Dial(infuraIPC)
Expect(err).NotTo(HaveOccurred())
rpcClient := client.NewRpcClient(rawRpcClient, infuraIPC)
ethClient := ethclient.NewClient(rawRpcClient)
blockChainClient := client.NewEthClient(ethClient)
node := node.MakeNode(rpcClient)
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
blockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
realFetcher := every_block.NewFetcher(blockChain)
result, err := realFetcher.FetchSupplyOf(constants.DaiAbiString, constants.DaiContractAddress, blockNumber)
Expect(err).NotTo(HaveOccurred())
@@ -86,12 +68,15 @@ var _ = Describe("ERC20 Fetcher", func() {
})
It("returns an error if the call to the blockchain fails", func() {
blockChain := fakes.NewMockBlockChain()
blockChain.SetFetchContractDataErr(fakes.FakeError)
errorFetcher := every_block.NewFetcher(blockChain)
result, err := errorFetcher.FetchSupplyOf("", "", 0)
Expect(result.String()).To(Equal("0"))
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("totalSupply"))
Expect(err.Error()).To(ContainSubstring(mocks.TestError.Error()))
Expect(err.Error()).To(ContainSubstring(fakes.FakeError.Error()))
})
})
})
@@ -20,34 +20,35 @@ import (
"github.com/vulcanize/vulcanizedb/examples/constants"
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher"
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/every_block"
"github.com/vulcanize/vulcanizedb/examples/mocks"
"github.com/vulcanize/vulcanizedb/examples/test_helpers"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
"math/big"
"strconv"
)
func setLastBlockOnChain(blockchain *mocks.Blockchain, blockNumber int64) {
func setLastBlockOnChain(blockChain *fakes.MockBlockChain, blockNumber int64) {
blockNumberString := strconv.FormatInt(blockNumber, 10)
lastBlockOnChain := big.Int{}
lastBlockOnChain.SetString(blockNumberString, 10)
blockchain.SetLastBlock(&lastBlockOnChain)
blockChain.SetLastBlock(&lastBlockOnChain)
}
var _ = Describe("Everyblock transformers", func() {
var db *postgres.DB
var blockchain mocks.Blockchain
var blockChain *fakes.MockBlockChain
var blockNumber int64
var blockId int64
var err error
BeforeEach(func() {
blockChain = fakes.NewMockBlockChain()
blockNumber = erc20_watcher.DaiConfig.FirstBlock
lastBlockNumber := blockNumber + 1
db = test_helpers.CreateNewDatabase()
setLastBlockOnChain(&blockchain, lastBlockNumber)
setLastBlockOnChain(blockChain, lastBlockNumber)
blockRepository := repositories.NewBlockRepository(db)
@@ -59,7 +60,7 @@ var _ = Describe("Everyblock transformers", func() {
It("creates a token_supply record for each block in the given range", func() {
initializer := every_block.TokenSupplyTransformerInitializer{Config: erc20_watcher.DaiConfig}
transformer := initializer.NewTokenSupplyTransformer(db, &blockchain)
transformer := initializer.NewTokenSupplyTransformer(db, blockChain)
transformer.Execute()
var tokenSupplyCount int
@@ -38,7 +38,7 @@ type TokenSupplyTransformerInitializer struct {
Config erc20_watcher.ContractConfig
}
func (i TokenSupplyTransformerInitializer) NewTokenSupplyTransformer(db *postgres.DB, blockchain core.Blockchain) shared.Transformer {
func (i TokenSupplyTransformerInitializer) NewTokenSupplyTransformer(db *postgres.DB, blockchain core.BlockChain) shared.Transformer {
fetcher := NewFetcher(blockchain)
repository := TokenSupplyRepository{DB: db}
transformer := Transformer{
@@ -74,7 +74,7 @@ func newTransformerError(err error, blockNumber int64, msg string) error {
func (t Transformer) Execute() error {
var upperBoundBlock int64
blockchain := t.Fetcher.GetBlockchain()
blockchain := t.Fetcher.GetBlockChain()
lastBlock := blockchain.LastBlock().Int64()
if t.Config.LastBlock == -1 {
@@ -21,6 +21,7 @@ import (
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher"
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/every_block"
"github.com/vulcanize/vulcanizedb/examples/mocks"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
"math/big"
"math/rand"
"strconv"
@@ -40,7 +41,7 @@ var _ = Describe("Everyblock transformer", func() {
var fetcher mocks.Fetcher
var repository mocks.TotalSupplyRepository
var transformer every_block.Transformer
var blockchain mocks.Blockchain
var blockChain *fakes.MockBlockChain
var initialSupply = "27647235749155415536952630"
var initialSupplyPlusOne = "27647235749155415536952631"
var initialSupplyPlusTwo = "27647235749155415536952632"
@@ -48,9 +49,9 @@ var _ = Describe("Everyblock transformer", func() {
var defaultLastBlock = big.Int{}
BeforeEach(func() {
blockchain = mocks.Blockchain{}
blockchain.SetLastBlock(&defaultLastBlock)
fetcher = mocks.Fetcher{Blockchain: &blockchain}
blockChain = fakes.NewMockBlockChain()
blockChain.SetLastBlock(&defaultLastBlock)
fetcher = mocks.Fetcher{BlockChain: blockChain}
fetcher.SetSupply(initialSupply)
repository = mocks.TotalSupplyRepository{}
repository.SetMissingBlocks([]int64{config.FirstBlock})
@@ -132,7 +133,7 @@ var _ = Describe("Everyblock transformer", func() {
mostRecentBlock := big.Int{}
mostRecentBlock.SetString(numberToString, 10)
blockchain.SetLastBlock(&mostRecentBlock)
blockChain.SetLastBlock(&mostRecentBlock)
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())
@@ -149,13 +150,14 @@ var _ = Describe("Everyblock transformer", func() {
}
err := transformer.Execute()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("TestError"))
Expect(err.Error()).To(ContainSubstring(fakes.FakeError.Error()))
Expect(err.Error()).To(ContainSubstring("fetching missing blocks"))
})
It("returns an error if the call to the blockchain fails", func() {
failureBlockchain := mocks.FailureBlockchain{}
It("returns an error if the call to the blockChain fails", func() {
failureBlockchain := fakes.NewMockBlockChain()
failureBlockchain.SetLastBlock(&defaultLastBlock)
failureBlockchain.SetFetchContractDataErr(fakes.FakeError)
fetcher := every_block.NewFetcher(failureBlockchain)
transformer = every_block.Transformer{
Fetcher: &fetcher,
@@ -163,7 +165,7 @@ var _ = Describe("Everyblock transformer", func() {
}
err := transformer.Execute()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("TestError"))
Expect(err.Error()).To(ContainSubstring(fakes.FakeError.Error()))
Expect(err.Error()).To(ContainSubstring("supply"))
})
@@ -178,7 +180,7 @@ var _ = Describe("Everyblock transformer", func() {
}
err := transformer.Execute()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("TestError"))
Expect(err.Error()).To(ContainSubstring(fakes.FakeError.Error()))
Expect(err.Error()).To(ContainSubstring("supply"))
})
})