From 81dec589fc2c1d28f34eeaa79636e183db9afa70 Mon Sep 17 00:00:00 2001 From: David Terry Date: Tue, 9 Oct 2018 14:11:02 +0300 Subject: [PATCH] vat.fold: rollback broken test refactoring --- pkg/transformers/vat_fold/repository_test.go | 140 +++++++++---------- 1 file changed, 66 insertions(+), 74 deletions(-) diff --git a/pkg/transformers/vat_fold/repository_test.go b/pkg/transformers/vat_fold/repository_test.go index a8c17c16..68360b54 100644 --- a/pkg/transformers/vat_fold/repository_test.go +++ b/pkg/transformers/vat_fold/repository_test.go @@ -82,94 +82,86 @@ var _ = Describe("", func() { Describe("MissingHeaders", func() { - It("returns headers with no associated vat event", func() { - startBlock := int64(1) - eventBlock := int64(2) - finalBlock := int64(3) - blockNumbers := []int64{startBlock, eventBlock, finalBlock, finalBlock + 1} + It("returns headers that haven't been checked", func() { + db := test_config.NewTestDB(core.Node{}) + test_config.CleanTestDB(db) + headerRepository := repositories.NewHeaderRepository(db) + startingBlockNumber := int64(1) + vatGrabBlockNumber := int64(2) + endingBlockNumber := int64(3) + blockNumbers := []int64{startingBlockNumber, vatGrabBlockNumber, endingBlockNumber, endingBlockNumber + 1} + var headerIDs []int64 + for _, n := range blockNumbers { + headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{BlockNumber: n}) + headerIDs = append(headerIDs, headerID) + Expect(err).NotTo(HaveOccurred()) + } + vatFoldRepository := vat_fold.NewVatFoldRepository(db) + err := vatFoldRepository.MarkHeaderChecked(headerIDs[1]) + Expect(err).NotTo(HaveOccurred()) - repository := initRepository( - repositoryOptions{ - cleanDB: true, - blockNumbers: blockNumbers, - storeEvent: true, - storedEventBlockNumber: 1, - }, - ) - - headers, err := repository.MissingHeaders(startBlock, finalBlock) + headers, err := vatFoldRepository.MissingHeaders(startingBlockNumber, endingBlockNumber) Expect(err).NotTo(HaveOccurred()) Expect(len(headers)).To(Equal(2)) - Expect(headers[0].BlockNumber).To(Or(Equal(startBlock), Equal(finalBlock))) - Expect(headers[1].BlockNumber).To(Or(Equal(startBlock), Equal(finalBlock))) + Expect(headers[0].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber))) + Expect(headers[1].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber))) + }) + + It("only treats headers as checked if vat fold logs have been checked", func() { + db := test_config.NewTestDB(core.Node{}) + test_config.CleanTestDB(db) + headerRepository := repositories.NewHeaderRepository(db) + startingBlockNumber := int64(1) + vatGrabdBlockNumber := int64(2) + endingBlockNumber := int64(3) + blockNumbers := []int64{startingBlockNumber, vatGrabdBlockNumber, endingBlockNumber, endingBlockNumber + 1} + var headerIDs []int64 + for _, n := range blockNumbers { + headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{BlockNumber: n}) + headerIDs = append(headerIDs, headerID) + Expect(err).NotTo(HaveOccurred()) + } + vatFoldRepository := vat_fold.NewVatFoldRepository(db) + _, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIDs[1]) + Expect(err).NotTo(HaveOccurred()) + + headers, err := vatFoldRepository.MissingHeaders(startingBlockNumber, endingBlockNumber) + + Expect(err).NotTo(HaveOccurred()) + Expect(len(headers)).To(Equal(3)) + Expect(headers[0].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(vatGrabdBlockNumber))) + Expect(headers[1].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(vatGrabdBlockNumber))) + Expect(headers[2].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(vatGrabdBlockNumber))) }) It("only returns headers associated with the current node", func() { + db := test_config.NewTestDB(core.Node{}) + test_config.CleanTestDB(db) blockNumbers := []int64{1, 2, 3} + headerRepository := repositories.NewHeaderRepository(db) + dbTwo := test_config.NewTestDB(core.Node{ID: "second"}) + headerRepositoryTwo := repositories.NewHeaderRepository(dbTwo) + var headerIDs []int64 + for _, n := range blockNumbers { + headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{BlockNumber: n}) + Expect(err).NotTo(HaveOccurred()) + headerIDs = append(headerIDs, headerID) + _, err = headerRepositoryTwo.CreateOrUpdateHeader(core.Header{BlockNumber: n}) + Expect(err).NotTo(HaveOccurred()) + } + vatFoldRepository := vat_fold.NewVatFoldRepository(db) + vatFoldRepositoryTwo := vat_fold.NewVatFoldRepository(dbTwo) + err := vatFoldRepository.MarkHeaderChecked(headerIDs[0]) + Expect(err).NotTo(HaveOccurred()) - repositoryOne := initRepository( - repositoryOptions{ - blockNumbers: blockNumbers, - nodeID: "first", - cleanDB: true, - storeEvent: true, - }, - ) - - repositoryTwo := initRepository( - repositoryOptions{ - blockNumbers: blockNumbers, - nodeID: "second", - cleanDB: false, - storeEvent: false, - }, - ) - - nodeOneMissingHeaders, err := repositoryOne.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) + nodeOneMissingHeaders, err := vatFoldRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) Expect(err).NotTo(HaveOccurred()) Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1)) - nodeTwoMissingHeaders, err := repositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) + nodeTwoMissingHeaders, err := vatFoldRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) Expect(err).NotTo(HaveOccurred()) Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers))) }) }) }) - -// ------------------------------------------------------------------------------------------- - -type repositoryOptions struct { - blockNumbers []int64 - nodeID string - cleanDB bool - storeEvent bool - storedEventBlockNumber int64 -} - -func initRepository(options repositoryOptions) vat_fold.VatFoldRepository { - db := test_config.NewTestDB(core.Node{ID: options.nodeID}) - if options.cleanDB { - test_config.CleanTestDB(db) - } - - headerRepository := repositories.NewHeaderRepository(db) - vatfoldRepository := vat_fold.NewVatFoldRepository(db) - - var headerIDs []int64 - for _, n := range options.blockNumbers { - headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{BlockNumber: n}) - headerIDs = append(headerIDs, headerID) - Expect(err).NotTo(HaveOccurred()) - } - - if options.storeEvent { - err := vatfoldRepository.Create( - headerIDs[options.storedEventBlockNumber], - []vat_fold.VatFoldModel{test_data.VatFoldModel}, - ) - Expect(err).NotTo(HaveOccurred()) - } - - return vatfoldRepository -}