diff --git a/pkg/transformers/vat_fold/repository_test.go b/pkg/transformers/vat_fold/repository_test.go index d703bc1e..6de7b3f4 100644 --- a/pkg/transformers/vat_fold/repository_test.go +++ b/pkg/transformers/vat_fold/repository_test.go @@ -21,6 +21,7 @@ import ( . "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/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_fold" @@ -28,21 +29,32 @@ import ( ) var _ = Describe("", func() { + Describe("Create", func() { - It("adds a vat event", func() { - db := test_config.NewTestDB(core.Node{}) + + var db *postgres.DB + var headerID int64 + var vatFoldRepository vat_fold.VatFoldRepository + + BeforeEach(func() { + db = test_config.NewTestDB(core.Node{}) test_config.CleanTestDB(db) + headerRepository := repositories.NewHeaderRepository(db) - headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{}) + id, err := headerRepository.CreateOrUpdateHeader(core.Header{}) Expect(err).NotTo(HaveOccurred()) - vatFoldRepository := vat_fold.NewVatFoldRepository(db) + headerID = id + vatFoldRepository = vat_fold.NewVatFoldRepository(db) err = vatFoldRepository.Create(headerID, test_data.VatFoldModel) + Expect(err).NotTo(HaveOccurred()) + }) - Expect(err).NotTo(HaveOccurred()) + It("adds a vat event", func() { var dbVatFold vat_fold.VatFoldModel - err = db.Get(&dbVatFold, `SELECT ilk, urn, rate, tx_idx, raw_log FROM maker.vat_fold WHERE header_id = $1`, headerID) + err := db.Get(&dbVatFold, `SELECT ilk, urn, rate, tx_idx, raw_log FROM maker.vat_fold WHERE header_id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) + Expect(dbVatFold.Ilk).To(Equal(test_data.VatFoldModel.Ilk)) Expect(dbVatFold.Urn).To(Equal(test_data.VatFoldModel.Urn)) Expect(dbVatFold.Rate).To(Equal(test_data.VatFoldModel.Rate)) @@ -51,34 +63,16 @@ var _ = Describe("", func() { }) It("does not duplicate vat events", func() { - db := test_config.NewTestDB(core.Node{}) - test_config.CleanTestDB(db) - headerRepository := repositories.NewHeaderRepository(db) - headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{}) - Expect(err).NotTo(HaveOccurred()) - vatfoldRepository := vat_fold.NewVatFoldRepository(db) - err = vatfoldRepository.Create(headerID, test_data.VatFoldModel) - Expect(err).NotTo(HaveOccurred()) - - err = vatfoldRepository.Create(headerID, test_data.VatFoldModel) + err := vatFoldRepository.Create(headerID, test_data.VatFoldModel) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) }) It("removes vat if corresponding header is deleted", func() { - db := test_config.NewTestDB(core.Node{}) - test_config.CleanTestDB(db) - headerRepository := repositories.NewHeaderRepository(db) - headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{}) - Expect(err).NotTo(HaveOccurred()) - vatfoldRepository := vat_fold.NewVatFoldRepository(db) - err = vatfoldRepository.Create(headerID, test_data.VatFoldModel) + _, err := db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) - _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) - - Expect(err).NotTo(HaveOccurred()) var dbVatFold vat_fold.VatFoldModel err = db.Get(&dbVatFold, `SELECT ilk, tx_idx, raw_log FROM maker.vat_fold WHERE header_id = $1`, headerID) Expect(err).To(HaveOccurred()) @@ -87,59 +81,92 @@ var _ = Describe("", func() { }) Describe("MissingHeaders", func() { - It("returns headers with no associated vat event", func() { - db := test_config.NewTestDB(core.Node{}) - test_config.CleanTestDB(db) - headerRepository := repositories.NewHeaderRepository(db) - startingBlockNumber := int64(1) - vatfoldBlockNumber := int64(2) - endingBlockNumber := int64(3) - blockNumbers := []int64{startingBlockNumber, vatfoldBlockNumber, 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.Create(headerIDs[1], test_data.VatFoldModel) - Expect(err).NotTo(HaveOccurred()) - headers, err := vatfoldRepository.MissingHeaders(startingBlockNumber, endingBlockNumber) + 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} + + repository := initRepository( + repositoryOptions{ + cleanDB: true, + blockNumbers: blockNumbers, + storeEvent: true, + storedEventBlockNumber: 1, + }, + ) + + headers, err := repository.MissingHeaders(startBlock, finalBlock) Expect(err).NotTo(HaveOccurred()) Expect(len(headers)).To(Equal(2)) - Expect(headers[0].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber))) - Expect(headers[1].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber))) + Expect(headers[0].BlockNumber).To(Or(Equal(startBlock), Equal(finalBlock))) + Expect(headers[1].BlockNumber).To(Or(Equal(startBlock), Equal(finalBlock))) }) 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.Create(headerIDs[0], test_data.VatFoldModel) - Expect(err).NotTo(HaveOccurred()) - nodeOneMissingHeaders, err := vatFoldRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) + 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]) Expect(err).NotTo(HaveOccurred()) Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1)) - nodeTwoMissingHeaders, err := vatFoldRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) + nodeTwoMissingHeaders, err := repositoryTwo.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], test_data.VatFoldModel) + Expect(err).NotTo(HaveOccurred()) + } + + return vatfoldRepository +}