forked from cerc-io/ipld-eth-server
Refactor repository tests (#99)
* Add shared test examples for repositories - Create - MissingHeaders - MarkHeaderChecked * Use shared repository tests for cat_file_chop_lump * Use shared repository test for cat_file_flip * Use shared repository test for cat_file_pit_vow * Updates to shared repository tests * Use shared repository tests for deal * Small updates to repo tests
This commit is contained in:
parent
bfe4531d7e
commit
a4abf3781e
@ -15,18 +15,14 @@
|
||||
package chop_lump_test
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
. "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/cat_file/chop_lump"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors"
|
||||
"github.com/vulcanize/vulcanizedb/test_config"
|
||||
)
|
||||
|
||||
@ -34,27 +30,32 @@ var _ = Describe("Cat file chop lump repository", func() {
|
||||
var (
|
||||
catFileRepository chop_lump.CatFileChopLumpRepository
|
||||
db *postgres.DB
|
||||
err error
|
||||
headerRepository datastore.HeaderRepository
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
db = test_config.NewTestDB(test_config.NewTestNode())
|
||||
test_config.CleanTestDB(db)
|
||||
headerRepository = repositories.NewHeaderRepository(db)
|
||||
catFileRepository = chop_lump.CatFileChopLumpRepository{}
|
||||
catFileRepository.SetDB(db)
|
||||
})
|
||||
|
||||
Describe("Create", func() {
|
||||
var headerID int64
|
||||
modelWithDifferentLogIdx := test_data.CatFileChopLumpModel
|
||||
modelWithDifferentLogIdx.LogIndex++
|
||||
inputs := shared_behaviors.CreateBehaviorInputs{
|
||||
CheckedHeaderColumnName: "cat_file_chop_lump_checked",
|
||||
LogEventTableName: "maker.cat_file_chop_lump",
|
||||
TestModel: test_data.CatFileChopLumpModel,
|
||||
ModelWithDifferentLogIdx: modelWithDifferentLogIdx,
|
||||
Repository: &catFileRepository,
|
||||
}
|
||||
|
||||
BeforeEach(func() {
|
||||
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
|
||||
|
||||
It("adds a cat file chop lump event", func() {
|
||||
headerRepository := repositories.NewHeaderRepository(db)
|
||||
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFileChopLumpModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
@ -68,167 +69,23 @@ var _ = Describe("Cat file chop lump repository", func() {
|
||||
Expect(dbResult.LogIndex).To(Equal(test_data.CatFileChopLumpModel.LogIndex))
|
||||
Expect(dbResult.Raw).To(MatchJSON(test_data.CatFileChopLumpModel.Raw))
|
||||
})
|
||||
|
||||
It("marks header as checked for logs", func() {
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFileChopLumpModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
err = db.Get(&headerChecked, `SELECT cat_file_chop_lump_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 = catFileRepository.Create(headerID, []interface{}{test_data.CatFileChopLumpModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
err = db.Get(&headerChecked, `SELECT cat_file_chop_lump_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(headerChecked).To(BeTrue())
|
||||
})
|
||||
|
||||
It("does not duplicate cat file chop lump events", func() {
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFileChopLumpModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFileChopLumpModel})
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
|
||||
})
|
||||
|
||||
It("allows for multiple cat file chop lump events in one transaction if they have different log indexes", func() {
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFileChopLumpModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
newCatFileChopLump := test_data.CatFileChopLumpModel
|
||||
newCatFileChopLump.LogIndex = newCatFileChopLump.LogIndex + 1
|
||||
|
||||
err = catFileRepository.Create(headerID, []interface{}{newCatFileChopLump})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("removes cat file chop lump if corresponding header is deleted", func() {
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFileChopLumpModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var dbResult chop_lump.CatFileChopLumpModel
|
||||
err = db.Get(&dbResult, `SELECT ilk, what, data, tx_idx, raw_log FROM maker.cat_file_chop_lump 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 = catFileRepository.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: "cat_file_chop_lump_checked",
|
||||
Repository: &catFileRepository,
|
||||
}
|
||||
|
||||
BeforeEach(func() {
|
||||
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("creates a row for a new headerID", func() {
|
||||
err = catFileRepository.MarkHeaderChecked(headerID)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
err = db.Get(&headerChecked, `SELECT cat_file_chop_lump_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 = catFileRepository.MarkHeaderChecked(headerID)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
err = db.Get(&headerChecked, `SELECT cat_file_chop_lump_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, catFileBlock int64
|
||||
blockNumbers, headerIDs []int64
|
||||
)
|
||||
inputs := shared_behaviors.MissingHeadersBehaviorInputs{
|
||||
Repository: &catFileRepository,
|
||||
RepositoryTwo: &chop_lump.CatFileChopLumpRepository{},
|
||||
}
|
||||
|
||||
BeforeEach(func() {
|
||||
startingBlock = GinkgoRandomSeed()
|
||||
catFileBlock = startingBlock + 1
|
||||
endingBlock = startingBlock + 2
|
||||
|
||||
blockNumbers = []int64{startingBlock, catFileBlock, 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 that haven't been checked", func() {
|
||||
err := catFileRepository.MarkHeaderChecked(headerIDs[1])
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
headers, err := catFileRepository.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 cat file chop lump 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 := catFileRepository.MissingHeaders(startingBlock, endingBlock)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(headers)).To(Equal(3))
|
||||
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(catFileBlock)))
|
||||
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(catFileBlock)))
|
||||
Expect(headers[2].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(catFileBlock)))
|
||||
})
|
||||
|
||||
It("only returns headers associated with the current node", func() {
|
||||
err := catFileRepository.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())
|
||||
}
|
||||
catFileRepositoryTwo := chop_lump.CatFileChopLumpRepository{}
|
||||
catFileRepositoryTwo.SetDB(dbTwo)
|
||||
|
||||
nodeOneMissingHeaders, err := catFileRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1))
|
||||
|
||||
nodeTwoMissingHeaders, err := catFileRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers)))
|
||||
})
|
||||
shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs)
|
||||
})
|
||||
})
|
||||
|
@ -15,47 +15,51 @@
|
||||
package flip_test
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
. "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/cat_file/flip"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors"
|
||||
"github.com/vulcanize/vulcanizedb/test_config"
|
||||
)
|
||||
|
||||
var _ = Describe("Cat file flip repository", func() {
|
||||
var (
|
||||
catFileRepository flip.CatFileFlipRepository
|
||||
db *postgres.DB
|
||||
err error
|
||||
headerRepository datastore.HeaderRepository
|
||||
catFileFlipRepository flip.CatFileFlipRepository
|
||||
db *postgres.DB
|
||||
headerRepository datastore.HeaderRepository
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
db = test_config.NewTestDB(test_config.NewTestNode())
|
||||
test_config.CleanTestDB(db)
|
||||
headerRepository = repositories.NewHeaderRepository(db)
|
||||
catFileRepository = flip.CatFileFlipRepository{}
|
||||
catFileRepository.SetDB(db)
|
||||
catFileFlipRepository = flip.CatFileFlipRepository{}
|
||||
catFileFlipRepository.SetDB(db)
|
||||
})
|
||||
|
||||
Describe("Create", func() {
|
||||
var headerID int64
|
||||
catFileFlipWithDifferentIdx := test_data.CatFileFlipModel
|
||||
catFileFlipWithDifferentIdx.LogIndex++
|
||||
inputs := shared_behaviors.CreateBehaviorInputs{
|
||||
CheckedHeaderColumnName: "cat_file_flip_checked",
|
||||
LogEventTableName: "maker.cat_file_flip",
|
||||
TestModel: test_data.CatFileFlipModel,
|
||||
ModelWithDifferentLogIdx: catFileFlipWithDifferentIdx,
|
||||
Repository: &catFileFlipRepository,
|
||||
}
|
||||
|
||||
BeforeEach(func() {
|
||||
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
|
||||
|
||||
It("adds a cat file flip event", func() {
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFileFlipModel})
|
||||
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
err = catFileFlipRepository.Create(headerID, []interface{}{test_data.CatFileFlipModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var dbResult flip.CatFileFlipModel
|
||||
@ -68,167 +72,22 @@ var _ = Describe("Cat file flip repository", func() {
|
||||
Expect(dbResult.LogIndex).To(Equal(test_data.CatFileFlipModel.LogIndex))
|
||||
Expect(dbResult.Raw).To(MatchJSON(test_data.CatFileFlipModel.Raw))
|
||||
})
|
||||
|
||||
It("marks header as checked for logs", func() {
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFileFlipModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
err = db.Get(&headerChecked, `SELECT cat_file_flip_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 = catFileRepository.Create(headerID, []interface{}{test_data.CatFileFlipModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
err = db.Get(&headerChecked, `SELECT cat_file_flip_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(headerChecked).To(BeTrue())
|
||||
})
|
||||
|
||||
It("does not duplicate cat file flip events", func() {
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFileFlipModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFileFlipModel})
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
|
||||
})
|
||||
|
||||
It("allows for multiple cat file flip events in one transaction if they have different log indexes", func() {
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFileFlipModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
catFileFlip := test_data.CatFileFlipModel
|
||||
catFileFlip.LogIndex = catFileFlip.LogIndex + 1
|
||||
|
||||
err = catFileRepository.Create(headerID, []interface{}{catFileFlip})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("removes cat file flip if corresponding header is deleted", func() {
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFileFlipModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var dbResult flip.CatFileFlipModel
|
||||
err = db.Get(&dbResult, `SELECT ilk, what, flip, tx_idx, raw_log FROM maker.cat_file_flip 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 = catFileRepository.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: "cat_file_flip_checked",
|
||||
Repository: &catFileFlipRepository,
|
||||
}
|
||||
|
||||
BeforeEach(func() {
|
||||
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("creates a row for a new headerID", func() {
|
||||
err = catFileRepository.MarkHeaderChecked(headerID)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
err = db.Get(&headerChecked, `SELECT cat_file_flip_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 = catFileRepository.MarkHeaderChecked(headerID)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
err = db.Get(&headerChecked, `SELECT cat_file_flip_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, catFileBlock int64
|
||||
blockNumbers, headerIDs []int64
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
startingBlock = GinkgoRandomSeed()
|
||||
catFileBlock = startingBlock + 1
|
||||
endingBlock = startingBlock + 2
|
||||
|
||||
blockNumbers = []int64{startingBlock, catFileBlock, 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 := catFileRepository.MarkHeaderChecked(headerIDs[1])
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
headers, err := catFileRepository.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 cat file flip 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 := catFileRepository.MissingHeaders(startingBlock, endingBlock)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(headers)).To(Equal(3))
|
||||
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(catFileBlock)))
|
||||
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(catFileBlock)))
|
||||
Expect(headers[2].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(catFileBlock)))
|
||||
})
|
||||
|
||||
It("only returns headers associated with the current node", func() {
|
||||
err := catFileRepository.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())
|
||||
}
|
||||
catFileRepositoryTwo := flip.CatFileFlipRepository{}
|
||||
catFileRepositoryTwo.SetDB(dbTwo)
|
||||
|
||||
nodeOneMissingHeaders, err := catFileRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1))
|
||||
|
||||
nodeTwoMissingHeaders, err := catFileRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers)))
|
||||
})
|
||||
inputs := shared_behaviors.MissingHeadersBehaviorInputs{
|
||||
Repository: &catFileFlipRepository,
|
||||
RepositoryTwo: &flip.CatFileFlipRepository{},
|
||||
}
|
||||
shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs)
|
||||
})
|
||||
})
|
||||
|
@ -15,47 +15,51 @@
|
||||
package pit_vow_test
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
. "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/cat_file/pit_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"
|
||||
)
|
||||
|
||||
var _ = Describe("Cat file pit vow repository", func() {
|
||||
var (
|
||||
catFileRepository pit_vow.CatFilePitVowRepository
|
||||
db *postgres.DB
|
||||
err error
|
||||
headerRepository datastore.HeaderRepository
|
||||
catFilePitVowRepository pit_vow.CatFilePitVowRepository
|
||||
db *postgres.DB
|
||||
headerRepository datastore.HeaderRepository
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
db = test_config.NewTestDB(test_config.NewTestNode())
|
||||
test_config.CleanTestDB(db)
|
||||
headerRepository = repositories.NewHeaderRepository(db)
|
||||
catFileRepository = pit_vow.CatFilePitVowRepository{}
|
||||
catFileRepository.SetDB(db)
|
||||
catFilePitVowRepository = pit_vow.CatFilePitVowRepository{}
|
||||
catFilePitVowRepository.SetDB(db)
|
||||
})
|
||||
|
||||
Describe("Create", func() {
|
||||
var headerID int64
|
||||
modelWithDifferentLogIdx := test_data.CatFilePitVowModel
|
||||
modelWithDifferentLogIdx.LogIndex++
|
||||
inputs := shared_behaviors.CreateBehaviorInputs{
|
||||
CheckedHeaderColumnName: "cat_file_pit_vow_checked",
|
||||
LogEventTableName: "maker.cat_file_pit_vow",
|
||||
TestModel: test_data.CatFilePitVowModel,
|
||||
ModelWithDifferentLogIdx: modelWithDifferentLogIdx,
|
||||
Repository: &catFilePitVowRepository,
|
||||
}
|
||||
|
||||
BeforeEach(func() {
|
||||
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
|
||||
|
||||
It("adds a cat file pit vow event", func() {
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFilePitVowModel})
|
||||
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
err = catFilePitVowRepository.Create(headerID, []interface{}{test_data.CatFilePitVowModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var dbResult pit_vow.CatFilePitVowModel
|
||||
@ -67,167 +71,22 @@ var _ = Describe("Cat file pit vow repository", func() {
|
||||
Expect(dbResult.LogIndex).To(Equal(test_data.CatFilePitVowModel.LogIndex))
|
||||
Expect(dbResult.Raw).To(MatchJSON(test_data.CatFilePitVowModel.Raw))
|
||||
})
|
||||
|
||||
It("marks header as checked for logs", func() {
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFilePitVowModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
err = db.Get(&headerChecked, `SELECT cat_file_pit_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 = catFileRepository.Create(headerID, []interface{}{test_data.CatFilePitVowModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
err = db.Get(&headerChecked, `SELECT cat_file_pit_vow_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(headerChecked).To(BeTrue())
|
||||
})
|
||||
|
||||
It("does not duplicate cat file pit vow events", func() {
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFilePitVowModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFilePitVowModel})
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
|
||||
})
|
||||
|
||||
It("allows for multiple cat file pit events in one transaction if they have different log indexes", func() {
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFilePitVowModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
catFilePitVow := test_data.CatFilePitVowModel
|
||||
catFilePitVow.LogIndex = catFilePitVow.LogIndex + 1
|
||||
|
||||
err = catFileRepository.Create(headerID, []interface{}{catFilePitVow})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("removes cat file pit vow if corresponding header is deleted", func() {
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFilePitVowModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var dbResult pit_vow.CatFilePitVowModel
|
||||
err = db.Get(&dbResult, `SELECT what, data, tx_idx, raw_log FROM maker.cat_file_pit_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 = catFileRepository.Create(headerID, []interface{}{test_data.WrongModel{}})
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.Error()).To(ContainSubstring("model of type"))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("MarkHeaderChecked", func() {
|
||||
var headerID int64
|
||||
|
||||
BeforeEach(func() {
|
||||
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("creates a row for a new headerID", func() {
|
||||
err = catFileRepository.MarkHeaderChecked(headerID)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
err = db.Get(&headerChecked, `SELECT cat_file_pit_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 = catFileRepository.MarkHeaderChecked(headerID)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
err = db.Get(&headerChecked, `SELECT cat_file_pit_vow_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(headerChecked).To(BeTrue())
|
||||
})
|
||||
inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{
|
||||
CheckedHeaderColumnName: "cat_file_pit_vow_checked",
|
||||
Repository: &catFilePitVowRepository,
|
||||
}
|
||||
shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs)
|
||||
})
|
||||
|
||||
Describe("MissingHeaders", func() {
|
||||
var (
|
||||
startingBlock, endingBlock, catFileBlock int64
|
||||
blockNumbers, headerIDs []int64
|
||||
)
|
||||
inputs := shared_behaviors.MissingHeadersBehaviorInputs{
|
||||
Repository: &catFilePitVowRepository,
|
||||
RepositoryTwo: &pit_vow.CatFilePitVowRepository{},
|
||||
}
|
||||
|
||||
BeforeEach(func() {
|
||||
startingBlock = GinkgoRandomSeed()
|
||||
endingBlock = startingBlock + 2
|
||||
catFileBlock = startingBlock + 1
|
||||
|
||||
blockNumbers = []int64{startingBlock, catFileBlock, 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 that haven't been checked", func() {
|
||||
err := catFileRepository.MarkHeaderChecked(headerIDs[1])
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
headers, err := catFileRepository.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 cat file pit 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 := catFileRepository.MissingHeaders(startingBlock, endingBlock)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(headers)).To(Equal(3))
|
||||
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(catFileBlock)))
|
||||
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(catFileBlock)))
|
||||
Expect(headers[2].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(catFileBlock)))
|
||||
})
|
||||
|
||||
It("only returns headers associated with the current node", func() {
|
||||
err := catFileRepository.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())
|
||||
}
|
||||
catFileRepositoryTwo := pit_vow.CatFilePitVowRepository{}
|
||||
catFileRepositoryTwo.SetDB(dbTwo)
|
||||
|
||||
nodeOneMissingHeaders, err := catFileRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1))
|
||||
|
||||
nodeTwoMissingHeaders, err := catFileRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers)))
|
||||
})
|
||||
shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs)
|
||||
})
|
||||
})
|
||||
|
@ -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/deal"
|
||||
"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("Deal Repository", func() {
|
||||
db *postgres.DB
|
||||
dealRepository deal.DealRepository
|
||||
headerRepository repositories.HeaderRepository
|
||||
err error
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
@ -44,14 +43,21 @@ var _ = Describe("Deal Repository", func() {
|
||||
})
|
||||
|
||||
Describe("Create", func() {
|
||||
var headerId int64
|
||||
modelWithDifferentLogIdx := test_data.DealModel
|
||||
modelWithDifferentLogIdx.LogIndex = modelWithDifferentLogIdx.LogIndex + 1
|
||||
inputs := shared_behaviors.CreateBehaviorInputs{
|
||||
CheckedHeaderColumnName: "deal_checked",
|
||||
LogEventTableName: "maker.deal",
|
||||
TestModel: test_data.DealModel,
|
||||
ModelWithDifferentLogIdx: modelWithDifferentLogIdx,
|
||||
Repository: &dealRepository,
|
||||
}
|
||||
|
||||
BeforeEach(func() {
|
||||
headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
|
||||
|
||||
It("persists a deal record", func() {
|
||||
headerId, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
err = dealRepository.Create(headerId, []interface{}{test_data.DealModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
@ -67,168 +73,23 @@ var _ = Describe("Deal Repository", func() {
|
||||
Expect(dbResult.TransactionIndex).To(Equal(test_data.DealModel.TransactionIndex))
|
||||
Expect(dbResult.Raw).To(MatchJSON(test_data.DealModel.Raw))
|
||||
})
|
||||
|
||||
It("marks header as checked for logs", func() {
|
||||
err = dealRepository.Create(headerId, []interface{}{test_data.DealModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
err = db.Get(&headerChecked, `SELECT deal_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 = dealRepository.Create(headerId, []interface{}{test_data.DealModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
err = db.Get(&headerChecked, `SELECT deal_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 deal record fails", func() {
|
||||
err = dealRepository.Create(headerId, []interface{}{test_data.DealModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = dealRepository.Create(headerId, []interface{}{test_data.DealModel})
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
|
||||
})
|
||||
|
||||
It("deletes the deal record if its corresponding header record is deleted", func() {
|
||||
err = dealRepository.Create(headerId, []interface{}{test_data.DealModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var count int
|
||||
err = db.QueryRow(`SELECT count(*) from maker.deal`).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.deal`).Scan(&count)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(count).To(Equal(0))
|
||||
})
|
||||
|
||||
It("returns an error if model is of wrong type", func() {
|
||||
err = dealRepository.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: "deal_checked",
|
||||
Repository: &dealRepository,
|
||||
}
|
||||
|
||||
BeforeEach(func() {
|
||||
headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("creates a row for a new headerID", func() {
|
||||
err = dealRepository.MarkHeaderChecked(headerId)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
err = db.Get(&headerChecked, `SELECT deal_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 = dealRepository.MarkHeaderChecked(headerId)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
err = db.Get(&headerChecked, `SELECT deal_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 (
|
||||
dealBlock, startingBlock, endingBlock int64
|
||||
blockNumbers, headerIds []int64
|
||||
)
|
||||
inputs := shared_behaviors.MissingHeadersBehaviorInputs{
|
||||
Repository: &dealRepository,
|
||||
RepositoryTwo: &deal.DealRepository{},
|
||||
}
|
||||
|
||||
BeforeEach(func() {
|
||||
dealBlock = GinkgoRandomSeed()
|
||||
startingBlock = dealBlock - 1
|
||||
endingBlock = dealBlock + 1
|
||||
outOfRangeBlockNumber := dealBlock + 2
|
||||
|
||||
blockNumbers = []int64{startingBlock, dealBlock, 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 don't have a corresponding deals", func() {
|
||||
err = dealRepository.MarkHeaderChecked(headerIds[1])
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
missingHeaders, err := dealRepository.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 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 := dealRepository.MissingHeaders(startingBlock, endingBlock)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(headers)).To(Equal(3))
|
||||
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dealBlock)))
|
||||
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dealBlock)))
|
||||
Expect(headers[2].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(dealBlock)))
|
||||
})
|
||||
|
||||
It("only returns missing headers for the given node", func() {
|
||||
err = dealRepository.MarkHeaderChecked(headerIds[1])
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
node2 := core.Node{}
|
||||
db2 := test_config.NewTestDB(node2)
|
||||
dealRepository2 := deal.DealRepository{}
|
||||
dealRepository2.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 := dealRepository.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 := dealRepository2.MissingHeaders(startingBlock, endingBlock)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(missingHeadersNode2)).To(Equal(3))
|
||||
Expect(missingHeadersNode2[0].BlockNumber).To(Or(Equal(startingBlock), Equal(dealBlock), Equal(endingBlock)))
|
||||
Expect(missingHeadersNode2[1].BlockNumber).To(Or(Equal(startingBlock), Equal(dealBlock), Equal(endingBlock)))
|
||||
Expect(missingHeadersNode2[2].BlockNumber).To(Or(Equal(startingBlock), Equal(dealBlock), Equal(endingBlock)))
|
||||
})
|
||||
shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs)
|
||||
})
|
||||
})
|
||||
|
@ -0,0 +1,265 @@
|
||||
// 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 shared_behaviors
|
||||
|
||||
import (
|
||||
. "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/factories"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
|
||||
"github.com/vulcanize/vulcanizedb/test_config"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
var (
|
||||
db *postgres.DB
|
||||
headerRepository datastore.HeaderRepository
|
||||
err error
|
||||
headerId int64
|
||||
)
|
||||
|
||||
type CreateBehaviorInputs struct {
|
||||
CheckedHeaderColumnName string
|
||||
LogEventTableName string
|
||||
TestModel interface{}
|
||||
ModelWithDifferentLogIdx interface{}
|
||||
Repository factories.Repository
|
||||
}
|
||||
|
||||
type MarkedHeaderCheckedBehaviorInputs struct {
|
||||
CheckedHeaderColumnName string
|
||||
Repository factories.Repository
|
||||
}
|
||||
|
||||
type MissingHeadersBehaviorInputs struct {
|
||||
Repository factories.Repository
|
||||
RepositoryTwo factories.Repository
|
||||
}
|
||||
|
||||
func SharedRepositoryCreateBehaviors(inputs *CreateBehaviorInputs) {
|
||||
Describe("Create", func() {
|
||||
var headerID int64
|
||||
var repository = inputs.Repository
|
||||
var checkedHeaderColumn = inputs.CheckedHeaderColumnName
|
||||
var logEventModel = inputs.TestModel
|
||||
|
||||
BeforeEach(func() {
|
||||
headerRepository = GetHeaderRepository()
|
||||
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("marks header as checked for logs", func() {
|
||||
err = repository.Create(headerID, []interface{}{logEventModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
query := `SELECT ` + checkedHeaderColumn + ` FROM public.checked_headers WHERE header_id = $1`
|
||||
err = db.Get(&headerChecked, query, 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 = repository.Create(headerID, []interface{}{logEventModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
query := `SELECT ` + checkedHeaderColumn + ` FROM public.checked_headers WHERE header_id = $1`
|
||||
err = db.Get(&headerChecked, query, headerID)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(headerChecked).To(BeTrue())
|
||||
})
|
||||
|
||||
It("returns an error if inserting duplicate log events", func() {
|
||||
err = repository.Create(headerID, []interface{}{logEventModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = repository.Create(headerID, []interface{}{logEventModel})
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
|
||||
})
|
||||
|
||||
It("allows for multiple log events of the same type in one transaction if they have different log indexes", func() {
|
||||
err = repository.Create(headerID, []interface{}{logEventModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
err = repository.Create(headerID, []interface{}{inputs.ModelWithDifferentLogIdx})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("removes the log event record if the corresponding header is deleted", func() {
|
||||
err = repository.Create(headerID, []interface{}{logEventModel})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
var count int
|
||||
query := `SELECT count(*) from ` + inputs.LogEventTableName
|
||||
err = db.QueryRow(query).Scan(&count)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(count).To(Equal(0))
|
||||
})
|
||||
|
||||
It("returns an error if model is of wrong type", func() {
|
||||
err = repository.Create(headerID, []interface{}{test_data.WrongModel{}})
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.Error()).To(ContainSubstring("model of type"))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func SharedRepositoryMissingHeadersBehaviors(inputs *MissingHeadersBehaviorInputs) {
|
||||
Describe("MissingHeaders", func() {
|
||||
var (
|
||||
repository = inputs.Repository
|
||||
startingBlockNumber int64
|
||||
endingBlockNumber int64
|
||||
eventSpecificBlockNumber int64
|
||||
blockNumbers []int64
|
||||
headerIDs []int64
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
headerRepository = GetHeaderRepository()
|
||||
startingBlockNumber = rand.Int63()
|
||||
eventSpecificBlockNumber = startingBlockNumber + 1
|
||||
endingBlockNumber = startingBlockNumber + 2
|
||||
outOfRangeBlockNumber := endingBlockNumber + 1
|
||||
|
||||
blockNumbers = []int64{startingBlockNumber, eventSpecificBlockNumber, endingBlockNumber, outOfRangeBlockNumber}
|
||||
|
||||
headerIDs = []int64{}
|
||||
for _, n := range blockNumbers {
|
||||
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
|
||||
headerIDs = append(headerIDs, headerID)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
})
|
||||
|
||||
It("returns headers that haven't been checked", func() {
|
||||
err := repository.MarkHeaderChecked(headerIDs[1])
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
headers, err := repository.MissingHeaders(startingBlockNumber, endingBlockNumber)
|
||||
|
||||
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)))
|
||||
})
|
||||
|
||||
It("only treats headers as checked if the event specific 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 := repository.MissingHeaders(startingBlockNumber, endingBlockNumber)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(headers)).To(Equal(3))
|
||||
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(eventSpecificBlockNumber)))
|
||||
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(eventSpecificBlockNumber)))
|
||||
Expect(headers[2].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber), Equal(eventSpecificBlockNumber)))
|
||||
})
|
||||
|
||||
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())
|
||||
}
|
||||
repositoryTwo := inputs.RepositoryTwo
|
||||
repositoryTwo.SetDB(dbTwo)
|
||||
|
||||
err := repository.MarkHeaderChecked(headerIDs[0])
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
nodeOneMissingHeaders, err := repository.MissingHeaders(startingBlockNumber, endingBlockNumber)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(nodeOneMissingHeaders)).To(Equal(2))
|
||||
Expect(nodeOneMissingHeaders[0].BlockNumber).To(Or(Equal(eventSpecificBlockNumber), Equal(endingBlockNumber)))
|
||||
Expect(nodeOneMissingHeaders[1].BlockNumber).To(Or(Equal(eventSpecificBlockNumber), Equal(endingBlockNumber)))
|
||||
|
||||
nodeTwoMissingHeaders, err := repositoryTwo.MissingHeaders(startingBlockNumber, endingBlockNumber)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(nodeTwoMissingHeaders)).To(Equal(3))
|
||||
Expect(nodeTwoMissingHeaders[0].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(eventSpecificBlockNumber), Equal(endingBlockNumber)))
|
||||
Expect(nodeTwoMissingHeaders[1].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(eventSpecificBlockNumber), Equal(endingBlockNumber)))
|
||||
Expect(nodeTwoMissingHeaders[2].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(eventSpecificBlockNumber), Equal(endingBlockNumber)))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func SharedRepositoryMarkHeaderCheckedBehaviors(inputs *MarkedHeaderCheckedBehaviorInputs) {
|
||||
var repository = inputs.Repository
|
||||
var checkedHeaderColumn = inputs.CheckedHeaderColumnName
|
||||
|
||||
Describe("MarkHeaderChecked", func() {
|
||||
BeforeEach(func() {
|
||||
headerRepository = GetHeaderRepository()
|
||||
headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("creates a row for a new headerId", func() {
|
||||
err = repository.MarkHeaderChecked(headerId)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
query := `SELECT ` + checkedHeaderColumn + ` FROM public.checked_headers WHERE header_id = $1`
|
||||
err = db.Get(&headerChecked, query, 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 = repository.MarkHeaderChecked(headerId)
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var headerChecked bool
|
||||
query := `SELECT ` + checkedHeaderColumn + ` FROM public.checked_headers WHERE header_id = $1`
|
||||
err = db.Get(&headerChecked, query, headerId)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(headerChecked).To(BeTrue())
|
||||
})
|
||||
|
||||
It("returns an error if upserting a record fails", func() {
|
||||
err = repository.MarkHeaderChecked(0)
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.Error()).To(ContainSubstring("violates foreign key constraint"))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func GetHeaderRepository() repositories.HeaderRepository {
|
||||
db = test_config.NewTestDB(test_config.NewTestNode())
|
||||
test_config.CleanTestDB(db)
|
||||
|
||||
return repositories.NewHeaderRepository(db)
|
||||
}
|
Loading…
Reference in New Issue
Block a user