From 0d325afbfbcaf42987a41f9af2731a9e692485d3 Mon Sep 17 00:00:00 2001 From: Takayuki Goto Date: Fri, 2 Nov 2018 09:13:10 -0500 Subject: [PATCH] flop kick transformer factory (#98) * flop kick transformer factory * add tests for wrong model/entity --- pkg/transformers/flop_kick/config.go | 5 +- pkg/transformers/flop_kick/converter.go | 35 +-- pkg/transformers/flop_kick/converter_test.go | 34 ++- pkg/transformers/flop_kick/repository.go | 36 +-- pkg/transformers/flop_kick/repository_test.go | 29 ++- pkg/transformers/flop_kick/transformer.go | 81 ------ .../flop_kick/transformer_test.go | 246 ------------------ .../integration_tests/flop_kick.go | 20 +- pkg/transformers/test_data/mocks/converter.go | 28 +- .../test_data/mocks/flop_kick/converter.go | 53 ---- .../test_data/mocks/flop_kick/repository.go | 68 ----- .../test_data/mocks/repository.go | 3 + pkg/transformers/transformers.go | 8 +- 13 files changed, 130 insertions(+), 516 deletions(-) delete mode 100644 pkg/transformers/flop_kick/transformer.go delete mode 100644 pkg/transformers/flop_kick/transformer_test.go delete mode 100644 pkg/transformers/test_data/mocks/flop_kick/converter.go delete mode 100644 pkg/transformers/test_data/mocks/flop_kick/repository.go diff --git a/pkg/transformers/flop_kick/config.go b/pkg/transformers/flop_kick/config.go index 59cec97f..0775b953 100644 --- a/pkg/transformers/flop_kick/config.go +++ b/pkg/transformers/flop_kick/config.go @@ -16,10 +16,11 @@ package flop_kick import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" -var Config = shared.TransformerConfig{ +var Config = shared.SingleTransformerConfig{ + TransformerName: shared.FlopKickLabel, ContractAddresses: []string{shared.FlopperContractAddress}, ContractAbi: shared.FlopperABI, - Topics: []string{shared.FlopKickSignature}, + Topic: shared.FlopKickSignature, StartingBlockNumber: 0, EndingBlockNumber: 10000000, } diff --git a/pkg/transformers/flop_kick/converter.go b/pkg/transformers/flop_kick/converter.go index a1c566a3..d36ca3b4 100644 --- a/pkg/transformers/flop_kick/converter.go +++ b/pkg/transformers/flop_kick/converter.go @@ -21,19 +21,15 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/core/types" + "fmt" "github.com/vulcanize/vulcanizedb/pkg/geth" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" ) -type Converter interface { - ToEntities(contractAbi string, ethLogs []types.Log) ([]Entity, error) - ToModels(entities []Entity) ([]Model, error) -} - type FlopKickConverter struct{} -func (FlopKickConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]Entity, error) { - var results []Entity +func (FlopKickConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]interface{}, error) { + var results []interface{} for _, ethLog := range ethLogs { entity := Entity{} address := ethLog.Address @@ -56,23 +52,28 @@ func (FlopKickConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([] return results, nil } -func (FlopKickConverter) ToModels(entities []Entity) ([]Model, error) { - var results []Model +func (FlopKickConverter) ToModels(entities []interface{}) ([]interface{}, error) { + var results []interface{} for _, entity := range entities { - endValue := shared.BigIntToInt64(entity.End) - rawLogJson, err := json.Marshal(entity.Raw) + flopKickEntity, ok := entity.(Entity) + if !ok { + return nil, fmt.Errorf("entity of type %T, not %T", entity, Entity{}) + } + + endValue := shared.BigIntToInt64(flopKickEntity.End) + rawLogJson, err := json.Marshal(flopKickEntity.Raw) if err != nil { return nil, err } model := Model{ - BidId: shared.BigIntToString(entity.Id), - Lot: shared.BigIntToString(entity.Lot), - Bid: shared.BigIntToString(entity.Bid), - Gal: entity.Gal.String(), + BidId: shared.BigIntToString(flopKickEntity.Id), + Lot: shared.BigIntToString(flopKickEntity.Lot), + Bid: shared.BigIntToString(flopKickEntity.Bid), + Gal: flopKickEntity.Gal.String(), End: time.Unix(endValue, 0), - TransactionIndex: entity.TransactionIndex, - LogIndex: entity.LogIndex, + TransactionIndex: flopKickEntity.TransactionIndex, + LogIndex: flopKickEntity.LogIndex, Raw: rawLogJson, } results = append(results, model) diff --git a/pkg/transformers/flop_kick/converter_test.go b/pkg/transformers/flop_kick/converter_test.go index f019b936..678e44f4 100644 --- a/pkg/transformers/flop_kick/converter_test.go +++ b/pkg/transformers/flop_kick/converter_test.go @@ -20,6 +20,7 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "encoding/json" "github.com/ethereum/go-ethereum/core/types" "github.com/vulcanize/vulcanizedb/pkg/transformers/flop_kick" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" @@ -50,27 +51,42 @@ var _ = Describe("FlopKick Converter", func() { var emptyAddressHex = "0x0000000000000000000000000000000000000000" var emptyString = "" var emptyTime = time.Unix(0, 0) - var emptyEntities = []flop_kick.Entity{flop_kick.Entity{}} + var emptyEntity = flop_kick.Entity{} It("converts an Entity to a Model", func() { converter := flop_kick.FlopKickConverter{} - models, err := converter.ToModels([]flop_kick.Entity{test_data.FlopKickEntity}) + models, err := converter.ToModels([]interface{}{test_data.FlopKickEntity}) Expect(err).NotTo(HaveOccurred()) Expect(models[0]).To(Equal(test_data.FlopKickModel)) }) - It("handles nil values", func() { + It("returns error if wrong entity", func() { converter := flop_kick.FlopKickConverter{} + _, err := converter.ToModels([]interface{}{test_data.WrongEntity{}}) - models, err := converter.ToModels(emptyEntities) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("entity of type test_data.WrongEntity, not flop_kick.Entity")) + }) + + It("handles nil values", func() { + emptyLog, err := json.Marshal(types.Log{}) + + converter := flop_kick.FlopKickConverter{} + expectedModel := flop_kick.Model{ + BidId: emptyString, + Lot: emptyString, + Bid: emptyString, + Gal: emptyAddressHex, + End: emptyTime, + TransactionIndex: 0, + Raw: emptyLog, + } + + models, err := converter.ToModels([]interface{}{emptyEntity}) model := models[0] Expect(err).NotTo(HaveOccurred()) - Expect(model.BidId).To(Equal(emptyString)) - Expect(model.Lot).To(Equal(emptyString)) - Expect(model.Bid).To(Equal(emptyString)) - Expect(model.Gal).To(Equal(emptyAddressHex)) - Expect(model.End).To(Equal(emptyTime)) + Expect(model).To(Equal(expectedModel)) }) }) }) diff --git a/pkg/transformers/flop_kick/repository.go b/pkg/transformers/flop_kick/repository.go index 555029bc..bfa14560 100644 --- a/pkg/transformers/flop_kick/repository.go +++ b/pkg/transformers/flop_kick/repository.go @@ -15,34 +15,30 @@ package flop_kick import ( + "fmt" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" ) -type Repository interface { - Create(headerId int64, flopKick []Model) error - MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) - MarkHeaderChecked(headerId int64) error -} - type FlopKickRepository struct { - DB *postgres.DB + db *postgres.DB } -func NewFlopKickRepository(db *postgres.DB) FlopKickRepository { - return FlopKickRepository{DB: db} -} - -func (r FlopKickRepository) Create(headerId int64, flopKicks []Model) error { - tx, err := r.DB.Begin() +func (r FlopKickRepository) Create(headerId int64, models []interface{}) error { + tx, err := r.db.Begin() if err != nil { return err } - for _, flopKick := range flopKicks { + for _, flopKick := range models { + flopKickModel, ok := flopKick.(Model) + + if !ok { + return fmt.Errorf("model of type %T, not %T", flopKick, Model{}) + } _, err = tx.Exec( `INSERT into maker.flop_kick (header_id, bid_id, lot, bid, gal, "end", tx_idx, log_idx, raw_log) VALUES($1, $2::NUMERIC, $3::NUMERIC, $4::NUMERIC, $5, $6, $7, $8, $9)`, - headerId, flopKick.BidId, flopKick.Lot, flopKick.Bid, flopKick.Gal, flopKick.End, flopKick.TransactionIndex, flopKick.LogIndex, flopKick.Raw, + headerId, flopKickModel.BidId, flopKickModel.Lot, flopKickModel.Bid, flopKickModel.Gal, flopKickModel.End, flopKickModel.TransactionIndex, flopKickModel.LogIndex, flopKickModel.Raw, ) if err != nil { tx.Rollback() @@ -63,7 +59,7 @@ func (r FlopKickRepository) Create(headerId int64, flopKicks []Model) error { } func (r FlopKickRepository) MarkHeaderChecked(headerId int64) error { - _, err := r.DB.Exec(`INSERT INTO public.checked_headers (header_id, flop_kick_checked) + _, err := r.db.Exec(`INSERT INTO public.checked_headers (header_id, flop_kick_checked) VALUES ($1, $2) ON CONFLICT (header_id) DO UPDATE SET flop_kick_checked = $2`, headerId, true) @@ -72,7 +68,7 @@ func (r FlopKickRepository) MarkHeaderChecked(headerId int64) error { func (r FlopKickRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { var result []core.Header - err := r.DB.Select( + err := r.db.Select( &result, `SELECT headers.id, headers.block_number FROM headers LEFT JOIN checked_headers on headers.id = header_id @@ -82,8 +78,12 @@ func (r FlopKickRepository) MissingHeaders(startingBlockNumber, endingBlockNumbe AND headers.eth_node_fingerprint = $3`, startingBlockNumber, endingBlockNumber, - r.DB.Node.ID, + r.db.Node.ID, ) return result, err } + +func (repository *FlopKickRepository) SetDB(db *postgres.DB) { + repository.db = db +} diff --git a/pkg/transformers/flop_kick/repository_test.go b/pkg/transformers/flop_kick/repository_test.go index bdb08b8c..9289a115 100644 --- a/pkg/transformers/flop_kick/repository_test.go +++ b/pkg/transformers/flop_kick/repository_test.go @@ -39,7 +39,8 @@ var _ = Describe("FlopRepository", func() { BeforeEach(func() { db = test_config.NewTestDB(test_config.NewTestNode()) test_config.CleanTestDB(db) - repository = flop_kick.NewFlopKickRepository(db) + repository = flop_kick.FlopKickRepository{} + repository.SetDB(db) headerRepository = repositories.NewHeaderRepository(db) dbResult = test_data.FlopKickDBResult{} }) @@ -53,7 +54,7 @@ var _ = Describe("FlopRepository", func() { }) It("creates FlopKick records", func() { - err := repository.Create(headerId, []flop_kick.Model{test_data.FlopKickModel}) + err := repository.Create(headerId, []interface{}{test_data.FlopKickModel}) Expect(err).NotTo(HaveOccurred()) err = db.QueryRowx(`SELECT * FROM maker.flop_kick WHERE header_id = $1`, headerId).StructScan(&dbResult) @@ -70,7 +71,7 @@ var _ = Describe("FlopRepository", func() { }) It("marks headerId as checked for flop kick logs", func() { - err := repository.Create(headerId, []flop_kick.Model{test_data.FlopKickModel}) + err := repository.Create(headerId, []interface{}{test_data.FlopKickModel}) Expect(err).NotTo(HaveOccurred()) var headerChecked bool @@ -82,7 +83,7 @@ var _ = Describe("FlopRepository", func() { It("updates the header to checked if checked headers row already exists", func() { _, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerId) Expect(err).NotTo(HaveOccurred()) - err = repository.Create(headerId, []flop_kick.Model{test_data.FlopKickModel}) + err = repository.Create(headerId, []interface{}{test_data.FlopKickModel}) Expect(err).NotTo(HaveOccurred()) var headerChecked bool @@ -92,27 +93,27 @@ var _ = Describe("FlopRepository", func() { }) It("returns an error if inserting the flop_kick record fails", func() { - err := repository.Create(headerId, []flop_kick.Model{test_data.FlopKickModel}) + err := repository.Create(headerId, []interface{}{test_data.FlopKickModel}) Expect(err).NotTo(HaveOccurred()) - err = repository.Create(headerId, []flop_kick.Model{test_data.FlopKickModel}) + err = repository.Create(headerId, []interface{}{test_data.FlopKickModel}) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) }) It("allows for multiple flop kick events in one transaction if they have different log indexes", func() { - err := repository.Create(headerId, []flop_kick.Model{test_data.FlopKickModel}) + err := repository.Create(headerId, []interface{}{test_data.FlopKickModel}) Expect(err).NotTo(HaveOccurred()) newFlopKick := test_data.FlopKickModel newFlopKick.LogIndex = newFlopKick.LogIndex + 1 - err = repository.Create(headerId, []flop_kick.Model{newFlopKick}) + err = repository.Create(headerId, []interface{}{newFlopKick}) Expect(err).NotTo(HaveOccurred()) }) It("deletes the flop_kick records if its corresponding header record is deleted", func() { - err := repository.Create(headerId, []flop_kick.Model{test_data.FlopKickModel}) + err := repository.Create(headerId, []interface{}{test_data.FlopKickModel}) Expect(err).NotTo(HaveOccurred()) var flopKickCount int @@ -208,7 +209,8 @@ var _ = Describe("FlopRepository", func() { node2 := core.Node{} db2 := test_config.NewTestDB(node2) headerRepository2 := repositories.NewHeaderRepository(db2) - flopKickRepository2 := flop_kick.NewFlopKickRepository(db2) + flopKickRepository2 := flop_kick.FlopKickRepository{} + flopKickRepository2.SetDB(db2) for _, number := range []int64{startingBlock, flopKickBlock, endingBlock} { headerRepository2.CreateOrUpdateHeader(fakes.GetFakeHeader(number)) @@ -226,5 +228,12 @@ var _ = Describe("FlopRepository", func() { Expect(err).NotTo(HaveOccurred()) Expect(len(node2MissingHeaders)).To(Equal(3)) }) + + It("returns an error when wrong model is passed", func() { + err = repository.Create(headerIds[0], []interface{}{test_data.WrongModel{}}) + + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("model of type test_data.WrongModel, not flop_kick.Model")) + }) }) }) diff --git a/pkg/transformers/flop_kick/transformer.go b/pkg/transformers/flop_kick/transformer.go deleted file mode 100644 index 1ea25dbb..00000000 --- a/pkg/transformers/flop_kick/transformer.go +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 2018 Vulcanize -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package flop_kick - -import ( - "github.com/ethereum/go-ethereum/common" - - "github.com/vulcanize/vulcanizedb/pkg/core" - "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" - "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" -) - -type Transformer struct { - Config shared.TransformerConfig - Converter Converter - Fetcher shared.LogFetcher - Repository Repository -} - -type FlopKickTransformerInitializer struct { - Config shared.TransformerConfig -} - -func (i FlopKickTransformerInitializer) NewFlopKickTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer { - return Transformer{ - Config: i.Config, - Converter: FlopKickConverter{}, - Fetcher: shared.NewFetcher(blockChain), - Repository: NewFlopKickRepository(db), - } -} - -func (t Transformer) Execute() error { - config := t.Config - headers, err := t.Repository.MissingHeaders(config.StartingBlockNumber, config.EndingBlockNumber) - if err != nil { - return err - } - - for _, header := range headers { - topics := [][]common.Hash{{common.HexToHash(shared.FlopKickSignature)}} - matchingLogs, err := t.Fetcher.FetchLogs(config.ContractAddresses, topics, header.BlockNumber) - if err != nil { - return err - } - if len(matchingLogs) < 1 { - err := t.Repository.MarkHeaderChecked(header.Id) - if err != nil { - return err - } - } - - entities, err := t.Converter.ToEntities(config.ContractAbi, matchingLogs) - if err != nil { - return err - } - - models, err := t.Converter.ToModels(entities) - if err != nil { - return err - } - - err = t.Repository.Create(header.Id, models) - if err != nil { - return err - } - } - return nil -} diff --git a/pkg/transformers/flop_kick/transformer_test.go b/pkg/transformers/flop_kick/transformer_test.go deleted file mode 100644 index d1ed392a..00000000 --- a/pkg/transformers/flop_kick/transformer_test.go +++ /dev/null @@ -1,246 +0,0 @@ -// Copyright 2018 Vulcanize -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package flop_kick_test - -import ( - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - - "github.com/vulcanize/vulcanizedb/pkg/core" - "github.com/vulcanize/vulcanizedb/pkg/fakes" - "github.com/vulcanize/vulcanizedb/pkg/transformers/flop_kick" - "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" - "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" - "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks" - flop_kick_mocks "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/flop_kick" -) - -var _ = Describe("FlopKick Transformer", func() { - var fetcher mocks.MockLogFetcher - var converter flop_kick_mocks.MockConverter - var repository flop_kick_mocks.MockRepository - var config = flop_kick.Config - var headerOne core.Header - var headerTwo core.Header - - BeforeEach(func() { - fetcher = mocks.MockLogFetcher{} - converter = flop_kick_mocks.MockConverter{} - repository = flop_kick_mocks.MockRepository{} - headerOne = core.Header{Id: GinkgoRandomSeed(), BlockNumber: GinkgoRandomSeed()} - headerTwo = core.Header{Id: GinkgoRandomSeed(), BlockNumber: GinkgoRandomSeed()} - }) - - It("gets missing headers for specified block numbers", func() { - transformer := flop_kick.Transformer{ - Config: config, - Converter: &converter, - Fetcher: &fetcher, - Repository: &repository, - } - - err := transformer.Execute() - Expect(err).NotTo(HaveOccurred()) - Expect(repository.PassedStartingBlockNumber).To(Equal(flop_kick.Config.StartingBlockNumber)) - Expect(repository.PassedEndingBlockNumber).To(Equal(flop_kick.Config.EndingBlockNumber)) - }) - - It("returns error if getting missing headers fails", func() { - transformer := flop_kick.Transformer{ - Config: config, - Converter: &converter, - Fetcher: &fetcher, - Repository: &repository, - } - repository.SetMissingHeadersError(fakes.FakeError) - - err := transformer.Execute() - - Expect(err).To(HaveOccurred()) - Expect(err).To(MatchError(fakes.FakeError)) - }) - - It("fetches logs for each missing header", func() { - transformer := flop_kick.Transformer{ - Config: config, - Converter: &converter, - Fetcher: &fetcher, - Repository: &repository, - } - repository.SetMissingHeaders([]core.Header{headerOne, headerTwo}) - - err := transformer.Execute() - - Expect(err).NotTo(HaveOccurred()) - Expect(fetcher.FetchedBlocks).To(Equal([]int64{headerOne.BlockNumber, headerTwo.BlockNumber})) - Expect(fetcher.FetchedContractAddresses).To(Equal([][]string{flop_kick.Config.ContractAddresses, flop_kick.Config.ContractAddresses})) - Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.FlopKickSignature)}})) - }) - - It("returns error if fetcher returns error", func() { - transformer := flop_kick.Transformer{ - Config: config, - Converter: &converter, - Fetcher: &fetcher, - Repository: &repository, - } - fetcher.SetFetcherError(fakes.FakeError) - repository.SetMissingHeaders([]core.Header{{}}) - - err := transformer.Execute() - - Expect(err).To(HaveOccurred()) - Expect(err).To(MatchError(fakes.FakeError)) - }) - - It("marks header as checked even if no logs were returned", func() { - transformer := flop_kick.Transformer{ - Config: config, - Converter: &converter, - Fetcher: &fetcher, - Repository: &repository, - } - repository.SetMissingHeaders([]core.Header{headerOne, headerTwo}) - fetcher.SetFetchedLogs([]types.Log{}) - - err := transformer.Execute() - - Expect(err).NotTo(HaveOccurred()) - Expect(repository.CheckedHeaderIds).To(ContainElement(headerOne.Id)) - Expect(repository.CheckedHeaderIds).To(ContainElement(headerTwo.Id)) - }) - - It("returns error if marking header checked returns err", func() { - transformer := flop_kick.Transformer{ - Config: config, - Converter: &converter, - Fetcher: &fetcher, - Repository: &repository, - } - repository.SetMissingHeaders([]core.Header{headerOne, headerTwo}) - repository.SetCheckedHeaderError(fakes.FakeError) - fetcher.SetFetchedLogs([]types.Log{}) - - err := transformer.Execute() - - Expect(err).To(HaveOccurred()) - Expect(err).To(MatchError(fakes.FakeError)) - }) - - It("converts matching logs to entity", func() { - transformer := flop_kick.Transformer{ - Config: config, - Converter: &converter, - Fetcher: &fetcher, - Repository: &repository, - } - repository.SetMissingHeaders([]core.Header{{BlockNumber: GinkgoRandomSeed()}}) - fetcher.SetFetchedLogs([]types.Log{test_data.FlopKickLog}) - - err := transformer.Execute() - - Expect(err).NotTo(HaveOccurred()) - Expect(converter.PassedContractAddresses).To(Equal(flop_kick.Config.ContractAddresses)) - Expect(converter.PassedContractABI).To(Equal(flop_kick.Config.ContractAbi)) - Expect(converter.PassedLogs).To(Equal([]types.Log{test_data.FlopKickLog})) - }) - - It("returns an error if converting logs to entity fails", func() { - transformer := flop_kick.Transformer{ - Config: config, - Converter: &converter, - Fetcher: &fetcher, - Repository: &repository, - } - repository.SetMissingHeaders([]core.Header{{BlockNumber: GinkgoRandomSeed()}}) - fetcher.SetFetchedLogs([]types.Log{test_data.FlopKickLog}) - converter.SetToEntityConverterError(fakes.FakeError) - - err := transformer.Execute() - - Expect(err).To(HaveOccurred()) - Expect(err).To(MatchError(fakes.FakeError)) - }) - - It("converts flop_kick entity to model", func() { - transformer := flop_kick.Transformer{ - Config: config, - Converter: &converter, - Fetcher: &fetcher, - Repository: &repository, - } - repository.SetMissingHeaders([]core.Header{{}}) - fetcher.SetFetchedLogs([]types.Log{test_data.FlopKickLog}) - - err := transformer.Execute() - - Expect(err).NotTo(HaveOccurred()) - Expect(converter.PassedEntities).To(Equal([]flop_kick.Entity{test_data.FlopKickEntity})) - }) - - It("returns an error if there's a failure in converting to model", func() { - transformer := flop_kick.Transformer{ - Config: config, - Converter: &converter, - Fetcher: &fetcher, - Repository: &repository, - } - repository.SetMissingHeaders([]core.Header{{}}) - fetcher.SetFetchedLogs([]types.Log{test_data.FlopKickLog}) - converter.SetToModelConverterError(fakes.FakeError) - - err := transformer.Execute() - - Expect(err).To(HaveOccurred()) - Expect(err).To(MatchError(fakes.FakeError)) - }) - - It("persists the flop_kick model", func() { - transformer := flop_kick.Transformer{ - Config: config, - Converter: &converter, - Fetcher: &fetcher, - Repository: &repository, - } - repository.SetMissingHeaders([]core.Header{headerOne, headerTwo}) - fetcher.SetFetchedLogs([]types.Log{test_data.FlopKickLog}) - - err := transformer.Execute() - - Expect(err).NotTo(HaveOccurred()) - Expect(repository.CreatedHeaderIds).To(ContainElement(headerOne.Id)) - Expect(repository.CreatedHeaderIds).To(ContainElement(headerTwo.Id)) - Expect(repository.CreatedModels).To(ContainElement(test_data.FlopKickModel)) - }) - - It("returns error if repository returns error for create", func() { - transformer := flop_kick.Transformer{ - Config: config, - Converter: &converter, - Fetcher: &fetcher, - Repository: &repository, - } - repository.SetMissingHeaders([]core.Header{{}}) - repository.SetCreateError(fakes.FakeError) - fetcher.SetFetchedLogs([]types.Log{test_data.FlopKickLog}) - - err := transformer.Execute() - - Expect(err).To(HaveOccurred()) - Expect(err).To(MatchError(fakes.FakeError)) - }) -}) diff --git a/pkg/transformers/integration_tests/flop_kick.go b/pkg/transformers/integration_tests/flop_kick.go index 2d7fe007..231190c8 100644 --- a/pkg/transformers/integration_tests/flop_kick.go +++ b/pkg/transformers/integration_tests/flop_kick.go @@ -20,7 +20,9 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "github.com/vulcanize/vulcanizedb/pkg/transformers/factories" "github.com/vulcanize/vulcanizedb/pkg/transformers/flop_kick" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/test_config" ) @@ -42,8 +44,13 @@ var _ = Describe("FlopKick Transformer", func() { err = persistHeader(db, blockNumber) Expect(err).NotTo(HaveOccurred()) - initializer := flop_kick.FlopKickTransformerInitializer{Config: config} - transformer := initializer.NewFlopKickTransformer(db, blockchain) + initializer := factories.Transformer{ + Config: flop_kick.Config, + Converter: &flop_kick.FlopKickConverter{}, + Repository: &flop_kick.FlopKickRepository{}, + Fetcher: &shared.Fetcher{}, + } + transformer := initializer.NewTransformer(db, blockchain) err = transformer.Execute() Expect(err).NotTo(HaveOccurred()) @@ -77,8 +84,13 @@ var _ = Describe("FlopKick Transformer", func() { err = persistHeader(db, blockNumber) Expect(err).NotTo(HaveOccurred()) - initializer := flop_kick.FlopKickTransformerInitializer{Config: config} - transformer := initializer.NewFlopKickTransformer(db, blockchain) + initializer := factories.Transformer{ + Config: flop_kick.Config, + Converter: &flop_kick.FlopKickConverter{}, + Repository: &flop_kick.FlopKickRepository{}, + Fetcher: &shared.Fetcher{}, + } + transformer := initializer.NewTransformer(db, blockchain) err = transformer.Execute() Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/transformers/test_data/mocks/converter.go b/pkg/transformers/test_data/mocks/converter.go index b6572bb2..0073a96f 100644 --- a/pkg/transformers/test_data/mocks/converter.go +++ b/pkg/transformers/test_data/mocks/converter.go @@ -5,16 +5,22 @@ import ( ) type MockConverter struct { - ToEntitiesError error - ToModelsError error - ContractAbi string - LogsToConvert []types.Log - EntitiesToConvert []interface{} - EntitiesToReturn []interface{} - ModelsToReturn []interface{} + ToEntitiesError error + PassedContractAddresses []string + ToModelsError error + entityConverterError error + modelConverterError error + ContractAbi string + LogsToConvert []types.Log + EntitiesToConvert []interface{} + EntitiesToReturn []interface{} + ModelsToReturn []interface{} } func (converter *MockConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]interface{}, error) { + for _, log := range ethLogs { + converter.PassedContractAddresses = append(converter.PassedContractAddresses, log.Address.Hex()) + } converter.ContractAbi = contractAbi converter.LogsToConvert = ethLogs return converter.EntitiesToReturn, converter.ToEntitiesError @@ -24,3 +30,11 @@ func (converter *MockConverter) ToModels(entities []interface{}) ([]interface{}, converter.EntitiesToConvert = entities return converter.ModelsToReturn, converter.ToModelsError } + +func (converter *MockConverter) SetToEntityConverterError(err error) { + converter.entityConverterError = err +} + +func (c *MockConverter) SetToModelConverterError(err error) { + c.modelConverterError = err +} diff --git a/pkg/transformers/test_data/mocks/flop_kick/converter.go b/pkg/transformers/test_data/mocks/flop_kick/converter.go deleted file mode 100644 index 2d3f9849..00000000 --- a/pkg/transformers/test_data/mocks/flop_kick/converter.go +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2018 Vulcanize -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package flop_kick - -import ( - "github.com/ethereum/go-ethereum/core/types" - "github.com/vulcanize/vulcanizedb/pkg/transformers/flop_kick" - "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" -) - -type MockConverter struct { - PassedContractAddresses []string - PassedContractABI string - PassedLogs []types.Log - PassedEntities []flop_kick.Entity - entityConverterError error - modelConverterError error -} - -func (c *MockConverter) ToEntities(contractAbi string, ethLogs []types.Log) ([]flop_kick.Entity, error) { - for _, log := range ethLogs { - c.PassedContractAddresses = append(c.PassedContractAddresses, log.Address.Hex()) - } - c.PassedContractABI = contractAbi - c.PassedLogs = ethLogs - - return []flop_kick.Entity{test_data.FlopKickEntity}, c.entityConverterError -} - -func (c *MockConverter) ToModels(entities []flop_kick.Entity) ([]flop_kick.Model, error) { - c.PassedEntities = entities - return []flop_kick.Model{test_data.FlopKickModel}, c.modelConverterError -} - -func (c *MockConverter) SetToEntityConverterError(err error) { - c.entityConverterError = err -} - -func (c *MockConverter) SetToModelConverterError(err error) { - c.modelConverterError = err -} diff --git a/pkg/transformers/test_data/mocks/flop_kick/repository.go b/pkg/transformers/test_data/mocks/flop_kick/repository.go deleted file mode 100644 index a46e9876..00000000 --- a/pkg/transformers/test_data/mocks/flop_kick/repository.go +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 2018 Vulcanize -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package flop_kick - -import ( - "github.com/vulcanize/vulcanizedb/pkg/core" - "github.com/vulcanize/vulcanizedb/pkg/transformers/flop_kick" -) - -type MockRepository struct { - PassedStartingBlockNumber int64 - PassedEndingBlockNumber int64 - CreatedHeaderIds []int64 - CreatedModels []flop_kick.Model - CheckedHeaderIds []int64 - missingHeaders []core.Header - missingHeadersError error - createError error - checkedHeaderError error -} - -func (r *MockRepository) Create(headerId int64, flopKicks []flop_kick.Model) error { - r.CreatedHeaderIds = append(r.CreatedHeaderIds, headerId) - r.CreatedModels = flopKicks - - return r.createError -} - -func (r *MockRepository) MarkHeaderChecked(headerId int64) error { - r.CheckedHeaderIds = append(r.CheckedHeaderIds, headerId) - - return r.checkedHeaderError -} - -func (r *MockRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - r.PassedStartingBlockNumber = startingBlockNumber - r.PassedEndingBlockNumber = endingBlockNumber - - return r.missingHeaders, r.missingHeadersError -} - -func (r *MockRepository) SetMissingHeaders(headers []core.Header) { - r.missingHeaders = headers -} - -func (r *MockRepository) SetMissingHeadersError(err error) { - r.missingHeadersError = err -} - -func (r *MockRepository) SetCreateError(err error) { - r.createError = err -} - -func (r *MockRepository) SetCheckedHeaderError(err error) { - r.checkedHeaderError = err -} diff --git a/pkg/transformers/test_data/mocks/repository.go b/pkg/transformers/test_data/mocks/repository.go index 719c57a2..d86e6874 100644 --- a/pkg/transformers/test_data/mocks/repository.go +++ b/pkg/transformers/test_data/mocks/repository.go @@ -11,6 +11,7 @@ type MockRepository struct { createError error markHeaderCheckedError error MarkHeaderCheckedPassedHeaderIDs []int64 + CreatedHeaderIds []int64 missingHeaders []core.Header missingHeadersError error PassedStartingBlockNumber int64 @@ -23,6 +24,8 @@ type MockRepository struct { func (repository *MockRepository) Create(headerID int64, models []interface{}) error { repository.PassedHeaderID = headerID repository.PassedModels = models + repository.CreatedHeaderIds = append(repository.CreatedHeaderIds, headerID) + return repository.createError } diff --git a/pkg/transformers/transformers.go b/pkg/transformers/transformers.go index ecc9de59..59ac5538 100644 --- a/pkg/transformers/transformers.go +++ b/pkg/transformers/transformers.go @@ -132,7 +132,6 @@ var ( Fetcher: &shared.Fetcher{}, }.NewLogNoteTransformer - FlopKickTransformerInitializer = flop_kick.FlopKickTransformerInitializer{Config: flop_kick.Config}.NewFlopKickTransformer FrobTransformerInitializer = factories.Transformer{ Config: frob.FrobConfig, @@ -141,6 +140,13 @@ var ( Fetcher: &shared.Fetcher{}, }.NewTransformer + FlopKickTransformerInitializer = factories.Transformer{ + Config: flop_kick.Config, + Converter: &flop_kick.FlopKickConverter{}, + Repository: &flop_kick.FlopKickRepository{}, + Fetcher: &shared.Fetcher{}, + }.NewTransformer + PitFileDebtCeilingTransformerInitializer = factories.LogNoteTransformer{ Config: debt_ceiling.DebtCeilingFileConfig, Converter: &debt_ceiling.PitFileDebtCeilingConverter{},