Update tests with new adding of transformers

This commit is contained in:
Edvard 2018-12-12 15:41:44 +01:00
parent dc43547612
commit 882a6dd7a2
8 changed files with 54 additions and 33 deletions

View File

@ -11,7 +11,6 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
"github.com/vulcanize/vulcanizedb/pkg/transformers"
shared2 "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks"
"github.com/vulcanize/vulcanizedb/test_config"
@ -47,48 +46,51 @@ func fakeTransformerInitializer(db *postgres.DB) shared2.Transformer {
return &MockTransformer{}
}
var fakeTransformerConfig = []shared2.TransformerConfig{{
TransformerName: "FakeTransformer",
ContractAddresses: []string{"FakeAddress"},
Topic: "FakeTopic",
}}
var _ = Describe("Watcher", func() {
It("initialises correctly", func() {
db := test_config.NewTestDB(core.Node{ID: "testNode"})
fetcher := mocks.MockLogFetcher{}
fetcher := &mocks.MockLogFetcher{}
repository := &mocks.MockWatcherRepository{}
configA := shared2.TransformerConfig{
ContractAddresses: []string{"0xA"},
Topic: "0xA",
}
configB := shared2.TransformerConfig{
ContractAddresses: []string{"0xB"},
Topic: "0xB",
}
configs := []shared2.TransformerConfig{configA, configB}
watcher := shared.NewWatcher(db, &fetcher, repository, configs)
chunker := shared2.NewLogChunker()
watcher := shared.NewWatcher(db, fetcher, repository, chunker)
Expect(watcher.DB).To(Equal(db))
Expect(watcher.Fetcher).NotTo(BeNil())
Expect(watcher.Chunker).NotTo(BeNil())
Expect(watcher.Fetcher).To(Equal(fetcher))
Expect(watcher.Chunker).To(Equal(chunker))
Expect(watcher.Repository).To(Equal(repository))
Expect(watcher.Topics).To(And(
ContainElement(common.HexToHash("0xA")), ContainElement(common.HexToHash("0xB"))))
Expect(watcher.Addresses).To(And(
ContainElement(common.HexToAddress("0xA")), ContainElement(common.HexToAddress("0xB"))))
})
It("adds transformers", func() {
watcher := shared.Watcher{}
chunker := shared2.NewLogChunker()
watcher := shared.NewWatcher(nil, nil, nil, chunker)
watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformerInitializer})
watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformerInitializer}, fakeTransformerConfig)
Expect(len(watcher.Transformers)).To(Equal(1))
Expect(watcher.Transformers).To(ConsistOf(&MockTransformer{}))
Expect(watcher.Topics).To(Equal([]common.Hash{common.HexToHash("FakeTopic")}))
Expect(watcher.Addresses).To(Equal([]common.Address{common.HexToAddress("FakeAddress")}))
})
It("adds transformers from multiple sources", func() {
watcher := shared.Watcher{}
chunker := shared2.NewLogChunker()
watcher := shared.NewWatcher(nil, nil, nil, chunker)
watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformerInitializer})
watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformerInitializer})
watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformerInitializer}, fakeTransformerConfig)
watcher.AddTransformers([]shared2.TransformerInitializer{fakeTransformerInitializer}, fakeTransformerConfig)
Expect(len(watcher.Transformers)).To(Equal(2))
Expect(watcher.Topics).To(Equal([]common.Hash{common.HexToHash("FakeTopic"),
common.HexToHash("FakeTopic")}))
Expect(watcher.Addresses).To(Equal([]common.Address{common.HexToAddress("FakeAddress"),
common.HexToAddress("FakeAddress")}))
})
Describe("with missing headers", func() {
@ -99,6 +101,7 @@ var _ = Describe("Watcher", func() {
headerRepository repositories.HeaderRepository
mockFetcher mocks.MockLogFetcher
repository mocks.MockWatcherRepository
chunker *shared2.LogChunker
)
BeforeEach(func() {
@ -108,9 +111,10 @@ var _ = Describe("Watcher", func() {
headerRepository = repositories.NewHeaderRepository(db)
_, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
chunker = shared2.NewLogChunker()
repository = mocks.MockWatcherRepository{}
watcher = shared.NewWatcher(db, &mockFetcher, &repository, transformers.TransformerConfigs())
watcher = shared.NewWatcher(db, &mockFetcher, &repository, chunker)
})
It("executes each transformer", func() {
@ -155,8 +159,10 @@ var _ = Describe("Watcher", func() {
Topics: []common.Hash{common.HexToHash("0xB")}}
mockFetcher.SetFetchedLogs([]types.Log{logA, logB})
chunker.AddConfigs(configs)
repository.SetMissingHeaders([]core.Header{fakes.FakeHeader})
watcher = shared.NewWatcher(db, &mockFetcher, &repository, configs)
watcher = shared.NewWatcher(db, &mockFetcher, &repository, chunker)
watcher.Transformers = []shared2.Transformer{transformerA, transformerB}
err := watcher.Execute()

View File

@ -40,7 +40,7 @@ var _ = Describe("Cat File transformer", func() {
rpcClient client.RpcClient
err error
ethClient *ethclient.Client
fetcher shared.Fetcher
fetcher *shared.Fetcher
)
BeforeEach(func() {

View File

@ -34,7 +34,7 @@ var _ = Describe("Deal transformer", func() {
blockChain core.BlockChain
config shared.TransformerConfig
initializer factories.LogNoteTransformer
fetcher shared.Fetcher
fetcher *shared.Fetcher
addresses []common.Address
topics []common.Hash
)

View File

@ -18,7 +18,7 @@ var _ = Describe("Dent transformer", func() {
var (
db *postgres.DB
blockChain core.BlockChain
fetcher shared.Fetcher
fetcher *shared.Fetcher
transformer shared.Transformer
config shared.TransformerConfig
addresses []common.Address

View File

@ -35,7 +35,7 @@ var _ = Describe("Frob Transformer", func() {
var (
db *postgres.DB
blockChain core.BlockChain
fetcher shared.Fetcher
fetcher *shared.Fetcher
config shared.TransformerConfig
initializer factories.Transformer
)

View File

@ -33,7 +33,7 @@ var _ = Describe("Price feeds transformer", func() {
db *postgres.DB
blockChain core.BlockChain
config shared.TransformerConfig
fetcher shared.Fetcher
fetcher *shared.Fetcher
initializer factories.LogNoteTransformer
topics []common.Hash
)

View File

@ -32,7 +32,7 @@ var _ = Describe("Tend LogNoteTransformer", func() {
db *postgres.DB
blockChain core.BlockChain
config shared.TransformerConfig
fetcher shared.Fetcher
fetcher *shared.Fetcher
initializer factories.LogNoteTransformer
addresses []common.Address
topics []common.Hash

View File

@ -25,7 +25,7 @@ import (
var _ = Describe("Log chunker", func() {
var (
configs []shared.TransformerConfig
chunker shared.LogChunker
chunker *shared.LogChunker
)
BeforeEach(func() {
@ -47,7 +47,8 @@ var _ = Describe("Log chunker", func() {
}
configs = []shared.TransformerConfig{configA, configB, configC}
chunker = shared.NewLogChunker(configs)
chunker = shared.NewLogChunker()
chunker.AddConfigs(configs)
})
Describe("initialisation", func() {
@ -66,6 +67,20 @@ var _ = Describe("Log chunker", func() {
})
})
Describe("AddConfigs", func() {
It("can add more configs later", func() {
configD := shared.TransformerConfig{
TransformerName: "TransformerD",
ContractAddresses: []string{"0x000000000000000000000000000000000000000D"},
Topic: "0xD",
}
chunker.AddConfigs([]shared.TransformerConfig{configD})
Expect(chunker.AddressToNames).To(ContainElement([]string{"TransformerD"}))
Expect(chunker.NameToTopic0).To(ContainElement(common.HexToHash("0xD")))
})
})
Describe("ChunkLogs", func() {
It("only associates logs with relevant topic0 and address to transformers", func() {
logs := []types.Log{log1, log2, log3, log4, log5}