diff --git a/pkg/transformers/pit_file/ilk/converter_test.go b/pkg/transformers/pit_file/ilk/converter_test.go index 6dba9b9b..8ddfa4e4 100644 --- a/pkg/transformers/pit_file/ilk/converter_test.go +++ b/pkg/transformers/pit_file/ilk/converter_test.go @@ -54,6 +54,6 @@ var _ = Describe("Pit file ilk converter", func() { Expect(err).NotTo(HaveOccurred()) Expect(len(models)).To(Equal(1)) - Expect(models[0]).To(Equal(test_data.PitFileIlkModel)) + Expect(models[0].(ilk.PitFileIlkModel)).To(Equal(test_data.PitFileIlkModel)) }) }) diff --git a/pkg/transformers/pit_file/ilk/repository_test.go b/pkg/transformers/pit_file/ilk/repository_test.go index feaf9dcd..1c6a7225 100644 --- a/pkg/transformers/pit_file/ilk/repository_test.go +++ b/pkg/transformers/pit_file/ilk/repository_test.go @@ -33,7 +33,7 @@ import ( var _ = Describe("Pit file ilk repository", func() { var ( db *postgres.DB - pitFileRepository ilk.Repository + pitFileRepository ilk.PitFileIlkRepository err error headerRepository datastore.HeaderRepository ) @@ -42,18 +42,24 @@ var _ = Describe("Pit file ilk repository", func() { db = test_config.NewTestDB(core.Node{}) test_config.CleanTestDB(db) headerRepository = repositories.NewHeaderRepository(db) - pitFileRepository = ilk.NewPitFileIlkRepository(db) + pitFileRepository = ilk.PitFileIlkRepository{DB: db} }) Describe("Create", func() { - var headerID int64 + var ( + db *postgres.DB + pitFileIlkRepository ilk.PitFileIlkRepository + err error + headerID int64 + ) BeforeEach(func() { headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) 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()) + pitFileIlkRepository = ilk.PitFileIlkRepository{DB: db} }) 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() { - err = pitFileRepository.Create(headerID, []ilk.PitFileIlkModel{test_data.PitFileIlkModel}) + err = pitFileRepository.Create(headerID, []interface{}{test_data.PitFileIlkModel}) Expect(err).To(HaveOccurred()) 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)) Expect(err).NotTo(HaveOccurred()) } - pitFileRepositoryTwo := ilk.NewPitFileIlkRepository(dbTwo) + + pitFileRepositoryTwo := ilk.PitFileIlkRepository{DB: dbTwo} err := pitFileRepository.MarkHeaderChecked(headerIDs[0]) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/transformers/pit_file/ilk/transformer_test.go b/pkg/transformers/pit_file/ilk/transformer_test.go index 2b6cf806..6f030a92 100644 --- a/pkg/transformers/pit_file/ilk/transformer_test.go +++ b/pkg/transformers/pit_file/ilk/transformer_test.go @@ -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" @@ -31,15 +32,31 @@ import ( ) var _ = Describe("Pit file ilk transformer", func() { - It("gets missing headers for block numbers specified in config", func() { - repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} - transformer := ilk.PitFileIlkTransformer{ - Config: pit_file.PitFileConfig, - Fetcher: &mocks.MockLogFetcher{}, - Converter: &pit_file_ilk_mocks.MockPitFileIlkConverter{}, - Repository: repository, - } + var ( + config = ilk.IlkFileConfig + fetcher mocks.MockLogFetcher + converter pit_file_ilk_mocks.MockPitFileIlkConverter + repository pit_file_ilk_mocks.MockPitFileIlkRepository + transformer shared.Transformer + 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() 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() { - repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} - repository.SetMissingHeadersErr(fakes.FakeError) - transformer := ilk.PitFileIlkTransformer{ - Fetcher: &mocks.MockLogFetcher{}, - Converter: &pit_file_ilk_mocks.MockPitFileIlkConverter{}, - Repository: repository, - } + repository.SetMissingHeadersError(fakes.FakeError) err := transformer.Execute() @@ -63,33 +74,19 @@ var _ = Describe("Pit file ilk transformer", func() { }) It("fetches logs for missing headers", func() { - fetcher := &mocks.MockLogFetcher{} - 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, - } + repository.SetMissingHeaders([]core.Header{headerOne, headerTwo}) err := transformer.Execute() 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.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.PitFileIlkSignature)}})) }) It("returns error if fetcher returns error", func() { - fetcher := &mocks.MockLogFetcher{} fetcher.SetFetcherError(fakes.FakeError) - repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} - repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) - transformer := ilk.PitFileIlkTransformer{ - Fetcher: fetcher, - Converter: &pit_file_ilk_mocks.MockPitFileIlkConverter{}, - Repository: repository, - } + repository.SetMissingHeaders([]core.Header{headerOne}) err := transformer.Execute() @@ -98,34 +95,17 @@ var _ = Describe("Pit file ilk transformer", func() { }) It("marks header checked if no logs returned", func() { - mockConverter := &pit_file_ilk_mocks.MockPitFileIlkConverter{} - 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, - } + 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 := &pit_file_ilk_mocks.MockPitFileIlkConverter{} - mockRepository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} - mockRepository.SetMissingHeaders([]core.Header{{Id: int64(123)}}) - mockRepository.SetMarkHeaderCheckedErr(fakes.FakeError) - mockFetcher := &mocks.MockLogFetcher{} - transformer := ilk.PitFileIlkTransformer{ - Converter: mockConverter, - Fetcher: mockFetcher, - Repository: mockRepository, - } + repository.SetMissingHeaders([]core.Header{headerOne}) + repository.SetMarkHeaderCheckedError(fakes.FakeError) err := transformer.Execute() @@ -134,16 +114,8 @@ var _ = Describe("Pit file ilk transformer", func() { }) It("converts matching logs", func() { - converter := &pit_file_ilk_mocks.MockPitFileIlkConverter{} - fetcher := &mocks.MockLogFetcher{} fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileIlkLog}) - repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} - repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) - transformer := ilk.PitFileIlkTransformer{ - Fetcher: fetcher, - Converter: converter, - Repository: repository, - } + repository.SetMissingHeaders([]core.Header{headerOne}) err := transformer.Execute() @@ -152,17 +124,9 @@ var _ = Describe("Pit file ilk transformer", func() { }) It("returns error if converter returns error", func() { - converter := &pit_file_ilk_mocks.MockPitFileIlkConverter{} converter.SetConverterError(fakes.FakeError) - fetcher := &mocks.MockLogFetcher{} fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileIlkLog}) - repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} - repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) - transformer := ilk.PitFileIlkTransformer{ - Fetcher: fetcher, - Converter: converter, - Repository: repository, - } + repository.SetMissingHeaders([]core.Header{headerOne}) err := transformer.Execute() @@ -171,37 +135,20 @@ var _ = Describe("Pit file ilk transformer", func() { }) It("persists pit file model", func() { - converter := &pit_file_ilk_mocks.MockPitFileIlkConverter{} - fetcher := &mocks.MockLogFetcher{} fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileIlkLog}) - repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} - fakeHeader := core.Header{BlockNumber: 1, Id: 2} - repository.SetMissingHeaders([]core.Header{fakeHeader}) - transformer := ilk.PitFileIlkTransformer{ - 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([]ilk.PitFileIlkModel{test_data.PitFileIlkModel})) + Expect(repository.PassedHeaderID).To(Equal(headerOne.Id)) + Expect(repository.PassedModels).To(Equal([]interface{}{test_data.PitFileIlkModel})) }) 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}) - repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} - repository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: 2}}) + repository.SetMissingHeaders([]core.Header{headerOne}) repository.SetCreateError(fakes.FakeError) - transformer := ilk.PitFileIlkTransformer{ - Fetcher: fetcher, - Converter: converter, - Repository: repository, - } err := transformer.Execute() diff --git a/pkg/transformers/test_data/mocks/pit_file/ilk/converter.go b/pkg/transformers/test_data/mocks/pit_file/ilk/converter.go index d7052c42..0924b5e9 100644 --- a/pkg/transformers/test_data/mocks/pit_file/ilk/converter.go +++ b/pkg/transformers/test_data/mocks/pit_file/ilk/converter.go @@ -17,7 +17,6 @@ package ilk import ( "github.com/ethereum/go-ethereum/core/types" - "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/ilk" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" ) @@ -30,7 +29,7 @@ func (converter *MockPitFileIlkConverter) SetConverterError(err error) { 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 - return []ilk.PitFileIlkModel{test_data.PitFileIlkModel}, converter.converterError + return []interface{}{test_data.PitFileIlkModel}, converter.converterError } diff --git a/pkg/transformers/test_data/mocks/pit_file/ilk/repository.go b/pkg/transformers/test_data/mocks/pit_file/ilk/repository.go index 4bb8db36..899ffab4 100644 --- a/pkg/transformers/test_data/mocks/pit_file/ilk/repository.go +++ b/pkg/transformers/test_data/mocks/pit_file/ilk/repository.go @@ -16,40 +16,40 @@ package ilk 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/ilk" ) type MockPitFileIlkRepository struct { createError error PassedEndingBlockNumber int64 - PassedModels []ilk.PitFileIlkModel + PassedModels []interface{} PassedHeaderID int64 PassedStartingBlockNumber int64 - markHeaderCheckedErr error + markHeaderCheckedError error markHeaderCheckedPassedHeaderID int64 missingHeaders []core.Header - missingHeadersErr error + missingHeadersError error } func (repository *MockPitFileIlkRepository) SetCreateError(err error) { repository.createError = err } -func (repository *MockPitFileIlkRepository) SetMarkHeaderCheckedErr(e error) { - repository.markHeaderCheckedErr = e +func (repository *MockPitFileIlkRepository) SetMarkHeaderCheckedError(e error) { + repository.markHeaderCheckedError = e } -func (repository *MockPitFileIlkRepository) SetMissingHeadersErr(err error) { - repository.missingHeadersErr = err +func (repository *MockPitFileIlkRepository) SetMissingHeadersError(err error) { + repository.missingHeadersError = err } func (repository *MockPitFileIlkRepository) SetMissingHeaders(headers []core.Header) { 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.PassedModels = models return repository.createError @@ -57,15 +57,17 @@ func (repository *MockPitFileIlkRepository) Create(headerID int64, models []ilk. func (repository *MockPitFileIlkRepository) MarkHeaderChecked(headerID int64) error { repository.markHeaderCheckedPassedHeaderID = headerID - return repository.markHeaderCheckedErr + return repository.markHeaderCheckedError } func (repository *MockPitFileIlkRepository) 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 *MockPitFileIlkRepository) AssertMarkHeaderCheckedCalledWith(i int64) { Expect(repository.markHeaderCheckedPassedHeaderID).To(Equal(i)) } + +func (repository *MockPitFileIlkRepository) SetDB(db *postgres.DB) {} \ No newline at end of file