Update pit file debt ceiling tests

This commit is contained in:
Edvard 2018-10-17 12:33:52 +02:00
parent 24199cb67e
commit d9b6603109
5 changed files with 80 additions and 135 deletions

View File

@ -17,10 +17,10 @@ package debt_ceiling_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/debt_ceiling"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/debt_ceiling"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
)
@ -54,6 +54,6 @@ var _ = Describe("Pit file debt ceiling converter", func() {
Expect(err).NotTo(HaveOccurred())
Expect(len(models)).To(Equal(1))
Expect(models[0]).To(Equal(test_data.PitFileDebtCeilingModel))
Expect(models[0].(debt_ceiling.PitFileDebtCeilingModel)).To(Equal(test_data.PitFileDebtCeilingModel))
})
})

View File

@ -32,17 +32,17 @@ import (
var _ = Describe("Pit file debt ceiling repository", func() {
var (
db *postgres.DB
pitFileRepository debt_ceiling.Repository
err error
headerRepository datastore.HeaderRepository
db *postgres.DB
pitFileDebtCeilingRepository debt_ceiling.PitFileDebtCeilingRepository
err error
headerRepository datastore.HeaderRepository
)
BeforeEach(func() {
db = test_config.NewTestDB(core.Node{})
test_config.CleanTestDB(db)
headerRepository = repositories.NewHeaderRepository(db)
pitFileRepository = debt_ceiling.NewPitFileDebtCeilingRepository(db)
pitFileDebtCeilingRepository = debt_ceiling.PitFileDebtCeilingRepository{DB: db}
})
Describe("Create", func() {
@ -52,7 +52,7 @@ var _ = Describe("Pit file debt ceiling repository", func() {
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err = pitFileRepository.Create(headerID, []debt_ceiling.PitFileDebtCeilingModel{test_data.PitFileDebtCeilingModel})
err = pitFileDebtCeilingRepository.Create(headerID, []interface{}{test_data.PitFileDebtCeilingModel})
Expect(err).NotTo(HaveOccurred())
})
@ -74,7 +74,7 @@ var _ = Describe("Pit file debt ceiling repository", func() {
})
It("does not duplicate pit file events", func() {
err = pitFileRepository.Create(headerID, []debt_ceiling.PitFileDebtCeilingModel{test_data.PitFileDebtCeilingModel})
err = pitFileDebtCeilingRepository.Create(headerID, []interface{}{test_data.PitFileDebtCeilingModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
@ -100,7 +100,7 @@ var _ = Describe("Pit file debt ceiling repository", func() {
})
It("creates a row for a new headerID", func() {
err = pitFileRepository.MarkHeaderChecked(headerID)
err = pitFileDebtCeilingRepository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
@ -112,7 +112,7 @@ var _ = Describe("Pit file debt ceiling repository", func() {
It("updates row when headerID already exists", func() {
_, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID)
err = pitFileRepository.MarkHeaderChecked(headerID)
err = pitFileDebtCeilingRepository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
@ -144,10 +144,11 @@ var _ = Describe("Pit file debt ceiling repository", func() {
})
It("returns headers that haven't been checked", func() {
err := pitFileRepository.MarkHeaderChecked(headerIDs[1])
err := pitFileDebtCeilingRepository.MarkHeaderChecked(headerIDs[1])
Expect(err).NotTo(HaveOccurred())
headers, err := pitFileRepository.MissingHeaders(startingBlock, endingBlock)
headers, err := pitFileDebtCeilingRepository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(2))
@ -159,7 +160,7 @@ var _ = Describe("Pit file debt ceiling repository", func() {
_, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1])
Expect(err).NotTo(HaveOccurred())
headers, err := pitFileRepository.MissingHeaders(startingBlock, endingBlock)
headers, err := pitFileDebtCeilingRepository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(3))
@ -175,8 +176,8 @@ var _ = Describe("Pit file debt ceiling repository", func() {
_, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred())
}
pitFileRepository := debt_ceiling.NewPitFileDebtCeilingRepository(db)
pitFileRepositoryTwo := debt_ceiling.NewPitFileDebtCeilingRepository(dbTwo)
pitFileRepository := debt_ceiling.PitFileDebtCeilingRepository{DB: db}
pitFileRepositoryTwo := debt_ceiling.PitFileDebtCeilingRepository{DB: dbTwo}
err := pitFileRepository.MarkHeaderChecked(headerIDs[0])
Expect(err).NotTo(HaveOccurred())

View File

@ -19,10 +19,10 @@ 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"
"github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file"
"github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/debt_ceiling"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
@ -31,31 +31,40 @@ import (
)
var _ = Describe("Pit file debt ceiling transformer", func() {
It("gets missing headers for block numbers specified in config", func() {
repository := &debt_ceiling_mocks.MockPitFileDebtCeilingRepository{}
transformer := debt_ceiling.PitFileDebtCeilingTransformer{
Config: pit_file.PitFileConfig,
Fetcher: &mocks.MockLogFetcher{},
Converter: &debt_ceiling_mocks.MockPitFileDebtCeilingConverter{},
Repository: repository,
}
var (
config = debt_ceiling.DebtCeilingFileConfig
fetcher mocks.MockLogFetcher
converter debt_ceiling_mocks.MockPitFileDebtCeilingConverter
repository debt_ceiling_mocks.MockPitFileDebtCeilingRepository
transformer shared.Transformer
headerOne core.Header
headerTwo core.Header
)
BeforeEach(func() {
fetcher = mocks.MockLogFetcher{}
converter = debt_ceiling_mocks.MockPitFileDebtCeilingConverter{}
repository = debt_ceiling_mocks.MockPitFileDebtCeilingRepository{}
headerOne = core.Header{Id: GinkgoRandomSeed(), BlockNumber: GinkgoRandomSeed()}
headerTwo = core.Header{Id: GinkgoRandomSeed(), BlockNumber: GinkgoRandomSeed()}
transformer = factories.Transformer{
Config: config,
Fetcher: &fetcher,
Converter: &converter,
Repository: &repository,
}
})
It("gets missing headers for block numbers specified in config", func() {
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())
Expect(repository.PassedStartingBlockNumber).To(Equal(pit_file.PitFileConfig.StartingBlockNumber))
Expect(repository.PassedEndingBlockNumber).To(Equal(pit_file.PitFileConfig.EndingBlockNumber))
Expect(repository.PassedStartingBlockNumber).To(Equal(config.StartingBlockNumber))
Expect(repository.PassedEndingBlockNumber).To(Equal(config.EndingBlockNumber))
})
It("returns error if repository returns error for missing headers", func() {
repository := &debt_ceiling_mocks.MockPitFileDebtCeilingRepository{}
repository.SetMissingHeadersErr(fakes.FakeError)
transformer := debt_ceiling.PitFileDebtCeilingTransformer{
Fetcher: &mocks.MockLogFetcher{},
Converter: &debt_ceiling_mocks.MockPitFileDebtCeilingConverter{},
Repository: repository,
}
repository.SetMissingHeadersError(fakes.FakeError)
err := transformer.Execute()
Expect(err).To(HaveOccurred())
@ -63,34 +72,18 @@ var _ = Describe("Pit file debt ceiling transformer", func() {
})
It("fetches logs for missing headers", func() {
fetcher := &mocks.MockLogFetcher{}
repository := &debt_ceiling_mocks.MockPitFileDebtCeilingRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}, {BlockNumber: 2}})
transformer := debt_ceiling.PitFileDebtCeilingTransformer{
Fetcher: fetcher,
Converter: &debt_ceiling_mocks.MockPitFileDebtCeilingConverter{},
Repository: repository,
}
repository.SetMissingHeaders([]core.Header{headerOne, headerTwo})
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())
Expect(fetcher.FetchedBlocks).To(Equal([]int64{1, 2}))
Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{pit_file.PitFileConfig.ContractAddresses, pit_file.PitFileConfig.ContractAddresses}))
Expect(fetcher.FetchedBlocks).To(Equal([]int64{headerOne.BlockNumber, headerTwo.BlockNumber}))
Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{config.ContractAddresses, config.ContractAddresses}))
Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.PitFileDebtCeilingSignature)}}))
})
It("returns error if fetcher returns error", func() {
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetcherError(fakes.FakeError)
repository := &debt_ceiling_mocks.MockPitFileDebtCeilingRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
transformer := debt_ceiling.PitFileDebtCeilingTransformer{
Fetcher: fetcher,
Converter: &debt_ceiling_mocks.MockPitFileDebtCeilingConverter{},
Repository: repository,
}
repository.SetMissingHeaders([]core.Header{headerOne})
err := transformer.Execute()
Expect(err).To(HaveOccurred())
@ -98,34 +91,17 @@ var _ = Describe("Pit file debt ceiling transformer", func() {
})
It("marks header checked if no logs returned", func() {
mockConverter := &debt_ceiling_mocks.MockPitFileDebtCeilingConverter{}
mockRepository := &debt_ceiling_mocks.MockPitFileDebtCeilingRepository{}
headerID := int64(123)
mockRepository.SetMissingHeaders([]core.Header{{Id: headerID}})
mockFetcher := &mocks.MockLogFetcher{}
transformer := debt_ceiling.PitFileDebtCeilingTransformer{
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 := &debt_ceiling_mocks.MockPitFileDebtCeilingConverter{}
mockRepository := &debt_ceiling_mocks.MockPitFileDebtCeilingRepository{}
mockRepository.SetMissingHeaders([]core.Header{{Id: int64(123)}})
mockRepository.SetMarkHeaderCheckedErr(fakes.FakeError)
mockFetcher := &mocks.MockLogFetcher{}
transformer := debt_ceiling.PitFileDebtCeilingTransformer{
Converter: mockConverter,
Fetcher: mockFetcher,
Repository: mockRepository,
}
repository.SetMissingHeaders([]core.Header{headerOne})
repository.SetMarkHeaderCheckedError(fakes.FakeError)
err := transformer.Execute()
@ -134,16 +110,8 @@ var _ = Describe("Pit file debt ceiling transformer", func() {
})
It("converts matching logs", func() {
converter := &debt_ceiling_mocks.MockPitFileDebtCeilingConverter{}
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileDebtCeilingLog})
repository := &debt_ceiling_mocks.MockPitFileDebtCeilingRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
transformer := debt_ceiling.PitFileDebtCeilingTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
}
repository.SetMissingHeaders([]core.Header{headerOne})
err := transformer.Execute()
@ -152,17 +120,9 @@ var _ = Describe("Pit file debt ceiling transformer", func() {
})
It("returns error if converter returns error", func() {
converter := &debt_ceiling_mocks.MockPitFileDebtCeilingConverter{}
converter.SetConverterError(fakes.FakeError)
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileDebtCeilingLog})
repository := &debt_ceiling_mocks.MockPitFileDebtCeilingRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}})
transformer := debt_ceiling.PitFileDebtCeilingTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
}
repository.SetMissingHeaders([]core.Header{headerOne})
err := transformer.Execute()
@ -171,37 +131,20 @@ var _ = Describe("Pit file debt ceiling transformer", func() {
})
It("persists pit file model", func() {
converter := &debt_ceiling_mocks.MockPitFileDebtCeilingConverter{}
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileDebtCeilingLog})
repository := &debt_ceiling_mocks.MockPitFileDebtCeilingRepository{}
fakeHeader := core.Header{BlockNumber: 1, Id: 2}
repository.SetMissingHeaders([]core.Header{fakeHeader})
transformer := debt_ceiling.PitFileDebtCeilingTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
}
repository.SetMissingHeaders([]core.Header{headerOne})
err := transformer.Execute()
Expect(err).NotTo(HaveOccurred())
Expect(repository.PassedHeaderID).To(Equal(fakeHeader.Id))
Expect(repository.PassedModels).To(Equal([]debt_ceiling.PitFileDebtCeilingModel{test_data.PitFileDebtCeilingModel}))
Expect(repository.PassedHeaderID).To(Equal(headerOne.Id))
Expect(repository.PassedModels).To(Equal([]interface{}{test_data.PitFileDebtCeilingModel}))
})
It("returns error if repository returns error for create", func() {
converter := &debt_ceiling_mocks.MockPitFileDebtCeilingConverter{}
fetcher := &mocks.MockLogFetcher{}
fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileDebtCeilingLog})
repository := &debt_ceiling_mocks.MockPitFileDebtCeilingRepository{}
repository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: 2}})
repository.SetMissingHeaders([]core.Header{headerOne})
repository.SetCreateError(fakes.FakeError)
transformer := debt_ceiling.PitFileDebtCeilingTransformer{
Fetcher: fetcher,
Converter: converter,
Repository: repository,
}
err := transformer.Execute()

