Add repository tests for Deal

This commit is contained in:
Rob Mulholand 2018-10-26 11:49:12 -05:00
parent 0e1675354f
commit 45a52a59f9
2 changed files with 35 additions and 4 deletions

View File

@ -31,7 +31,7 @@ func (repository DealRepository) Create(headerId int64, models []interface{}) er
} }
for _, model := range models { for _, model := range models {
deal, ok := model.(DealModel) dealModel, ok := model.(DealModel)
if !ok { if !ok {
tx.Rollback() tx.Rollback()
return fmt.Errorf("model of type %T, not %T", model, DealModel{}) return fmt.Errorf("model of type %T, not %T", model, DealModel{})
@ -40,7 +40,7 @@ func (repository DealRepository) Create(headerId int64, models []interface{}) er
_, err = tx.Exec( _, err = tx.Exec(
`INSERT into maker.deal (header_id, bid_id, contract_address, log_idx, tx_idx, raw_log) `INSERT into maker.deal (header_id, bid_id, contract_address, log_idx, tx_idx, raw_log)
VALUES($1, $2, $3, $4, $5, $6)`, VALUES($1, $2, $3, $4, $5, $6)`,
headerId, deal.BidId, deal.ContractAddress, deal.LogIndex, deal.TransactionIndex, deal.Raw, headerId, dealModel.BidId, dealModel.ContractAddress, dealModel.LogIndex, dealModel.TransactionIndex, dealModel.Raw,
) )
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()

View File

@ -49,11 +49,12 @@ var _ = Describe("Deal Repository", func() {
BeforeEach(func() { BeforeEach(func() {
headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader) headerId, err = headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
err := dealRepository.Create(headerId, []interface{}{test_data.DealModel})
Expect(err).NotTo(HaveOccurred())
}) })
It("persists a deal record", func() { It("persists a deal record", func() {
err = dealRepository.Create(headerId, []interface{}{test_data.DealModel})
Expect(err).NotTo(HaveOccurred())
var count int var count int
db.QueryRow(`SELECT count(*) FROM maker.deal`).Scan(&count) db.QueryRow(`SELECT count(*) FROM maker.deal`).Scan(&count)
Expect(count).To(Equal(1)) Expect(count).To(Equal(1))
@ -68,6 +69,22 @@ var _ = Describe("Deal Repository", func() {
}) })
It("marks header as checked for logs", func() { 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 var headerChecked bool
err = db.Get(&headerChecked, `SELECT deal_checked FROM public.checked_headers WHERE header_id = $1`, headerId) err = db.Get(&headerChecked, `SELECT deal_checked FROM public.checked_headers WHERE header_id = $1`, headerId)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
@ -76,21 +93,35 @@ var _ = Describe("Deal Repository", func() {
It("returns an error if inserting a deal record fails", func() { It("returns an error if inserting a deal record fails", func() {
err = dealRepository.Create(headerId, []interface{}{test_data.DealModel}) 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).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) 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() { 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 var count int
err = db.QueryRow(`SELECT count(*) from maker.deal`).Scan(&count) err = db.QueryRow(`SELECT count(*) from maker.deal`).Scan(&count)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(1)) Expect(count).To(Equal(1))
_, err = db.Exec(`DELETE FROM headers where id = $1`, headerId) _, err = db.Exec(`DELETE FROM headers where id = $1`, headerId)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
err = db.QueryRow(`SELECT count(*) from maker.deal`).Scan(&count) err = db.QueryRow(`SELECT count(*) from maker.deal`).Scan(&count)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(0)) 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() { Describe("MarkHeaderChecked", func() {