From 0ab5ea1b25a6caf7ca3f4328146007ff9be91fd9 Mon Sep 17 00:00:00 2001 From: Elizabeth Date: Tue, 6 Nov 2018 10:51:26 -0600 Subject: [PATCH] Refactor more repo tests (#101) * Use shared repository tests for deal * Small updates to repo tests * Use shared repository test examples for dent * Use shared repository test examples for drip_drip * Use shared repository test examples for drip_file_ilk * Use shared repository test examples for drip_file_repo * Use shared repository test examples for drip_file_vow * Update formatting --- pkg/transformers/dent/repository_test.go | 186 +++--------------- pkg/transformers/drip_drip/repository_test.go | 179 +++-------------- .../drip_file/ilk/repository_test.go | 179 +++-------------- .../drip_file/repo/repository_test.go | 177 +++-------------- .../drip_file/vow/repository_test.go | 178 +++-------------- .../factories/transformer_test.go | 17 +- .../test_data/mocks/log_note_converter.go | 2 +- .../test_data/mocks/repository.go | 2 +- 8 files changed, 128 insertions(+), 792 deletions(-) diff --git a/pkg/transformers/dent/repository_test.go b/pkg/transformers/dent/repository_test.go index 65445a11..cb0e3969 100644 --- a/pkg/transformers/dent/repository_test.go +++ b/pkg/transformers/dent/repository_test.go @@ -18,12 +18,12 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/dent" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" ) @@ -32,7 +32,6 @@ var _ = Describe("Dent Repository", func() { db *postgres.DB dentRepository dent.DentRepository headerRepository repositories.HeaderRepository - err error ) BeforeEach(func() { @@ -44,15 +43,22 @@ var _ = Describe("Dent Repository", func() { }) Describe("Create", func() { - var headerId int64 + modelWithDifferentLogIdx := test_data.DentModel + modelWithDifferentLogIdx.LogIndex++ + inputs := shared_behaviors.CreateBehaviorInputs{ + CheckedHeaderColumnName: "dent_checked", + LogEventTableName: "maker.dent", + TestModel: test_data.DentModel, + ModelWithDifferentLogIdx: modelWithDifferentLogIdx, + Repository: &dentRepository, + } - BeforeEach(func() { - headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) - Expect(err).NotTo(HaveOccurred()) - }) + shared_behaviors.SharedRepositoryCreateBehaviors(&inputs) It("persists a dent record", func() { - err := dentRepository.Create(headerId, []interface{}{test_data.DentModel}) + headerId, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) + Expect(err).NotTo(HaveOccurred()) + err = dentRepository.Create(headerId, []interface{}{test_data.DentModel}) Expect(err).NotTo(HaveOccurred()) var count int @@ -71,167 +77,23 @@ var _ = Describe("Dent Repository", func() { Expect(dbResult.TransactionIndex).To(Equal(test_data.DentModel.TransactionIndex)) Expect(dbResult.Raw).To(MatchJSON(test_data.DentModel.Raw)) }) - - It("marks header as checked for logs", func() { - err := dentRepository.Create(headerId, []interface{}{test_data.DentModel}) - Expect(err).NotTo(HaveOccurred()) - - var headerChecked bool - err = db.Get(&headerChecked, `SELECT dent_checked FROM public.checked_headers WHERE header_id = $1`, headerId) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) - - It("updates the checked_header row if it already exists", func() { - _, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerId) - Expect(err).NotTo(HaveOccurred()) - - err = dentRepository.Create(headerId, []interface{}{test_data.DentModel}) - Expect(err).NotTo(HaveOccurred()) - - var headerChecked bool - err = db.Get(&headerChecked, `SELECT dent_checked FROM public.checked_headers WHERE header_id = $1`, headerId) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) - - It("returns an error if inserting a dent record fails", func() { - err := dentRepository.Create(headerId, []interface{}{test_data.DentModel}) - Expect(err).NotTo(HaveOccurred()) - - err = dentRepository.Create(headerId, []interface{}{test_data.DentModel}) - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) - }) - - It("returns an error if model is of wrong type", func() { - err = dentRepository.Create(headerId, []interface{}{test_data.WrongModel{}}) - - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("model of type")) - }) - - It("deletes the dent record if its corresponding header record is deleted", func() { - err := dentRepository.Create(headerId, []interface{}{test_data.DentModel}) - Expect(err).NotTo(HaveOccurred()) - - var count int - err = db.QueryRow(`SELECT count(*) from maker.dent`).Scan(&count) - Expect(err).NotTo(HaveOccurred()) - Expect(count).To(Equal(1)) - - _, err = db.Exec(`DELETE FROM headers where id = $1`, headerId) - Expect(err).NotTo(HaveOccurred()) - - err = db.QueryRow(`SELECT count(*) from maker.dent`).Scan(&count) - Expect(err).NotTo(HaveOccurred()) - Expect(count).To(Equal(0)) - }) }) Describe("MarkHeaderChecked", func() { - var headerId int64 + inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ + CheckedHeaderColumnName: "dent_checked", + Repository: &dentRepository, + } - BeforeEach(func() { - headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) - Expect(err).NotTo(HaveOccurred()) - }) - - It("creates a row for a new headerId", func() { - err = dentRepository.MarkHeaderChecked(headerId) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT dent_checked FROM public.checked_headers WHERE header_id = $1`, headerId) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) - - It("updates row when headerId already exists", func() { - _, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerId) - Expect(err).NotTo(HaveOccurred()) - - err = dentRepository.MarkHeaderChecked(headerId) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT dent_checked FROM public.checked_headers WHERE header_id = $1`, headerId) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) + shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs) }) Describe("MissingHeaders", func() { - var ( - dentBlock, startingBlock, endingBlock int64 - blockNumbers, headerIds []int64 - ) + inputs := shared_behaviors.MissingHeadersBehaviorInputs{ + Repository: &dentRepository, + RepositoryTwo: &dent.DentRepository{}, + } - BeforeEach(func() { - dentBlock = GinkgoRandomSeed() - startingBlock = dentBlock - 1 - endingBlock = dentBlock + 1 - outOfRangeBlockNumber := dentBlock + 2 - - blockNumbers = []int64{startingBlock, dentBlock, endingBlock, outOfRangeBlockNumber} - - headerIds = []int64{} - for _, number := range blockNumbers { - headerId, err := headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(number)) - Expect(err).NotTo(HaveOccurred()) - headerIds = append(headerIds, headerId) - } - }) - - It("returns header records that haven't been checked", func() { - dentRepository.MarkHeaderChecked(headerIds[1]) - missingHeaders, err := dentRepository.MissingHeaders(startingBlock, endingBlock) - - Expect(err).NotTo(HaveOccurred()) - Expect(len(missingHeaders)).To(Equal(2)) - Expect(missingHeaders[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock))) - Expect(missingHeaders[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock))) - }) - - It("only treats headers as checked if dent has been checked", func() { - _, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIds[1]) - Expect(err).NotTo(HaveOccurred()) - - headers, err := dentRepository.MissingHeaders(startingBlock, endingBlock) - - Expect(err).NotTo(HaveOccurred()) - Expect(len(headers)).To(Equal(3)) - Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dentBlock))) - Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dentBlock))) - Expect(headers[2].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dentBlock))) - }) - - It("only returns missing headers for the given node", func() { - dentRepository.MarkHeaderChecked(headerIds[1]) - node2 := core.Node{} - db2 := test_config.NewTestDB(node2) - dentRepository2 := dent.DentRepository{} - dentRepository2.SetDB(db2) - headerRepository2 := repositories.NewHeaderRepository(db2) - var node2HeaderIds []int64 - for _, number := range blockNumbers { - id, err := headerRepository2.CreateOrUpdateHeader(fakes.GetFakeHeader(number)) - node2HeaderIds = append(node2HeaderIds, id) - Expect(err).NotTo(HaveOccurred()) - } - - missingHeadersNode1, err := dentRepository.MissingHeaders(startingBlock, endingBlock) - Expect(err).NotTo(HaveOccurred()) - Expect(len(missingHeadersNode1)).To(Equal(2)) - Expect(missingHeadersNode1[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock))) - Expect(missingHeadersNode1[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock))) - - missingHeadersNode2, err := dentRepository2.MissingHeaders(startingBlock, endingBlock) - Expect(err).NotTo(HaveOccurred()) - Expect(len(missingHeadersNode2)).To(Equal(3)) - Expect(missingHeadersNode2[0].BlockNumber).To(Or(Equal(startingBlock), Equal(dentBlock), Equal(endingBlock))) - Expect(missingHeadersNode2[1].BlockNumber).To(Or(Equal(startingBlock), Equal(dentBlock), Equal(endingBlock))) - Expect(missingHeadersNode2[2].BlockNumber).To(Or(Equal(startingBlock), Equal(dentBlock), Equal(endingBlock))) - }) + shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs) }) }) diff --git a/pkg/transformers/drip_drip/repository_test.go b/pkg/transformers/drip_drip/repository_test.go index 4e6959f5..3584a9c7 100644 --- a/pkg/transformers/drip_drip/repository_test.go +++ b/pkg/transformers/drip_drip/repository_test.go @@ -15,19 +15,15 @@ package drip_drip_test import ( - "database/sql" - "math/rand" - . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - - "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_drip" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" ) @@ -35,7 +31,6 @@ var _ = Describe("Drip drip repository", func() { var ( db *postgres.DB dripDripRepository drip_drip.DripDripRepository - err error headerRepository datastore.HeaderRepository ) @@ -48,15 +43,21 @@ var _ = Describe("Drip drip repository", func() { }) Describe("Create", func() { - var headerID int64 + modelWithDifferentLogIdx := test_data.DripDripModel + modelWithDifferentLogIdx.LogIndex++ + inputs := shared_behaviors.CreateBehaviorInputs{ + CheckedHeaderColumnName: "drip_drip_checked", + LogEventTableName: "maker.drip_drip", + TestModel: test_data.DripDripModel, + ModelWithDifferentLogIdx: modelWithDifferentLogIdx, + Repository: &dripDripRepository, + } - BeforeEach(func() { - headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) - Expect(err).NotTo(HaveOccurred()) - - }) + shared_behaviors.SharedRepositoryCreateBehaviors(&inputs) It("adds a drip drip event", func() { + headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) + Expect(err).NotTo(HaveOccurred()) err = dripDripRepository.Create(headerID, []interface{}{test_data.DripDripModel}) Expect(err).NotTo(HaveOccurred()) @@ -68,157 +69,23 @@ var _ = Describe("Drip drip repository", func() { Expect(dbDripDrip.TransactionIndex).To(Equal(test_data.DripDripModel.TransactionIndex)) Expect(dbDripDrip.Raw).To(MatchJSON(test_data.DripDripModel.Raw)) }) - - It("marks header as checked for logs", func() { - err = dripDripRepository.Create(headerID, []interface{}{test_data.DripDripModel}) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT drip_drip_checked FROM public.checked_headers WHERE header_id = $1`, headerID) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) - - 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 = dripDripRepository.Create(headerID, []interface{}{test_data.DripDripModel}) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT drip_drip_checked FROM public.checked_headers WHERE header_id = $1`, headerID) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) - - It("does not duplicate drip drip events", func() { - err = dripDripRepository.Create(headerID, []interface{}{test_data.DripDripModel}) - Expect(err).NotTo(HaveOccurred()) - - err = dripDripRepository.Create(headerID, []interface{}{test_data.DripDripModel}) - - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) - }) - - It("removes drip drip if corresponding header is deleted", func() { - err = dripDripRepository.Create(headerID, []interface{}{test_data.DripDripModel}) - Expect(err).NotTo(HaveOccurred()) - - _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) - - Expect(err).NotTo(HaveOccurred()) - var dbDripDrip drip_drip.DripDripModel - err = db.Get(&dbDripDrip, `SELECT ilk, log_idx, tx_idx, raw_log FROM maker.drip_drip WHERE header_id = $1`, headerID) - Expect(err).To(HaveOccurred()) - Expect(err).To(MatchError(sql.ErrNoRows)) - }) - - It("returns an error if model is of wrong type", func() { - err = dripDripRepository.Create(headerID, []interface{}{test_data.WrongModel{}}) - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("model of type")) - }) }) Describe("MarkHeaderChecked", func() { - var headerID int64 + inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ + CheckedHeaderColumnName: "drip_drip_checked", + Repository: &dripDripRepository, + } - BeforeEach(func() { - headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) - Expect(err).NotTo(HaveOccurred()) - }) - - It("creates a row for a new headerID", func() { - err = dripDripRepository.MarkHeaderChecked(headerID) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT drip_drip_checked FROM public.checked_headers WHERE header_id = $1`, headerID) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) - - It("updates row when headerID already exists", func() { - _, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID) - - err = dripDripRepository.MarkHeaderChecked(headerID) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT drip_drip_checked FROM public.checked_headers WHERE header_id = $1`, headerID) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) + shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs) }) Describe("MissingHeaders", func() { - var ( - startingBlock, endingBlock, dripDripBlock int64 - blockNumbers, headerIDs []int64 - ) + inputs := shared_behaviors.MissingHeadersBehaviorInputs{ + Repository: &dripDripRepository, + RepositoryTwo: &drip_drip.DripDripRepository{}, + } - BeforeEach(func() { - startingBlock = rand.Int63() - dripDripBlock = startingBlock + 1 - endingBlock = startingBlock + 2 - - blockNumbers = []int64{startingBlock, dripDripBlock, endingBlock, endingBlock + 1} - - headerIDs = []int64{} - for _, n := range blockNumbers { - headerID, err := headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(n)) - Expect(err).NotTo(HaveOccurred()) - headerIDs = append(headerIDs, headerID) - } - - }) - - It("returns headers that haven't been checked", func() { - err := dripDripRepository.MarkHeaderChecked(headerIDs[1]) - Expect(err).NotTo(HaveOccurred()) - - headers, err := dripDripRepository.MissingHeaders(startingBlock, endingBlock) - - Expect(err).NotTo(HaveOccurred()) - Expect(len(headers)).To(Equal(2)) - Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock))) - Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock))) - }) - - It("only treats headers as checked if drip drip logs have been checked", func() { - _, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1]) - Expect(err).NotTo(HaveOccurred()) - - headers, err := dripDripRepository.MissingHeaders(startingBlock, endingBlock) - - Expect(err).NotTo(HaveOccurred()) - Expect(len(headers)).To(Equal(3)) - Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dripDripBlock))) - Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dripDripBlock))) - Expect(headers[2].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dripDripBlock))) - }) - - It("only returns headers associated with the current node", func() { - err := dripDripRepository.MarkHeaderChecked(headerIDs[0]) - Expect(err).NotTo(HaveOccurred()) - dbTwo := test_config.NewTestDB(core.Node{ID: "second"}) - headerRepositoryTwo := repositories.NewHeaderRepository(dbTwo) - for _, n := range blockNumbers { - _, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n)) - Expect(err).NotTo(HaveOccurred()) - } - dripDripRepositoryTwo := drip_drip.DripDripRepository{} - dripDripRepositoryTwo.SetDB(dbTwo) - - nodeOneMissingHeaders, err := dripDripRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) - Expect(err).NotTo(HaveOccurred()) - Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1)) - - nodeTwoMissingHeaders, err := dripDripRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) - Expect(err).NotTo(HaveOccurred()) - Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers))) - }) + shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs) }) }) diff --git a/pkg/transformers/drip_file/ilk/repository_test.go b/pkg/transformers/drip_file/ilk/repository_test.go index c1870e1c..485e4aca 100644 --- a/pkg/transformers/drip_file/ilk/repository_test.go +++ b/pkg/transformers/drip_file/ilk/repository_test.go @@ -15,19 +15,15 @@ package ilk_test import ( - "database/sql" - "math/rand" - . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - - "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/ilk" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" ) @@ -35,7 +31,6 @@ var _ = Describe("Drip file ilk repository", func() { var ( db *postgres.DB dripFileIlkRepository ilk.DripFileIlkRepository - err error headerRepository datastore.HeaderRepository ) @@ -48,15 +43,21 @@ var _ = Describe("Drip file ilk repository", func() { }) Describe("Create", func() { - var headerID int64 + modelWithDifferentLogIdx := test_data.DripFileIlkModel + modelWithDifferentLogIdx.LogIndex++ + inputs := shared_behaviors.CreateBehaviorInputs{ + CheckedHeaderColumnName: "drip_file_ilk_checked", + LogEventTableName: "maker.drip_file_ilk", + TestModel: test_data.DripFileIlkModel, + ModelWithDifferentLogIdx: modelWithDifferentLogIdx, + Repository: &dripFileIlkRepository, + } - BeforeEach(func() { - headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) - Expect(err).NotTo(HaveOccurred()) - - }) + shared_behaviors.SharedRepositoryCreateBehaviors(&inputs) It("adds a drip file ilk event", func() { + headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) + Expect(err).NotTo(HaveOccurred()) err = dripFileIlkRepository.Create(headerID, []interface{}{test_data.DripFileIlkModel}) Expect(err).NotTo(HaveOccurred()) @@ -70,157 +71,23 @@ var _ = Describe("Drip file ilk repository", func() { Expect(dbDripFileIlk.TransactionIndex).To(Equal(test_data.DripFileIlkModel.TransactionIndex)) Expect(dbDripFileIlk.Raw).To(MatchJSON(test_data.DripFileIlkModel.Raw)) }) - - It("marks header as checked for logs", func() { - err = dripFileIlkRepository.Create(headerID, []interface{}{test_data.DripFileIlkModel}) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT drip_file_ilk_checked FROM public.checked_headers WHERE header_id = $1`, headerID) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) - - 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 = dripFileIlkRepository.Create(headerID, []interface{}{test_data.DripFileIlkModel}) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT drip_file_ilk_checked FROM public.checked_headers WHERE header_id = $1`, headerID) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) - - It("does not duplicate drip file events", func() { - err = dripFileIlkRepository.Create(headerID, []interface{}{test_data.DripFileIlkModel}) - Expect(err).NotTo(HaveOccurred()) - - err = dripFileIlkRepository.Create(headerID, []interface{}{test_data.DripFileIlkModel}) - - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) - }) - - It("removes drip file if corresponding header is deleted", func() { - err = dripFileIlkRepository.Create(headerID, []interface{}{test_data.DripFileIlkModel}) - Expect(err).NotTo(HaveOccurred()) - - _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) - - Expect(err).NotTo(HaveOccurred()) - var dbDripFileIlk ilk.DripFileIlkModel - err = db.Get(&dbDripFileIlk, `SELECT ilk, vow, tax, log_idx, tx_idx, raw_log FROM maker.drip_file_ilk WHERE header_id = $1`, headerID) - Expect(err).To(HaveOccurred()) - Expect(err).To(MatchError(sql.ErrNoRows)) - }) - - It("returns an error if model is of wrong type", func() { - err = dripFileIlkRepository.Create(headerID, []interface{}{test_data.WrongModel{}}) - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("model of type")) - }) }) Describe("MarkHeaderChecked", func() { - var headerID int64 + inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ + CheckedHeaderColumnName: "drip_file_ilk_checked", + Repository: &dripFileIlkRepository, + } - BeforeEach(func() { - headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) - Expect(err).NotTo(HaveOccurred()) - }) - - It("creates a row for a new headerID", func() { - err = dripFileIlkRepository.MarkHeaderChecked(headerID) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT drip_file_ilk_checked FROM public.checked_headers WHERE header_id = $1`, headerID) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) - - It("updates row when headerID already exists", func() { - _, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID) - - err = dripFileIlkRepository.MarkHeaderChecked(headerID) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT drip_file_ilk_checked FROM public.checked_headers WHERE header_id = $1`, headerID) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) + shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs) }) Describe("MissingHeaders", func() { - var ( - startingBlock, endingBlock, dripFileBlock int64 - blockNumbers, headerIDs []int64 - ) + inputs := shared_behaviors.MissingHeadersBehaviorInputs{ + Repository: &dripFileIlkRepository, + RepositoryTwo: &ilk.DripFileIlkRepository{}, + } - BeforeEach(func() { - startingBlock = rand.Int63() - dripFileBlock = startingBlock + 1 - endingBlock = startingBlock + 2 - - blockNumbers = []int64{startingBlock, dripFileBlock, endingBlock, endingBlock + 1} - - headerIDs = []int64{} - - for _, n := range blockNumbers { - headerID, err := headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(n)) - Expect(err).NotTo(HaveOccurred()) - headerIDs = append(headerIDs, headerID) - } - }) - - It("returns headers with no associated drip file event", func() { - err := dripFileIlkRepository.MarkHeaderChecked(headerIDs[1]) - Expect(err).NotTo(HaveOccurred()) - - headers, err := dripFileIlkRepository.MissingHeaders(startingBlock, endingBlock) - - Expect(err).NotTo(HaveOccurred()) - Expect(len(headers)).To(Equal(2)) - Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock))) - Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock))) - }) - - It("only treats headers as checked if drip file ilk logs have been checked", func() { - _, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1]) - Expect(err).NotTo(HaveOccurred()) - - headers, err := dripFileIlkRepository.MissingHeaders(startingBlock, endingBlock) - - Expect(err).NotTo(HaveOccurred()) - Expect(len(headers)).To(Equal(3)) - Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dripFileBlock))) - Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dripFileBlock))) - Expect(headers[2].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dripFileBlock))) - }) - - It("only returns headers associated with the current node", func() { - dbTwo := test_config.NewTestDB(core.Node{ID: "second"}) - headerRepositoryTwo := repositories.NewHeaderRepository(dbTwo) - for _, n := range blockNumbers { - _, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n)) - Expect(err).NotTo(HaveOccurred()) - } - dripFileIlkRepositoryTwo := ilk.DripFileIlkRepository{} - dripFileIlkRepositoryTwo.SetDB(dbTwo) - err := dripFileIlkRepository.MarkHeaderChecked(headerIDs[0]) - Expect(err).NotTo(HaveOccurred()) - - nodeOneMissingHeaders, err := dripFileIlkRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) - Expect(err).NotTo(HaveOccurred()) - Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1)) - - nodeTwoMissingHeaders, err := dripFileIlkRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) - Expect(err).NotTo(HaveOccurred()) - Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers))) - }) + shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs) }) }) diff --git a/pkg/transformers/drip_file/repo/repository_test.go b/pkg/transformers/drip_file/repo/repository_test.go index dc9e75ed..f7756150 100644 --- a/pkg/transformers/drip_file/repo/repository_test.go +++ b/pkg/transformers/drip_file/repo/repository_test.go @@ -15,19 +15,16 @@ package repo_test import ( - "database/sql" - "math/rand" - . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/repo" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" ) @@ -35,12 +32,11 @@ var _ = Describe("Drip file repo repository", func() { var ( db *postgres.DB dripFileRepoRepository repo.DripFileRepoRepository - err error headerRepository datastore.HeaderRepository ) BeforeEach(func() { - db = test_config.NewTestDB(core.Node{}) + db = test_config.NewTestDB(test_config.NewTestNode()) test_config.CleanTestDB(db) headerRepository = repositories.NewHeaderRepository(db) dripFileRepoRepository = repo.DripFileRepoRepository{} @@ -48,14 +44,21 @@ var _ = Describe("Drip file repo repository", func() { }) Describe("Create", func() { - var headerID int64 + modelWithDifferentLogIdx := test_data.DripFileRepoModel + modelWithDifferentLogIdx.LogIndex++ + inputs := shared_behaviors.CreateBehaviorInputs{ + CheckedHeaderColumnName: "drip_file_repo_checked", + LogEventTableName: "maker.drip_file_repo", + TestModel: test_data.DripFileRepoModel, + ModelWithDifferentLogIdx: modelWithDifferentLogIdx, + Repository: &dripFileRepoRepository, + } - BeforeEach(func() { - headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) - Expect(err).NotTo(HaveOccurred()) - }) + shared_behaviors.SharedRepositoryCreateBehaviors(&inputs) It("adds a drip file repo event", func() { + headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) + Expect(err).NotTo(HaveOccurred()) err = dripFileRepoRepository.Create(headerID, []interface{}{test_data.DripFileRepoModel}) Expect(err).NotTo(HaveOccurred()) @@ -68,155 +71,23 @@ var _ = Describe("Drip file repo repository", func() { Expect(dbDripFileRepo.TransactionIndex).To(Equal(test_data.DripFileRepoModel.TransactionIndex)) Expect(dbDripFileRepo.Raw).To(MatchJSON(test_data.DripFileRepoModel.Raw)) }) - - It("marks header as checked for logs", func() { - err = dripFileRepoRepository.Create(headerID, []interface{}{test_data.DripFileRepoModel}) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT drip_file_repo_checked FROM public.checked_headers WHERE header_id = $1`, headerID) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) - - 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 = dripFileRepoRepository.Create(headerID, []interface{}{test_data.DripFileRepoModel}) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT drip_file_repo_checked FROM public.checked_headers WHERE header_id = $1`, headerID) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) - - It("does not duplicate drip file events", func() { - err = dripFileRepoRepository.Create(headerID, []interface{}{test_data.DripFileRepoModel}) - Expect(err).NotTo(HaveOccurred()) - - err = dripFileRepoRepository.Create(headerID, []interface{}{test_data.DripFileRepoModel}) - - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) - }) - - It("removes drip file if corresponding header is deleted", func() { - err = dripFileRepoRepository.Create(headerID, []interface{}{test_data.DripFileRepoModel}) - Expect(err).NotTo(HaveOccurred()) - _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) - - Expect(err).NotTo(HaveOccurred()) - var dbDripFileRepo repo.DripFileRepoModel - err = db.Get(&dbDripFileRepo, `SELECT what, data, log_idx, tx_idx, raw_log FROM maker.drip_file_repo WHERE header_id = $1`, headerID) - Expect(err).To(HaveOccurred()) - Expect(err).To(MatchError(sql.ErrNoRows)) - }) - - It("returns an error if model is of wrong type", func() { - err = dripFileRepoRepository.Create(headerID, []interface{}{test_data.WrongModel{}}) - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("model of type")) - }) }) Describe("MarkHeaderChecked", func() { - var headerID int64 + inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ + CheckedHeaderColumnName: "drip_file_repo_checked", + Repository: &dripFileRepoRepository, + } - BeforeEach(func() { - headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) - Expect(err).NotTo(HaveOccurred()) - }) - - It("creates a row for a new headerID", func() { - err = dripFileRepoRepository.MarkHeaderChecked(headerID) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT drip_file_repo_checked FROM public.checked_headers WHERE header_id = $1`, headerID) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) - - It("updates row when headerID already exists", func() { - _, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID) - - err = dripFileRepoRepository.MarkHeaderChecked(headerID) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT drip_file_repo_checked FROM public.checked_headers WHERE header_id = $1`, headerID) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) + shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs) }) Describe("MissingHeaders", func() { - var ( - startingBlock, endingBlock, dripFileBlock int64 - blockNumbers, headerIDs []int64 - ) + inputs := shared_behaviors.MissingHeadersBehaviorInputs{ + Repository: &dripFileRepoRepository, + RepositoryTwo: &repo.DripFileRepoRepository{}, + } - BeforeEach(func() { - startingBlock = rand.Int63() - dripFileBlock = startingBlock + 1 - endingBlock = startingBlock + 2 - - blockNumbers = []int64{startingBlock, dripFileBlock, endingBlock, endingBlock + 1} - - headerIDs = []int64{} - for _, n := range blockNumbers { - headerID, err := headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(n)) - headerIDs = append(headerIDs, headerID) - Expect(err).NotTo(HaveOccurred()) - } - }) - - It("returns headers with no associated drip file event", func() { - err := dripFileRepoRepository.MarkHeaderChecked(headerIDs[1]) - Expect(err).NotTo(HaveOccurred()) - - headers, err := dripFileRepoRepository.MissingHeaders(startingBlock, endingBlock) - - Expect(err).NotTo(HaveOccurred()) - Expect(len(headers)).To(Equal(2)) - Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock))) - Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock))) - }) - - It("only treats headers as checked if drip file repo logs have been checked", func() { - _, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1]) - Expect(err).NotTo(HaveOccurred()) - - headers, err := dripFileRepoRepository.MissingHeaders(startingBlock, endingBlock) - - Expect(err).NotTo(HaveOccurred()) - Expect(len(headers)).To(Equal(3)) - Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dripFileBlock))) - Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dripFileBlock))) - Expect(headers[2].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dripFileBlock))) - }) - - It("only returns headers associated with the current node", func() { - dbTwo := test_config.NewTestDB(core.Node{ID: "second"}) - headerRepositoryTwo := repositories.NewHeaderRepository(dbTwo) - for _, n := range blockNumbers { - _, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n)) - Expect(err).NotTo(HaveOccurred()) - } - dripFileRepoRepositoryTwo := repo.DripFileRepoRepository{} - dripFileRepoRepositoryTwo.SetDB(dbTwo) - err := dripFileRepoRepository.MarkHeaderChecked(headerIDs[0]) - Expect(err).NotTo(HaveOccurred()) - - nodeOneMissingHeaders, err := dripFileRepoRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) - Expect(err).NotTo(HaveOccurred()) - Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1)) - - nodeTwoMissingHeaders, err := dripFileRepoRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) - Expect(err).NotTo(HaveOccurred()) - Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers))) - }) + shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs) }) }) diff --git a/pkg/transformers/drip_file/vow/repository_test.go b/pkg/transformers/drip_file/vow/repository_test.go index d8b8016b..7672a4a5 100644 --- a/pkg/transformers/drip_file/vow/repository_test.go +++ b/pkg/transformers/drip_file/vow/repository_test.go @@ -15,19 +15,15 @@ package vow_test import ( - "database/sql" - "math/rand" - . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - - "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/drip_file/vow" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors" "github.com/vulcanize/vulcanizedb/test_config" ) @@ -35,12 +31,11 @@ var _ = Describe("Drip file vow repository", func() { var ( db *postgres.DB dripFileVowRepository vow.DripFileVowRepository - err error headerRepository datastore.HeaderRepository ) BeforeEach(func() { - db = test_config.NewTestDB(core.Node{}) + db = test_config.NewTestDB(test_config.NewTestNode()) test_config.CleanTestDB(db) headerRepository = repositories.NewHeaderRepository(db) dripFileVowRepository = vow.DripFileVowRepository{} @@ -48,14 +43,21 @@ var _ = Describe("Drip file vow repository", func() { }) Describe("Create", func() { - var headerID int64 + modelWithDifferentLogIdx := test_data.DripFileVowModel + modelWithDifferentLogIdx.LogIndex++ + inputs := shared_behaviors.CreateBehaviorInputs{ + CheckedHeaderColumnName: "drip_file_vow_checked", + LogEventTableName: "maker.drip_file_vow", + TestModel: test_data.DripFileVowModel, + ModelWithDifferentLogIdx: modelWithDifferentLogIdx, + Repository: &dripFileVowRepository, + } - BeforeEach(func() { - headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) - Expect(err).NotTo(HaveOccurred()) - }) + shared_behaviors.SharedRepositoryCreateBehaviors(&inputs) It("adds a drip file vow event", func() { + headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) + Expect(err).NotTo(HaveOccurred()) err = dripFileVowRepository.Create(headerID, []interface{}{test_data.DripFileVowModel}) Expect(err).NotTo(HaveOccurred()) @@ -68,155 +70,23 @@ var _ = Describe("Drip file vow repository", func() { Expect(dbDripFileVow.TransactionIndex).To(Equal(test_data.DripFileVowModel.TransactionIndex)) Expect(dbDripFileVow.Raw).To(MatchJSON(test_data.DripFileVowModel.Raw)) }) - - It("marks header as checked for logs", func() { - err = dripFileVowRepository.Create(headerID, []interface{}{test_data.DripFileVowModel}) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT drip_file_vow_checked FROM public.checked_headers WHERE header_id = $1`, headerID) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) - - 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 = dripFileVowRepository.Create(headerID, []interface{}{test_data.DripFileVowModel}) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT drip_file_vow_checked FROM public.checked_headers WHERE header_id = $1`, headerID) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) - - It("does not duplicate drip file events", func() { - err = dripFileVowRepository.Create(headerID, []interface{}{test_data.DripFileVowModel}) - Expect(err).NotTo(HaveOccurred()) - - err = dripFileVowRepository.Create(headerID, []interface{}{test_data.DripFileVowModel}) - - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) - }) - - It("removes drip file if corresponding header is deleted", func() { - err = dripFileVowRepository.Create(headerID, []interface{}{test_data.DripFileVowModel}) - Expect(err).NotTo(HaveOccurred()) - _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) - - Expect(err).NotTo(HaveOccurred()) - var dbDripFileVow vow.DripFileVowModel - err = db.Get(&dbDripFileVow, `SELECT what, data, log_idx, tx_idx, raw_log FROM maker.drip_file_vow WHERE header_id = $1`, headerID) - Expect(err).To(HaveOccurred()) - Expect(err).To(MatchError(sql.ErrNoRows)) - }) - - It("returns an error if model is of wrong type", func() { - err = dripFileVowRepository.Create(headerID, []interface{}{test_data.WrongModel{}}) - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("model of type")) - }) }) Describe("MarkHeaderChecked", func() { - var headerID int64 + inputs := shared_behaviors.MissingHeadersBehaviorInputs{ + Repository: &dripFileVowRepository, + RepositoryTwo: &vow.DripFileVowRepository{}, + } - BeforeEach(func() { - headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) - Expect(err).NotTo(HaveOccurred()) - }) - - It("creates a row for a new headerID", func() { - err = dripFileVowRepository.MarkHeaderChecked(headerID) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT drip_file_vow_checked FROM public.checked_headers WHERE header_id = $1`, headerID) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) - - It("updates row when headerID already exists", func() { - _, err = db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerID) - - err = dripFileVowRepository.MarkHeaderChecked(headerID) - - Expect(err).NotTo(HaveOccurred()) - var headerChecked bool - err = db.Get(&headerChecked, `SELECT drip_file_vow_checked FROM public.checked_headers WHERE header_id = $1`, headerID) - Expect(err).NotTo(HaveOccurred()) - Expect(headerChecked).To(BeTrue()) - }) + shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs) }) Describe("MissingHeaders", func() { - var ( - startingBlock, endingBlock, dripFileBlock int64 - blockNumbers, headerIDs []int64 - ) + inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{ + CheckedHeaderColumnName: "drip_file_vow_checked", + Repository: &dripFileVowRepository, + } - BeforeEach(func() { - startingBlock = rand.Int63() - dripFileBlock = startingBlock + 1 - endingBlock = startingBlock + 2 - - blockNumbers = []int64{startingBlock, dripFileBlock, endingBlock, endingBlock + 1} - - headerIDs = []int64{} - for _, n := range blockNumbers { - headerID, err := headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(n)) - Expect(err).NotTo(HaveOccurred()) - headerIDs = append(headerIDs, headerID) - } - }) - - It("returns headers that haven't been checked", func() { - err := dripFileVowRepository.MarkHeaderChecked(headerIDs[1]) - Expect(err).NotTo(HaveOccurred()) - - headers, err := dripFileVowRepository.MissingHeaders(startingBlock, endingBlock) - - Expect(err).NotTo(HaveOccurred()) - Expect(len(headers)).To(Equal(2)) - Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock))) - Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock))) - }) - - It("only treats headers as checked if drip file vow logs have been checked", func() { - _, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1]) - Expect(err).NotTo(HaveOccurred()) - - headers, err := dripFileVowRepository.MissingHeaders(startingBlock, endingBlock) - - Expect(err).NotTo(HaveOccurred()) - Expect(len(headers)).To(Equal(3)) - Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dripFileBlock))) - Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dripFileBlock))) - Expect(headers[2].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dripFileBlock))) - }) - - It("only returns headers associated with the current node", func() { - dbTwo := test_config.NewTestDB(core.Node{ID: "second"}) - headerRepositoryTwo := repositories.NewHeaderRepository(dbTwo) - for _, n := range blockNumbers { - _, err = headerRepositoryTwo.CreateOrUpdateHeader(fakes.GetFakeHeader(n)) - Expect(err).NotTo(HaveOccurred()) - } - dripFileVowRepositoryTwo := vow.DripFileVowRepository{} - dripFileVowRepositoryTwo.SetDB(dbTwo) - err := dripFileVowRepository.MarkHeaderChecked(headerIDs[0]) - Expect(err).NotTo(HaveOccurred()) - - nodeOneMissingHeaders, err := dripFileVowRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) - Expect(err).NotTo(HaveOccurred()) - Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1)) - - nodeTwoMissingHeaders, err := dripFileVowRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) - Expect(err).NotTo(HaveOccurred()) - Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers))) - }) + shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs) }) }) diff --git a/pkg/transformers/factories/transformer_test.go b/pkg/transformers/factories/transformer_test.go index 71a01320..929b7f8d 100644 --- a/pkg/transformers/factories/transformer_test.go +++ b/pkg/transformers/factories/transformer_test.go @@ -29,14 +29,14 @@ import ( var _ = Describe("Transformer", func() { var ( - repository mocks.MockRepository - fetcher mocks.MockLogFetcher - converter mocks.MockConverter - transformer shared.Transformer - headerOne core.Header - headerTwo core.Header - config = test_data.GenericTestConfig - logs = test_data.GenericTestLogs + repository mocks.MockRepository + fetcher mocks.MockLogFetcher + converter mocks.MockConverter + transformer shared.Transformer + headerOne core.Header + headerTwo core.Header + config = test_data.GenericTestConfig + logs = test_data.GenericTestLogs ) BeforeEach(func() { @@ -106,7 +106,6 @@ var _ = Describe("Transformer", func() { repository.AssertMarkHeaderCheckedCalledWith(headerID) }) - It("doesn't attempt to convert or persist an empty collection when there are no logs", func() { repository.SetMissingHeaders([]core.Header{headerOne, headerTwo}) diff --git a/pkg/transformers/test_data/mocks/log_note_converter.go b/pkg/transformers/test_data/mocks/log_note_converter.go index 2cd71ef4..9a43434e 100644 --- a/pkg/transformers/test_data/mocks/log_note_converter.go +++ b/pkg/transformers/test_data/mocks/log_note_converter.go @@ -13,7 +13,7 @@ type MockLogNoteConverter struct { func (converter *MockLogNoteConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) { converter.PassedLogs = ethLogs - converter.ToModelsCalledCounter ++ + converter.ToModelsCalledCounter++ return converter.returnModels, converter.err } diff --git a/pkg/transformers/test_data/mocks/repository.go b/pkg/transformers/test_data/mocks/repository.go index ae8722f8..1cf5bf0a 100644 --- a/pkg/transformers/test_data/mocks/repository.go +++ b/pkg/transformers/test_data/mocks/repository.go @@ -26,7 +26,7 @@ func (repository *MockRepository) Create(headerID int64, models []interface{}) e repository.PassedHeaderID = headerID repository.PassedModels = models repository.CreatedHeaderIds = append(repository.CreatedHeaderIds, headerID) - repository.CreateCalledCounter ++ + repository.CreateCalledCounter++ return repository.createError }