diff --git a/pkg/transformers/pit_file/stability_fee/repository_test.go b/pkg/transformers/pit_file/stability_fee/repository_test.go index 499f71fa..62f3b416 100644 --- a/pkg/transformers/pit_file/stability_fee/repository_test.go +++ b/pkg/transformers/pit_file/stability_fee/repository_test.go @@ -32,27 +32,31 @@ import ( var _ = Describe("Pit file stability fee repository", func() { var ( - db *postgres.DB - pitFileRepository stability_fee.Repository - err error - headerRepository datastore.HeaderRepository + db *postgres.DB + pitFileStabilityFeeRepository stability_fee.PitFileStabilityFeeRepository + err error + headerRepository datastore.HeaderRepository ) BeforeEach(func() { db = test_config.NewTestDB(core.Node{}) test_config.CleanTestDB(db) headerRepository = repositories.NewHeaderRepository(db) - pitFileRepository = stability_fee.NewPitFileStabilityFeeRepository(db) + pitFileStabilityFeeRepository = stability_fee.PitFileStabilityFeeRepository{DB: db} }) Describe("Create", func() { - var headerID int64 + var ( + db *postgres.DB + err error + headerID int64 + ) BeforeEach(func() { headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) Expect(err).NotTo(HaveOccurred()) - err = pitFileRepository.Create(headerID, []stability_fee.PitFileStabilityFeeModel{test_data.PitFileStabilityFeeModel}) + err = pitFileStabilityFeeRepository.Create(headerID, []interface{}{test_data.PitFileStabilityFeeModel}) Expect(err).NotTo(HaveOccurred()) }) @@ -74,7 +78,7 @@ var _ = Describe("Pit file stability fee repository", func() { }) It("does not duplicate pit file events", func() { - err = pitFileRepository.Create(headerID, []stability_fee.PitFileStabilityFeeModel{test_data.PitFileStabilityFeeModel}) + err = pitFileStabilityFeeRepository.Create(headerID, []interface{}{test_data.PitFileStabilityFeeModel}) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) @@ -100,7 +104,7 @@ var _ = Describe("Pit file stability fee repository", func() { }) It("creates a row for a new headerID", func() { - err = pitFileRepository.MarkHeaderChecked(headerID) + err = pitFileStabilityFeeRepository.MarkHeaderChecked(headerID) Expect(err).NotTo(HaveOccurred()) var headerChecked bool @@ -112,7 +116,7 @@ var _ = Describe("Pit file stability fee 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 = pitFileStabilityFeeRepository.MarkHeaderChecked(headerID) Expect(err).NotTo(HaveOccurred()) var headerChecked bool @@ -144,10 +148,10 @@ var _ = Describe("Pit file stability fee repository", func() { }) It("returns headers that haven't been checked", func() { - err := pitFileRepository.MarkHeaderChecked(headerIDs[1]) + err = pitFileStabilityFeeRepository.MarkHeaderChecked(headerIDs[1]) Expect(err).NotTo(HaveOccurred()) - headers, err := pitFileRepository.MissingHeaders(startingBlock, endingBlock) + headers, err := pitFileStabilityFeeRepository.MissingHeaders(startingBlock, endingBlock) Expect(err).NotTo(HaveOccurred()) Expect(len(headers)).To(Equal(2)) @@ -156,10 +160,10 @@ var _ = Describe("Pit file stability fee repository", func() { }) It("only treats headers as checked if pit file stability fee logs have been checked", func() { - _, 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()) - headers, err := pitFileRepository.MissingHeaders(startingBlock, endingBlock) + headers, err := pitFileStabilityFeeRepository.MissingHeaders(startingBlock, endingBlock) Expect(err).NotTo(HaveOccurred()) Expect(len(headers)).To(Equal(3)) @@ -175,11 +179,12 @@ var _ = Describe("Pit file stability fee repository", func() { _, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n)) Expect(err).NotTo(HaveOccurred()) } - pitFileRepositoryTwo := stability_fee.NewPitFileStabilityFeeRepository(dbTwo) - err := pitFileRepository.MarkHeaderChecked(headerIDs[0]) + + pitFileRepositoryTwo := stability_fee.PitFileStabilityFeeRepository{DB: dbTwo} + err = pitFileStabilityFeeRepository.MarkHeaderChecked(headerIDs[0]) Expect(err).NotTo(HaveOccurred()) - nodeOneMissingHeaders, err := pitFileRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) + nodeOneMissingHeaders, err := pitFileStabilityFeeRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) Expect(err).NotTo(HaveOccurred()) Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1)) diff --git a/pkg/transformers/pit_file/stability_fee/transformer_test.go b/pkg/transformers/pit_file/stability_fee/transformer_test.go index b2464af7..58a6bba4 100644 --- a/pkg/transformers/pit_file/stability_fee/transformer_test.go +++ b/pkg/transformers/pit_file/stability_fee/transformer_test.go @@ -19,11 +19,12 @@ 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/transformers/pit_file/stability_fee" "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/stability_fee" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks" @@ -31,15 +32,31 @@ import ( ) var _ = Describe("Pit file stability fee transformer", func() { - It("gets missing headers for block numbers specified in config", func() { - repository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} - transformer := stability_fee.PitFileStabilityFeeTransformer{ - Config: pit_file.PitFileConfig, - Fetcher: &mocks.MockLogFetcher{}, - Converter: &stability_fee_mocks.MockPitFileStabilityFeeConverter{}, - Repository: repository, - } + var ( + config = stability_fee.StabilityFeeFileConfig + fetcher mocks.MockLogFetcher + converter stability_fee_mocks.MockPitFileStabilityFeeConverter + repository stability_fee_mocks.MockPitFileStabilityFeeRepository + transformer shared.Transformer + headerOne core.Header + headerTwo core.Header + ) + BeforeEach(func() { + fetcher = mocks.MockLogFetcher{} + converter = stability_fee_mocks.MockPitFileStabilityFeeConverter{} + repository = stability_fee_mocks.MockPitFileStabilityFeeRepository{} + transformer = factories.Transformer{ + Config: config, + Fetcher: &fetcher, + Converter: &converter, + Repository: &repository, + }.NewTransformer(nil, nil) + 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() { err := transformer.Execute() Expect(err).NotTo(HaveOccurred()) @@ -48,14 +65,7 @@ var _ = Describe("Pit file stability fee transformer", func() { }) It("returns error if repository returns error for missing headers", func() { - repository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} repository.SetMissingHeadersErr(fakes.FakeError) - transformer := stability_fee.PitFileStabilityFeeTransformer{ - Fetcher: &mocks.MockLogFetcher{}, - Converter: &stability_fee_mocks.MockPitFileStabilityFeeConverter{}, - Repository: repository, - } - err := transformer.Execute() Expect(err).To(HaveOccurred()) @@ -63,33 +73,19 @@ var _ = Describe("Pit file stability fee transformer", func() { }) It("fetches logs for missing headers", func() { - fetcher := &mocks.MockLogFetcher{} - repository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} - repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}, {BlockNumber: 2}}) - transformer := stability_fee.PitFileStabilityFeeTransformer{ - Fetcher: fetcher, - Converter: &stability_fee_mocks.MockPitFileStabilityFeeConverter{}, - 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{ + pit_file.PitFileConfig.ContractAddresses, pit_file.PitFileConfig.ContractAddresses})) Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.PitFileStabilityFeeSignature)}})) }) It("returns error if fetcher returns error", func() { - fetcher := &mocks.MockLogFetcher{} fetcher.SetFetcherError(fakes.FakeError) - repository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} - repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) - transformer := stability_fee.PitFileStabilityFeeTransformer{ - Fetcher: fetcher, - Converter: &stability_fee_mocks.MockPitFileStabilityFeeConverter{}, - Repository: repository, - } + repository.SetMissingHeaders([]core.Header{headerOne}) err := transformer.Execute() @@ -98,34 +94,17 @@ var _ = Describe("Pit file stability fee transformer", func() { }) It("marks header checked if no logs returned", func() { - mockConverter := &stability_fee_mocks.MockPitFileStabilityFeeConverter{} - mockRepository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} - headerID := int64(123) - mockRepository.SetMissingHeaders([]core.Header{{Id: headerID}}) - mockFetcher := &mocks.MockLogFetcher{} - transformer := stability_fee.PitFileStabilityFeeTransformer{ - 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 := &stability_fee_mocks.MockPitFileStabilityFeeConverter{} - mockRepository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} - mockRepository.SetMissingHeaders([]core.Header{{Id: int64(123)}}) - mockRepository.SetMarkHeaderCheckedErr(fakes.FakeError) - mockFetcher := &mocks.MockLogFetcher{} - transformer := stability_fee.PitFileStabilityFeeTransformer{ - Converter: mockConverter, - Fetcher: mockFetcher, - Repository: mockRepository, - } + repository.SetMissingHeaders([]core.Header{headerOne}) + repository.SetMarkHeaderCheckedErr(fakes.FakeError) err := transformer.Execute() @@ -134,16 +113,8 @@ var _ = Describe("Pit file stability fee transformer", func() { }) It("converts matching logs", func() { - converter := &stability_fee_mocks.MockPitFileStabilityFeeConverter{} - fetcher := &mocks.MockLogFetcher{} fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileStabilityFeeLog}) - repository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} - repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) - transformer := stability_fee.PitFileStabilityFeeTransformer{ - Fetcher: fetcher, - Converter: converter, - Repository: repository, - } + repository.SetMissingHeaders([]core.Header{headerOne}) err := transformer.Execute() @@ -152,17 +123,9 @@ var _ = Describe("Pit file stability fee transformer", func() { }) It("returns error if converter returns error", func() { - converter := &stability_fee_mocks.MockPitFileStabilityFeeConverter{} converter.SetConverterError(fakes.FakeError) - fetcher := &mocks.MockLogFetcher{} fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileStabilityFeeLog}) - repository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} - repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) - transformer := stability_fee.PitFileStabilityFeeTransformer{ - Fetcher: fetcher, - Converter: converter, - Repository: repository, - } + repository.SetMissingHeaders([]core.Header{headerOne}) err := transformer.Execute() @@ -171,37 +134,20 @@ var _ = Describe("Pit file stability fee transformer", func() { }) It("persists pit file model", func() { - converter := &stability_fee_mocks.MockPitFileStabilityFeeConverter{} - fetcher := &mocks.MockLogFetcher{} fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileStabilityFeeLog}) - repository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} - fakeHeader := core.Header{BlockNumber: 1, Id: 2} - repository.SetMissingHeaders([]core.Header{fakeHeader}) - transformer := stability_fee.PitFileStabilityFeeTransformer{ - 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([]stability_fee.PitFileStabilityFeeModel{test_data.PitFileStabilityFeeModel})) + Expect(repository.PassedHeaderID).To(Equal(headerOne.Id)) + Expect(repository.PassedModels).To(Equal([]interface{}{test_data.PitFileStabilityFeeModel})) }) It("returns error if repository returns error for create", func() { - converter := &stability_fee_mocks.MockPitFileStabilityFeeConverter{} - fetcher := &mocks.MockLogFetcher{} fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileStabilityFeeLog}) - repository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} - repository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: 2}}) + repository.SetMissingHeaders([]core.Header{headerOne}) repository.SetCreateError(fakes.FakeError) - transformer := stability_fee.PitFileStabilityFeeTransformer{ - Fetcher: fetcher, - Converter: converter, - Repository: repository, - } err := transformer.Execute() diff --git a/pkg/transformers/test_data/mocks/pit_file/stability_fee/converter.go b/pkg/transformers/test_data/mocks/pit_file/stability_fee/converter.go index 8539d367..6e342cad 100644 --- a/pkg/transformers/test_data/mocks/pit_file/stability_fee/converter.go +++ b/pkg/transformers/test_data/mocks/pit_file/stability_fee/converter.go @@ -17,20 +17,19 @@ package stability_fee import ( "github.com/ethereum/go-ethereum/core/types" - "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/stability_fee" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" ) type MockPitFileStabilityFeeConverter struct { - converterErr error - PassedLogs []types.Log + converterError error + PassedLogs []types.Log } -func (converter *MockPitFileStabilityFeeConverter) ToModels(ethLogs []types.Log) ([]stability_fee.PitFileStabilityFeeModel, error) { +func (converter *MockPitFileStabilityFeeConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) { converter.PassedLogs = ethLogs - return []stability_fee.PitFileStabilityFeeModel{test_data.PitFileStabilityFeeModel}, converter.converterErr + return []interface{}{test_data.PitFileStabilityFeeModel}, converter.converterError } func (converter *MockPitFileStabilityFeeConverter) SetConverterError(e error) { - converter.converterErr = e + converter.converterError = e } diff --git a/pkg/transformers/test_data/mocks/pit_file/stability_fee/repository.go b/pkg/transformers/test_data/mocks/pit_file/stability_fee/repository.go index 907538a8..53967bd0 100644 --- a/pkg/transformers/test_data/mocks/pit_file/stability_fee/repository.go +++ b/pkg/transformers/test_data/mocks/pit_file/stability_fee/repository.go @@ -16,46 +16,46 @@ package stability_fee 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/stability_fee" ) type MockPitFileStabilityFeeRepository 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 []stability_fee.PitFileStabilityFeeModel + PassedModels []interface{} } -func (repository *MockPitFileStabilityFeeRepository) Create(headerID int64, models []stability_fee.PitFileStabilityFeeModel) error { +func (repository *MockPitFileStabilityFeeRepository) Create(headerID int64, models []interface{}) error { repository.PassedModels = models repository.PassedHeaderID = headerID - return repository.createErr + return repository.createError } func (repository *MockPitFileStabilityFeeRepository) MarkHeaderChecked(headerID int64) error { repository.markHeaderCheckedPassedHeaderID = headerID - return repository.markHeaderCheckedErr + return repository.markHeaderCheckedError } func (repository *MockPitFileStabilityFeeRepository) 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 *MockPitFileStabilityFeeRepository) SetMarkHeaderCheckedErr(e error) { - repository.markHeaderCheckedErr = e + repository.markHeaderCheckedError = e } func (repository *MockPitFileStabilityFeeRepository) SetMissingHeadersErr(e error) { - repository.missingHeadersErr = e + repository.missingHeadersError = e } func (repository *MockPitFileStabilityFeeRepository) SetMissingHeaders(headers []core.Header) { @@ -63,9 +63,11 @@ func (repository *MockPitFileStabilityFeeRepository) SetMissingHeaders(headers [ } func (repository *MockPitFileStabilityFeeRepository) SetCreateError(e error) { - repository.createErr = e + repository.createError = e } func (repository *MockPitFileStabilityFeeRepository) AssertMarkHeaderCheckedCalledWith(i int64) { Expect(repository.markHeaderCheckedPassedHeaderID).To(Equal(i)) } + +func (repository *MockPitFileStabilityFeeRepository) SetDB(db *postgres.DB) {}