Update tests for pit file ilk transformer

This commit is contained in:
Edvard 2018-10-16 12:46:00 +02:00
parent 98062a7970
commit 399b3b78a0
5 changed files with 68 additions and 113 deletions

View File

@ -54,6 +54,6 @@ var _ = Describe("Pit file ilk converter", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(len(models)).To(Equal(1)) Expect(len(models)).To(Equal(1))
Expect(models[0]).To(Equal(test_data.PitFileIlkModel)) Expect(models[0].(ilk.PitFileIlkModel)).To(Equal(test_data.PitFileIlkModel))
}) })
}) })

View File

@ -33,7 +33,7 @@ import (
var _ = Describe("Pit file ilk repository", func() { var _ = Describe("Pit file ilk repository", func() {
var ( var (
db *postgres.DB db *postgres.DB
pitFileRepository ilk.Repository pitFileRepository ilk.PitFileIlkRepository
err error err error
headerRepository datastore.HeaderRepository headerRepository datastore.HeaderRepository
) )
@ -42,18 +42,24 @@ var _ = Describe("Pit file ilk repository", func() {
db = test_config.NewTestDB(core.Node{}) db = test_config.NewTestDB(core.Node{})
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
headerRepository = repositories.NewHeaderRepository(db) headerRepository = repositories.NewHeaderRepository(db)
pitFileRepository = ilk.NewPitFileIlkRepository(db) pitFileRepository = ilk.PitFileIlkRepository{DB: db}
}) })
Describe("Create", func() { Describe("Create", func() {
var headerID int64 var (
db *postgres.DB
pitFileIlkRepository ilk.PitFileIlkRepository
err error
headerID int64
)
BeforeEach(func() { BeforeEach(func() {
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
err = pitFileRepository.Create(headerID, []ilk.PitFileIlkModel{test_data.PitFileIlkModel}) err = pitFileRepository.Create(headerID, []interface{}{test_data.PitFileIlkModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
pitFileIlkRepository = ilk.PitFileIlkRepository{DB: db}
}) })
It("adds a pit file ilk event", func() { It("adds a pit file ilk event", func() {
@ -75,7 +81,7 @@ var _ = Describe("Pit file ilk repository", func() {
}) })
It("does not duplicate pit file ilk events", func() { It("does not duplicate pit file ilk events", func() {
err = pitFileRepository.Create(headerID, []ilk.PitFileIlkModel{test_data.PitFileIlkModel}) err = pitFileRepository.Create(headerID, []interface{}{test_data.PitFileIlkModel})
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
@ -176,7 +182,8 @@ var _ = Describe("Pit file ilk repository", func() {
_, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n)) _, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
} }
pitFileRepositoryTwo := ilk.NewPitFileIlkRepository(dbTwo)
pitFileRepositoryTwo := ilk.PitFileIlkRepository{DB: dbTwo}
err := pitFileRepository.MarkHeaderChecked(headerIDs[0]) err := pitFileRepository.MarkHeaderChecked(headerIDs[0])
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())

View File

@ -19,6 +19,7 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/transformers/factories"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/fakes"
@ -31,15 +32,31 @@ import (
) )
var _ = Describe("Pit file ilk transformer", func() { var _ = Describe("Pit file ilk transformer", func() {
It("gets missing headers for block numbers specified in config", func() { var (
repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} config = ilk.IlkFileConfig
transformer := ilk.PitFileIlkTransformer{ fetcher mocks.MockLogFetcher
Config: pit_file.PitFileConfig, converter pit_file_ilk_mocks.MockPitFileIlkConverter
Fetcher: &mocks.MockLogFetcher{}, repository pit_file_ilk_mocks.MockPitFileIlkRepository
Converter: &pit_file_ilk_mocks.MockPitFileIlkConverter{}, transformer shared.Transformer
Repository: repository, headerOne core.Header
} headerTwo core.Header
)
BeforeEach(func() {
fetcher = mocks.MockLogFetcher{}
converter = pit_file_ilk_mocks.MockPitFileIlkConverter{}
repository = pit_file_ilk_mocks.MockPitFileIlkRepository{}
headerOne = core.Header{Id: GinkgoRandomSeed(), BlockNumber: GinkgoRandomSeed()}
headerTwo = core.Header{Id: GinkgoRandomSeed(), BlockNumber: GinkgoRandomSeed()}
transformer = factories.Transformer{
Config: config,
Converter: &converter,
Repository: &repository,
Fetcher: &fetcher,
}.NewTransformer(nil, nil)
})
It("gets missing headers for block numbers specified in config", func() {
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
@ -48,13 +65,7 @@ var _ = Describe("Pit file ilk transformer", func() {
}) })
It("returns error if repository returns error for missing headers", func() { It("returns error if repository returns error for missing headers", func() {
repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} repository.SetMissingHeadersError(fakes.FakeError)
repository.SetMissingHeadersErr(fakes.FakeError)
transformer := ilk.PitFileIlkTransformer{
Fetcher: &mocks.MockLogFetcher{},
Converter: &pit_file_ilk_mocks.MockPitFileIlkConverter{},
Repository: repository,
}
err := transformer.Execute() err := transformer.Execute()
@ -63,33 +74,19 @@ var _ = Describe("Pit file ilk transformer", func() {
}) })
It("fetches logs for missing headers", func() { It("fetches logs for missing headers", func() {
fetcher := &mocks.MockLogFetcher{} repository.SetMissingHeaders([]core.Header{headerOne, headerTwo})
repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}, {BlockNumber: 2}})
transformer := ilk.PitFileIlkTransformer{
Fetcher: fetcher,
Converter: &pit_file_ilk_mocks.MockPitFileIlkConverter{},
Repository: repository,
}
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(fetcher.FetchedBlocks).To(Equal([]int64{1, 2})) Expect(fetcher.FetchedBlocks).To(Equal([]int64{headerOne.BlockNumber, headerTwo.BlockNumber}))
Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{pit_file.PitFileConfig.ContractAddresses, pit_file.PitFileConfig.ContractAddresses})) Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{pit_file.PitFileConfig.ContractAddresses, pit_file.PitFileConfig.ContractAddresses}))
Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.PitFileIlkSignature)}})) Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.PitFileIlkSignature)}}))
}) })
It("returns error if fetcher returns error", func() { It("returns error if fetcher returns error", func() {
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetcherError(fakes.FakeError) fetcher.SetFetcherError(fakes.FakeError)
repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} repository.SetMissingHeaders([]core.Header{headerOne})
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
transformer := ilk.PitFileIlkTransformer{
Fetcher: fetcher,
Converter: &pit_file_ilk_mocks.MockPitFileIlkConverter{},
Repository: repository,
}
err := transformer.Execute() err := transformer.Execute()
@ -98,34 +95,17 @@ var _ = Describe("Pit file ilk transformer", func() {
}) })
It("marks header checked if no logs returned", func() { It("marks header checked if no logs returned", func() {
mockConverter := &pit_file_ilk_mocks.MockPitFileIlkConverter{} repository.SetMissingHeaders([]core.Header{headerOne})
mockRepository := &pit_file_ilk_mocks.MockPitFileIlkRepository{}
headerID := int64(123)
mockRepository.SetMissingHeaders([]core.Header{{Id: headerID}})
mockFetcher := &mocks.MockLogFetcher{}
transformer := ilk.PitFileIlkTransformer{
Converter: mockConverter,
Fetcher: mockFetcher,
Repository: mockRepository,
}
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
mockRepository.AssertMarkHeaderCheckedCalledWith(headerID) repository.AssertMarkHeaderCheckedCalledWith(headerOne.Id)
}) })
It("returns error if marking header checked returns err", func() { It("returns error if marking header checked returns err", func() {
mockConverter := &pit_file_ilk_mocks.MockPitFileIlkConverter{} repository.SetMissingHeaders([]core.Header{headerOne})
mockRepository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} repository.SetMarkHeaderCheckedError(fakes.FakeError)
mockRepository.SetMissingHeaders([]core.Header{{Id: int64(123)}})
mockRepository.SetMarkHeaderCheckedErr(fakes.FakeError)
mockFetcher := &mocks.MockLogFetcher{}
transformer := ilk.PitFileIlkTransformer{
Converter: mockConverter,
Fetcher: mockFetcher,
Repository: mockRepository,
}
err := transformer.Execute() err := transformer.Execute()
@ -134,16 +114,8 @@ var _ = Describe("Pit file ilk transformer", func() {
}) })
It("converts matching logs", func() { It("converts matching logs", func() {
converter := &pit_file_ilk_mocks.MockPitFileIlkConverter{}
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileIlkLog}) fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileIlkLog})
repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} repository.SetMissingHeaders([]core.Header{headerOne})
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
transformer := ilk.PitFileIlkTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
}
err := transformer.Execute() err := transformer.Execute()
@ -152,17 +124,9 @@ var _ = Describe("Pit file ilk transformer", func() {
}) })
It("returns error if converter returns error", func() { It("returns error if converter returns error", func() {
converter := &pit_file_ilk_mocks.MockPitFileIlkConverter{}
converter.SetConverterError(fakes.FakeError) converter.SetConverterError(fakes.FakeError)
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileIlkLog}) fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileIlkLog})
repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} repository.SetMissingHeaders([]core.Header{headerOne})
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
transformer := ilk.PitFileIlkTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
}
err := transformer.Execute() err := transformer.Execute()
@ -171,37 +135,20 @@ var _ = Describe("Pit file ilk transformer", func() {
}) })
It("persists pit file model", func() { It("persists pit file model", func() {
converter := &pit_file_ilk_mocks.MockPitFileIlkConverter{}
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileIlkLog}) fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileIlkLog})
repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} repository.SetMissingHeaders([]core.Header{headerOne})
fakeHeader := core.Header{BlockNumber: 1, Id: 2}
repository.SetMissingHeaders([]core.Header{fakeHeader})
transformer := ilk.PitFileIlkTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
}
err := transformer.Execute() err := transformer.Execute()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(repository.PassedHeaderID).To(Equal(fakeHeader.Id)) Expect(repository.PassedHeaderID).To(Equal(headerOne.Id))
Expect(repository.PassedModels).To(Equal([]ilk.PitFileIlkModel{test_data.PitFileIlkModel})) Expect(repository.PassedModels).To(Equal([]interface{}{test_data.PitFileIlkModel}))
}) })
It("returns error if repository returns error for create", func() { It("returns error if repository returns error for create", func() {
converter := &pit_file_ilk_mocks.MockPitFileIlkConverter{}
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileIlkLog}) fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileIlkLog})
repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} repository.SetMissingHeaders([]core.Header{headerOne})
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: 2}})
repository.SetCreateError(fakes.FakeError) repository.SetCreateError(fakes.FakeError)
transformer := ilk.PitFileIlkTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
}
err := transformer.Execute() err := transformer.Execute()

