VDB 207 refactor more repo tests (#107)

* Use shared repo tests for vat flux

* Use shared repo methods for vat flux

* Use shared repo test examples for vat fold

* Use shared repository methods for vat fold

* Use shared repo test examples for vat grab

* Use shared repo methods for vat grab

* Use shared repo test examples for vat heal

* Use shared repo methods for vat heal
This commit is contained in:
Elizabeth 2018-11-09 14:54:22 -06:00 committed by GitHub
parent c1e10f09fb
commit 672269effa
9 changed files with 139 additions and 773 deletions

View File

@ -16,4 +16,8 @@ var (
PitFileStabilityFeeChecked = "pit_file_stability_fee_checked" PitFileStabilityFeeChecked = "pit_file_stability_fee_checked"
PriceFeedsChecked = "price_feeds_checked" PriceFeedsChecked = "price_feeds_checked"
TendChecked = "tend_checked" TendChecked = "tend_checked"
VatFluxChecked = "vat_flux_checked"
VatFoldChecked = "vat_fold_checked"
VatGrabChecked = "vat_grab_checked"
VatHealChecked = "vat_heal_checked"
) )

View File

@ -18,6 +18,8 @@ import (
"fmt" "fmt"
"github.com/vulcanize/vulcanizedb/pkg/core" "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/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
) )
type VatFluxRepository struct { type VatFluxRepository struct {
@ -46,10 +48,7 @@ func (repository VatFluxRepository) Create(headerId int64, models []interface{})
} }
} }
_, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, vat_flux_checked) err = shared.MarkHeaderCheckedInTransaction(headerId, tx, constants.VatFluxChecked)
VALUES($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vat_flux_checked = $2`, headerId, true)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
@ -59,26 +58,11 @@ func (repository VatFluxRepository) Create(headerId int64, models []interface{})
} }
func (repository VatFluxRepository) MarkHeaderChecked(headerId int64) error { func (repository VatFluxRepository) MarkHeaderChecked(headerId int64) error {
_, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, vat_flux_checked) return shared.MarkHeaderChecked(headerId, repository.db, constants.VatFluxChecked)
VALUES($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vat_flux_checked = $2`, headerId, true)
return err
} }
func (repository VatFluxRepository) MissingHeaders(startingBlock, endingBlock int64) ([]core.Header, error) { func (repository VatFluxRepository) MissingHeaders(startingBlock, endingBlock int64) ([]core.Header, error) {
var headers []core.Header return shared.MissingHeaders(startingBlock, endingBlock, repository.db, constants.VatFluxChecked)
err := repository.db.Select(&headers,
`SELECT headers.id, block_number from headers
LEFT JOIN checked_headers on headers.id = header_id
WHERE (header_id ISNULL OR vat_flux_checked IS FALSE)
AND headers.block_number >= $1
AND headers.block_number <= $2
AND headers.eth_node_fingerprint = $3`,
startingBlock, endingBlock, repository.db.Node.ID)
return headers, err
} }
func (repository *VatFluxRepository) SetDB(db *postgres.DB) { func (repository *VatFluxRepository) SetDB(db *postgres.DB) {

View File

@ -15,27 +15,23 @@
package vat_flux_test package vat_flux_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/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/shared_behaviors"
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_flux" "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_flux"
"github.com/vulcanize/vulcanizedb/test_config" "github.com/vulcanize/vulcanizedb/test_config"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
) )
var _ = Describe("VatFlux Repository", func() { var _ = Describe("VatFlux Repository", func() {
var ( var (
db *postgres.DB db *postgres.DB
repository vat_flux.VatFluxRepository repository vat_flux.VatFluxRepository
headerRepository repositories.HeaderRepository
headerId int64
err error
) )
BeforeEach(func() { BeforeEach(func() {
@ -44,9 +40,6 @@ var _ = Describe("VatFlux Repository", func() {
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
repository = vat_flux.VatFluxRepository{} repository = vat_flux.VatFluxRepository{}
repository.SetDB(db) repository.SetDB(db)
headerRepository = repositories.NewHeaderRepository(db)
headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
}) })
type VatFluxDBResult struct { type VatFluxDBResult struct {
@ -55,12 +48,24 @@ var _ = Describe("VatFlux Repository", func() {
HeaderId int64 `db:"header_id"` HeaderId int64 `db:"header_id"`
} }
type CheckedHeaderResult struct {
VatFluxChecked bool `db:"vat_flux_checked"`
}
Describe("Create", func() { Describe("Create", func() {
vatFluxWithDifferentLogIdx := test_data.VatFluxModel
vatFluxWithDifferentLogIdx.LogIndex++
inputs := shared_behaviors.CreateBehaviorInputs{
CheckedHeaderColumnName: constants.VatFluxChecked,
LogEventTableName: "maker.vat_flux",
TestModel: test_data.VatFluxModel,
ModelWithDifferentLogIdx: vatFluxWithDifferentLogIdx,
Repository: &repository,
}
shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
It("persists vat flux records", func() { It("persists vat flux records", func() {
headerRepository := repositories.NewHeaderRepository(db)
headerId, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
anotherVatFlux := test_data.VatFluxModel anotherVatFlux := test_data.VatFluxModel
anotherVatFlux.TransactionIndex = test_data.VatFluxModel.TransactionIndex + 1 anotherVatFlux.TransactionIndex = test_data.VatFluxModel.TransactionIndex + 1
err = repository.Create(headerId, []interface{}{test_data.VatFluxModel, anotherVatFlux}) err = repository.Create(headerId, []interface{}{test_data.VatFluxModel, anotherVatFlux})
@ -79,175 +84,23 @@ var _ = Describe("VatFlux Repository", func() {
Expect(dbResult[0].Raw).To(MatchJSON(test_data.VatFluxModel.Raw)) Expect(dbResult[0].Raw).To(MatchJSON(test_data.VatFluxModel.Raw))
Expect(dbResult[0].HeaderId).To(Equal(headerId)) Expect(dbResult[0].HeaderId).To(Equal(headerId))
}) })
It("returns an error if the insertion fails", func() {
err = repository.Create(headerId, []interface{}{test_data.VatFluxModel})
Expect(err).NotTo(HaveOccurred())
err = repository.Create(headerId, []interface{}{test_data.VatFluxModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
It("allows for multiple vat flux events in one transaction if they have different log indexes", func() {
err = repository.Create(headerId, []interface{}{test_data.VatFluxModel})
Expect(err).NotTo(HaveOccurred())
anotherVatFlux := test_data.VatFluxModel
anotherVatFlux.LogIndex = anotherVatFlux.LogIndex + 1
err = repository.Create(headerId, []interface{}{anotherVatFlux})
Expect(err).NotTo(HaveOccurred())
})
It("marks the header as checked for vat flux logs", func() {
err = repository.Create(headerId, []interface{}{test_data.VatFluxModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_flux_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 = repository.Create(headerId, []interface{}{test_data.VatFluxModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_flux_checked FROM public.checked_headers WHERE header_id = $1`, headerId)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("removes vat flux if corresponding header is deleted", func() {
err = repository.Create(headerId, []interface{}{test_data.VatFluxModel})
Expect(err).NotTo(HaveOccurred())
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerId)
Expect(err).NotTo(HaveOccurred())
var count int
err = db.QueryRow(`SELECT count(*) from maker.vat_flux`).Scan(&count)
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(0))
})
It("wraps create in a transaction", func() {
err = repository.Create(headerId, []interface{}{test_data.VatFluxModel, test_data.VatFluxModel})
Expect(err).To(HaveOccurred())
var count int
err = db.QueryRowx(`SELECT count(*) FROM maker.vat_flux`).Scan(&count)
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"))
})
}) })
Describe("MissingHeaders", func() { Describe("MissingHeaders", func() {
var ( inputs := shared_behaviors.MissingHeadersBehaviorInputs{
startingBlock, vatFluxBlock, endingBlock, outsideRangeBlock int64 Repository: &repository,
headerIds, blockNumbers []int64 RepositoryTwo: &vat_flux.VatFluxRepository{},
) }
BeforeEach(func() { shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs)
startingBlock = rand.Int63()
vatFluxBlock = startingBlock + 1
endingBlock = startingBlock + 2
outsideRangeBlock = startingBlock + 3
blockNumbers = []int64{startingBlock, vatFluxBlock, endingBlock, outsideRangeBlock}
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 = repository.MarkHeaderChecked(headerIds[0])
Expect(err).NotTo(HaveOccurred())
headers, err := repository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(headers[0].Id).To(Or(Equal(headerIds[1]), Equal(headerIds[2])))
Expect(headers[1].Id).To(Or(Equal(headerIds[1]), Equal(headerIds[2])))
Expect(len(headers)).To(Equal(2))
})
It("returns header ids when checked_headers.vat_flux is false", func() {
err = repository.MarkHeaderChecked(headerIds[0])
_, err = db.Exec(`INSERT INTO checked_headers (header_id) VALUES ($1)`, headerIds[1])
Expect(err).NotTo(HaveOccurred())
headers, err := repository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(headers[0].Id).To(Or(Equal(headerIds[1]), Equal(headerIds[2])))
Expect(headers[1].Id).To(Or(Equal(headerIds[1]), Equal(headerIds[2])))
Expect(len(headers)).To(Equal(2))
})
It("only returns header ids for the current node", func() {
db2 := test_config.NewTestDB(core.Node{ID: "second node"})
headerRepository2 := repositories.NewHeaderRepository(db2)
repository2 := vat_flux.VatFluxRepository{}
repository2.SetDB(db2)
for _, n := range blockNumbers {
_, err = headerRepository2.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred())
}
err = repository.MarkHeaderChecked(headerIds[0])
Expect(err).NotTo(HaveOccurred())
nodeOneMissingHeaders, err := repository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeOneMissingHeaders)).To(Equal(2))
nodeTwoMissingHeaders, err := repository2.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeTwoMissingHeaders)).To(Equal(3))
})
}) })
Describe("MarkHeaderChecked", func() { Describe("MarkHeaderChecked", func() {
It("creates a new checked_header record", func() { inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{
err := repository.MarkHeaderChecked(headerId) CheckedHeaderColumnName: constants.VatFluxChecked,
Expect(err).NotTo(HaveOccurred()) Repository: &repository,
}
var checkedHeaderResult = CheckedHeaderResult{} shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs)
err = db.Get(&checkedHeaderResult, `SELECT vat_flux_checked FROM checked_headers WHERE header_id = $1`, headerId)
Expect(err).NotTo(HaveOccurred())
Expect(checkedHeaderResult.VatFluxChecked).To(BeTrue())
})
It("updates an existing checked header", func() {
_, err := db.Exec(`INSERT INTO checked_headers (header_id) VALUES($1)`, headerId)
Expect(err).NotTo(HaveOccurred())
var checkedHeaderResult CheckedHeaderResult
err = db.Get(&checkedHeaderResult, `SELECT vat_flux_checked FROM checked_headers WHERE header_id = $1`, headerId)
Expect(err).NotTo(HaveOccurred())
Expect(checkedHeaderResult.VatFluxChecked).To(BeFalse())
err = repository.MarkHeaderChecked(headerId)
Expect(err).NotTo(HaveOccurred())
err = db.Get(&checkedHeaderResult, `SELECT vat_flux_checked FROM checked_headers WHERE header_id = $1`, headerId)
Expect(err).NotTo(HaveOccurred())
Expect(checkedHeaderResult.VatFluxChecked).To(BeTrue())
})
}) })
}) })

View File

@ -19,6 +19,8 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/core" "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/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
) )
type VatFoldRepository struct { type VatFoldRepository struct {
@ -47,13 +49,8 @@ func (repository VatFoldRepository) Create(headerID int64, models []interface{})
return err return err
} }
} }
_, err = tx.Exec(
`INSERT INTO public.checked_headers (header_id, vat_fold_checked) err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.VatFoldChecked)
VALUES($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vat_fold_checked = $2`,
headerID, true,
)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
@ -63,31 +60,11 @@ func (repository VatFoldRepository) Create(headerID int64, models []interface{})
} }
func (repository VatFoldRepository) MarkHeaderChecked(headerID int64) error { func (repository VatFoldRepository) MarkHeaderChecked(headerID int64) error {
_, err := repository.db.Exec( return shared.MarkHeaderChecked(headerID, repository.db, constants.VatFoldChecked)
`INSERT INTO public.checked_headers (header_id, vat_fold_checked)
VALUES ($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vat_fold_checked = $2`,
headerID, true,
)
return err
} }
func (repository VatFoldRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { func (repository VatFoldRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
var result []core.Header return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.VatFoldChecked)
err := repository.db.Select(
&result,
`SELECT headers.id, headers.block_number FROM headers
LEFT JOIN checked_headers on headers.id = header_id
WHERE (header_id ISNULL OR vat_fold_checked IS FALSE)
AND headers.block_number >= $1
AND headers.block_number <= $2
AND headers.eth_node_fingerprint = $3`,
startingBlockNumber,
endingBlockNumber,
repository.db.Node.ID,
)
return result, err
} }
func (repository *VatFoldRepository) SetDB(db *postgres.DB) { func (repository *VatFoldRepository) SetDB(db *postgres.DB) {

View File

@ -15,27 +15,23 @@
package vat_fold_test package vat_fold_test
import ( import (
"database/sql"
"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/shared/constants"
"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/pkg/transformers/vat_fold" "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_fold"
"github.com/vulcanize/vulcanizedb/test_config" "github.com/vulcanize/vulcanizedb/test_config"
) )
var _ = Describe("Vat.fold repository", func() { var _ = Describe("Vat.fold repository", func() {
var ( var (
db *postgres.DB db *postgres.DB
repository vat_fold.VatFoldRepository repository vat_fold.VatFoldRepository
headerRepository repositories.HeaderRepository
err error
) )
BeforeEach(func() { BeforeEach(func() {
@ -44,23 +40,31 @@ var _ = Describe("Vat.fold repository", func() {
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
repository = vat_fold.VatFoldRepository{} repository = vat_fold.VatFoldRepository{}
repository.SetDB(db) repository.SetDB(db)
headerRepository = repositories.NewHeaderRepository(db)
}) })
Describe("Create", func() { Describe("Create", func() {
var headerID int64 modelWithDifferentLogIdx := test_data.VatFoldModel
modelWithDifferentLogIdx.LogIndex++
BeforeEach(func() { inputs := shared_behaviors.CreateBehaviorInputs{
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) CheckedHeaderColumnName: constants.VatFoldChecked,
Expect(err).NotTo(HaveOccurred()) LogEventTableName: "maker.vat_fold",
}) TestModel: test_data.VatFoldModel,
ModelWithDifferentLogIdx: modelWithDifferentLogIdx,
Repository: &repository,
}
shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
It("adds a vat fold event", func() { It("adds a vat fold event", func() {
headerRepository := repositories.NewHeaderRepository(db)
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err = repository.Create(headerID, []interface{}{test_data.VatFoldModel}) err = repository.Create(headerID, []interface{}{test_data.VatFoldModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var dbVatFold vat_fold.VatFoldModel var dbVatFold vat_fold.VatFoldModel
err := db.Get(&dbVatFold, `SELECT ilk, urn, rate, log_idx, tx_idx, raw_log FROM maker.vat_fold WHERE header_id = $1`, headerID) err = db.Get(&dbVatFold, `SELECT ilk, urn, rate, log_idx, tx_idx, raw_log FROM maker.vat_fold WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(dbVatFold.Ilk).To(Equal(test_data.VatFoldModel.Ilk)) Expect(dbVatFold.Ilk).To(Equal(test_data.VatFoldModel.Ilk))
@ -70,169 +74,23 @@ var _ = Describe("Vat.fold repository", func() {
Expect(dbVatFold.TransactionIndex).To(Equal(test_data.VatFoldModel.TransactionIndex)) Expect(dbVatFold.TransactionIndex).To(Equal(test_data.VatFoldModel.TransactionIndex))
Expect(dbVatFold.Raw).To(MatchJSON(test_data.VatFoldModel.Raw)) Expect(dbVatFold.Raw).To(MatchJSON(test_data.VatFoldModel.Raw))
}) })
It("marks header as checked for logs", func() {
err = repository.Create(headerID, []interface{}{test_data.VatFoldModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_fold_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 = repository.Create(headerID, []interface{}{test_data.VatFoldModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_fold_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("does not duplicate vat fold events", func() {
err = repository.Create(headerID, []interface{}{test_data.VatFoldModel})
Expect(err).NotTo(HaveOccurred())
err = repository.Create(headerID, []interface{}{test_data.VatFoldModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
It("removes vat fold if corresponding header is deleted", func() {
err = repository.Create(headerID, []interface{}{test_data.VatFoldModel})
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, urn, rate, log_idx, tx_idx, raw_log FROM maker.vat_fold 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 = repository.Create(headerID, []interface{}{test_data.WrongModel{}})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("model of type"))
})
}) })
Describe("MarkHeaderChecked", func() { Describe("MarkHeaderChecked", func() {
var headerID int64 inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{
CheckedHeaderColumnName: constants.VatFoldChecked,
BeforeEach(func() { Repository: &repository,
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
})
type CheckedHeaderResult struct {
VatFoldChecked bool `db:"vat_fold_checked"`
} }
It("creates a new checked header record", func() { shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs)
err := repository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var checkedHeaderResult = CheckedHeaderResult{}
err = db.Get(&checkedHeaderResult, `SELECT vat_fold_checked FROM checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(checkedHeaderResult.VatFoldChecked).To(BeTrue())
})
It("updates an existing checked header", func() {
_, err := db.Exec(`INSERT INTO checked_headers (header_id) VALUES($1)`, headerID)
Expect(err).NotTo(HaveOccurred())
var checkedHeaderResult CheckedHeaderResult
err = db.Get(&checkedHeaderResult, `SELECT vat_fold_checked FROM checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(checkedHeaderResult.VatFoldChecked).To(BeFalse())
err = repository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
err = db.Get(&checkedHeaderResult, `SELECT vat_fold_checked FROM checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(checkedHeaderResult.VatFoldChecked).To(BeTrue())
})
}) })
Describe("MissingHeaders", func() { Describe("MissingHeaders", func() {
var ( inputs := shared_behaviors.MissingHeadersBehaviorInputs{
startingBlock, vatFoldBlock, endingBlock int64 Repository: &repository,
blockNumbers, headerIDs []int64 RepositoryTwo: &vat_fold.VatFoldRepository{},
) }
BeforeEach(func() { shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs)
startingBlock = rand.Int63()
vatFoldBlock = startingBlock + 1
endingBlock = startingBlock + 2
blockNumbers = []int64{startingBlock, vatFoldBlock, 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 := repository.MarkHeaderChecked(headerIDs[1])
Expect(err).NotTo(HaveOccurred())
headers, err := repository.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 vat fold 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(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(3))
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(vatFoldBlock)))
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(vatFoldBlock)))
Expect(headers[2].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(vatFoldBlock)))
})
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 := vat_fold.VatFoldRepository{}
repositoryTwo.SetDB(dbTwo)
err := repository.MarkHeaderChecked(headerIDs[0])
Expect(err).NotTo(HaveOccurred())
nodeOneMissingHeaders, err := repository.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])
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers)))
})
}) })
}) })

View File

@ -5,6 +5,8 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/core" "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/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
) )
type VatGrabRepository struct { type VatGrabRepository struct {
@ -32,10 +34,7 @@ func (repository VatGrabRepository) Create(headerID int64, models []interface{})
return err return err
} }
} }
_, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, vat_grab_checked) err = shared.MarkHeaderCheckedInTransaction(headerID, tx, constants.VatGrabChecked)
VALUES ($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vat_grab_checked = $2`, headerID, true)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
@ -44,28 +43,11 @@ func (repository VatGrabRepository) Create(headerID int64, models []interface{})
} }
func (repository VatGrabRepository) MarkHeaderChecked(headerID int64) error { func (repository VatGrabRepository) MarkHeaderChecked(headerID int64) error {
_, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, vat_grab_checked) return shared.MarkHeaderChecked(headerID, repository.db, constants.VatGrabChecked)
VALUES ($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vat_grab_checked = $2`, headerID, true)
return err
} }
func (repository VatGrabRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { func (repository VatGrabRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
var result []core.Header return shared.MissingHeaders(startingBlockNumber, endingBlockNumber, repository.db, constants.VatGrabChecked)
err := repository.db.Select(
&result,
`SELECT headers.id, headers.block_number FROM headers
LEFT JOIN checked_headers on headers.id = header_id
WHERE (header_id ISNULL OR vat_grab_checked IS FALSE)
AND headers.block_number >= $1
AND headers.block_number <= $2
AND headers.eth_node_fingerprint = $3`,
startingBlockNumber,
endingBlockNumber,
repository.db.Node.ID,
)
return result, err
} }
func (repository *VatGrabRepository) SetDB(db *postgres.DB) { func (repository *VatGrabRepository) SetDB(db *postgres.DB) {

View File

@ -1,17 +1,16 @@
package vat_grab_test package vat_grab_test
import ( import (
"database/sql"
. "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" "github.com/vulcanize/vulcanizedb/pkg/datastore"
"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/factories" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
"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/pkg/transformers/vat_grab" "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_grab"
"github.com/vulcanize/vulcanizedb/test_config" "github.com/vulcanize/vulcanizedb/test_config"
) )
@ -19,30 +18,37 @@ import (
var _ = Describe("Vat grab repository", func() { var _ = Describe("Vat grab repository", func() {
var ( var (
db *postgres.DB db *postgres.DB
vatGrabRepository factories.Repository vatGrabRepository vat_grab.VatGrabRepository
err error
headerRepository datastore.HeaderRepository headerRepository datastore.HeaderRepository
) )
BeforeEach(func() { BeforeEach(func() {
db = test_config.NewTestDB(core.Node{}) db = test_config.NewTestDB(test_config.NewTestNode())
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
headerRepository = repositories.NewHeaderRepository(db) headerRepository = repositories.NewHeaderRepository(db)
vatGrabRepository = &vat_grab.VatGrabRepository{} vatGrabRepository = vat_grab.VatGrabRepository{}
vatGrabRepository.SetDB(db) vatGrabRepository.SetDB(db)
}) })
Describe("Create", func() { Describe("Create", func() {
var headerID int64 modelWithDifferentLogIdx := test_data.VatGrabModel
modelWithDifferentLogIdx.LogIndex++
BeforeEach(func() { inputs := shared_behaviors.CreateBehaviorInputs{
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) CheckedHeaderColumnName: constants.VatGrabChecked,
Expect(err).NotTo(HaveOccurred()) LogEventTableName: "maker.vat_grab",
}) TestModel: test_data.VatGrabModel,
ModelWithDifferentLogIdx: modelWithDifferentLogIdx,
Repository: &vatGrabRepository,
}
shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
It("adds a vat grab event", func() { It("adds a vat grab event", func() {
err = vatGrabRepository.Create(headerID, []interface{}{test_data.VatGrabModel}) headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
err = vatGrabRepository.Create(headerID, []interface{}{test_data.VatGrabModel})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
var dbVatGrab vat_grab.VatGrabModel var dbVatGrab vat_grab.VatGrabModel
err = db.Get(&dbVatGrab, `SELECT ilk, urn, v, w, dink, dart, log_idx, tx_idx, raw_log FROM maker.vat_grab WHERE header_id = $1`, headerID) err = db.Get(&dbVatGrab, `SELECT ilk, urn, v, w, dink, dart, log_idx, tx_idx, raw_log FROM maker.vat_grab WHERE header_id = $1`, headerID)
@ -57,157 +63,23 @@ var _ = Describe("Vat grab repository", func() {
Expect(dbVatGrab.TransactionIndex).To(Equal(test_data.VatGrabModel.TransactionIndex)) Expect(dbVatGrab.TransactionIndex).To(Equal(test_data.VatGrabModel.TransactionIndex))
Expect(dbVatGrab.Raw).To(MatchJSON(test_data.VatGrabModel.Raw)) Expect(dbVatGrab.Raw).To(MatchJSON(test_data.VatGrabModel.Raw))
}) })
It("marks header as checked for logs", func() {
err = vatGrabRepository.Create(headerID, []interface{}{test_data.VatGrabModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_grab_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 = vatGrabRepository.Create(headerID, []interface{}{test_data.VatGrabModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_grab_checked FROM public.checked_headers WHERE header_id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("does not duplicate vat grab events", func() {
err = vatGrabRepository.Create(headerID, []interface{}{test_data.VatGrabModel})
Expect(err).NotTo(HaveOccurred())
err = vatGrabRepository.Create(headerID, []interface{}{test_data.VatGrabModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
It("removes vat grab if corresponding header is deleted", func() {
err = vatGrabRepository.Create(headerID, []interface{}{test_data.VatGrabModel})
Expect(err).NotTo(HaveOccurred())
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID)
Expect(err).NotTo(HaveOccurred())
var dbVatGrab vat_grab.VatGrabModel
err = db.Get(&dbVatGrab, `SELECT ilk, urn, v, w, dink, dart, log_idx, tx_idx, raw_log FROM maker.vat_grab WHERE header_id = $1`, headerID)
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(sql.ErrNoRows))
})
It("returns an error if the wrong model is passed in", func() {
badModel := struct{}{}
err = vatGrabRepository.Create(headerID, []interface{}{badModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("model of type struct {}, not vat_grab.VatGrabModel"))
})
}) })
Describe("MarkHeaderChecked", func() { Describe("MarkHeaderChecked", func() {
var headerID int64 inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{
CheckedHeaderColumnName: constants.VatGrabChecked,
Repository: &vatGrabRepository,
}
BeforeEach(func() { shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs)
headerID, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
})
It("creates a row for a new headerID", func() {
err = vatGrabRepository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_grab_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 = vatGrabRepository.MarkHeaderChecked(headerID)
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_grab_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{
startingBlock, vatGrabBlock, endingBlock int64 Repository: &vatGrabRepository,
blockNumbers, headerIDs []int64 RepositoryTwo: &vat_grab.VatGrabRepository{},
) }
BeforeEach(func() { shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs)
startingBlock = GinkgoRandomSeed()
vatGrabBlock = startingBlock + 1
endingBlock = startingBlock + 2
blockNumbers = []int64{startingBlock, vatGrabBlock, 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 := vatGrabRepository.MarkHeaderChecked(headerIDs[1])
Expect(err).NotTo(HaveOccurred())
headers, err := vatGrabRepository.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 vat grab 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 := vatGrabRepository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(headers)).To(Equal(3))
Expect(headers[0].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(vatGrabBlock)))
Expect(headers[1].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(vatGrabBlock)))
Expect(headers[2].BlockNumber).To(Or(Equal(startingBlock), Equal(endingBlock), Equal(vatGrabBlock)))
})
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())
}
vatGrabRepositoryTwo := vat_grab.VatGrabRepository{}
vatGrabRepositoryTwo.SetDB(dbTwo)
err := vatGrabRepository.MarkHeaderChecked(headerIDs[0])
Expect(err).NotTo(HaveOccurred())
nodeOneMissingHeaders, err := vatGrabRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1))
nodeTwoMissingHeaders, err := vatGrabRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1])
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers)))
})
}) })
}) })

View File

@ -19,6 +19,8 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/core" "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/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
) )
type VatHealRepository struct { type VatHealRepository struct {
@ -50,10 +52,7 @@ func (repository VatHealRepository) Create(headerId int64, models []interface{})
} }
} }
_, err = tx.Exec(`INSERT INTO public.checked_headers (header_id, vat_heal_checked) err = shared.MarkHeaderCheckedInTransaction(headerId, tx, constants.VatHealChecked)
VALUES($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vat_heal_checked = $2`, headerId, true)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
@ -62,24 +61,9 @@ func (repository VatHealRepository) Create(headerId int64, models []interface{})
} }
func (repository VatHealRepository) MissingHeaders(startingBlock, endingBlock int64) ([]core.Header, error) { func (repository VatHealRepository) MissingHeaders(startingBlock, endingBlock int64) ([]core.Header, error) {
var headers []core.Header return shared.MissingHeaders(startingBlock, endingBlock, repository.db, constants.VatHealChecked)
err := repository.db.Select(&headers,
`SELECT headers.id, block_number from headers
LEFT JOIN checked_headers on headers.id = header_id
WHERE (header_id ISNULL OR vat_heal_checked IS FALSE)
AND headers.block_number >= $1
AND headers.block_number <= $2
AND headers.eth_node_fingerprint = $3`,
startingBlock, endingBlock, repository.db.Node.ID)
return headers, err
} }
func (repository VatHealRepository) MarkHeaderChecked(headerId int64) error { func (repository VatHealRepository) MarkHeaderChecked(headerId int64) error {
_, err := repository.db.Exec(`INSERT INTO public.checked_headers (header_id, vat_heal_checked) return shared.MarkHeaderChecked(headerId, repository.db, constants.VatHealChecked)
VALUES($1, $2)
ON CONFLICT (header_id) DO
UPDATE SET vat_heal_checked = $2`, headerId, true)
return err
} }

View File

@ -15,26 +15,23 @@
package vat_heal_test package vat_heal_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/shared/constants"
"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/pkg/transformers/vat_heal" "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_heal"
"github.com/vulcanize/vulcanizedb/test_config" "github.com/vulcanize/vulcanizedb/test_config"
) )
var _ = Describe("VatHeal Repository", func() { var _ = Describe("VatHeal Repository", func() {
var ( var (
db *postgres.DB db *postgres.DB
repository vat_heal.VatHealRepository repository vat_heal.VatHealRepository
headerRepository repositories.HeaderRepository
err error
) )
BeforeEach(func() { BeforeEach(func() {
@ -42,7 +39,6 @@ var _ = Describe("VatHeal Repository", func() {
test_config.CleanTestDB(db) test_config.CleanTestDB(db)
repository = vat_heal.VatHealRepository{} repository = vat_heal.VatHealRepository{}
repository.SetDB(db) repository.SetDB(db)
headerRepository = repositories.NewHeaderRepository(db)
}) })
type VatHealDBResult struct { type VatHealDBResult struct {
@ -51,19 +47,23 @@ var _ = Describe("VatHeal Repository", func() {
HeaderId int64 `db:"header_id"` HeaderId int64 `db:"header_id"`
} }
type CheckedHeaderResult struct {
VatHealChecked bool `db:"vat_heal_checked"`
}
Describe("Create", func() { Describe("Create", func() {
var headerId int64 modelWithDifferentLogIdx := test_data.VatHealModel
modelWithDifferentLogIdx.LogIndex++
inputs := shared_behaviors.CreateBehaviorInputs{
CheckedHeaderColumnName: constants.VatHealChecked,
LogEventTableName: "maker.vat_heal",
TestModel: test_data.VatHealModel,
ModelWithDifferentLogIdx: modelWithDifferentLogIdx,
Repository: &repository,
}
BeforeEach(func() { shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
})
It("persists vat heal records", func() { It("persists vat heal records", func() {
headerRepository := repositories.NewHeaderRepository(db)
headerId, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
anotherVatHeal := test_data.VatHealModel anotherVatHeal := test_data.VatHealModel
anotherVatHeal.LogIndex = test_data.VatHealModel.LogIndex + 1 anotherVatHeal.LogIndex = test_data.VatHealModel.LogIndex + 1
err = repository.Create(headerId, []interface{}{test_data.VatHealModel, anotherVatHeal}) err = repository.Create(headerId, []interface{}{test_data.VatHealModel, anotherVatHeal})
@ -81,171 +81,23 @@ var _ = Describe("VatHeal Repository", func() {
Expect(dbResult[0].Raw).To(MatchJSON(test_data.VatHealModel.Raw)) Expect(dbResult[0].Raw).To(MatchJSON(test_data.VatHealModel.Raw))
Expect(dbResult[0].HeaderId).To(Equal(headerId)) Expect(dbResult[0].HeaderId).To(Equal(headerId))
}) })
It("returns an error if the insertion fails", func() {
err = repository.Create(headerId, []interface{}{test_data.VatHealModel})
Expect(err).NotTo(HaveOccurred())
err = repository.Create(headerId, []interface{}{test_data.VatHealModel})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint"))
})
It("marks the header as checked for vat heal logs", func() {
err = repository.Create(headerId, []interface{}{test_data.VatHealModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_heal_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 = repository.Create(headerId, []interface{}{test_data.VatHealModel})
Expect(err).NotTo(HaveOccurred())
var headerChecked bool
err = db.Get(&headerChecked, `SELECT vat_heal_checked FROM public.checked_headers WHERE header_id = $1`, headerId)
Expect(err).NotTo(HaveOccurred())
Expect(headerChecked).To(BeTrue())
})
It("removes vat heal if corresponding header is deleted", func() {
err = repository.Create(headerId, []interface{}{test_data.VatHealModel})
Expect(err).NotTo(HaveOccurred())
_, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerId)
Expect(err).NotTo(HaveOccurred())
var count int
err = db.QueryRow(`SELECT count(*) from maker.vat_heal`).Scan(&count)
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(0))
})
It("returns an error if the wrong model types are passed in", func() {
err = repository.Create(headerId, []interface{}{test_data.WrongModel{}})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("model of type test_data.WrongModel, not vat_heal.VatHealModel"))
})
It("wraps create in a transaction", func() {
err = repository.Create(headerId, []interface{}{test_data.VatHealModel, test_data.VatHealModel})
Expect(err).To(HaveOccurred())
var count int
err = db.QueryRowx(`SELECT count(*) FROM maker.vat_heal`).Scan(&count)
Expect(count).To(Equal(0))
})
}) })
Describe("MissingHeaders", func() { Describe("MissingHeaders", func() {
var ( inputs := shared_behaviors.MissingHeadersBehaviorInputs{
startingBlock, vatHealBlock, endingBlock, outsideRangeBlock int64 Repository: &repository,
blockNumbers, headerIds []int64 RepositoryTwo: &vat_heal.VatHealRepository{},
) }
BeforeEach(func() { shared_behaviors.SharedRepositoryMissingHeadersBehaviors(&inputs)
startingBlock = rand.Int63()
vatHealBlock = startingBlock + 1
endingBlock = startingBlock + 2
outsideRangeBlock = startingBlock + 3
headerIds = []int64{}
blockNumbers = []int64{startingBlock, vatHealBlock, endingBlock, outsideRangeBlock}
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 = repository.MarkHeaderChecked(headerIds[0])
Expect(err).NotTo(HaveOccurred())
headers, err := repository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(headers[0].Id).To(Or(Equal(headerIds[1]), Equal(headerIds[2])))
Expect(headers[1].Id).To(Or(Equal(headerIds[1]), Equal(headerIds[2])))
Expect(len(headers)).To(Equal(2))
})
It("returns header ids when checked_headers.vat_heal is false", func() {
err = repository.MarkHeaderChecked(headerIds[0])
_, err = db.Exec(`INSERT INTO checked_headers (header_id) VALUES ($1)`, headerIds[1])
Expect(err).NotTo(HaveOccurred())
headers, err := repository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(headers[0].Id).To(Or(Equal(headerIds[1]), Equal(headerIds[2])))
Expect(headers[1].Id).To(Or(Equal(headerIds[1]), Equal(headerIds[2])))
Expect(len(headers)).To(Equal(2))
})
It("only returns header ids for the current node", func() {
db2 := test_config.NewTestDB(core.Node{ID: "second node"})
headerRepository2 := repositories.NewHeaderRepository(db2)
repository2 := vat_heal.VatHealRepository{}
repository2.SetDB(db2)
for _, n := range blockNumbers {
_, err = headerRepository2.CreateOrUpdateHeader(fakes.GetFakeHeader(n))
Expect(err).NotTo(HaveOccurred())
}
err = repository.MarkHeaderChecked(headerIds[0])
Expect(err).NotTo(HaveOccurred())
nodeOneMissingHeaders, err := repository.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeOneMissingHeaders)).To(Equal(2))
nodeTwoMissingHeaders, err := repository2.MissingHeaders(startingBlock, endingBlock)
Expect(err).NotTo(HaveOccurred())
Expect(len(nodeTwoMissingHeaders)).To(Equal(3))
})
}) })
Describe("MarkCheckedHeader", func() { Describe("MarkCheckedHeader", func() {
var headerId int64 inputs := shared_behaviors.MarkedHeaderCheckedBehaviorInputs{
CheckedHeaderColumnName: constants.VatHealChecked,
Repository: &repository,
}
BeforeEach(func() { shared_behaviors.SharedRepositoryMarkHeaderCheckedBehaviors(&inputs)
headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred())
})
It("creates a new checked_header record", func() {
err := repository.MarkHeaderChecked(headerId)
Expect(err).NotTo(HaveOccurred())
var checkedHeaderResult = CheckedHeaderResult{}
err = db.Get(&checkedHeaderResult, `SELECT vat_heal_checked FROM checked_headers WHERE header_id = $1`, headerId)
Expect(err).NotTo(HaveOccurred())
Expect(checkedHeaderResult.VatHealChecked).To(BeTrue())
})
It("updates an existing checked header", func() {
_, err := db.Exec(`INSERT INTO checked_headers (header_id) VALUES($1)`, headerId)
Expect(err).NotTo(HaveOccurred())
var checkedHeaderResult CheckedHeaderResult
err = db.Get(&checkedHeaderResult, `SELECT vat_heal_checked FROM checked_headers WHERE header_id = $1`, headerId)
Expect(err).NotTo(HaveOccurred())
Expect(checkedHeaderResult.VatHealChecked).To(BeFalse())
err = repository.MarkHeaderChecked(headerId)
Expect(err).NotTo(HaveOccurred())
err = db.Get(&checkedHeaderResult, `SELECT vat_heal_checked FROM checked_headers WHERE header_id = $1`, headerId)
Expect(err).NotTo(HaveOccurred())
Expect(checkedHeaderResult.VatHealChecked).To(BeTrue())
})
}) })
}) })