(VDB-210) Refactor tend repository tests

This commit is contained in:
Rob Mulholand 2018-11-07 16:52:33 -06:00
parent 1c1a9c699a
commit 8c5192f82f

View File

@ -15,17 +15,15 @@
package tend_test package tend_test
import ( import (
"math/rand"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/fakes"
"github.com/vulcanize/vulcanizedb/pkg/transformers/tend" "github.com/vulcanize/vulcanizedb/pkg/transformers/tend"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors"
"github.com/vulcanize/vulcanizedb/test_config" "github.com/vulcanize/vulcanizedb/test_config"
) )
@ -34,13 +32,10 @@ var _ = Describe("TendRepository", func() {
db *postgres.DB db *postgres.DB
tendRepository tend.TendRepository tendRepository tend.TendRepository
headerRepository repositories.HeaderRepository headerRepository repositories.HeaderRepository
headerId int64
err error
) )
BeforeEach(func() { BeforeEach(func() {
node := test_config.NewTestNode() db = test_config.NewTestDB(test_config.NewTestNode())
db = test_config.NewTestDB(node)
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
headerRepository = repositories.NewHeaderRepository(db) headerRepository = repositories.NewHeaderRepository(db)
tendRepository = tend.TendRepository{} tendRepository = tend.TendRepository{}
@ -48,13 +43,22 @@ var _ = Describe("TendRepository", func() {
}) })
Describe("Create", func() { Describe("Create", func() {
BeforeEach(func() { modelWithDifferentLogIdx := test_data.TendModel
headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) modelWithDifferentLogIdx.LogIndex = modelWithDifferentLogIdx.LogIndex + 1
Expect(err).NotTo(HaveOccurred()) inputs := shared_behaviors.CreateBehaviorInputs{
}) CheckedHeaderColumnName: "tend_checked",
LogEventTableName: "maker.tend",
TestModel: test_data.TendModel,
ModelWithDifferentLogIdx: modelWithDifferentLogIdx,
Repository: &tendRepository,
}
shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
It("persists a tend record", func() { It("persists a tend record", func() {
err = tendRepository.Create(headerId, []interface{}{test_data.TendModel}) headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err = tendRepository.Create(headerID, []interface{}{test_data.TendModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var count int var count int
@ -63,7 +67,7 @@ var _ = Describe("TendRepository", func() {
Expect(count).To(Equal(1)) Expect(count).To(Equal(1))
dbResult := tend.TendModel{} dbResult := tend.TendModel{}
err = db.Get(&dbResult, `SELECT bid_id, lot, bid, guy, tic, log_idx, tx_idx, raw_log FROM maker.tend WHERE header_id = $1`, headerId) err = db.Get(&dbResult, `SELECT bid_id, lot, bid, guy, tic, log_idx, tx_idx, raw_log FROM maker.tend WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(dbResult.BidId).To(Equal(test_data.TendModel.BidId)) Expect(dbResult.BidId).To(Equal(test_data.TendModel.BidId))
@ -76,158 +80,23 @@ var _ = Describe("TendRepository", func() {
Expect(dbResult.Raw).To(MatchJSON(test_data.RawLogNoteJson)) Expect(dbResult.Raw).To(MatchJSON(test_data.RawLogNoteJson))
}) })
It("marks header as checked", func() {
err = tendRepository.Create(headerId, []interface{}{test_data.TendModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT tend_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 = tendRepository.Create(headerId, []interface{}{test_data.TendModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT tend_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 tend record fails", func() {
err = tendRepository.Create(headerId, []interface{}{test_data.TendModel})
Expect(err).NotTo(HaveOccurred())
err = tendRepository.Create(headerId, []interface{}{test_data.TendModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
It("deletes the tend record if its corresponding header record is deleted", func() {
err = tendRepository.Create(headerId, []interface{}{test_data.TendModel})
Expect(err).NotTo(HaveOccurred())
var count int
err = db.QueryRow(`SELECT count(*) from maker.tend`).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.tend`).Scan(&count)
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(0))
})
It("returns an error if model is of wrong type", func() {
err = tendRepository.Create(headerId, []interface{}{test_data.WrongModel{}})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("model of type"))
})
}) })
Describe("MarkHeaderChecked", func() { Describe("MarkHeaderChecked", func() {
BeforeEach(func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{
headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) CheckedHeaderColumnName: "tend_checked",
Expect(err).NotTo(HaveOccurred()) Repository: &tendRepository,
}) }
It("creates a row for a new headerID", func() { shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs)
err = tendRepository.MarkHeaderChecked(headerId)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT tend_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 = tendRepository.MarkHeaderChecked(headerId)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT tend_checked FROM public.checked_headers WHERE header_id = $1`, headerId)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
}) })
Describe("MissingHeaders", func() { Describe("MissingHeaders", func() {
var ( inputs := shared_behaviors.MissingHeadersBehaviorInputs{
tendBlock, startingBlock, endingBlock, outOfRangeBlock int64 Repository: &tendRepository,
headerIds []int64 RepositoryTwo: &tend.TendRepository{},
) }
BeforeEach(func() { shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs)
tendBlock = rand.Int63()
startingBlock = tendBlock - 1
endingBlock = tendBlock + 1
outOfRangeBlock = tendBlock + 2
headerIds = []int64{}
for _, number := range []int64{startingBlock, tendBlock, endingBlock, outOfRangeBlock} {
headerId, err := headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(number))
Expect(err).NotTo(HaveOccurred())
headerIds = append(headerIds, headerId)
}
})
It("returns headers for which there isn't an associated tend record", func() {
err = tendRepository.MarkHeaderChecked(headerIds[1])
Expect(err).NotTo(HaveOccurred())
headers, err := tendRepository.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 deal have been checked", func() {
_, err := db.Exec(`INSERT INTO public.checked_headers (header_id) VALUES ($1)`, headerIds[1])
Expect(err).NotTo(HaveOccurred())
headers, err := tendRepository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(3))
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(tendBlock)))
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(tendBlock)))
Expect(headers[2].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(tendBlock)))
})
It("only returns missing headers for the current node", func() {
node2 := core.Node{}
db2 := test_config.NewTestDB(node2)
headerRepository2 := repositories.NewHeaderRepository(db2)
tendRepository2 := tend.TendRepository{}
tendRepository2.SetDB(db2)
for _, number := range []int64{startingBlock, tendBlock, endingBlock} {
headerRepository2.CreateOrUpdateHeader(fakes.GetFakeHeader(number))
Expect(err).NotTo(HaveOccurred())
}
err = tendRepository.MarkHeaderChecked(headerIds[1])
Expect(err).NotTo(HaveOccurred())
node1MissingHeaders, err := tendRepository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(node1MissingHeaders)).To(Equal(2))
node2MissingHeaders, err := tendRepository2.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(node2MissingHeaders)).To(Equal(3))
})
}) })
}) })