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/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_init"
) )
type MockVatInitConverter struct { type MockVatInitConverter struct {
converterErr error converterError error
PassedLogs []types.Log 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 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) { func (converter *MockVatInitConverter) SetConverterError(e error) {
converter.converterErr = e converter.converterError = e
} }

View File

@ -16,46 +16,46 @@ package vat_init
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/vat_init"
) )
type MockVatInitRepository struct { type MockVatInitRepository struct {
createErr error createError error
markHeaderCheckedErr error markHeaderCheckedError error
markHeaderCheckedPassedHeaderID int64 markHeaderCheckedPassedHeaderID int64
missingHeaders []core.Header missingHeaders []core.Header
missingHeadersErr error missingHeadersError error
PassedStartingBlockNumber int64 PassedStartingBlockNumber int64
PassedEndingBlockNumber int64 PassedEndingBlockNumber int64
PassedHeaderID 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.PassedHeaderID = headerID
repository.PassedModels = models repository.PassedModels = models
return repository.createErr return repository.createError
} }
func (repository *MockVatInitRepository) MarkHeaderChecked(headerID int64) error { func (repository *MockVatInitRepository) MarkHeaderChecked(headerID int64) error {
repository.markHeaderCheckedPassedHeaderID = headerID repository.markHeaderCheckedPassedHeaderID = headerID
return repository.markHeaderCheckedErr return repository.markHeaderCheckedError
} }
func (repository *MockVatInitRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { func (repository *MockVatInitRepository) 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 *MockVatInitRepository) SetMarkHeaderCheckedErr(e error) { func (repository *MockVatInitRepository) SetMarkHeaderCheckedError(e error) {
repository.markHeaderCheckedErr = e repository.markHeaderCheckedError = e
} }
func (repository *MockVatInitRepository) SetMissingHeadersErr(e error) { func (repository *MockVatInitRepository) SetMissingHeadersError(e error) {
repository.missingHeadersErr = e repository.missingHeadersError = e
} }
func (repository *MockVatInitRepository) SetMissingHeaders(headers []core.Header) { func (repository *MockVatInitRepository) SetMissingHeaders(headers []core.Header) {
@ -63,9 +63,11 @@ func (repository *MockVatInitRepository) SetMissingHeaders(headers []core.Header
} }
func (repository *MockVatInitRepository) SetCreateError(e error) { func (repository *MockVatInitRepository) SetCreateError(e error) {
repository.createErr = e repository.createError = e
} }
func (repository *MockVatInitRepository) AssertMarkHeaderCheckedCalledWith(i int64) { func (repository *MockVatInitRepository) AssertMarkHeaderCheckedCalledWith(i int64) {
Expect(repository.markHeaderCheckedPassedHeaderID).To(Equal(i)) 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 _ = Describe("Vat init repository", func() {
var ( var (
db *postgres.DB db *postgres.DB
vatInitRepository vat_init.Repository vatInitRepository vat_init.VatInitRepository
headerRepository repositories.HeaderRepository headerRepository repositories.HeaderRepository
err error err error
) )
@ -40,7 +40,7 @@ var _ = Describe("Vat init repository", func() {
BeforeEach(func() { BeforeEach(func() {
db = test_config.NewTestDB(core.Node{}) db = test_config.NewTestDB(core.Node{})
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
vatInitRepository = vat_init.NewVatInitRepository(db) vatInitRepository = vat_init.VatInitRepository{DB: db}
headerRepository = repositories.NewHeaderRepository(db) headerRepository = repositories.NewHeaderRepository(db)
}) })
@ -51,13 +51,13 @@ var _ = Describe("Vat init repository", func() {
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred()) 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()) Expect(err).NotTo(HaveOccurred())
}) })
It("adds a vat event", func() { It("adds a vat event", func() {
var dbVatInit vat_init.VatInitModel 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(err).NotTo(HaveOccurred())
Expect(dbVatInit.Ilk).To(Equal(test_data.VatInitModel.Ilk)) Expect(dbVatInit.Ilk).To(Equal(test_data.VatInitModel.Ilk))
Expect(dbVatInit.TransactionIndex).To(Equal(test_data.VatInitModel.TransactionIndex)) 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() { 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).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"))
@ -90,12 +93,20 @@ var _ = Describe("Vat init repository", func() {
}) })
Describe("MarkHeaderChecked", func() { Describe("MarkHeaderChecked", func() {
var headerID int64 var (
db *postgres.DB
vatInitRepository vat_init.VatInitRepository
err error
headerID int64
)
BeforeEach(func() { 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()) Expect(err).NotTo(HaveOccurred())
vatInitRepository = vat_init.VatInitRepository{DB: db}
}) })
It("creates a row for a new headerID", func() { 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() { 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]) _, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1])
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
@ -174,7 +195,9 @@ var _ = Describe("Vat init repository", func() {
_, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n)) _, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred()) 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]) err := vatInitRepository.MarkHeaderChecked(headerIDs[0])
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
@ -187,4 +210,14 @@ var _ = Describe("Vat init repository", func() {
Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers))) 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/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"
@ -30,14 +31,30 @@ import (
) )
var _ = Describe("Vat init transformer", func() { 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() { It("gets missing headers for block numbers specified in config", func() {
repository := &vat_init_mocks.MockVatInitRepository{} transformer := factories.Transformer{
transformer := vat_init.VatInitTransformer{ Config: config,
Config: vat_init.VatInitConfig,
Fetcher: &mocks.MockLogFetcher{}, Fetcher: &mocks.MockLogFetcher{},
Converter: &vat_init_mocks.MockVatInitConverter{}, Converter: &vat_init_mocks.MockVatInitConverter{},
Repository: repository, Repository: &repository,
} }.NewTransformer(nil, nil)
err := transformer.Execute() err := transformer.Execute()
@ -47,12 +64,12 @@ var _ = Describe("Vat init 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 := &vat_init_mocks.MockVatInitRepository{} repository.SetMissingHeadersError(fakes.FakeError)
repository.SetMissingHeadersErr(fakes.FakeError) transformer := factories.Transformer{
transformer := vat_init.VatInitTransformer{ Config: config,
Fetcher: &mocks.MockLogFetcher{}, Fetcher: &mocks.MockLogFetcher{},
Converter: &vat_init_mocks.MockVatInitConverter{}, Converter: &vat_init_mocks.MockVatInitConverter{},
Repository: repository, Repository: &repository,
} }
err := transformer.Execute() err := transformer.Execute()
@ -62,32 +79,31 @@ var _ = Describe("Vat init 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 := &vat_init_mocks.MockVatInitRepository{} transformer := factories.Transformer{
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}, {BlockNumber: 2}}) Config: config,
transformer := vat_init.VatInitTransformer{ Fetcher: &fetcher,
Fetcher: fetcher, Converter: &converter,
Converter: &vat_init_mocks.MockVatInitConverter{}, Repository: &repository,
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{vat_init.VatInitConfig.ContractAddresses, vat_init.VatInitConfig.ContractAddresses})) Expect(fetcher.FetchedContractAddresses).To(Equal(
[][]string{transformer.Config.ContractAddresses, transformer.Config.ContractAddresses}))
Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.VatInitSignature)}})) Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.VatInitSignature)}}))
}) })
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 := &vat_init_mocks.MockVatInitRepository{} repository.SetMissingHeaders([]core.Header{headerOne})
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) transformer := factories.Transformer{
transformer := vat_init.VatInitTransformer{ Config: config,
Fetcher: fetcher, Fetcher: &fetcher,
Converter: &vat_init_mocks.MockVatInitConverter{}, Converter: &converter,
Repository: repository, Repository: &repository,
} }
err := transformer.Execute() err := transformer.Execute()
@ -97,33 +113,28 @@ var _ = Describe("Vat init transformer", func() {
}) })
It("marks header checked if no logs returned", func() { It("marks header checked if no logs returned", func() {
mockConverter := &vat_init_mocks.MockVatInitConverter{} repository.SetMissingHeaders([]core.Header{headerOne})
mockRepository := &vat_init_mocks.MockVatInitRepository{} transformer := factories.Transformer{
headerID := int64(123) Config: config,
mockRepository.SetMissingHeaders([]core.Header{{Id: headerID}}) Fetcher: &fetcher,
mockFetcher := &mocks.MockLogFetcher{} Converter: &converter,
transformer := vat_init.VatInitTransformer{ Repository: &repository,
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 := &vat_init_mocks.MockVatInitConverter{} repository.SetMissingHeaders([]core.Header{headerOne})
mockRepository := &vat_init_mocks.MockVatInitRepository{} repository.SetMarkHeaderCheckedError(fakes.FakeError)
mockRepository.SetMissingHeaders([]core.Header{{Id: int64(123)}}) transformer := factories.Transformer{
mockRepository.SetMarkHeaderCheckedErr(fakes.FakeError) Config: config,
mockFetcher := &mocks.MockLogFetcher{} Fetcher: &fetcher,
transformer := vat_init.VatInitTransformer{ Converter: &converter,
Converter: mockConverter, Repository: &repository,
Fetcher: mockFetcher,
Repository: mockRepository,
} }
err := transformer.Execute() err := transformer.Execute()
@ -133,15 +144,13 @@ var _ = Describe("Vat init transformer", func() {
}) })
It("converts matching logs", func() { It("converts matching logs", func() {
converter := &vat_init_mocks.MockVatInitConverter{}
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatInitLog}) fetcher.SetFetchedLogs([]types.Log{test_data.EthVatInitLog})
repository := &vat_init_mocks.MockVatInitRepository{} repository.SetMissingHeaders([]core.Header{headerOne})
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) transformer := factories.Transformer{
transformer := vat_init.VatInitTransformer{ Config: config,
Fetcher: fetcher, Fetcher: &fetcher,
Converter: converter, Converter: &converter,
Repository: repository, Repository: &repository,
} }
err := transformer.Execute() err := transformer.Execute()
@ -151,16 +160,14 @@ var _ = Describe("Vat init transformer", func() {
}) })
It("returns error if converter returns error", func() { It("returns error if converter returns error", func() {
converter := &vat_init_mocks.MockVatInitConverter{}
converter.SetConverterError(fakes.FakeError) converter.SetConverterError(fakes.FakeError)
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatInitLog}) fetcher.SetFetchedLogs([]types.Log{test_data.EthVatInitLog})
repository := &vat_init_mocks.MockVatInitRepository{} repository.SetMissingHeaders([]core.Header{headerOne})
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) transformer := factories.Transformer{
transformer := vat_init.VatInitTransformer{ Config: config,
Fetcher: fetcher, Fetcher: &fetcher,
Converter: converter, Converter: &converter,
Repository: repository, Repository: &repository,
} }
err := transformer.Execute() err := transformer.Execute()
@ -170,36 +177,31 @@ var _ = Describe("Vat init transformer", func() {
}) })
It("persists vat init model", func() { It("persists vat init model", func() {
converter := &vat_init_mocks.MockVatInitConverter{}
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthVatInitLog}) fetcher.SetFetchedLogs([]types.Log{test_data.EthVatInitLog})
repository := &vat_init_mocks.MockVatInitRepository{} repository.SetMissingHeaders([]core.Header{headerOne})
fakeHeader := core.Header{BlockNumber: 1, Id: 2} transformer := factories.Transformer{
repository.SetMissingHeaders([]core.Header{fakeHeader}) Config: config,
transformer := vat_init.VatInitTransformer{ Fetcher: &fetcher,
Fetcher: fetcher, Converter: &converter,
Converter: converter, Repository: &repository,
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([]vat_init.VatInitModel{test_data.VatInitModel})) Expect(repository.PassedModels).To(Equal([]interface{}{test_data.VatInitModel}))
}) })
It("returns error if repository returns error for create", func() { 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}) fetcher.SetFetchedLogs([]types.Log{test_data.EthVatInitLog})
repository := &vat_init_mocks.MockVatInitRepository{} repository.SetMissingHeaders([]core.Header{headerOne})
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: 2}})
repository.SetCreateError(fakes.FakeError) repository.SetCreateError(fakes.FakeError)
transformer := vat_init.VatInitTransformer{ transformer := factories.Transformer{
Fetcher: fetcher, Config: config,
Converter: converter, Fetcher: &fetcher,
Repository: repository, Converter: &converter,
Repository: &repository,
} }
err := transformer.Execute() err := transformer.Execute()