Update vat init tests for factory

This commit is contained in:
Edvard 2018-10-11 16:07:00 +02:00
parent fedbce80f6
commit 719ffd5819
4 changed files with 144 additions and 108 deletions

View File

@ -18,19 +18,18 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_init"
)
type MockVatInitConverter struct {
converterErr error
PassedLogs []types.Log
converterError error
PassedLogs []types.Log
}
func (converter *MockVatInitConverter) ToModels(ethLogs []types.Log) ([]vat_init.VatInitModel, error) {
func (converter *MockVatInitConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
converter.PassedLogs = ethLogs
return []vat_init.VatInitModel{test_data.VatInitModel}, converter.converterErr
return []interface{}{test_data.VatInitModel}, converter.converterError
}
func (converter *MockVatInitConverter) SetConverterError(e error) {
converter.converterErr = e
converter.converterError = e
}

View File

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

View File

@ -32,7 +32,7 @@ import (
var _ = Describe("Vat init repository", func() {
var (
db *postgres.DB
vatInitRepository vat_init.Repository
vatInitRepository vat_init.VatInitRepository
headerRepository repositories.HeaderRepository
err error
)
@ -40,7 +40,7 @@ var _ = Describe("Vat init repository", func() {
BeforeEach(func() {
db = test_config.NewTestDB(core.Node{})
test_config.CleanTestDB(db)
vatInitRepository = vat_init.NewVatInitRepository(db)
vatInitRepository = vat_init.VatInitRepository{DB: db}
headerRepository = repositories.NewHeaderRepository(db)
})
@ -51,13 +51,13 @@ var _ = Describe("Vat init repository", func() {
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err = vatInitRepository.Create(headerID, []vat_init.VatInitModel{test_data.VatInitModel})
err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel})
Expect(err).NotTo(HaveOccurred())
})
It("adds a vat event", func() {
var dbVatInit vat_init.VatInitModel
err = db.Get(&dbVatInit, `SELECT ilk,tx_idx, raw_log FROM maker.vat_init WHERE header_id = $1`, headerID)
err = db.Get(&dbVatInit, `SELECT ilk, tx_idx, raw_log FROM maker.vat_init WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(dbVatInit.Ilk).To(Equal(test_data.VatInitModel.Ilk))
Expect(dbVatInit.TransactionIndex).To(Equal(test_data.VatInitModel.TransactionIndex))
@ -65,7 +65,10 @@ var _ = Describe("Vat init repository", func() {
})
It("does not duplicate vat events", func() {
err = vatInitRepository.Create(headerID, []vat_init.VatInitModel{test_data.VatInitModel})
err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel})
Expect(err).NotTo(HaveOccurred())
err = vatInitRepository.Create(headerID, []interface{}{test_data.VatInitModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
@ -90,12 +93,20 @@ var _ = Describe("Vat init repository", func() {
})
Describe("MarkHeaderChecked", func() {
var headerID int64
var (
db *postgres.DB
vatInitRepository vat_init.VatInitRepository
err error
headerID int64
)
BeforeEach(func() {
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
db = test_config.NewTestDB(core.Node{})
test_config.CleanTestDB(db)
headerRepository := repositories.NewHeaderRepository(db)
headerID, err = headerRepository.CreateOrUpdateHeader(core.Header{})
Expect(err).NotTo(HaveOccurred())
vatInitRepository = vat_init.VatInitRepository{DB: db}
})
It("creates a row for a new headerID", func() {
@ -155,6 +166,16 @@ var _ = Describe("Vat init repository", func() {
})
It("only treats headers as checked if drip drip logs have been checked", func() {
startingBlockNumber := int64(1)
vatInitBlockNumber := int64(2)
endingBlockNumber := int64(3)
blockNumbers = []int64{startingBlockNumber, vatInitBlockNumber, endingBlockNumber, endingBlockNumber + 1}
for _, n := range blockNumbers {
headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{BlockNumber: n})
headerIDs = append(headerIDs, headerID)
Expect(err).NotTo(HaveOccurred())
}
_, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1])
Expect(err).NotTo(HaveOccurred())
@ -174,7 +195,9 @@ var _ = Describe("Vat init repository", func() {
_, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred())
}
vatInitRepositoryTwo := vat_init.NewVatInitRepository(dbTwo)
vatInitRepository := vat_init.VatInitRepository{DB: db}
vatInitRepositoryTwo := vat_init.VatInitRepository{DB: dbTwo}
err := vatInitRepository.MarkHeaderChecked(headerIDs[0])
Expect(err).NotTo(HaveOccurred())
@ -187,4 +210,14 @@ var _ = Describe("Vat init repository", func() {
Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers)))
})
})
Describe("SetDB", func() {
It("sets the repository db", func() {
db := test_config.NewTestDB(core.Node{})
vatInitRepository := vat_init.VatInitRepository{DB: nil}
Expect(vatInitRepository.DB).To(BeNil())
vatInitRepository.SetDB(db)
Expect(vatInitRepository.DB).To(Equal(db))
})
})
})

View File

@ -19,6 +19,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
. "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/fakes"
@ -30,14 +31,30 @@ import (
)
var _ = Describe("Vat init transformer", func() {
var (
config = vat_init.VatInitConfig
converter vat_init_mocks.MockVatInitConverter
repository vat_init_mocks.MockVatInitRepository
fetcher mocks.MockLogFetcher
headerOne core.Header
headerTwo core.Header
)
BeforeEach(func() {
converter = vat_init_mocks.MockVatInitConverter{}
repository = vat_init_mocks.MockVatInitRepository{}
fetcher = mocks.MockLogFetcher{}
headerOne = core.Header{Id: GinkgoRandomSeed(), BlockNumber: GinkgoRandomSeed()}
headerTwo = core.Header{Id: GinkgoRandomSeed(), BlockNumber: GinkgoRandomSeed()}
})
It("gets missing headers for block numbers specified in config", func() {
repository := &vat_init_mocks.MockVatInitRepository{}
transformer := vat_init.VatInitTransformer{
Config: vat_init.VatInitConfig,
transformer := factories.Transformer{
Config: config,
Fetcher: &mocks.MockLogFetcher{},
Converter: &vat_init_mocks.MockVatInitConverter{},
Repository: repository,
}
Repository: &repository,
}.NewTransformer(nil, nil)
err := transformer.Execute()
@ -47,12 +64,12 @@ var _ = Describe("Vat init transformer", func() {
})
It("returns error if repository returns error for missing headers", func() {
repository := &vat_init_mocks.MockVatInitRepository{}
repository.SetMissingHeadersErr(fakes.FakeError)
transformer := vat_init.VatInitTransformer{
repository.SetMissingHeadersError(fakes.FakeError)
transformer := factories.Transformer{
Config: config,
Fetcher: &mocks.MockLogFetcher{},
Converter: &vat_init_mocks.MockVatInitConverter{},
Repository: repository,
Repository: &repository,
}
err := transformer.Execute()
@ -62,32 +79,31 @@ var _ = Describe("Vat init transformer", func() {
})
It("fetches logs for missing headers", func() {
fetcher := &mocks.MockLogFetcher{}
repository := &vat_init_mocks.MockVatInitRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}, {BlockNumber: 2}})
transformer := vat_init.VatInitTransformer{
Fetcher: fetcher,
Converter: &vat_init_mocks.MockVatInitConverter{},
Repository: repository,
repository.SetMissingHeaders([]core.Header{headerOne, headerTwo})
transformer := factories.Transformer{
Config: config,
Fetcher: &fetcher,
Converter: &converter,
Repository: &repository,
}
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())
Expect(fetcher.FetchedBlocks).To(Equal([]int64{1, 2}))
Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{vat_init.VatInitConfig.ContractAddresses, vat_init.VatInitConfig.ContractAddresses}))
Expect(fetcher.FetchedBlocks).To(Equal([]int64{headerOne.BlockNumber, headerTwo.BlockNumber}))
Expect(fetcher.FetchedContractAddresses).To(Equal(
[][]string{transformer.Config.ContractAddresses, transformer.Config.ContractAddresses}))
Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.VatInitSignature)}}))
})
It("returns error if fetcher returns error", func() {
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetcherError(fakes.FakeError)
repository := &vat_init_mocks.MockVatInitRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
transformer := vat_init.VatInitTransformer{
Fetcher: fetcher,
Converter: &vat_init_mocks.MockVatInitConverter{},
Repository: repository,
repository.SetMissingHeaders([]core.Header{headerOne})
transformer := factories.Transformer{
Config: config,
Fetcher: &fetcher,
Converter: &converter,
Repository: &repository,
}
err := transformer.Execute()
@ -97,33 +113,28 @@ var _ = Describe("Vat init transformer", func() {
})
It("marks header checked if no logs returned", func() {
mockConverter := &vat_init_mocks.MockVatInitConverter{}
mockRepository := &vat_init_mocks.MockVatInitRepository{}
headerID := int64(123)
mockRepository.SetMissingHeaders([]core.Header{{Id: headerID}})
mockFetcher := &mocks.MockLogFetcher{}
transformer := vat_init.VatInitTransformer{
Converter: mockConverter,
Fetcher: mockFetcher,
Repository: mockRepository,
repository.SetMissingHeaders([]core.Header{headerOne})
transformer := factories.Transformer{
Config: config,
Fetcher: &fetcher,
Converter: &converter,
Repository: &repository,
}
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 := &vat_init_mocks.MockVatInitConverter{}
mockRepository := &vat_init_mocks.MockVatInitRepository{}
mockRepository.SetMissingHeaders([]core.Header{{Id: int64(123)}})
mockRepository.SetMarkHeaderCheckedErr(fakes.FakeError)
mockFetcher := &mocks.MockLogFetcher{}
transformer := vat_init.VatInitTransformer{
Converter: mockConverter,
Fetcher: mockFetcher,
Repository: mockRepository,
repository.SetMissingHeaders([]core.Header{headerOne})
repository.SetMarkHeaderCheckedError(fakes.FakeError)
transformer := factories.Transformer{
Config: config,
Fetcher: &fetcher,
Converter: &converter,
Repository: &repository,
}
err := transformer.Execute()
@ -133,15 +144,13 @@ var _ = Describe("Vat init transformer", func() {
})
It("converts matching logs", func() {
converter := &vat_init_mocks.MockVatInitConverter{}
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatInitLog})
repository := &vat_init_mocks.MockVatInitRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
transformer := vat_init.VatInitTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
repository.SetMissingHeaders([]core.Header{headerOne})
transformer := factories.Transformer{
Config: config,
Fetcher: &fetcher,
Converter: &converter,
Repository: &repository,
}
err := transformer.Execute()
@ -151,16 +160,14 @@ var _ = Describe("Vat init transformer", func() {
})
It("returns error if converter returns error", func() {
converter := &vat_init_mocks.MockVatInitConverter{}
converter.SetConverterError(fakes.FakeError)
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatInitLog})
repository := &vat_init_mocks.MockVatInitRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
transformer := vat_init.VatInitTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
repository.SetMissingHeaders([]core.Header{headerOne})
transformer := factories.Transformer{
Config: config,
Fetcher: &fetcher,
Converter: &converter,
Repository: &repository,
}
err := transformer.Execute()
@ -170,36 +177,31 @@ var _ = Describe("Vat init transformer", func() {
})
It("persists vat init model", func() {
converter := &vat_init_mocks.MockVatInitConverter{}
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatInitLog})
repository := &vat_init_mocks.MockVatInitRepository{}
fakeHeader := core.Header{BlockNumber: 1, Id: 2}
repository.SetMissingHeaders([]core.Header{fakeHeader})
transformer := vat_init.VatInitTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
repository.SetMissingHeaders([]core.Header{headerOne})
transformer := factories.Transformer{
Config: config,
Fetcher: &fetcher,
Converter: &converter,
Repository: &repository,
}
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())
Expect(repository.PassedHeaderID).To(Equal(fakeHeader.Id))
Expect(repository.PassedModels).To(Equal([]vat_init.VatInitModel{test_data.VatInitModel}))
Expect(repository.PassedHeaderID).To(Equal(headerOne.Id))
Expect(repository.PassedModels).To(Equal([]interface{}{test_data.VatInitModel}))
})
It("returns error if repository returns error for create", func() {
converter := &vat_init_mocks.MockVatInitConverter{}
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatInitLog})
repository := &vat_init_mocks.MockVatInitRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: 2}})
repository.SetMissingHeaders([]core.Header{headerOne})
repository.SetCreateError(fakes.FakeError)
transformer := vat_init.VatInitTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
transformer := factories.Transformer{
Config: config,
Fetcher: &fetcher,
Converter: &converter,
Repository: &repository,
}
err := transformer.Execute()