Update tests for deal transformer

This commit is contained in:
Edvard 2018-10-26 14:36:07 +02:00 committed by Rob Mulholand
parent 35a8009410
commit 0e1675354f
6 changed files with 62 additions and 169 deletions

View File

@ -38,7 +38,8 @@ var _ = Describe("Deal Repository", func() {
BeforeEach(func() {
db = test_config.NewTestDB(test_config.NewTestNode())
test_config.CleanTestDB(db)
dealRepository = deal.NewDealRepository(db)
dealRepository = deal.DealRepository{}
dealRepository.SetDB(db)
headerRepository = repositories.NewHeaderRepository(db)
})
@ -48,7 +49,7 @@ var _ = Describe("Deal Repository", func() {
BeforeEach(func() {
headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err := dealRepository.Create(headerId, []deal.DealModel{test_data.DealModel})
err := dealRepository.Create(headerId, []interface{}{test_data.DealModel})
Expect(err).NotTo(HaveOccurred())
})
@ -74,7 +75,7 @@ var _ = Describe("Deal Repository", func() {
})
It("returns an error if inserting a deal record fails", func() {
err = dealRepository.Create(headerId, []deal.DealModel{test_data.DealModel})
err = dealRepository.Create(headerId, []interface{}{test_data.DealModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
@ -175,7 +176,8 @@ var _ = Describe("Deal Repository", func() {
Expect(err).NotTo(HaveOccurred())
node2 := core.Node{}
db2 := test_config.NewTestDB(node2)
dealRepository2 := deal.NewDealRepository(db2)
dealRepository2 := deal.DealRepository{}
dealRepository2.SetDB(db2)
headerRepository2 := repositories.NewHeaderRepository(db2)
var node2HeaderIds []int64
for _, number := range blockNumbers {

View File

@ -17,6 +17,7 @@ package deal_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/transformers/factories"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
@ -26,56 +27,63 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks"
deal_mocks "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/deal"
"math/rand"
)
var _ = Describe("DealTransformer", func() {
var config = deal.Config
var dealRepository deal_mocks.MockDealRepository
var config = deal.DealConfig
var repository mocks.MockRepository
var fetcher mocks.MockLogFetcher
var converter deal_mocks.MockDealConverter
var transformer deal.DealTransformer
var converter mocks.MockConverter
var transformer shared.Transformer
var headerOne core.Header
var headerTwo core.Header
BeforeEach(func() {
dealRepository = deal_mocks.MockDealRepository{}
repository = mocks.MockRepository{}
fetcher = mocks.MockLogFetcher{}
converter = deal_mocks.MockDealConverter{}
transformer = deal.DealTransformer{
Repository: &dealRepository,
converter = mocks.MockConverter{}
transformer = factories.Transformer{
Config: config,
Fetcher: &fetcher,
Converter: &converter,
}
Repository: &repository,
Fetcher: &fetcher,
}.NewTransformer(nil, nil)
headerOne = core.Header{BlockNumber: rand.Int63(), Id: rand.Int63()}
headerTwo = core.Header{BlockNumber: rand.Int63(), Id: rand.Int63()}
})
It("sets the blockchain and database", func() {
Expect(fetcher.SetBcCalled).To(BeTrue())
Expect(repository.SetDbCalled).To(BeTrue())
})
It("gets missing headers", func() {
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())
Expect(dealRepository.PassedStartingBlockNumber).To(Equal(config.StartingBlockNumber))
Expect(dealRepository.PassedEndingBlockNumber).To(Equal(config.EndingBlockNumber))
Expect(repository.PassedStartingBlockNumber).To(Equal(config.StartingBlockNumber))
Expect(repository.PassedEndingBlockNumber).To(Equal(config.EndingBlockNumber))
})
It("returns an error if fetching the missing headers fails", func() {
dealRepository.SetMissingHeadersErr(fakes.FakeError)
repository.SetMissingHeadersError(fakes.FakeError)
err := transformer.Execute()
Expect(err).To(HaveOccurred())
})
It("fetches logs for each missing header", func() {
header1 := core.Header{BlockNumber: rand.Int63()}
header2 := core.Header{BlockNumber: rand.Int63()}
dealRepository.SetMissingHeaders([]core.Header{header1, header2})
repository.SetMissingHeaders([]core.Header{headerOne, headerTwo})
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())
Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{config.ContractAddresses, config.ContractAddresses}))
Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{
config.ContractAddresses, config.ContractAddresses}))
expectedTopics := [][]common.Hash{{common.HexToHash(shared.DealSignature)}}
Expect(fetcher.FetchedTopics).To(Equal(expectedTopics))
Expect(fetcher.FetchedBlocks).To(Equal([]int64{header1.BlockNumber, header2.BlockNumber}))
Expect(fetcher.FetchedBlocks).To(Equal([]int64{headerOne.BlockNumber, headerTwo.BlockNumber}))
})
It("returns an error if fetching logs fails", func() {
dealRepository.SetMissingHeaders([]core.Header{{}})
repository.SetMissingHeaders([]core.Header{{}})
fetcher.SetFetcherError(fakes.FakeError)
err := transformer.Execute()
Expect(err).To(HaveOccurred())
@ -83,34 +91,17 @@ var _ = Describe("DealTransformer", func() {
})
It("marks header checked if no logs returned", func() {
mockConverter := &deal_mocks.MockDealConverter{}
mockRepository := &deal_mocks.MockDealRepository{}
headerID := int64(123)
mockRepository.SetMissingHeaders([]core.Header{{Id: headerID}})
mockFetcher := &mocks.MockLogFetcher{}
transformer := deal.DealTransformer{
Converter: mockConverter,
Fetcher: mockFetcher,
Repository: mockRepository,
}
repository.SetMissingHeaders([]core.Header{headerOne})
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())
mockRepository.AssertMarkHeaderCheckedCalledWith(headerID)
repository.AssertMarkHeaderCheckedCalledWith(headerOne.Id)
})
It("returns error if marking header checked returns err", func() {
mockConverter := &deal_mocks.MockDealConverter{}
mockRepository := &deal_mocks.MockDealRepository{}
mockRepository.SetMissingHeaders([]core.Header{{Id: int64(123)}})
mockRepository.SetMarkHeaderCheckedErr(fakes.FakeError)
mockFetcher := &mocks.MockLogFetcher{}
transformer := deal.DealTransformer{
Converter: mockConverter,
Fetcher: mockFetcher,
Repository: mockRepository,
}
repository.SetMissingHeaders([]core.Header{headerOne})
repository.SetMarkHeaderCheckedError(fakes.FakeError)
err := transformer.Execute()
@ -119,37 +110,40 @@ var _ = Describe("DealTransformer", func() {
})
It("converts each eth log to a Model", func() {
dealRepository.SetMissingHeaders([]core.Header{{}})
repository.SetMissingHeaders([]core.Header{headerOne})
fetcher.SetFetchedLogs([]types.Log{test_data.DealLogNote})
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())
Expect(converter.LogsToConvert).To(Equal([]types.Log{test_data.DealLogNote}))
Expect(converter.PassedLogs).To(Equal([]types.Log{test_data.DealLogNote}))
})
It("returns an error if converting the eth log fails", func() {
dealRepository.SetMissingHeaders([]core.Header{{}})
repository.SetMissingHeaders([]core.Header{{}})
fetcher.SetFetchedLogs([]types.Log{test_data.DealLogNote})
converter.SetConverterError(fakes.FakeError)
err := transformer.Execute()
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(fakes.FakeError))
})
It("persists each model as a Deal record", func() {
header1 := core.Header{Id: rand.Int63()}
header2 := core.Header{Id: rand.Int63()}
dealRepository.SetMissingHeaders([]core.Header{header1, header2})
repository.SetMissingHeaders([]core.Header{headerOne})
fetcher.SetFetchedLogs([]types.Log{test_data.DealLogNote})
converter.SetReturnModels([]interface{}{test_data.DealModel})
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())
Expect(dealRepository.PassedDealModels).To(Equal([]deal.DealModel{test_data.DealModel, test_data.DealModel}))
Expect(dealRepository.PassedHeaderIDs).To(Equal([]int64{header1.Id, header2.Id}))
Expect(repository.PassedModels).To(Equal([]interface{}{test_data.DealModel}))
Expect(repository.PassedHeaderID).To(Equal(headerOne.Id))
})
It("returns an error if persisting deal record fails", func() {
dealRepository.SetMissingHeaders([]core.Header{{}})
dealRepository.SetCreateError(fakes.FakeError)
repository.SetMissingHeaders([]core.Header{headerOne})
repository.SetCreateError(fakes.FakeError)
fetcher.SetFetchedLogs([]types.Log{test_data.DealLogNote})
err := transformer.Execute()
Expect(err).To(HaveOccurred())
})

View File

@ -18,6 +18,7 @@ import (
"github.com/ethereum/go-ethereum/ethclient"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/transformers/factories"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
@ -51,12 +52,17 @@ var _ = Describe("Deal transformer", func() {
err = persistHeader(db, flipBlockNumber)
Expect(err).NotTo(HaveOccurred())
config := deal.Config
config := deal.DealConfig
config.StartingBlockNumber = flipBlockNumber
config.EndingBlockNumber = flipBlockNumber
initializer := deal.DealTransformerInitializer{Config: config}
transformer := initializer.NewDealTransformer(db, blockchain)
initializer := factories.Transformer{
Config: config,
Converter: &deal.DealConverter{},
Repository: &deal.DealRepository{},
Fetcher: &shared.Fetcher{},
}
transformer := initializer.NewTransformer(db, blockchain)
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())

View File

@ -1,38 +0,0 @@
// Copyright 2018 Vulcanize
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package deal
import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/transformers/deal"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
)
type MockDealConverter struct {
ConverterContract string
ConverterAbi string
LogsToConvert []types.Log
ConverterError error
}
func (c *MockDealConverter) ToModels(ethLogs []types.Log) ([]deal.DealModel, error) {
c.LogsToConvert = append(c.LogsToConvert, ethLogs...)
return []deal.DealModel{test_data.DealModel}, c.ConverterError
}
func (c *MockDealConverter) SetConverterError(err error) {
c.ConverterError = err
}

View File

@ -1,71 +0,0 @@
// Copyright 2018 Vulcanize
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package deal
import (
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/transformers/deal"
)
type MockDealRepository struct {
createError error
PassedEndingBlockNumber int64
PassedHeaderIDs []int64
PassedStartingBlockNumber int64
PassedDealModels []deal.DealModel
markHeaderCheckedErr error
markHeaderCheckedPassedHeaderID int64
missingHeaders []core.Header
missingHeadersErr error
}
func (repository *MockDealRepository) Create(headerId int64, deals []deal.DealModel) error {
repository.PassedHeaderIDs = append(repository.PassedHeaderIDs, headerId)
repository.PassedDealModels = append(repository.PassedDealModels, deals...)
return repository.createError
}
func (repository *MockDealRepository) SetCreateError(err error) {
repository.createError = err
}
func (repository *MockDealRepository) SetMarkHeaderCheckedErr(err error) {
repository.markHeaderCheckedErr = err
}
func (repository *MockDealRepository) SetMissingHeadersErr(err error) {
repository.missingHeadersErr = err
}
func (repository *MockDealRepository) SetMissingHeaders(headers []core.Header) {
repository.missingHeaders = headers
}
func (repository *MockDealRepository) MarkHeaderChecked(headerID int64) error {
repository.markHeaderCheckedPassedHeaderID = headerID
return repository.markHeaderCheckedErr
}
func (repository *MockDealRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
repository.PassedStartingBlockNumber = startingBlockNumber
repository.PassedEndingBlockNumber = endingBlockNumber
return repository.missingHeaders, repository.missingHeadersErr
}
func (repository *MockDealRepository) AssertMarkHeaderCheckedCalledWith(headerID int64) {
Expect(repository.markHeaderCheckedPassedHeaderID).To(Equal(headerID))
}

View File

@ -48,7 +48,7 @@ import (
)
var (
BiteTransformerInitializer = bite.BiteTransformerInitializer{Config: bite.BiteConfig}.NewBiteTransformer
BiteTransformerInitializer = bite.BiteTransformerInitializer{Config: bite.BiteConfig}.NewBiteTransformer
CatFileChopLumpTransformerInitializer = factories.Transformer{
Config: chop_lump.CatFileChopLumpConfig,
@ -71,7 +71,7 @@ var (
Fetcher: &shared.Fetcher{},
}.NewTransformer
DealTransformerInitializer = factories.Transformer{
DealTransformerInitializer = factories.Transformer{
Config: deal.DealConfig,
Converter: &deal.DealConverter{},
Repository: &deal.DealRepository{},