From 0a6dbb8a27500ccf3eb940a9a9a17cd0e84b85a2 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman Date: Fri, 21 Dec 2018 11:43:06 -0600 Subject: [PATCH] Update testing - remove statediff ginkgo test suite file - move mocks to their own dir --- statediff/extractor/extractor_test.go | 18 ++++---- statediff/publisher/publisher_test.go | 3 +- statediff/service/service_test.go | 50 ++--------------------- statediff/statediff_suite_test.go | 16 -------- statediff/testhelpers/mocks.go | 49 ---------------------- statediff/testhelpers/mocks/blockchain.go | 35 ++++++++++++++++ statediff/testhelpers/mocks/builder.go | 33 +++++++++++++++ statediff/testhelpers/mocks/error.go | 5 +++ statediff/testhelpers/mocks/extractor.go | 21 ++++++++++ statediff/testhelpers/mocks/publisher.go | 17 ++++++++ 10 files changed, 125 insertions(+), 122 deletions(-) delete mode 100644 statediff/statediff_suite_test.go delete mode 100644 statediff/testhelpers/mocks.go create mode 100644 statediff/testhelpers/mocks/blockchain.go create mode 100644 statediff/testhelpers/mocks/builder.go create mode 100644 statediff/testhelpers/mocks/error.go create mode 100644 statediff/testhelpers/mocks/extractor.go create mode 100644 statediff/testhelpers/mocks/publisher.go diff --git a/statediff/extractor/extractor_test.go b/statediff/extractor/extractor_test.go index 8a481cbb9..57429fefc 100644 --- a/statediff/extractor/extractor_test.go +++ b/statediff/extractor/extractor_test.go @@ -2,7 +2,6 @@ package extractor_test import ( "testing" - "github.com/ethereum/go-ethereum/statediff/testhelpers" "math/big" "math/rand" b "github.com/ethereum/go-ethereum/statediff/builder" @@ -10,10 +9,11 @@ import ( "github.com/ethereum/go-ethereum/core/types" "bytes" "reflect" + "github.com/ethereum/go-ethereum/statediff/testhelpers/mocks" ) -var publisher testhelpers.MockPublisher -var builder testhelpers.MockBuilder +var publisher mocks.Publisher +var builder mocks.Builder var currentBlockNumber *big.Int var parentBlock, currentBlock *types.Block var expectedStateDiff b.StateDiff @@ -21,8 +21,8 @@ var extractor e.Extractor var err error func TestExtractor(t *testing.T) { - publisher = testhelpers.MockPublisher{} - builder = testhelpers.MockBuilder{} + publisher = mocks.Publisher{} + builder = mocks.Builder{} extractor, err = e.NewExtractor(&builder, &publisher) if err != nil { t.Error(err) @@ -63,14 +63,14 @@ func testBuildStateDiffStruct(t *testing.T) { } func testBuildStateDiffErrorHandling(t *testing.T) { - builder.SetBuilderError(testhelpers.MockError) + builder.SetBuilderError(mocks.Error) _, err = extractor.ExtractStateDiff(*parentBlock, *currentBlock) if err == nil { t.Error(err) } - if !equals(err, testhelpers.MockError) { t.Error() } + if !equals(err, mocks.Error) { t.Error() } builder.SetBuilderError(nil) } @@ -86,13 +86,13 @@ func testPublishingStateDiff(t *testing.T) { } func testPublisherErrorHandling(t *testing.T) { - publisher.SetPublisherError(testhelpers.MockError) + publisher.SetPublisherError(mocks.Error) _, err = extractor.ExtractStateDiff(*parentBlock, *currentBlock) if err == nil { t.Error("Expected an error, but it didn't occur.") } - if !equals(err, testhelpers.MockError) { t.Error() } + if !equals(err, mocks.Error) { t.Error() } publisher.SetPublisherError(nil) } diff --git a/statediff/publisher/publisher_test.go b/statediff/publisher/publisher_test.go index acc795ab9..9b8484131 100644 --- a/statediff/publisher/publisher_test.go +++ b/statediff/publisher/publisher_test.go @@ -76,7 +76,8 @@ func TestPublisher(t *testing.T) { type Test func(t *testing.T) - var tests = []Test{testColumnHeaders, + var tests = []Test{ + testColumnHeaders, testAccountDiffs, testWhenNoDiff, testDefaultPublisher, diff --git a/statediff/service/service_test.go b/statediff/service/service_test.go index c739492cf..039f4bbec 100644 --- a/statediff/service/service_test.go +++ b/statediff/service/service_test.go @@ -7,54 +7,10 @@ import ( service2 "github.com/ethereum/go-ethereum/statediff/service" "reflect" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/event" "math/big" "math/rand" + "github.com/ethereum/go-ethereum/statediff/testhelpers/mocks" ) - -type MockExtractor struct { - ParentBlocks []types.Block - CurrentBlocks []types.Block - extractError error -} - -func (me *MockExtractor) ExtractStateDiff(parent, current types.Block) (string, error) { - me.ParentBlocks = append(me.ParentBlocks, parent) - me.CurrentBlocks = append(me.CurrentBlocks, current) - - return "", me.extractError -} - -func (me *MockExtractor) SetExtractError(err error) { - me.extractError = err -} - -type MockChain struct { - ParentHashesLookedUp []common.Hash - parentBlocksToReturn []*types.Block - callCount int -} - -func (mc *MockChain) SetParentBlockToReturn(blocks []*types.Block) { - mc.parentBlocksToReturn = blocks -} - -func (mc *MockChain) GetBlockByHash(hash common.Hash) *types.Block { - mc.ParentHashesLookedUp = append(mc.ParentHashesLookedUp, hash) - - var parentBlock types.Block - if len(mc.parentBlocksToReturn) > 0 { - parentBlock = *mc.parentBlocksToReturn[mc.callCount] - } - - mc.callCount++ - return &parentBlock -} - -func (MockChain) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { - panic("implement me") -} - func TestServiceLoop(t *testing.T) { testServiceLoop(t) } @@ -85,10 +41,10 @@ func testServiceLoop(t *testing.T) { eventsChannel <- event1 eventsChannel <- event2 - extractor := MockExtractor{} + extractor := mocks.Extractor{} close(eventsChannel) - blockChain := MockChain{} + blockChain := mocks.BlockChain{} service := service2.StateDiffService{ Builder: nil, Extractor: &extractor, diff --git a/statediff/statediff_suite_test.go b/statediff/statediff_suite_test.go deleted file mode 100644 index e01d6eee0..000000000 --- a/statediff/statediff_suite_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package statediff_test - -import ( - "testing" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -func TestStatediff(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "Statediff Suite") -} - -//convert this over to use built in golang library -//only save the new value, and have a pointer to the old value - not sure how this pointer will work for the CSV version diff --git a/statediff/testhelpers/mocks.go b/statediff/testhelpers/mocks.go deleted file mode 100644 index 686550361..000000000 --- a/statediff/testhelpers/mocks.go +++ /dev/null @@ -1,49 +0,0 @@ -package testhelpers - -import ( - "errors" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/statediff/builder" -) - -var MockError = errors.New("mock error") - -type MockBuilder struct { - OldStateRoot common.Hash - NewStateRoot common.Hash - BlockNumber int64 - BlockHash common.Hash - stateDiff *builder.StateDiff - builderError error -} - -func (builder *MockBuilder) BuildStateDiff(oldStateRoot, newStateRoot common.Hash, blockNumber int64, blockHash common.Hash) (*builder.StateDiff, error) { - builder.OldStateRoot = oldStateRoot - builder.NewStateRoot = newStateRoot - builder.BlockNumber = blockNumber - builder.BlockHash = blockHash - - return builder.stateDiff, builder.builderError -} - -func (builder *MockBuilder) SetStateDiffToBuild(stateDiff *builder.StateDiff) { - builder.stateDiff = stateDiff -} - -func (builder *MockBuilder) SetBuilderError(err error) { - builder.builderError = err -} - -type MockPublisher struct { - StateDiff *builder.StateDiff - publisherError error -} - -func (publisher *MockPublisher) PublishStateDiff(sd *builder.StateDiff) (string, error) { - publisher.StateDiff = sd - return "", publisher.publisherError -} - -func (publisher *MockPublisher) SetPublisherError(err error) { - publisher.publisherError = err -} diff --git a/statediff/testhelpers/mocks/blockchain.go b/statediff/testhelpers/mocks/blockchain.go new file mode 100644 index 000000000..2b98ae97e --- /dev/null +++ b/statediff/testhelpers/mocks/blockchain.go @@ -0,0 +1,35 @@ +package mocks + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/event" +) + +type BlockChain struct { + ParentHashesLookedUp []common.Hash + parentBlocksToReturn []*types.Block + callCount int +} + +func (mc *BlockChain) SetParentBlockToReturn(blocks []*types.Block) { + mc.parentBlocksToReturn = blocks +} + +func (mc *BlockChain) GetBlockByHash(hash common.Hash) *types.Block { + mc.ParentHashesLookedUp = append(mc.ParentHashesLookedUp, hash) + + var parentBlock types.Block + if len(mc.parentBlocksToReturn) > 0 { + parentBlock = *mc.parentBlocksToReturn[mc.callCount] + } + + mc.callCount++ + return &parentBlock +} + +func (BlockChain) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { + panic("implement me") +} + diff --git a/statediff/testhelpers/mocks/builder.go b/statediff/testhelpers/mocks/builder.go new file mode 100644 index 000000000..13f6450ee --- /dev/null +++ b/statediff/testhelpers/mocks/builder.go @@ -0,0 +1,33 @@ +package mocks + +import ( + "github.com/ethereum/go-ethereum/statediff/builder" + "github.com/ethereum/go-ethereum/common" +) + +type Builder struct { + OldStateRoot common.Hash + NewStateRoot common.Hash + BlockNumber int64 + BlockHash common.Hash + stateDiff *builder.StateDiff + builderError error +} + +func (builder *Builder) BuildStateDiff(oldStateRoot, newStateRoot common.Hash, blockNumber int64, blockHash common.Hash) (*builder.StateDiff, error) { + builder.OldStateRoot = oldStateRoot + builder.NewStateRoot = newStateRoot + builder.BlockNumber = blockNumber + builder.BlockHash = blockHash + + return builder.stateDiff, builder.builderError +} + +func (builder *Builder) SetStateDiffToBuild(stateDiff *builder.StateDiff) { + builder.stateDiff = stateDiff +} + +func (builder *Builder) SetBuilderError(err error) { + builder.builderError = err +} + diff --git a/statediff/testhelpers/mocks/error.go b/statediff/testhelpers/mocks/error.go new file mode 100644 index 000000000..7c40452ae --- /dev/null +++ b/statediff/testhelpers/mocks/error.go @@ -0,0 +1,5 @@ +package mocks + +import "errors" + +var Error = errors.New("mock error") diff --git a/statediff/testhelpers/mocks/extractor.go b/statediff/testhelpers/mocks/extractor.go new file mode 100644 index 000000000..ac0f82975 --- /dev/null +++ b/statediff/testhelpers/mocks/extractor.go @@ -0,0 +1,21 @@ +package mocks + +import "github.com/ethereum/go-ethereum/core/types" + +type Extractor struct { + ParentBlocks []types.Block + CurrentBlocks []types.Block + extractError error +} + +func (me *Extractor) ExtractStateDiff(parent, current types.Block) (string, error) { + me.ParentBlocks = append(me.ParentBlocks, parent) + me.CurrentBlocks = append(me.CurrentBlocks, current) + + return "", me.extractError +} + +func (me *Extractor) SetExtractError(err error) { + me.extractError = err +} + diff --git a/statediff/testhelpers/mocks/publisher.go b/statediff/testhelpers/mocks/publisher.go new file mode 100644 index 000000000..efbe4e2ab --- /dev/null +++ b/statediff/testhelpers/mocks/publisher.go @@ -0,0 +1,17 @@ +package mocks + +import "github.com/ethereum/go-ethereum/statediff/builder" + +type Publisher struct { + StateDiff *builder.StateDiff + publisherError error +} + +func (publisher *Publisher) PublishStateDiff(sd *builder.StateDiff) (string, error) { + publisher.StateDiff = sd + return "", publisher.publisherError +} + +func (publisher *Publisher) SetPublisherError(err error) { + publisher.publisherError = err +}