View File

@ -17,20 +17,19 @@ package debt_ceiling
import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/debt_ceiling"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
)
type MockPitFileDebtCeilingConverter struct {
converterErr error
PassedLogs []types.Log
converterError error
PassedLogs []types.Log
}
func (converter *MockPitFileDebtCeilingConverter) ToModels(ethLogs []types.Log) ([]debt_ceiling.PitFileDebtCeilingModel, error) {
func (converter *MockPitFileDebtCeilingConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
converter.PassedLogs = ethLogs
return []debt_ceiling.PitFileDebtCeilingModel{test_data.PitFileDebtCeilingModel}, converter.converterErr
return []interface{}{test_data.PitFileDebtCeilingModel}, converter.converterError
}
func (converter *MockPitFileDebtCeilingConverter) SetConverterError(e error) {
converter.converterErr = e
converter.converterError = e
}

View File

@ -16,46 +16,46 @@ package debt_ceiling
import (
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/debt_ceiling"
)
type MockPitFileDebtCeilingRepository 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 []debt_ceiling.PitFileDebtCeilingModel
PassedModels []interface{}
}
func (repository *MockPitFileDebtCeilingRepository) MarkHeaderChecked(headerID int64) error {
repository.markHeaderCheckedPassedHeaderID = headerID
return repository.markHeaderCheckedErr
return repository.markHeaderCheckedError
}
func (repository *MockPitFileDebtCeilingRepository) Create(headerID int64, models []debt_ceiling.PitFileDebtCeilingModel) error {
func (repository *MockPitFileDebtCeilingRepository) Create(headerID int64, models []interface{}) error {
repository.PassedHeaderID = headerID
repository.PassedModels = models
return repository.createErr
return repository.createError
}
func (repository *MockPitFileDebtCeilingRepository) 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 *MockPitFileDebtCeilingRepository) SetMarkHeaderCheckedErr(e error) {
repository.markHeaderCheckedErr = e
func (repository *MockPitFileDebtCeilingRepository) SetMarkHeaderCheckedError(e error) {
repository.markHeaderCheckedError = e
}
func (repository *MockPitFileDebtCeilingRepository) SetMissingHeadersErr(e error) {
repository.missingHeadersErr = e
func (repository *MockPitFileDebtCeilingRepository) SetMissingHeadersError(e error) {
repository.missingHeadersError = e
}
func (repository *MockPitFileDebtCeilingRepository) SetMissingHeaders(headers []core.Header) {
@ -63,9 +63,11 @@ func (repository *MockPitFileDebtCeilingRepository) SetMissingHeaders(headers []
}
func (repository *MockPitFileDebtCeilingRepository) SetCreateError(e error) {
repository.createErr = e
repository.createError = e
}
func (repository *MockPitFileDebtCeilingRepository) AssertMarkHeaderCheckedCalledWith(i int64) {
Expect(repository.markHeaderCheckedPassedHeaderID).To(Equal(i))
}
func (repository *MockPitFileDebtCeilingRepository) SetDB(db *postgres.DB) {}