Move MockTransformer to test_data

This commit is contained in:
Edvard 2018-12-13 13:03:51 +01:00
parent d30fcfed80
commit e5db740ae4
2 changed files with 59 additions and 51 deletions

View File

@ -16,42 +16,6 @@ import (
"github.com/vulcanize/vulcanizedb/test_config" "github.com/vulcanize/vulcanizedb/test_config"
) )
type MockTransformer struct {
executeWasCalled bool
executeError error
passedLogs []types.Log
passedHeader core.Header
config shared2.TransformerConfig
}
func (mh *MockTransformer) Execute(logs []types.Log, header core.Header) error {
if mh.executeError != nil {
return mh.executeError
}
mh.executeWasCalled = true
mh.passedLogs = logs
mh.passedHeader = header
return nil
}
func (mh *MockTransformer) GetConfig() shared2.TransformerConfig {
return mh.config
}
func (mh *MockTransformer) SetTransformerConfig(config shared2.TransformerConfig) {
mh.config = config
}
func (mh *MockTransformer) FakeTransformerInitializer(db *postgres.DB) shared2.Transformer {
return mh
}
var fakeTransformerConfig = shared2.TransformerConfig{
TransformerName: "FakeTransformer",
ContractAddresses: []string{"FakeAddress"},
Topic: "FakeTopic",
}
var _ = Describe("Watcher", func() { var _ = Describe("Watcher", func() {
It("initialises correctly", func() { It("initialises correctly", func() {
db := test_config.NewTestDB(core.Node{ID: "testNode"}) db := test_config.NewTestDB(core.Node{ID: "testNode"})
@ -68,8 +32,8 @@ var _ = Describe("Watcher", func() {
It("adds transformers", func() { It("adds transformers", func() {
watcher := shared.NewWatcher(nil, nil, nil) watcher := shared.NewWatcher(nil, nil, nil)
fakeTransformer := &MockTransformer{} fakeTransformer := &mocks.MockTransformer{}
fakeTransformer.SetTransformerConfig(fakeTransformerConfig) fakeTransformer.SetTransformerConfig(mocks.FakeTransformerConfig)
watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformer.FakeTransformerInitializer}) watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformer.FakeTransformerInitializer})
Expect(len(watcher.Transformers)).To(Equal(1)) Expect(len(watcher.Transformers)).To(Equal(1))
@ -80,11 +44,11 @@ var _ = Describe("Watcher", func() {
It("adds transformers from multiple sources", func() { It("adds transformers from multiple sources", func() {
watcher := shared.NewWatcher(nil, nil, nil) watcher := shared.NewWatcher(nil, nil, nil)
fakeTransformer1 := &MockTransformer{} fakeTransformer1 := &mocks.MockTransformer{}
fakeTransformer1.SetTransformerConfig(fakeTransformerConfig) fakeTransformer1.SetTransformerConfig(mocks.FakeTransformerConfig)
fakeTransformer2 := &MockTransformer{} fakeTransformer2 := &mocks.MockTransformer{}
fakeTransformer2.SetTransformerConfig(fakeTransformerConfig) fakeTransformer2.SetTransformerConfig(mocks.FakeTransformerConfig)
watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformer1.FakeTransformerInitializer}) watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformer1.FakeTransformerInitializer})
watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformer2.FakeTransformerInitializer}) watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformer2.FakeTransformerInitializer})
@ -100,7 +64,7 @@ var _ = Describe("Watcher", func() {
var ( var (
db *postgres.DB db *postgres.DB
watcher shared.Watcher watcher shared.Watcher
fakeTransformer *MockTransformer fakeTransformer *mocks.MockTransformer
headerRepository repositories.HeaderRepository headerRepository repositories.HeaderRepository
mockFetcher mocks.MockLogFetcher mockFetcher mocks.MockLogFetcher
repository mocks.MockWatcherRepository repository mocks.MockWatcherRepository
@ -119,30 +83,30 @@ var _ = Describe("Watcher", func() {
}) })
It("executes each transformer", func() { It("executes each transformer", func() {
fakeTransformer = &MockTransformer{} fakeTransformer = &mocks.MockTransformer{}
watcher.Transformers = []shared2.Transformer{fakeTransformer} watcher.Transformers = []shared2.Transformer{fakeTransformer}
repository.SetMissingHeaders([]core.Header{fakes.FakeHeader}) repository.SetMissingHeaders([]core.Header{fakes.FakeHeader})
err := watcher.Execute() err := watcher.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(fakeTransformer.executeWasCalled).To(BeTrue()) Expect(fakeTransformer.ExecuteWasCalled).To(BeTrue())
}) })
It("returns an error if transformer returns an error", func() { It("returns an error if transformer returns an error", func() {
fakeTransformer = &MockTransformer{executeError: errors.New("Something bad happened")} fakeTransformer = &mocks.MockTransformer{ExecuteError: errors.New("Something bad happened")}
watcher.Transformers = []shared2.Transformer{fakeTransformer} watcher.Transformers = []shared2.Transformer{fakeTransformer}
repository.SetMissingHeaders([]core.Header{fakes.FakeHeader}) repository.SetMissingHeaders([]core.Header{fakes.FakeHeader})
err := watcher.Execute() err := watcher.Execute()
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(fakeTransformer.executeWasCalled).To(BeFalse()) Expect(fakeTransformer.ExecuteWasCalled).To(BeFalse())
}) })
It("passes only relevant logs to each transformer", func() { It("passes only relevant logs to each transformer", func() {
transformerA := &MockTransformer{} transformerA := &mocks.MockTransformer{}
transformerB := &MockTransformer{} transformerB := &mocks.MockTransformer{}
configA := shared2.TransformerConfig{TransformerName: "transformerA", configA := shared2.TransformerConfig{TransformerName: "transformerA",
ContractAddresses: []string{"0x000000000000000000000000000000000000000A"}, ContractAddresses: []string{"0x000000000000000000000000000000000000000A"},
@ -167,8 +131,8 @@ var _ = Describe("Watcher", func() {
err := watcher.Execute() err := watcher.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(transformerA.passedLogs).To(Equal([]types.Log{logA})) Expect(transformerA.PassedLogs).To(Equal([]types.Log{logA}))
Expect(transformerB.passedLogs).To(Equal([]types.Log{logB})) Expect(transformerB.PassedLogs).To(Equal([]types.Log{logB}))
}) })
Describe("uses the repository correctly:", func() { Describe("uses the repository correctly:", func() {

View File

@ -0,0 +1,44 @@
package mocks
import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
)
type MockTransformer struct {
ExecuteWasCalled bool
ExecuteError error
PassedLogs []types.Log
PassedHeader core.Header
config shared.TransformerConfig
}
func (mh *MockTransformer) Execute(logs []types.Log, header core.Header) error {
if mh.ExecuteError != nil {
return mh.ExecuteError
}
mh.ExecuteWasCalled = true
mh.PassedLogs = logs
mh.PassedHeader = header
return nil
}
func (mh *MockTransformer) GetConfig() shared.TransformerConfig {
return mh.config
}
func (mh *MockTransformer) SetTransformerConfig(config shared.TransformerConfig) {
mh.config = config
}
func (mh *MockTransformer) FakeTransformerInitializer(db *postgres.DB) shared.Transformer {
return mh
}
var FakeTransformerConfig = shared.TransformerConfig{
TransformerName: "FakeTransformer",
ContractAddresses: []string{"FakeAddress"},
Topic: "FakeTopic",
}