View File

@ -17,7 +17,6 @@ package ilk
import ( import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/ilk"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
) )
@ -30,7 +29,7 @@ func (converter *MockPitFileIlkConverter) SetConverterError(err error) {
converter.converterError = err converter.converterError = err
} }
func (converter *MockPitFileIlkConverter) ToModels(ethLogs []types.Log) ([]ilk.PitFileIlkModel, error) { func (converter *MockPitFileIlkConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
converter.PassedLogs = ethLogs converter.PassedLogs = ethLogs
return []ilk.PitFileIlkModel{test_data.PitFileIlkModel}, converter.converterError return []interface{}{test_data.PitFileIlkModel}, converter.converterError
} }

View File

@ -16,40 +16,40 @@ package ilk
import ( import (
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/ilk"
) )
type MockPitFileIlkRepository struct { type MockPitFileIlkRepository struct {
createError error createError error
PassedEndingBlockNumber int64 PassedEndingBlockNumber int64
PassedModels []ilk.PitFileIlkModel PassedModels []interface{}
PassedHeaderID int64 PassedHeaderID int64
PassedStartingBlockNumber int64 PassedStartingBlockNumber int64
markHeaderCheckedErr error markHeaderCheckedError error
markHeaderCheckedPassedHeaderID int64 markHeaderCheckedPassedHeaderID int64
missingHeaders []core.Header missingHeaders []core.Header
missingHeadersErr error missingHeadersError error
} }
func (repository *MockPitFileIlkRepository) SetCreateError(err error) { func (repository *MockPitFileIlkRepository) SetCreateError(err error) {
repository.createError = err repository.createError = err
} }
func (repository *MockPitFileIlkRepository) SetMarkHeaderCheckedErr(e error) { func (repository *MockPitFileIlkRepository) SetMarkHeaderCheckedError(e error) {
repository.markHeaderCheckedErr = e repository.markHeaderCheckedError = e
} }
func (repository *MockPitFileIlkRepository) SetMissingHeadersErr(err error) { func (repository *MockPitFileIlkRepository) SetMissingHeadersError(err error) {
repository.missingHeadersErr = err repository.missingHeadersError = err
} }
func (repository *MockPitFileIlkRepository) SetMissingHeaders(headers []core.Header) { func (repository *MockPitFileIlkRepository) SetMissingHeaders(headers []core.Header) {
repository.missingHeaders = headers repository.missingHeaders = headers
} }
func (repository *MockPitFileIlkRepository) Create(headerID int64, models []ilk.PitFileIlkModel) error { func (repository *MockPitFileIlkRepository) Create(headerID int64, models []interface{}) error {
repository.PassedHeaderID = headerID repository.PassedHeaderID = headerID
repository.PassedModels = models repository.PassedModels = models
return repository.createError return repository.createError
@ -57,15 +57,17 @@ func (repository *MockPitFileIlkRepository) Create(headerID int64, models []ilk.
func (repository *MockPitFileIlkRepository) MarkHeaderChecked(headerID int64) error { func (repository *MockPitFileIlkRepository) MarkHeaderChecked(headerID int64) error {
repository.markHeaderCheckedPassedHeaderID = headerID repository.markHeaderCheckedPassedHeaderID = headerID
return repository.markHeaderCheckedErr return repository.markHeaderCheckedError
} }
func (repository *MockPitFileIlkRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { func (repository *MockPitFileIlkRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
repository.PassedStartingBlockNumber = startingBlockNumber repository.PassedStartingBlockNumber = startingBlockNumber
repository.PassedEndingBlockNumber = endingBlockNumber repository.PassedEndingBlockNumber = endingBlockNumber
return repository.missingHeaders, repository.missingHeadersErr return repository.missingHeaders, repository.missingHeadersError
} }
func (repository *MockPitFileIlkRepository) AssertMarkHeaderCheckedCalledWith(i int64) { func (repository *MockPitFileIlkRepository) AssertMarkHeaderCheckedCalledWith(i int64) {
Expect(repository.markHeaderCheckedPassedHeaderID).To(Equal(i)) Expect(repository.markHeaderCheckedPassedHeaderID).To(Equal(i))
} }
func (repository *MockPitFileIlkRepository) SetDB(db *postgres.DB) {}