From d522d335315face9b4b1d695a12948bc7f4854b9 Mon Sep 17 00:00:00 2001 From: Elizabeth Date: Fri, 26 Oct 2018 11:40:24 -0500 Subject: [PATCH] Update vat heal to use transformer factory (#88) * Update vat heal to use transformer factory --- .../integration_tests/vat_heal.go | 11 ++- .../test_data/mocks/repository.go | 12 ++- .../test_data/mocks/vat_heal/converter.go | 35 -------- .../test_data/mocks/vat_heal/repository.go | 65 -------------- pkg/transformers/transformers.go | 7 +- pkg/transformers/vat_heal/config.go | 5 +- pkg/transformers/vat_heal/converter.go | 8 +- pkg/transformers/vat_heal/repository.go | 33 +++---- pkg/transformers/vat_heal/repository_test.go | 46 ++++++---- pkg/transformers/vat_heal/transformer.go | 80 ----------------- pkg/transformers/vat_heal/transformer_test.go | 88 +++++++++++-------- 11 files changed, 124 insertions(+), 266 deletions(-) delete mode 100644 pkg/transformers/test_data/mocks/vat_heal/converter.go delete mode 100644 pkg/transformers/test_data/mocks/vat_heal/repository.go delete mode 100644 pkg/transformers/vat_heal/transformer.go diff --git a/pkg/transformers/integration_tests/vat_heal.go b/pkg/transformers/integration_tests/vat_heal.go index 9ceccf89..0a2609e3 100644 --- a/pkg/transformers/integration_tests/vat_heal.go +++ b/pkg/transformers/integration_tests/vat_heal.go @@ -19,6 +19,8 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "github.com/vulcanize/vulcanizedb/pkg/transformers/factories" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_heal" "github.com/vulcanize/vulcanizedb/test_config" ) @@ -41,8 +43,13 @@ var _ = Describe("VatHeal Transformer", func() { err = persistHeader(db, blockNumber) Expect(err).NotTo(HaveOccurred()) - initializer := vat_heal.VatHealTransformerInitializer{Config: config} - transformer := initializer.NewVatHealTransformer(db, blockchain) + transformer := factories.Transformer{ + Config: vat_heal.VatHealConfig, + Converter: &vat_heal.VatHealConverter{}, + Repository: &vat_heal.VatHealRepository{}, + Fetcher: &shared.Fetcher{}, + }.NewTransformer(db, blockchain) + err = transformer.Execute() Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/transformers/test_data/mocks/repository.go b/pkg/transformers/test_data/mocks/repository.go index e4ea61b4..719c57a2 100644 --- a/pkg/transformers/test_data/mocks/repository.go +++ b/pkg/transformers/test_data/mocks/repository.go @@ -49,10 +49,6 @@ func (repository *MockRepository) SetMissingHeaders(headers []core.Header) { repository.missingHeaders = headers } -func (repository *MockRepository) AssertMarkHeaderCheckedCalledWith(i int64) { - Expect(repository.MarkHeaderCheckedPassedHeaderIDs).To(ContainElement(i)) -} - func (repository *MockRepository) SetMarkHeaderCheckedError(e error) { repository.markHeaderCheckedError = e } @@ -60,3 +56,11 @@ func (repository *MockRepository) SetMarkHeaderCheckedError(e error) { func (repository *MockRepository) SetCreateError(e error) { repository.createError = e } + +func (repository *MockRepository) AssertMarkHeaderCheckedCalledWith(i int64) { + Expect(repository.MarkHeaderCheckedPassedHeaderIDs).To(ContainElement(i)) +} + +func (repository *MockRepository) AssertMarkHeaderCheckedNotCalled() { + Expect(len(repository.MarkHeaderCheckedPassedHeaderIDs)).To(Equal(0)) +} diff --git a/pkg/transformers/test_data/mocks/vat_heal/converter.go b/pkg/transformers/test_data/mocks/vat_heal/converter.go deleted file mode 100644 index 387832ea..00000000 --- a/pkg/transformers/test_data/mocks/vat_heal/converter.go +++ /dev/null @@ -1,35 +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 vat_heal - -import ( - "github.com/ethereum/go-ethereum/core/types" - "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" - "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_heal" -) - -type MockVatHealConverter struct { - converterErr error - PassedLogs []types.Log -} - -func (converter *MockVatHealConverter) ToModels(ethLogs []types.Log) ([]vat_heal.VatHealModel, error) { - converter.PassedLogs = ethLogs - return []vat_heal.VatHealModel{test_data.VatHealModel}, converter.converterErr -} - -func (converter *MockVatHealConverter) SetConverterError(e error) { - converter.converterErr = e -} diff --git a/pkg/transformers/test_data/mocks/vat_heal/repository.go b/pkg/transformers/test_data/mocks/vat_heal/repository.go deleted file mode 100644 index e29afd36..00000000 --- a/pkg/transformers/test_data/mocks/vat_heal/repository.go +++ /dev/null @@ -1,65 +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 vat_heal - -import ( - "github.com/vulcanize/vulcanizedb/pkg/core" - "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_heal" -) - -type MockVatHealRepository struct { - createErr error - markHeaderCheckedErr error - MarkHeaderCheckedPassedHeaderID int64 - missingHeaders []core.Header - missingHeadersErr error - PassedStartingBlockNumber int64 - PassedEndingBlockNumber int64 - PassedHeaderID int64 - PassedModels []vat_heal.VatHealModel -} - -func (repository *MockVatHealRepository) MarkCheckedHeader(headerId int64) error { - repository.MarkHeaderCheckedPassedHeaderID = headerId - return repository.markHeaderCheckedErr -} - -func (repository *MockVatHealRepository) Create(headerID int64, models []vat_heal.VatHealModel) error { - repository.PassedHeaderID = headerID - repository.PassedModels = models - return repository.createErr -} - -func (repository *MockVatHealRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - repository.PassedStartingBlockNumber = startingBlockNumber - repository.PassedEndingBlockNumber = endingBlockNumber - return repository.missingHeaders, repository.missingHeadersErr -} - -func (repository *MockVatHealRepository) SetMarkHeaderCheckedErr(e error) { - repository.markHeaderCheckedErr = e -} - -func (repository *MockVatHealRepository) SetMissingHeadersErr(e error) { - repository.missingHeadersErr = e -} - -func (repository *MockVatHealRepository) SetMissingHeaders(headers []core.Header) { - repository.missingHeaders = headers -} - -func (repository *MockVatHealRepository) SetCreateError(e error) { - repository.createErr = e -} diff --git a/pkg/transformers/transformers.go b/pkg/transformers/transformers.go index dfa6f955..63c583ea 100644 --- a/pkg/transformers/transformers.go +++ b/pkg/transformers/transformers.go @@ -151,13 +151,18 @@ var ( Repository: &vat_grab.VatGrabRepository{}, Fetcher: &shared.Fetcher{}, }.NewTransformer - VatHealTransformerInitializer = vat_heal.VatHealTransformerInitializer{Config: vat_heal.VatHealConfig}.NewVatHealTransformer VatFoldTransformerInitializer = factories.Transformer{ Config: vat_fold.VatFoldConfig, Converter: &vat_fold.VatFoldConverter{}, Repository: &vat_fold.VatFoldRepository{}, Fetcher: &shared.Fetcher{}, }.NewTransformer + VatHealTransformerInitializer = factories.Transformer{ + Config: vat_heal.VatHealConfig, + Converter: &vat_heal.VatHealConverter{}, + Repository: &vat_heal.VatHealRepository{}, + Fetcher: &shared.Fetcher{}, + }.NewTransformer VatMoveTransformerInitializer = factories.Transformer{ Config: vat_move.VatMoveConfig, Converter: &vat_move.VatMoveConverter{}, diff --git a/pkg/transformers/vat_heal/config.go b/pkg/transformers/vat_heal/config.go index 1cc28524..25f769dc 100644 --- a/pkg/transformers/vat_heal/config.go +++ b/pkg/transformers/vat_heal/config.go @@ -16,10 +16,11 @@ package vat_heal import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" -var VatHealConfig = shared.TransformerConfig{ +var VatHealConfig = shared.SingleTransformerConfig{ + TransformerName: shared.VatHealLabel, ContractAddresses: []string{shared.VatContractAddress}, ContractAbi: shared.VatABI, - Topics: []string{shared.VatHealSignature}, + Topic: shared.VatHealSignature, StartingBlockNumber: 0, EndingBlockNumber: 10000000, } diff --git a/pkg/transformers/vat_heal/converter.go b/pkg/transformers/vat_heal/converter.go index 8a30494f..c23dab3f 100644 --- a/pkg/transformers/vat_heal/converter.go +++ b/pkg/transformers/vat_heal/converter.go @@ -22,14 +22,10 @@ import ( "github.com/ethereum/go-ethereum/core/types" ) -type Converter interface { - ToModels(ethLogs []types.Log) ([]VatHealModel, error) -} - type VatHealConverter struct{} -func (VatHealConverter) ToModels(ethLogs []types.Log) ([]VatHealModel, error) { - var models []VatHealModel +func (VatHealConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) { + var models []interface{} for _, ethLog := range ethLogs { err := verifyLog(ethLog) if err != nil { diff --git a/pkg/transformers/vat_heal/repository.go b/pkg/transformers/vat_heal/repository.go index 18ee9318..5d4b8562 100644 --- a/pkg/transformers/vat_heal/repository.go +++ b/pkg/transformers/vat_heal/repository.go @@ -15,34 +15,35 @@ package vat_heal import ( + "fmt" + "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" ) -type Repository interface { - Create(headerId int64, models []VatHealModel) error - MissingHeaders(startingBlock, endingBlock int64) ([]core.Header, error) - MarkCheckedHeader(headerId int64) error -} - type VatHealRepository struct { - DB *postgres.DB + db *postgres.DB } -func NewVatHealRepository(db *postgres.DB) VatHealRepository { - return VatHealRepository{DB: db} +func (repository *VatHealRepository) SetDB(db *postgres.DB) { + repository.db = db } -func (repository VatHealRepository) Create(headerId int64, models []VatHealModel) error { - tx, err := repository.DB.Begin() +func (repository VatHealRepository) Create(headerId int64, models []interface{}) error { + tx, err := repository.db.Begin() if err != nil { return err } for _, model := range models { + vatHeal, ok := model.(VatHealModel) + if !ok { + tx.Rollback() + return fmt.Errorf("model of type %T, not %T", model, VatHealModel{}) + } _, err := tx.Exec(`INSERT INTO maker.vat_heal (header_id, urn, v, rad, log_idx, tx_idx, raw_log) VALUES($1, $2, $3, $4::NUMERIC, $5, $6, $7)`, - headerId, model.Urn, model.V, model.Rad, model.LogIndex, model.TransactionIndex, model.Raw) + headerId, vatHeal.Urn, vatHeal.V, vatHeal.Rad, vatHeal.LogIndex, vatHeal.TransactionIndex, vatHeal.Raw) if err != nil { tx.Rollback() return err @@ -62,20 +63,20 @@ func (repository VatHealRepository) Create(headerId int64, models []VatHealModel func (repository VatHealRepository) MissingHeaders(startingBlock, endingBlock int64) ([]core.Header, error) { var headers []core.Header - err := repository.DB.Select(&headers, + err := repository.db.Select(&headers, `SELECT headers.id, block_number from headers LEFT JOIN checked_headers on headers.id = header_id WHERE (header_id ISNULL OR vat_heal_checked IS FALSE) AND headers.block_number >= $1 AND headers.block_number <= $2 AND headers.eth_node_fingerprint = $3`, - startingBlock, endingBlock, repository.DB.Node.ID) + startingBlock, endingBlock, repository.db.Node.ID) return headers, err } -func (repository VatHealRepository) MarkCheckedHeader(headerId int64) error { - _, err := repository.DB.Exec(`INSERT INTO public.checked_headers (header_id, vat_heal_checked) +func (repository VatHealRepository) MarkHeaderChecked(headerId int64) error { + _, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, vat_heal_checked) VALUES($1, $2) ON CONFLICT (header_id) DO UPDATE SET vat_heal_checked = $2`, headerId, true) diff --git a/pkg/transformers/vat_heal/repository_test.go b/pkg/transformers/vat_heal/repository_test.go index b3f90c42..7b4a3741 100644 --- a/pkg/transformers/vat_heal/repository_test.go +++ b/pkg/transformers/vat_heal/repository_test.go @@ -15,6 +15,8 @@ package vat_heal_test import ( + "math/rand" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -38,7 +40,8 @@ var _ = Describe("VatHeal Repository", func() { BeforeEach(func() { db = test_config.NewTestDB(test_config.NewTestNode()) test_config.CleanTestDB(db) - repository = vat_heal.VatHealRepository{DB: db} + repository = vat_heal.VatHealRepository{} + repository.SetDB(db) headerRepository = repositories.NewHeaderRepository(db) }) @@ -63,7 +66,7 @@ var _ = Describe("VatHeal Repository", func() { It("persists vat heal records", func() { anotherVatHeal := test_data.VatHealModel anotherVatHeal.LogIndex = test_data.VatHealModel.LogIndex + 1 - err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel, anotherVatHeal}) + err = repository.Create(headerId, []interface{}{test_data.VatHealModel, anotherVatHeal}) var dbResult []VatHealDBResult err = db.Select(&dbResult, `SELECT * from maker.vat_heal where header_id = $1`, headerId) @@ -80,17 +83,17 @@ var _ = Describe("VatHeal Repository", func() { }) It("returns an error if the insertion fails", func() { - err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel}) + err = repository.Create(headerId, []interface{}{test_data.VatHealModel}) Expect(err).NotTo(HaveOccurred()) - err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel}) + err = repository.Create(headerId, []interface{}{test_data.VatHealModel}) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) }) It("marks the header as checked for vat heal logs", func() { - err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel}) + err = repository.Create(headerId, []interface{}{test_data.VatHealModel}) Expect(err).NotTo(HaveOccurred()) var headerChecked bool @@ -103,7 +106,7 @@ var _ = Describe("VatHeal Repository", func() { _, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerId) Expect(err).NotTo(HaveOccurred()) - err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel}) + err = repository.Create(headerId, []interface{}{test_data.VatHealModel}) Expect(err).NotTo(HaveOccurred()) var headerChecked bool @@ -113,7 +116,7 @@ var _ = Describe("VatHeal Repository", func() { }) It("removes vat heal if corresponding header is deleted", func() { - err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel}) + err = repository.Create(headerId, []interface{}{test_data.VatHealModel}) Expect(err).NotTo(HaveOccurred()) _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerId) @@ -125,11 +128,17 @@ var _ = Describe("VatHeal Repository", func() { Expect(count).To(Equal(0)) }) + It("returns an error if the wrong model types are passed in", func() { + err = repository.Create(headerId, []interface{}{test_data.WrongModel{}}) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("model of type test_data.WrongModel, not vat_heal.VatHealModel")) + }) + It("wraps create in a transaction", func() { - err = repository.Create(headerId, []vat_heal.VatHealModel{test_data.VatHealModel, test_data.VatHealModel}) + err = repository.Create(headerId, []interface{}{test_data.VatHealModel, test_data.VatHealModel}) Expect(err).To(HaveOccurred()) var count int - err = repository.DB.QueryRowx(`SELECT count(*) FROM maker.vat_heal`).Scan(&count) + err = db.QueryRowx(`SELECT count(*) FROM maker.vat_heal`).Scan(&count) Expect(count).To(Equal(0)) }) }) @@ -141,7 +150,7 @@ var _ = Describe("VatHeal Repository", func() { ) BeforeEach(func() { - startingBlock = GinkgoRandomSeed() + startingBlock = rand.Int63() vatHealBlock = startingBlock + 1 endingBlock = startingBlock + 2 outsideRangeBlock = startingBlock + 3 @@ -156,7 +165,7 @@ var _ = Describe("VatHeal Repository", func() { }) It("returns headers that haven't been checked", func() { - err = repository.MarkCheckedHeader(headerIds[0]) + err = repository.MarkHeaderChecked(headerIds[0]) Expect(err).NotTo(HaveOccurred()) headers, err := repository.MissingHeaders(startingBlock, endingBlock) @@ -168,8 +177,8 @@ var _ = Describe("VatHeal Repository", func() { }) It("returns header ids when checked_headers.vat_heal is false", func() { - err = repository.MarkCheckedHeader(headerIds[0]) - _, err = repository.DB.Exec(`INSERT INTO checked_headers (header_id) VALUES ($1)`, headerIds[1]) + err = repository.MarkHeaderChecked(headerIds[0]) + _, err = db.Exec(`INSERT INTO checked_headers (header_id) VALUES ($1)`, headerIds[1]) Expect(err).NotTo(HaveOccurred()) headers, err := repository.MissingHeaders(startingBlock, endingBlock) @@ -183,14 +192,15 @@ var _ = Describe("VatHeal Repository", func() { It("only returns header ids for the current node", func() { db2 := test_config.NewTestDB(core.Node{ID: "second node"}) headerRepository2 := repositories.NewHeaderRepository(db2) - repository2 := vat_heal.NewVatHealRepository(db2) + repository2 := vat_heal.VatHealRepository{} + repository2.SetDB(db2) for _, n := range blockNumbers { _, err = headerRepository2.CreateOrUpdateHeader(fakes.GetFakeHeader(n)) Expect(err).NotTo(HaveOccurred()) } - err = repository.MarkCheckedHeader(headerIds[0]) + err = repository.MarkHeaderChecked(headerIds[0]) Expect(err).NotTo(HaveOccurred()) nodeOneMissingHeaders, err := repository.MissingHeaders(startingBlock, endingBlock) @@ -212,7 +222,7 @@ var _ = Describe("VatHeal Repository", func() { }) It("creates a new checked_header record", func() { - err := repository.MarkCheckedHeader(headerId) + err := repository.MarkHeaderChecked(headerId) Expect(err).NotTo(HaveOccurred()) var checkedHeaderResult = CheckedHeaderResult{} @@ -222,7 +232,7 @@ var _ = Describe("VatHeal Repository", func() { }) It("updates an existing checked header", func() { - _, err := repository.DB.Exec(`INSERT INTO checked_headers (header_id) VALUES($1)`, headerId) + _, err := db.Exec(`INSERT INTO checked_headers (header_id) VALUES($1)`, headerId) Expect(err).NotTo(HaveOccurred()) var checkedHeaderResult CheckedHeaderResult @@ -230,7 +240,7 @@ var _ = Describe("VatHeal Repository", func() { Expect(err).NotTo(HaveOccurred()) Expect(checkedHeaderResult.VatHealChecked).To(BeFalse()) - err = repository.MarkCheckedHeader(headerId) + err = repository.MarkHeaderChecked(headerId) Expect(err).NotTo(HaveOccurred()) err = db.Get(&checkedHeaderResult, `SELECT vat_heal_checked FROM checked_headers WHERE header_id = $1`, headerId) diff --git a/pkg/transformers/vat_heal/transformer.go b/pkg/transformers/vat_heal/transformer.go deleted file mode 100644 index 70ba78d2..00000000 --- a/pkg/transformers/vat_heal/transformer.go +++ /dev/null @@ -1,80 +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 vat_heal - -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 VatHealTransformer struct { - Config shared.TransformerConfig - Converter Converter - Fetcher shared.LogFetcher - Repository Repository -} - -type VatHealTransformerInitializer struct { - Config shared.TransformerConfig -} - -func (i VatHealTransformerInitializer) NewVatHealTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer { - fetcher := shared.NewFetcher(blockChain) - repository := NewVatHealRepository(db) - transformer := VatHealTransformer{ - Fetcher: fetcher, - Repository: repository, - Converter: VatHealConverter{}, - Config: i.Config, - } - - return transformer -} - -func (transformer VatHealTransformer) Execute() error { - config := transformer.Config - topics := [][]common.Hash{{common.HexToHash(config.Topics[0])}} - headers, err := transformer.Repository.MissingHeaders(config.StartingBlockNumber, config.EndingBlockNumber) - if err != nil { - return err - } - - for _, header := range headers { - logs, err := transformer.Fetcher.FetchLogs(config.ContractAddresses, topics, header.BlockNumber) - if err != nil { - return err - } - - if len(logs) < 1 { - err = transformer.Repository.MarkCheckedHeader(header.Id) - } - if err != nil { - return err - } - - models, err := transformer.Converter.ToModels(logs) - if err != nil { - return err - } - - err = transformer.Repository.Create(header.Id, models) - if err != nil { - return err - } - } - return nil -} diff --git a/pkg/transformers/vat_heal/transformer_test.go b/pkg/transformers/vat_heal/transformer_test.go index 69a9eded..b90dfcd8 100644 --- a/pkg/transformers/vat_heal/transformer_test.go +++ b/pkg/transformers/vat_heal/transformer_test.go @@ -15,47 +15,60 @@ package vat_heal_test import ( + "math/rand" + "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/factories" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks" - mock_vat_heal "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/vat_heal" "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_heal" ) var _ = Describe("VatHeal Transformer", func() { - var mockRepository mock_vat_heal.MockVatHealRepository - var transformer vat_heal.VatHealTransformer - var fetcher mocks.MockLogFetcher - var converter mock_vat_heal.MockVatHealConverter + var ( + repository mocks.MockRepository + transformer shared.Transformer + fetcher mocks.MockLogFetcher + converter mocks.MockConverter + config = vat_heal.VatHealConfig + ) BeforeEach(func() { - mockRepository = mock_vat_heal.MockVatHealRepository{} + repository = mocks.MockRepository{} fetcher = mocks.MockLogFetcher{} - converter = mock_vat_heal.MockVatHealConverter{} - transformer = vat_heal.VatHealTransformer{ - Repository: &mockRepository, - Config: vat_heal.VatHealConfig, + converter = mocks.MockConverter{} + transformer = factories.Transformer{ + Repository: &repository, + Config: config, Fetcher: &fetcher, Converter: &converter, - } + }.NewTransformer(nil, nil) + }) + + It("sets the database and blockchain", func() { + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(repository.SetDbCalled).To(BeTrue()) }) It("gets all of the missing header ids", func() { err := transformer.Execute() Expect(err).NotTo(HaveOccurred()) - Expect(mockRepository.PassedStartingBlockNumber).To(Equal(vat_heal.VatHealConfig.StartingBlockNumber)) - Expect(mockRepository.PassedEndingBlockNumber).To(Equal(vat_heal.VatHealConfig.EndingBlockNumber)) + Expect(repository.PassedStartingBlockNumber).To(Equal(config.StartingBlockNumber)) + Expect(repository.PassedEndingBlockNumber).To(Equal(config.EndingBlockNumber)) }) It("returns and error if getting the missing headers fails", func() { - mockRepository.SetMissingHeadersErr(fakes.FakeError) + repository.SetMissingHeadersError(fakes.FakeError) err := transformer.Execute() Expect(err).To(HaveOccurred()) @@ -63,8 +76,8 @@ var _ = Describe("VatHeal Transformer", func() { }) It("fetches vat heal logs for the headers", func() { - header := core.Header{BlockNumber: GinkgoRandomSeed()} - mockRepository.SetMissingHeaders([]core.Header{header}) + header := core.Header{BlockNumber: rand.Int63()} + repository.SetMissingHeaders([]core.Header{header}) err := transformer.Execute() Expect(err).NotTo(HaveOccurred()) @@ -74,8 +87,8 @@ var _ = Describe("VatHeal Transformer", func() { }) It("returns and error if fetching the logs fails", func() { - header := core.Header{BlockNumber: GinkgoRandomSeed()} - mockRepository.SetMissingHeaders([]core.Header{header}) + header := core.Header{BlockNumber: rand.Int63()} + repository.SetMissingHeaders([]core.Header{header}) fetcher.SetFetcherError(fakes.FakeError) err := transformer.Execute() @@ -84,8 +97,8 @@ var _ = Describe("VatHeal Transformer", func() { }) It("converts the logs to models", func() { - header := core.Header{BlockNumber: GinkgoRandomSeed()} - mockRepository.SetMissingHeaders([]core.Header{header}) + header := core.Header{BlockNumber: rand.Int63()} + repository.SetMissingHeaders([]core.Header{header}) fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog}) err := transformer.Execute() @@ -95,8 +108,8 @@ var _ = Describe("VatHeal Transformer", func() { }) It("returns an error if converting fails", func() { - header := core.Header{BlockNumber: GinkgoRandomSeed()} - mockRepository.SetMissingHeaders([]core.Header{header}) + header := core.Header{BlockNumber: rand.Int63()} + repository.SetMissingHeaders([]core.Header{header}) fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog}) converter.SetConverterError(fakes.FakeError) @@ -107,22 +120,23 @@ var _ = Describe("VatHeal Transformer", func() { }) It("persists the vat heal models", func() { - header := core.Header{Id: GinkgoRandomSeed()} - mockRepository.SetMissingHeaders([]core.Header{header}) + header := core.Header{Id: rand.Int63()} + repository.SetMissingHeaders([]core.Header{header}) fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog}) + converter.SetReturnModels([]interface{}{test_data.VatHealModel}) err := transformer.Execute() Expect(err).NotTo(HaveOccurred()) - Expect(mockRepository.PassedModels).To(ContainElement(test_data.VatHealModel)) - Expect(mockRepository.PassedHeaderID).To(Equal(header.Id)) + Expect(repository.PassedModels).To(ContainElement(test_data.VatHealModel)) + Expect(repository.PassedHeaderID).To(Equal(header.Id)) }) It("returns an error if persisting the vat heal models fails", func() { - header := core.Header{Id: GinkgoRandomSeed()} - mockRepository.SetMissingHeaders([]core.Header{header}) + header := core.Header{Id: rand.Int63()} + repository.SetMissingHeaders([]core.Header{header}) fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog}) - mockRepository.SetCreateError(fakes.FakeError) + repository.SetCreateError(fakes.FakeError) err := transformer.Execute() @@ -131,29 +145,29 @@ var _ = Describe("VatHeal Transformer", func() { }) It("marks the header as checked when there are no logs", func() { - header := core.Header{Id: GinkgoRandomSeed()} - mockRepository.SetMissingHeaders([]core.Header{header}) + header := core.Header{Id: rand.Int63()} + repository.SetMissingHeaders([]core.Header{header}) err := transformer.Execute() Expect(err).NotTo(HaveOccurred()) - Expect(mockRepository.MarkHeaderCheckedPassedHeaderID).To(Equal(header.Id)) + repository.AssertMarkHeaderCheckedCalledWith(header.Id) }) It("doesn't call MarkCheckedHeader when there are logs", func() { header := core.Header{Id: GinkgoRandomSeed()} - mockRepository.SetMissingHeaders([]core.Header{header}) + repository.SetMissingHeaders([]core.Header{header}) fetcher.SetFetchedLogs([]types.Log{test_data.VatHealLog}) err := transformer.Execute() Expect(err).NotTo(HaveOccurred()) - Expect(mockRepository.MarkHeaderCheckedPassedHeaderID).To(Equal(int64(0))) + repository.AssertMarkHeaderCheckedNotCalled() }) It("returns an error if MarkCheckedHeader fails", func() { - header := core.Header{Id: GinkgoRandomSeed()} - mockRepository.SetMissingHeaders([]core.Header{header}) - mockRepository.SetMissingHeadersErr(fakes.FakeError) + header := core.Header{Id: rand.Int63()} + repository.SetMissingHeaders([]core.Header{header}) + repository.SetMissingHeadersError(fakes.FakeError) err := transformer.Execute()